itemQuery

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

View file

@ -10,6 +10,7 @@ import ox
from ox.django import fields from ox.django import fields
from annotation.models import Annotation from annotation.models import Annotation
from item.models import Item
import managers import managers
@ -47,7 +48,9 @@ class Event(models.Model):
durationTime = models.BigIntegerField(default=0) durationTime = models.BigIntegerField(default=0)
type = models.CharField(default='', max_length=255) type = models.CharField(default='', max_length=255)
matches = models.IntegerField(default=0) matches = models.IntegerField(default=0)
items = models.ManyToManyField(Item, blank=True, related_name='events')
def get_matches(self): def get_matches(self):
q = Q(value__icontains=" " + self.name)|Q(value__startswith=self.name) q = Q(value__icontains=" " + self.name)|Q(value__startswith=self.name)
@ -56,7 +59,13 @@ class Event(models.Model):
return Annotation.objects.filter(q) return Annotation.objects.filter(q)
def update_matches(self): 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() self.save()
def save(self, *args, **kwargs): def save(self, *args, **kwargs):

View file

@ -96,6 +96,9 @@ def findEvents(request):
query: query object, more on query syntax at query: query object, more on query syntax at
https://wiki.0x2620.org/wiki/pandora/QuerySyntax https://wiki.0x2620.org/wiki/pandora/QuerySyntax
itemQuery: {
//see find request
},
sort: array of key, operator dics sort: array of key, operator dics
[ [
{ {
@ -109,6 +112,8 @@ def findEvents(request):
] ]
range: result range, array [from, to] 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: with keys, items is list of dicts with requested properties:
return {'status': {'code': int, 'text': string}, return {'status': {'code': int, 'text': string},
'data': {items: array}} 'data': {items: array}}

View file

@ -10,6 +10,8 @@ from django.db.models import Q
import managers import managers
from annotation.models import Annotation from annotation.models import Annotation
from item.models import Item
class Place(models.Model): class Place(models.Model):
''' '''
@ -40,6 +42,7 @@ class Place(models.Model):
area = models.FloatField(default=0) area = models.FloatField(default=0)
matches = models.IntegerField(default=0) matches = models.IntegerField(default=0)
items = models.ManyToManyField(Item, blank=True, related_name='places')
objects = managers.PlaceManager() objects = managers.PlaceManager()
@ -77,7 +80,13 @@ class Place(models.Model):
return Annotation.objects.filter(q) return Annotation.objects.filter(q)
def update_matches(self): 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() self.save()
def save(self, *args, **kwargs): def save(self, *args, **kwargs):

View file

@ -128,6 +128,9 @@ def parse_query(data, user):
if key in data: if key in data:
query[key] = data[key] query[key] = data[key]
query['qs'] = models.Place.objects.find(query, user) 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 return query
def order_query(qs, sort): def order_query(qs, sort):
@ -159,6 +162,9 @@ def findPlaces(request):
] ]
operator: "," operator: ","
}, },
itemQuery: {
//see find request
},
sort: [{key: 'name', operator: '+'}], sort: [{key: 'name', operator: '+'}],
range: [0, 100] range: [0, 100]
keys: [] keys: []
@ -167,6 +173,9 @@ def findPlaces(request):
possible query keys: possible query keys:
name, geoname, user 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: possible keys:
name, geoname, user name, geoname, user
@ -212,7 +221,6 @@ Positions
positions: ids of places for which positions are required positions: ids of places for which positions are required
''' '''
data = json.loads(request.POST['data']) data = json.loads(request.POST['data'])
print data
response = json_response() response = json_response()
query = parse_query(data, request.user) query = parse_query(data, request.user)