From ca0ddbe4fffe1e3243539877ef87ab94f974ea8e Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Sun, 5 Jun 2011 18:30:30 +0200 Subject: [PATCH] itemQuery --- pandora/event/models.py | 11 ++++++++++- pandora/event/views.py | 5 +++++ pandora/place/models.py | 11 ++++++++++- pandora/place/views.py | 10 +++++++++- 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/pandora/event/models.py b/pandora/event/models.py index 7d203ac0..32f0fe8f 100644 --- a/pandora/event/models.py +++ b/pandora/event/models.py @@ -10,6 +10,7 @@ import ox from ox.django import fields from annotation.models import Annotation +from item.models import Item import managers @@ -47,7 +48,9 @@ class Event(models.Model): durationTime = models.BigIntegerField(default=0) type = models.CharField(default='', max_length=255) + matches = models.IntegerField(default=0) + items = models.ManyToManyField(Item, blank=True, related_name='events') def get_matches(self): q = Q(value__icontains=" " + self.name)|Q(value__startswith=self.name) @@ -56,7 +59,13 @@ class Event(models.Model): return Annotation.objects.filter(q) def update_matches(self): - self.matches = self.get_matches().count() + matches = self.get_matches() + self.matches = matches.count() + ids = list(set([a.item.id for a in matches])) + for i in self.items.exclude(id__in=ids): + self.items.remove(i) + for i in Item.objects.filter(id__in=ids).exclude(id__in=self.items.all()): + self.items.add(i) self.save() def save(self, *args, **kwargs): diff --git a/pandora/event/views.py b/pandora/event/views.py index c7db5e8d..ff11a9ce 100644 --- a/pandora/event/views.py +++ b/pandora/event/views.py @@ -96,6 +96,9 @@ def findEvents(request): query: query object, more on query syntax at https://wiki.0x2620.org/wiki/pandora/QuerySyntax + itemQuery: { + //see find request + }, sort: array of key, operator dics [ { @@ -109,6 +112,8 @@ def findEvents(request): ] range: result range, array [from, to] + itemQuery can be used to limit the resuts to matches in those items. + with keys, items is list of dicts with requested properties: return {'status': {'code': int, 'text': string}, 'data': {items: array}} diff --git a/pandora/place/models.py b/pandora/place/models.py index 23bbdc6f..6d6424ef 100644 --- a/pandora/place/models.py +++ b/pandora/place/models.py @@ -10,6 +10,8 @@ from django.db.models import Q import managers from annotation.models import Annotation +from item.models import Item + class Place(models.Model): ''' @@ -40,6 +42,7 @@ class Place(models.Model): area = models.FloatField(default=0) matches = models.IntegerField(default=0) + items = models.ManyToManyField(Item, blank=True, related_name='places') objects = managers.PlaceManager() @@ -77,7 +80,13 @@ class Place(models.Model): return Annotation.objects.filter(q) def update_matches(self): - self.matches = self.get_matches().count() + matches = self.get_matches() + self.matches = matches.count() + ids = list(set([a.item.id for a in matches])) + for i in self.items.exclude(id__in=ids): + self.items.remove(i) + for i in Item.objects.filter(id__in=ids).exclude(id__in=self.items.all()): + self.items.add(i) self.save() def save(self, *args, **kwargs): diff --git a/pandora/place/views.py b/pandora/place/views.py index ed6b8cbb..d01a3470 100644 --- a/pandora/place/views.py +++ b/pandora/place/views.py @@ -128,6 +128,9 @@ def parse_query(data, user): if key in data: query[key] = data[key] query['qs'] = models.Place.objects.find(query, user) + if 'itemQuery' in data: + item_query = models.Item.objects.find({'query': data['itemQuery']}, user) + query['qs'] = query['qs'].filter(items__in=item_query) return query def order_query(qs, sort): @@ -159,6 +162,9 @@ def findPlaces(request): ] operator: "," }, + itemQuery: { + //see find request + }, sort: [{key: 'name', operator: '+'}], range: [0, 100] keys: [] @@ -167,6 +173,9 @@ def findPlaces(request): possible query keys: name, geoname, user + itemQuery can be used to limit the resuts to matches in those items. + Uses the same query syntax as used in the find request. + possible keys: name, geoname, user @@ -212,7 +221,6 @@ Positions positions: ids of places for which positions are required ''' data = json.loads(request.POST['data']) - print data response = json_response() query = parse_query(data, request.user)