itemQuery

This commit is contained in:
j 2011-06-05 18:30:30 +02:00
parent 8ed6444355
commit ca0ddbe4ff
4 changed files with 34 additions and 3 deletions

View file

@ -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):

View file

@ -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}}

View file

@ -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):

View file

@ -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)