Permalink
Browse files

Make the json response limitable with &limit=1 etc

  • Loading branch information...
1 parent 7adb487 commit 7d9548bc5af44928446c266cb118c3a7c9512dc8 @meldra committed Jan 4, 2013
Showing with 18 additions and 5 deletions.
  1. +18 −5 brain/views.py
View
@@ -533,16 +533,20 @@ class EmailChoices(forms.Form):
email = forms.ChoiceField(choices=emails, label='')
-def mboxperson(person, json=False):
+def mboxperson(person, json=False, limit=0):
mboxquery = Archive.objects.filter(Q(sender=person) | Q(others__contains=person)).order_by('-date')
+
+ if limit > 0:
+ mboxquery = mboxquery[:limit]
+
jsondata = serializers.serialize('json', mboxquery)
rows = simplejson.loads(jsondata)
mboxlist = []
m = mboxlist.append
for l in rows:
- m(l['fields'])
+ m(l['fields'])
if json == True:
return mboxlist
@@ -594,16 +598,25 @@ def search(request):
format = ''
try:
+ limit = int(request.GET['limit'])
+ except:
+ limit = 0
+
+ try:
addr = unquote(request.GET['email'])
- valid = is_valid_email(addr)
+ validaddr = is_valid_email(addr)
+ validint = isinstance(limit, int)
- if valid == True:
+ if validaddr == True and validint == True:
now = date.today()
dayname = now.weekday()
mbox(now, dayname, False)
if format == 'json':
- results = mboxperson(addr, True)
+ if limit > 0:
+ results = mboxperson(addr, True, limit)
+ else:
+ results = mboxperson(addr, True)
else:
results = mboxperson(addr)

0 comments on commit 7d9548b

Please sign in to comment.