filter keys

This commit is contained in:
j 2011-01-11 16:05:39 +05:30
parent 9487df7903
commit e700020707
2 changed files with 20 additions and 12 deletions

View file

@ -63,15 +63,16 @@ class List(models.Model):
return True
return False
def json(self, user=None):
return {
'user': self.user.username,
'name': self.name,
'public': self.public,
'featured': self.featured,
'query': self.query,
'items': self.get_number_of_items(user)
}
def json(self, keys, user=None):
response = {}
for key in keys:
if key == 'items':
response[key] = self.get_number_of_items(user)
elif key == 'user':
response[key] = self.user.username
else:
response[key] = getattr(self, key)
return response
class ListItem(models.Model):
created = models.DateTimeField(auto_now_add=True)

View file

@ -49,11 +49,15 @@ def findList(request):
},
sort: [{key: 'name', operator: '+'}],
range: [0, 100]
keys: []
}
possible query keys:
name, user, featured, subscribed
possible keys:
name, user, featured, subscribed, query
}
return {status: {code: int, text: string},
data: {
@ -69,10 +73,13 @@ def findList(request):
#order
qs = _order_query(query['qs'], query['sort'])
#range
qs = qs[query['range'][0]:query['range'][1]]
response = json_response()
response['data']['lists'] = [l.json(request.user) for l in qs]
if 'keys' in data:
qs = qs[query['range'][0]:query['range'][1]]
response['data']['items'] = [l.json(data['keys'], request.user) for l in qs]
else:
response['data']['items'] = qs.count()
return render_to_json_response(response)
actions.register(findList)