From f7e0893d20b2e654d5424f39518f7bfa3ef37f8b Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Tue, 11 Jan 2011 16:05:39 +0530 Subject: [PATCH] filter keys --- pandora/itemlist/models.py | 19 ++++++++++--------- pandora/itemlist/views.py | 13 ++++++++++--- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/pandora/itemlist/models.py b/pandora/itemlist/models.py index 802cabf..b000477 100644 --- a/pandora/itemlist/models.py +++ b/pandora/itemlist/models.py @@ -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) diff --git a/pandora/itemlist/views.py b/pandora/itemlist/views.py index a6faaaf..4fea6ba 100644 --- a/pandora/itemlist/views.py +++ b/pandora/itemlist/views.py @@ -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)