always use get_operator, avoid case-insensitive match if possible
This commit is contained in:
parent
5b545e6e43
commit
e9863c238e
24 changed files with 289 additions and 370 deletions
|
@ -6,12 +6,25 @@ from django.db.models import Q, Manager
|
|||
from oxdjango.query import QuerySet
|
||||
|
||||
from item.utils import decode_id
|
||||
from oxdjango.managers import get_operator
|
||||
|
||||
keymap = {
|
||||
'user': 'user__username',
|
||||
'place': 'places__id',
|
||||
'event': 'events__id',
|
||||
'in': 'start',
|
||||
'out': 'end',
|
||||
'id': 'public_id',
|
||||
'item': 'item__public_id',
|
||||
'value': 'findvalue',
|
||||
}
|
||||
case_insensitive_keys = ('user__username', )
|
||||
case_sensitive_keys = (
|
||||
'public_id',
|
||||
'layer',
|
||||
'item__public_id',
|
||||
)
|
||||
default_key = 'findvalue'
|
||||
|
||||
|
||||
def parseCondition(condition, user):
|
||||
|
@ -27,18 +40,9 @@ def parseCondition(condition, user):
|
|||
}
|
||||
'''
|
||||
k = condition.get('key')
|
||||
k = {
|
||||
'user': 'user__username',
|
||||
'place': 'places__id',
|
||||
'event': 'events__id',
|
||||
'in': 'start',
|
||||
'out': 'end',
|
||||
'id': 'public_id',
|
||||
'item': 'item__public_id',
|
||||
'value': 'findvalue',
|
||||
}.get(k, k)
|
||||
k = keymap.get(k, k)
|
||||
if not k:
|
||||
k = 'findvalue'
|
||||
k = default_key
|
||||
v = condition['value']
|
||||
op = condition.get('operator')
|
||||
if not op:
|
||||
|
@ -57,38 +61,12 @@ def parseCondition(condition, user):
|
|||
return q
|
||||
if k in ('places__id', 'events__id'):
|
||||
v = decode_id(v)
|
||||
if isinstance(v, bool): #featured and public flag
|
||||
if isinstance(v, bool):
|
||||
key = k
|
||||
elif k in ('places__id', 'events__id'):
|
||||
key = "%s%s" % (k, {
|
||||
'>': '__gt',
|
||||
'>=': '__gte',
|
||||
'<': '__lt',
|
||||
'<=': '__lte',
|
||||
}.get(op, ''))
|
||||
elif k in case_sensitive_keys:
|
||||
key = "%s%s" % (k, {
|
||||
'>': '__gt',
|
||||
'>=': '__gte',
|
||||
'<': '__lt',
|
||||
'<=': '__lte',
|
||||
'==': '__exact',
|
||||
'=': '__contains',
|
||||
'^': '__startswith',
|
||||
'$': '__endswith',
|
||||
}.get(op, '__contains'))
|
||||
key = k + get_operator(op, 'int')
|
||||
else:
|
||||
key = "%s%s" % (k, {
|
||||
'>': '__gt',
|
||||
'>=': '__gte',
|
||||
'<': '__lt',
|
||||
'<=': '__lte',
|
||||
'==': '__iexact',
|
||||
'=': '__icontains',
|
||||
'^': '__istartswith',
|
||||
'$': '__iendswith',
|
||||
}.get(op, '__icontains'))
|
||||
|
||||
key = k + get_operator(op, 'istr' if k in case_insensitive_keys else 'str')
|
||||
key = str(key)
|
||||
if isinstance(v, unicode):
|
||||
v = unicodedata.normalize('NFKD', v)
|
||||
|
|
|
@ -22,9 +22,11 @@ from tasks import update_matches
|
|||
|
||||
def get_super_matches(obj, model):
|
||||
super_matches = []
|
||||
q = Q(name_find__contains=" " + obj.name)|Q(name_find__contains="|%s"%obj.name)
|
||||
name_lower = obj.name.lower()
|
||||
q = Q(name_find__contains=" " + name_lower) | Q(name_find__contains="|%s" % name_lower)
|
||||
for name in obj.alternativeNames:
|
||||
q = q|Q(name_find__contains=" " + name)|Q(name_find__contains="|%s"%name)
|
||||
name_lower = name.lower()
|
||||
q = q | Q(name_find__contains=" " + name_lower) | Q(name_find__contains="|%s" % name_lower)
|
||||
for p in model.objects.filter(q).exclude(id=obj.id):
|
||||
for othername in [p.name] + list(p.alternativeNames):
|
||||
for name in [obj.name] + list(obj.alternativeNames):
|
||||
|
@ -48,11 +50,11 @@ def get_matches(obj, model, layer_type, qs=None):
|
|||
if contains:
|
||||
name = ox.decode_html(obj.name)
|
||||
name = unicodedata.normalize('NFKD', name).lower()
|
||||
q = Q(findvalue__icontains=" " + name)|Q(findvalue__istartswith=name)
|
||||
q = Q(findvalue__contains=" " + name) | Q(findvalue__startswith=name)
|
||||
for name in obj.alternativeNames:
|
||||
name = ox.decode_html(name)
|
||||
name = unicodedata.normalize('NFKD', name).lower()
|
||||
q = q|Q(findvalue__icontains=" " + name)|Q(findvalue__istartswith=name)
|
||||
q = q | Q(findvalue__contains=" " + name) | Q(findvalue__startswith=name)
|
||||
contains_matches = q&Q(layer__in=contains)
|
||||
if f:
|
||||
f = contains_matches | f
|
||||
|
|
|
@ -4,8 +4,14 @@ from django.db.models import Q, Manager
|
|||
from oxdjango.query import QuerySet
|
||||
|
||||
from item.utils import decode_id
|
||||
from oxdjango.managers import get_operator
|
||||
|
||||
|
||||
keymap = {
|
||||
'user': 'user__username',
|
||||
}
|
||||
default_key = 'name'
|
||||
|
||||
def parseCondition(condition, user):
|
||||
'''
|
||||
condition: {
|
||||
|
@ -19,12 +25,10 @@ def parseCondition(condition, user):
|
|||
}
|
||||
...
|
||||
'''
|
||||
k = condition.get('key', 'name')
|
||||
k = {
|
||||
'user': 'user__username',
|
||||
}.get(k, k)
|
||||
k = condition.get('key', default_key)
|
||||
k = keymap.get(k, k)
|
||||
if not k:
|
||||
k = 'name'
|
||||
k = default_key
|
||||
v = condition['value']
|
||||
op = condition.get('operator')
|
||||
if not op:
|
||||
|
@ -43,22 +47,12 @@ def parseCondition(condition, user):
|
|||
return q
|
||||
if k == 'id':
|
||||
v = decode_id(v)
|
||||
if isinstance(v, bool): #featured and public flag
|
||||
if isinstance(v, bool):
|
||||
key = k
|
||||
elif k in ('id', 'created'):
|
||||
key = '%s%s' % (k, {
|
||||
'>': '__gt',
|
||||
'>=': '__gte',
|
||||
'<': '__lt',
|
||||
'<=': '__lte',
|
||||
}.get(op,''))
|
||||
key = k + get_operator(op, 'int')
|
||||
else:
|
||||
key = '%s%s' % (k, {
|
||||
'==': '__iexact',
|
||||
'^': '__istartswith',
|
||||
'$': '__iendswith',
|
||||
}.get(op,'__icontains'))
|
||||
|
||||
key = k + get_operator(op, 'istr')
|
||||
key = str(key)
|
||||
if exclude:
|
||||
q = ~Q(**{key: v})
|
||||
|
|
|
@ -8,8 +8,21 @@ from django.conf import settings
|
|||
from oxdjango.query import QuerySet
|
||||
|
||||
from item.utils import decode_id, get_by_id
|
||||
from oxdjango.managers import get_operator
|
||||
|
||||
|
||||
keymap = {
|
||||
'event': 'annotations__events__id',
|
||||
'in': 'start',
|
||||
'out': 'end',
|
||||
'place': 'annotations__places__id',
|
||||
'text': 'findvalue',
|
||||
'annotations': 'findvalue',
|
||||
'user': 'annotations__user__username',
|
||||
}
|
||||
case_insensitive_keys = ('annotations__user__username', )
|
||||
default_key = 'name'
|
||||
|
||||
def parseCondition(condition, user):
|
||||
'''
|
||||
condition: {
|
||||
|
@ -22,18 +35,10 @@ def parseCondition(condition, user):
|
|||
operator: "!="
|
||||
}
|
||||
'''
|
||||
k = condition.get('key', 'name')
|
||||
k = {
|
||||
'event': 'annotations__events__id',
|
||||
'in': 'start',
|
||||
'out': 'end',
|
||||
'place': 'annotations__places__id',
|
||||
'text': 'findvalue',
|
||||
'annotations': 'findvalue',
|
||||
'user': 'annotations__user__username',
|
||||
}.get(k, k)
|
||||
k = condition.get('key', default_key)
|
||||
k = keymap.get(k, k)
|
||||
if not k:
|
||||
k = 'name'
|
||||
k = default_key
|
||||
v = condition['value']
|
||||
op = condition.get('operator')
|
||||
if not op:
|
||||
|
@ -60,35 +65,18 @@ def parseCondition(condition, user):
|
|||
|
||||
if k == 'id':
|
||||
public_id, points = v.split('/')
|
||||
points = [float('%0.03f'%float(p)) for p in points.split('-')]
|
||||
points = [float('%0.03f' % float(p)) for p in points.split('-')]
|
||||
q = Q(item__public_id=public_id, start=points[0], end=points[1])
|
||||
return exclude and ~q or q
|
||||
|
||||
elif k.endswith('__id'):
|
||||
v = decode_id(v)
|
||||
if isinstance(v, bool): #featured and public flag
|
||||
key = k
|
||||
elif k in ('lat', 'lng', 'area', 'south', 'west', 'north', 'east', 'matches',
|
||||
'id') or k.endswith('__id'):
|
||||
key = "%s%s" % (k, {
|
||||
'>': '__gt',
|
||||
'>=': '__gte',
|
||||
'<': '__lt',
|
||||
'<=': '__lte',
|
||||
}.get(op, ''))
|
||||
else:
|
||||
key = "%s%s" % (k, {
|
||||
'>': '__gt',
|
||||
'>=': '__gte',
|
||||
'<': '__lt',
|
||||
'<=': '__lte',
|
||||
'===': '__exact',
|
||||
'==': '__iexact',
|
||||
'=': '__icontains',
|
||||
'^': '__istartswith',
|
||||
'$': '__iendswith',
|
||||
}.get(op, '__icontains'))
|
||||
|
||||
if isinstance(v, bool):
|
||||
key = k
|
||||
elif k in ('id', ) or k.endswith('__id'):
|
||||
key = k + get_operator(op, 'int')
|
||||
else:
|
||||
key = k + get_operator(op, 'istr' if k in case_insensitive_keys else 'str')
|
||||
key = str(key)
|
||||
if isinstance(v, unicode) and op != '===':
|
||||
v = unicodedata.normalize('NFKD', v).lower()
|
||||
|
@ -137,8 +125,6 @@ def parseConditions(conditions, operator, user):
|
|||
return q
|
||||
return None
|
||||
|
||||
|
||||
|
||||
class ClipManager(Manager):
|
||||
|
||||
def get_query_set(self):
|
||||
|
@ -150,17 +136,9 @@ class ClipManager(Manager):
|
|||
conditions = data.get('query', {}).get('conditions', [])
|
||||
conditions = filter(lambda c: c['key'] in keys, conditions)
|
||||
operator = data.get('query', {}).get('operator', '&')
|
||||
|
||||
def parse(condition):
|
||||
key = "%s%s" % ('findvalue', {
|
||||
'>': '__gt',
|
||||
'>=': '__gte',
|
||||
'<': '__lt',
|
||||
'<=': '__lte',
|
||||
'==': '__iexact',
|
||||
'=': '__icontains',
|
||||
'^': '__istartswith',
|
||||
'$': '__iendswith',
|
||||
}.get(condition.get('operator', ''), '__icontains'))
|
||||
key = 'findvalue' + get_operator(condition.get('operator', ''))
|
||||
v = condition['value']
|
||||
if isinstance(v, unicode):
|
||||
v = unicodedata.normalize('NFKD', v).lower()
|
||||
|
@ -168,6 +146,7 @@ class ClipManager(Manager):
|
|||
if condition['key'] in layer_ids:
|
||||
q = q & Q(layer=condition['key'])
|
||||
return q
|
||||
|
||||
conditions = map(parse, conditions)
|
||||
if conditions:
|
||||
q = conditions[0]
|
||||
|
|
|
@ -6,18 +6,22 @@ import ox
|
|||
from oxdjango.query import QuerySet
|
||||
|
||||
import entity.managers
|
||||
from oxdjango.managers import get_operator
|
||||
|
||||
|
||||
keymap = {
|
||||
'user': 'user__username',
|
||||
'item': 'items__public_id',
|
||||
}
|
||||
default_key = 'name'
|
||||
|
||||
def parseCondition(condition, user, item=None):
|
||||
'''
|
||||
'''
|
||||
k = condition.get('key', 'name')
|
||||
k = {
|
||||
'user': 'user__username',
|
||||
'item': 'items__public_id',
|
||||
}.get(k, k)
|
||||
k = condition.get('key', default_key)
|
||||
k = keymap.get(k, k)
|
||||
if not k:
|
||||
k = 'name'
|
||||
k = default_key
|
||||
if item and k == 'description':
|
||||
item_conditions = condition.copy()
|
||||
item_conditions['key'] = 'items__itemproperties__description'
|
||||
|
@ -38,17 +42,13 @@ def buildCondition(k, op, v):
|
|||
if k == 'id':
|
||||
v = ox.fromAZ(v)
|
||||
return Q(**{k: v})
|
||||
if isinstance(v, bool): #featured and public flag
|
||||
if isinstance(v, bool):
|
||||
key = k
|
||||
elif k == 'entity':
|
||||
entity_key, v = entity.managers.namePredicate(op, v)
|
||||
key = 'entities__' + entity_key
|
||||
else:
|
||||
key = "%s%s" % (k, {
|
||||
'==': '__iexact',
|
||||
'^': '__istartswith',
|
||||
'$': '__iendswith',
|
||||
}.get(op, '__icontains'))
|
||||
key = k + get_operator(op, 'istr')
|
||||
key = str(key)
|
||||
return Q(**{key: v})
|
||||
|
||||
|
|
|
@ -3,19 +3,21 @@
|
|||
from django.db.models import Q, Manager
|
||||
|
||||
from oxdjango.query import QuerySet
|
||||
from oxdjango.managers import get_operator
|
||||
|
||||
|
||||
keymap = {
|
||||
'user': 'user__username',
|
||||
'position': 'position__position',
|
||||
'posterFrames': 'poster_frames',
|
||||
}
|
||||
default_key = 'name'
|
||||
|
||||
def parseCondition(condition, user):
|
||||
'''
|
||||
'''
|
||||
k = condition.get('key', 'name')
|
||||
k = {
|
||||
'user': 'user__username',
|
||||
'position': 'position__position',
|
||||
'posterFrames': 'poster_frames',
|
||||
}.get(k, k)
|
||||
k = condition.get('key', default_key)
|
||||
k = keymap.get(k, k)
|
||||
if not k:
|
||||
k = 'name'
|
||||
k = default_key
|
||||
v = condition.get('value', '')
|
||||
op = condition.get('operator')
|
||||
if not op:
|
||||
|
@ -36,14 +38,10 @@ def parseCondition(condition, user):
|
|||
if k == 'subscribed':
|
||||
key = 'subscribed_users__username'
|
||||
v = user.username
|
||||
elif isinstance(v, bool): #featured and public flag
|
||||
elif isinstance(v, bool):
|
||||
key = k
|
||||
else:
|
||||
key = "%s%s" % (k, {
|
||||
'==': '__iexact',
|
||||
'^': '__istartswith',
|
||||
'$': '__iendswith',
|
||||
}.get(op, '__icontains'))
|
||||
key = k + get_operator(op, 'istr')
|
||||
key = str(key)
|
||||
if exclude:
|
||||
q = ~Q(**{key: v})
|
||||
|
|
|
@ -3,7 +3,16 @@
|
|||
from django.db.models import Q, Manager
|
||||
|
||||
import ox
|
||||
|
||||
from oxdjango.query import QuerySet
|
||||
from oxdjango.managers import get_operator
|
||||
|
||||
keymap = {
|
||||
'user': 'user__username',
|
||||
'name': 'name_find'
|
||||
}
|
||||
case_insensitive_keys = ('user__username',)
|
||||
default_key = 'name'
|
||||
|
||||
|
||||
def namePredicate(op, value):
|
||||
|
@ -13,17 +22,14 @@ def namePredicate(op, value):
|
|||
'$': '%s|',
|
||||
}.get(op, '%s')
|
||||
|
||||
return ('name_find__icontains', pat % value)
|
||||
return ('name_find__contains', pat % value.lower())
|
||||
|
||||
|
||||
def parseCondition(condition, user, item=None):
|
||||
'''
|
||||
'''
|
||||
k = condition.get('key', 'name')
|
||||
k = {
|
||||
'user': 'user__username',
|
||||
'name': 'name_find'
|
||||
}.get(k, k)
|
||||
k = condition.get('key', default_key)
|
||||
k = keymap.get(k, k)
|
||||
|
||||
v = condition['value']
|
||||
op = condition.get('operator')
|
||||
|
@ -42,7 +48,7 @@ def buildCondition(k, op, v):
|
|||
if k == 'id':
|
||||
v = ox.fromAZ(v)
|
||||
return Q(**{k: v})
|
||||
if isinstance(v, bool): #featured and public flag
|
||||
if isinstance(v, bool):
|
||||
key = k
|
||||
elif k == 'name_find':
|
||||
key, v = namePredicate(op, v)
|
||||
|
@ -52,11 +58,7 @@ def buildCondition(k, op, v):
|
|||
elif k not in ('id', 'user__username', 'type'):
|
||||
find_key = k
|
||||
k = 'find__value'
|
||||
key = "%s%s" % (k, {
|
||||
'==': '__iexact',
|
||||
'^': '__istartswith',
|
||||
'$': '__iendswith',
|
||||
}.get(op, '__icontains'))
|
||||
key = k + get_operator(op, 'istr' if k in case_insensitive_keys else 'str')
|
||||
key = str(key)
|
||||
if find_key:
|
||||
return Q(**{'find__key': find_key, key: v})
|
||||
|
|
|
@ -56,6 +56,7 @@ class Entity(models.Model):
|
|||
else:
|
||||
self.name_sort = ox.sort_string(self.name or u'')[:255].lower() or None
|
||||
self.name_find = '||' + '||'.join((self.name,) + self.alternativeNames) + '||'
|
||||
self.name_find = self.name_find.lower()
|
||||
super(Entity, self).save(*args, **kwargs)
|
||||
self.update_matches()
|
||||
self.update_annotations()
|
||||
|
@ -70,11 +71,11 @@ class Entity(models.Model):
|
|||
|
||||
@classmethod
|
||||
def get_by_name(cls, name, type):
|
||||
return cls.objects.get(name_find__icontains=u'|%s|'%name, type=type)
|
||||
return cls.objects.get(name_find__contains=u'|%s|' % name.lower(), type=type)
|
||||
|
||||
@classmethod
|
||||
def get_or_create(model, name):
|
||||
qs = model.objects.filter(name_find__icontains=u'|%s|'%name)
|
||||
qs = model.objects.filter(name_find__contains=u'|%s|' % name.lower())
|
||||
if qs.count() == 0:
|
||||
instance = model(name=name)
|
||||
instance.save()
|
||||
|
@ -115,7 +116,7 @@ class Entity(models.Model):
|
|||
data['name'] = "Unnamed"
|
||||
name = data['name']
|
||||
n = 1
|
||||
while Entity.objects.filter(name_find__icontains=u'|%s|'%name).exclude(id=self.id).count() > 0:
|
||||
while Entity.objects.filter(name_find__contains=u'|%s|' % name.lower()).exclude(id=self.id).count() > 0:
|
||||
n += 1
|
||||
name = data['name'] + ' [%d]' % n
|
||||
self.name = name
|
||||
|
@ -130,7 +131,7 @@ class Entity(models.Model):
|
|||
name_ = name
|
||||
n = 1
|
||||
while name in used_names or \
|
||||
Entity.objects.filter(name_find__icontains=u'|%s|'%name).exclude(id=self.id).count() > 0:
|
||||
Entity.objects.filter(name_find__contains=u'|%s|' % name.lower()).exclude(id=self.id).count() > 0:
|
||||
n += 1
|
||||
name = name_ + ' [%d]' % n
|
||||
names.append(name)
|
||||
|
|
|
@ -3,16 +3,19 @@
|
|||
import unicodedata
|
||||
|
||||
from django.db.models import Q, Manager
|
||||
from oxdjango.query import QuerySet
|
||||
|
||||
from oxdjango.query import QuerySet
|
||||
from oxdjango.managers import get_operator
|
||||
from item.utils import decode_id
|
||||
|
||||
keymap = {
|
||||
'user': 'user__username',
|
||||
}
|
||||
default_key = 'name'
|
||||
|
||||
def parseCondition(condition, user):
|
||||
k = condition.get('key', 'name')
|
||||
k = {
|
||||
'user': 'user__username',
|
||||
}.get(k, k)
|
||||
k = condition.get('key', default_key)
|
||||
k = keymap.get(k, k)
|
||||
v = condition['value']
|
||||
op = condition.get('operator')
|
||||
if not op:
|
||||
|
@ -25,12 +28,7 @@ def parseCondition(condition, user):
|
|||
if k == 'id':
|
||||
v = decode_id(v)
|
||||
|
||||
key = '%s%s' % (k, {
|
||||
'==': '__iexact',
|
||||
'^': '__istartswith',
|
||||
'$': '__iendswith',
|
||||
}.get(op,'__icontains'))
|
||||
|
||||
key = k + get_operator(op, 'istr')
|
||||
key = str(key)
|
||||
if isinstance(v, unicode):
|
||||
v = unicodedata.normalize('NFKD', v).lower()
|
||||
|
|
|
@ -62,7 +62,7 @@ class Event(models.Model):
|
|||
|
||||
@classmethod
|
||||
def get_or_create(model, name):
|
||||
qs = model.objects.filter(name_find__icontains=u'|%s|'%name)
|
||||
qs = model.objects.filter(name_find__contains=u'|%s|' % name.lower())
|
||||
if qs.count() == 0:
|
||||
instance = model(name=name)
|
||||
instance.save()
|
||||
|
@ -131,6 +131,7 @@ class Event(models.Model):
|
|||
if not self.name_sort:
|
||||
self.set_name_sort()
|
||||
self.name_find = '||' + '||'.join((self.name,) + self.alternativeNames) + '||'
|
||||
self.name_find = self.name_find.lower()
|
||||
self.defined = len(filter(None, [getattr(self, key)
|
||||
for key in ('start', 'end')])) > 0
|
||||
if self.endTime and self.startTime:
|
||||
|
|
|
@ -13,29 +13,7 @@ import models
|
|||
import utils
|
||||
|
||||
from oxdjango.query import QuerySet
|
||||
|
||||
def get_operator(op, type='str'):
|
||||
return {
|
||||
'str': {
|
||||
'==': '__iexact',
|
||||
'>': '__gt',
|
||||
'>=': '__gte',
|
||||
'<': '__lt',
|
||||
'<=': '__lte',
|
||||
'^': '__istartswith',
|
||||
'$': '__iendswith',
|
||||
},
|
||||
'int': {
|
||||
'==': '__exact',
|
||||
'>': '__gt',
|
||||
'>=': '__gte',
|
||||
'<': '__lt',
|
||||
'<=': '__lte',
|
||||
}
|
||||
}[type].get(op, {
|
||||
'str': '__icontains',
|
||||
'int': ''
|
||||
}[type])
|
||||
from oxdjango.managers import get_operator
|
||||
|
||||
def parseCondition(condition, user, owner=None):
|
||||
'''
|
||||
|
@ -97,8 +75,7 @@ def parseCondition(condition, user, owner=None):
|
|||
|
||||
if (not exclude and op == '=' or op in ('$', '^')) and v == '':
|
||||
return Q()
|
||||
elif k == 'filename' and (user.is_anonymous() or \
|
||||
not user.profile.capability('canSeeMedia')):
|
||||
elif k == 'filename' and (user.is_anonymous() or not user.profile.capability('canSeeMedia')):
|
||||
return Q(id=0)
|
||||
elif k == 'oshash':
|
||||
return Q(files__oshash=v)
|
||||
|
@ -140,19 +117,17 @@ def parseCondition(condition, user, owner=None):
|
|||
value_key = 'find__value'
|
||||
else:
|
||||
value_key = k
|
||||
if isinstance(v, unicode):
|
||||
v = unicodedata.normalize('NFKD', v).lower()
|
||||
if k in facet_keys:
|
||||
in_find = False
|
||||
facet_value = 'facets__value' + get_operator(op)
|
||||
v = models.Item.objects.filter(**{'facets__key':k, facet_value:v})
|
||||
facet_value = 'facets__value' + get_operator(op, 'istr')
|
||||
v = models.Item.objects.filter(**{'facets__key': k, facet_value: v})
|
||||
value_key = 'id__in'
|
||||
else:
|
||||
value_key = value_key + get_operator(op)
|
||||
if 'find__value' in value_key:
|
||||
value_key = value_key.replace('_icontains', '_contains')
|
||||
k = str(k)
|
||||
value_key = str(value_key)
|
||||
if isinstance(v, unicode):
|
||||
v = unicodedata.normalize('NFKD', v).lower()
|
||||
if k == '*':
|
||||
q = Q(**{value_key: v})
|
||||
elif in_find:
|
||||
|
|
|
@ -124,9 +124,10 @@ def get_item(info, user=None):
|
|||
item.save(sync=True)
|
||||
tasks.update_poster.delay(item.public_id)
|
||||
else:
|
||||
qs = Item.objects.filter(find__key='title', find__value__iexact=info['title'])
|
||||
title = unicodedata.normalize('NFKD', info['title']).lower()
|
||||
qs = Item.objects.filter(find__key='title', find__value=title)
|
||||
if 'year' in info:
|
||||
qs = qs.filter(find__key='year', find__value__iexact=str(info['year']))
|
||||
qs = qs.filter(find__key='year', find__value=str(info['year']))
|
||||
if qs.count() == 1:
|
||||
item = qs[0]
|
||||
else:
|
||||
|
|
|
@ -3,19 +3,21 @@
|
|||
|
||||
from django.db.models import Q, Manager
|
||||
|
||||
from oxdjango.managers import get_operator
|
||||
from oxdjango.query import QuerySet
|
||||
|
||||
keymap = {
|
||||
'user': 'user__username',
|
||||
}
|
||||
default_key = 'name'
|
||||
|
||||
def parseCondition(condition, user):
|
||||
'''
|
||||
'''
|
||||
k = condition.get('key', 'name')
|
||||
k = {
|
||||
'user': 'user__username',
|
||||
'position': 'position__position',
|
||||
'posterFrames': 'poster_frames',
|
||||
}.get(k, k)
|
||||
k = condition.get('key', default_key)
|
||||
k = keymap.get(k, k)
|
||||
if not k:
|
||||
k = 'name'
|
||||
k = default_key
|
||||
v = condition.get('value', '')
|
||||
op = condition.get('operator')
|
||||
if not op:
|
||||
|
@ -36,14 +38,10 @@ def parseCondition(condition, user):
|
|||
if k == 'subscribed':
|
||||
key = 'subscribed_users__username'
|
||||
v = user.username
|
||||
elif isinstance(v, bool): #featured and public flag
|
||||
elif isinstance(v, bool):
|
||||
key = k
|
||||
else:
|
||||
key = "%s%s" % (k, {
|
||||
'==': '__iexact',
|
||||
'^': '__istartswith',
|
||||
'$': '__iendswith',
|
||||
}.get(op, '__icontains'))
|
||||
key = k + get_operator(op, 'istr')
|
||||
key = str(key)
|
||||
if exclude:
|
||||
q = ~Q(**{key: v})
|
||||
|
|
|
@ -1,11 +1,17 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# vi:si:et:sw=4:sts=4:ts=4
|
||||
from django.db.models import Q, Manager
|
||||
from oxdjango.query import QuerySet
|
||||
|
||||
from oxdjango.query import QuerySet
|
||||
from oxdjango.managers import get_operator
|
||||
from item.utils import decode_id
|
||||
|
||||
|
||||
keymap = {
|
||||
'user': 'user__username',
|
||||
}
|
||||
default_key = 'name'
|
||||
|
||||
def parseCondition(condition, user):
|
||||
'''
|
||||
condition: {
|
||||
|
@ -19,12 +25,10 @@ def parseCondition(condition, user):
|
|||
}
|
||||
...
|
||||
'''
|
||||
k = condition.get('key', 'name')
|
||||
k = {
|
||||
'user': 'user__username',
|
||||
}.get(k, k)
|
||||
k = condition.get('key', default_key)
|
||||
k = keymap.get(k, k)
|
||||
if not k:
|
||||
k = 'name'
|
||||
k = default_key
|
||||
v = condition['value']
|
||||
op = condition.get('operator')
|
||||
if not op:
|
||||
|
@ -43,22 +47,12 @@ def parseCondition(condition, user):
|
|||
return q
|
||||
if k == 'id':
|
||||
v = decode_id(v)
|
||||
if isinstance(v, bool): #featured and public flag
|
||||
if isinstance(v, bool):
|
||||
key = k
|
||||
elif k in ('id', ):
|
||||
key = '%s%s' % (k, {
|
||||
'>': '__gt',
|
||||
'>=': '__gte',
|
||||
'<': '__lt',
|
||||
'<=': '__lte',
|
||||
}.get(op,''))
|
||||
key = k + get_operator(op, 'int')
|
||||
else:
|
||||
key = '%s%s' % (k, {
|
||||
'==': '__iexact',
|
||||
'^': '__istartswith',
|
||||
'$': '__iendswith',
|
||||
}.get(op,'__icontains'))
|
||||
|
||||
key = k + get_operator(op, 'istr')
|
||||
key = str(key)
|
||||
if exclude:
|
||||
q = ~Q(**{key: v})
|
||||
|
|
|
@ -4,15 +4,18 @@ from django.db.models import Q, Manager
|
|||
from oxdjango.query import QuerySet
|
||||
|
||||
from item.utils import decode_id
|
||||
from oxdjango.managers import get_operator
|
||||
|
||||
keymap = {
|
||||
'user': 'user__username',
|
||||
}
|
||||
default_key = 'name'
|
||||
|
||||
def parseCondition(condition, user):
|
||||
k = condition.get('key', 'name')
|
||||
k = {
|
||||
'user': 'user__username',
|
||||
}.get(k, k)
|
||||
k = condition.get('key', defauly_key)
|
||||
k = keymap.get(k, k)
|
||||
if not k:
|
||||
k = 'name'
|
||||
k = default_key
|
||||
v = condition['value']
|
||||
op = condition.get('operator')
|
||||
if not op:
|
||||
|
@ -31,26 +34,12 @@ def parseCondition(condition, user):
|
|||
return q
|
||||
if k == 'id':
|
||||
v = decode_id(v)
|
||||
if isinstance(v, bool): #featured and public flag
|
||||
if isinstance(v, bool):
|
||||
key = k
|
||||
elif k in ('id',):
|
||||
key = "%s%s" % (k, {
|
||||
'>': '__gt',
|
||||
'>=': '__gte',
|
||||
'<': '__lt',
|
||||
'<=': '__lte',
|
||||
}.get(op, ''))
|
||||
key = k + get_operator(op, 'int')
|
||||
else:
|
||||
key = "%s%s" % (k, {
|
||||
'>': '__gt',
|
||||
'>=': '__gte',
|
||||
'<': '__lt',
|
||||
'<=': '__lte',
|
||||
'==': '__iexact',
|
||||
'=': '__icontains',
|
||||
'^': '__istartswith',
|
||||
'$': '__iendswith',
|
||||
}.get(op, '__icontains'))
|
||||
key = k + get_operator(op, 'istr')
|
||||
|
||||
key = str(key)
|
||||
if exclude:
|
||||
|
|
36
pandora/oxdjango/managers.py
Normal file
36
pandora/oxdjango/managers.py
Normal file
|
@ -0,0 +1,36 @@
|
|||
|
||||
def get_operator(op, type='str'):
|
||||
return {
|
||||
'str': {
|
||||
'==': '',
|
||||
'===': '',
|
||||
'>': '__gt',
|
||||
'>=': '__gte',
|
||||
'<': '__lt',
|
||||
'<=': '__lte',
|
||||
'^': '__startswith',
|
||||
'$': '__endswith',
|
||||
},
|
||||
'istr': {
|
||||
'==': '__iexact',
|
||||
'===': '',
|
||||
'>': '__gt',
|
||||
'>=': '__gte',
|
||||
'<': '__lt',
|
||||
'<=': '__lte',
|
||||
'^': '__istartswith',
|
||||
'$': '__iendswith',
|
||||
},
|
||||
'int': {
|
||||
'==': '',
|
||||
'>': '__gt',
|
||||
'>=': '__gte',
|
||||
'<': '__lt',
|
||||
'<=': '__lte',
|
||||
}
|
||||
}[type].get(op, {
|
||||
'str': '__contains',
|
||||
'istr': '__icontains',
|
||||
'int': ''
|
||||
}[type])
|
||||
|
|
@ -3,10 +3,16 @@
|
|||
import unicodedata
|
||||
|
||||
from django.db.models import Q, Manager
|
||||
from oxdjango.query import QuerySet
|
||||
|
||||
from item.utils import decode_id
|
||||
from oxdjango.query import QuerySet
|
||||
from oxdjango.managers import get_operator
|
||||
|
||||
keymap = {
|
||||
'user': 'user__username',
|
||||
}
|
||||
case_insensitive_keys = ('user__username', 'subscribed_users__username')
|
||||
default_key = 'name'
|
||||
|
||||
def parseCondition(condition, user):
|
||||
'''
|
||||
|
@ -21,12 +27,10 @@ def parseCondition(condition, user):
|
|||
}
|
||||
...
|
||||
'''
|
||||
k = condition.get('key', 'name')
|
||||
k = {
|
||||
'user': 'user__username',
|
||||
}.get(k, k)
|
||||
k = condition.get('key', default_key)
|
||||
k = keymap.get(k, k)
|
||||
if not k:
|
||||
k = 'name'
|
||||
k = default_key
|
||||
v = condition['value']
|
||||
op = condition.get('operator')
|
||||
if not op:
|
||||
|
@ -45,22 +49,12 @@ def parseCondition(condition, user):
|
|||
return q
|
||||
if k == 'id':
|
||||
v = decode_id(v)
|
||||
if isinstance(v, bool): #featured and public flag
|
||||
if isinstance(v, bool):
|
||||
key = k
|
||||
elif k in ('lat', 'lng', 'area', 'south', 'west', 'north', 'east', 'matches', 'id'):
|
||||
key = '%s%s' % (k, {
|
||||
'>': '__gt',
|
||||
'>=': '__gte',
|
||||
'<': '__lt',
|
||||
'<=': '__lte',
|
||||
}.get(op,''))
|
||||
elif k in ('id',):
|
||||
key = k + get_operator(op, 'int')
|
||||
else:
|
||||
key = '%s%s' % (k, {
|
||||
'==': '__iexact',
|
||||
'^': '__istartswith',
|
||||
'$': '__iendswith',
|
||||
}.get(op,'__icontains'))
|
||||
|
||||
key = k + get_operator(op, 'istr')
|
||||
key = str(key)
|
||||
if isinstance(v, unicode):
|
||||
v = unicodedata.normalize('NFKD', v).lower()
|
||||
|
|
|
@ -3,10 +3,15 @@
|
|||
import unicodedata
|
||||
|
||||
from django.db.models import Q, Manager
|
||||
from oxdjango.query import QuerySet
|
||||
|
||||
from item.utils import decode_id
|
||||
from oxdjango.managers import get_operator
|
||||
from oxdjango.query import QuerySet
|
||||
|
||||
keymap = {
|
||||
'user': 'user__username',
|
||||
}
|
||||
default_key = 'name'
|
||||
|
||||
def parseCondition(condition, user):
|
||||
'''
|
||||
|
@ -21,12 +26,10 @@ def parseCondition(condition, user):
|
|||
}
|
||||
...
|
||||
'''
|
||||
k = condition.get('key', 'name')
|
||||
k = {
|
||||
'user': 'user__username',
|
||||
}.get(k, k)
|
||||
k = condition.get('key', default_key)
|
||||
k = keymap.get(k, k)
|
||||
if not k:
|
||||
k = 'name'
|
||||
k = default_key
|
||||
v = condition['value']
|
||||
op = condition.get('operator')
|
||||
if not op:
|
||||
|
@ -44,21 +47,12 @@ def parseCondition(condition, user):
|
|||
return q
|
||||
if k == 'id':
|
||||
v = decode_id(v)
|
||||
if isinstance(v, bool): #featured and public flag
|
||||
if isinstance(v, bool):
|
||||
key = k
|
||||
elif k in ('lat', 'lng', 'area', 'south', 'west', 'north', 'east', 'matches', 'id'):
|
||||
key = '%s%s' % (k, {
|
||||
'>': '__gt',
|
||||
'>=': '__gte',
|
||||
'<': '__lt',
|
||||
'<=': '__lte',
|
||||
}.get(op,''))
|
||||
key = k + get_operator(op, 'int')
|
||||
else:
|
||||
key = '%s%s' % (k, {
|
||||
'==': '__iexact',
|
||||
'^': '__istartswith',
|
||||
'$': '__iendswith',
|
||||
}.get(op,'__icontains'))
|
||||
key = k + get_operator(op, 'istr')
|
||||
|
||||
key = str(key)
|
||||
if isinstance(v, unicode):
|
||||
|
|
|
@ -56,7 +56,7 @@ class Place(models.Model):
|
|||
|
||||
@classmethod
|
||||
def get_or_create(model, name):
|
||||
qs = model.objects.filter(name_find__icontains=u'|%s|'%name)
|
||||
qs = model.objects.filter(name_find__contains=u'|%s|' % name.lower())
|
||||
if qs.count() == 0:
|
||||
instance = model(name=name)
|
||||
instance.save()
|
||||
|
@ -148,7 +148,7 @@ class Place(models.Model):
|
|||
self.name_sort = self.name #', '.join(self.name)
|
||||
if self.geoname:
|
||||
self.geoname_sort = ', '.join(reversed(self.geoname.split(', ')))
|
||||
self.name_find = '|%s|'%'|'.join([self.name]+list(self.alternativeNames))
|
||||
self.name_find = '|%s|' % '|'.join([self.name]+list(self.alternativeNames)).lower()
|
||||
|
||||
self.defined = len(filter(None, [getattr(self, key)
|
||||
for key in ('south', 'west', 'north', 'east')])) > 0
|
||||
|
|
|
@ -50,7 +50,7 @@ def addPlace(request, data):
|
|||
n = 0
|
||||
while _exists:
|
||||
_exists = models.Place.objects.filter(defined=True,
|
||||
name_find__icontains=u'|%s|'%name).count() > 0
|
||||
name_find__contains=u'|%s|' % name.lower()).count() > 0
|
||||
if _exists:
|
||||
name = 'Untitled [%s]' %n
|
||||
n += 1
|
||||
|
@ -61,7 +61,7 @@ def addPlace(request, data):
|
|||
for n in names:
|
||||
n = ox.decode_html(name)
|
||||
if models.Place.objects.filter(defined=True,
|
||||
name_find__icontains=u'|%s|'%n).count() != 0:
|
||||
name_find__contains=u'|%s|' % n.lower()).count() != 0:
|
||||
exists = True
|
||||
existing_names.append(n)
|
||||
'''
|
||||
|
@ -130,7 +130,7 @@ def editPlace(request, data):
|
|||
for name in names + alternative_names:
|
||||
name = ox.decode_html(name)
|
||||
if models.Place.objects.filter(defined=True,
|
||||
name_find__icontains=u'|%s|'%name).exclude(id=place.id).count() != 0:
|
||||
name_find__contains=u'|%s|' % name.lower()).exclude(id=place.id).count() != 0:
|
||||
conflict = True
|
||||
conflict_names.append(name)
|
||||
'''
|
||||
|
|
|
@ -2,12 +2,20 @@
|
|||
# vi:si:et:sw=4:sts=4:ts=4
|
||||
from django.db.models import Q, Manager
|
||||
|
||||
from oxdjango.query import QuerySet
|
||||
|
||||
from item.utils import decode_id
|
||||
from oxdjango.managers import get_operator
|
||||
from oxdjango.query import QuerySet
|
||||
|
||||
import models
|
||||
|
||||
keymap = {
|
||||
'in': 'start',
|
||||
'out': 'end'
|
||||
}
|
||||
case_insensitive_keys = ('user__username', 'subscribed_users__username')
|
||||
default_key = 'name'
|
||||
|
||||
|
||||
def parseCondition(condition, user):
|
||||
'''
|
||||
|
@ -21,13 +29,10 @@ def parseCondition(condition, user):
|
|||
operator: "!="
|
||||
}
|
||||
'''
|
||||
k = condition.get('key', 'name')
|
||||
k = {
|
||||
'in': 'start',
|
||||
'out': 'end'
|
||||
}.get(k, k)
|
||||
k = condition.get('key', default_key)
|
||||
k = keymap.get(k, k)
|
||||
if not k:
|
||||
k = 'name'
|
||||
k = default_key
|
||||
v = condition['value']
|
||||
op = condition.get('operator')
|
||||
if not op:
|
||||
|
@ -47,31 +52,22 @@ def parseCondition(condition, user):
|
|||
|
||||
if k == 'id':
|
||||
public_id, points = v.split('/')
|
||||
points = [float('%0.03f'%float(p)) for p in points.split('-')]
|
||||
points = [float('%0.03f' % float(p)) for p in points.split('-')]
|
||||
q = Q(sort__item__public_id=public_id, start=points[0], end=points[1])
|
||||
return exclude and ~q or q
|
||||
if k == 'hash':
|
||||
v = models.parse_hash(v)
|
||||
v = models.parse_hash(v)
|
||||
if k == 'mode':
|
||||
v = models.Sequence.MODE[v]
|
||||
if k.endswith('__id'):
|
||||
v = decode_id(v)
|
||||
if isinstance(v, bool): #featured and public flag
|
||||
|
||||
if isinstance(v, bool):
|
||||
key = k
|
||||
elif k in ('mode', 'hash'):
|
||||
key = k
|
||||
else:
|
||||
if k in ('mode', 'hash'):
|
||||
key = k
|
||||
else:
|
||||
key = "%s%s" % (k, {
|
||||
'>': '__gt',
|
||||
'>=': '__gte',
|
||||
'<': '__lt',
|
||||
'<=': '__lte',
|
||||
'==': '__iexact',
|
||||
'=': '__icontains',
|
||||
'^': '__istartswith',
|
||||
'$': '__iendswith',
|
||||
}.get(op, '__icontains'))
|
||||
key = k + get_operator(op, 'istr')
|
||||
|
||||
key = str(key)
|
||||
if exclude:
|
||||
|
|
|
@ -2,19 +2,24 @@
|
|||
# vi:si:et:sw=4:sts=4:ts=4
|
||||
from django.db.models import Q, Manager
|
||||
|
||||
from oxdjango.managers import get_operator
|
||||
from oxdjango.query import QuerySet
|
||||
|
||||
|
||||
keymap = {
|
||||
'user': 'user__username',
|
||||
'position': 'position__position',
|
||||
'posterFrames': 'poster_frames',
|
||||
}
|
||||
default_key = 'name'
|
||||
|
||||
def parseCondition(condition, user):
|
||||
'''
|
||||
'''
|
||||
k = condition.get('key', 'name')
|
||||
k = {
|
||||
'user': 'user__username',
|
||||
'position': 'position__position',
|
||||
'posterFrames': 'poster_frames',
|
||||
}.get(k, k)
|
||||
k = condition.get('key', default_key)
|
||||
k = keymap.get(k, k)
|
||||
if not k:
|
||||
k = 'name'
|
||||
k = default_key
|
||||
v = condition['value']
|
||||
op = condition.get('operator')
|
||||
if not op:
|
||||
|
@ -35,14 +40,10 @@ def parseCondition(condition, user):
|
|||
if k == 'subscribed':
|
||||
key = 'subscribed_users__username'
|
||||
v = user.username
|
||||
elif isinstance(v, bool): #featured and public flag
|
||||
elif isinstance(v, bool):
|
||||
key = k
|
||||
else:
|
||||
key = "%s%s" % (k, {
|
||||
'==': '__iexact',
|
||||
'^': '__istartswith',
|
||||
'$': '__iendswith',
|
||||
}.get(op, '__icontains'))
|
||||
key = k + get_operator(op, 'istr')
|
||||
key = str(key)
|
||||
if exclude:
|
||||
q = ~Q(**{key: v})
|
||||
|
|
|
@ -2,10 +2,14 @@
|
|||
# vi:si:et:sw=4:sts=4:ts=4
|
||||
import unicodedata
|
||||
from django.db.models import Q, Manager
|
||||
from oxdjango.query import QuerySet
|
||||
|
||||
from item.utils import decode_id
|
||||
from oxdjango.managers import get_operator
|
||||
from oxdjango.query import QuerySet
|
||||
|
||||
keymap = {
|
||||
}
|
||||
default_key = 'title'
|
||||
|
||||
def parseCondition(condition, user):
|
||||
'''
|
||||
|
@ -20,12 +24,10 @@ def parseCondition(condition, user):
|
|||
}
|
||||
...
|
||||
'''
|
||||
k = condition.get('key', 'title')
|
||||
k = {
|
||||
'user': 'user__usertitle',
|
||||
}.get(k, k)
|
||||
k = condition.get('key', default_key)
|
||||
k = keymap.get(k, k)
|
||||
if not k:
|
||||
k = 'title'
|
||||
k = default_key
|
||||
v = condition['value']
|
||||
op = condition.get('operator')
|
||||
if not op:
|
||||
|
@ -45,23 +47,13 @@ def parseCondition(condition, user):
|
|||
if k == 'id':
|
||||
v = decode_id(v)
|
||||
elif isinstance(v, unicode):
|
||||
v = unicodedata.normalize('NFKD', v)
|
||||
if isinstance(v, bool): #featured and public flag
|
||||
v = unicodedata.normalize('NFKD', v).lower()
|
||||
if isinstance(v, bool):
|
||||
key = k
|
||||
elif k in ('lat', 'lng', 'area', 'south', 'west', 'north', 'east', 'matches', 'id'):
|
||||
key = '%s%s' % (k, {
|
||||
'>': '__gt',
|
||||
'>=': '__gte',
|
||||
'<': '__lt',
|
||||
'<=': '__lte',
|
||||
}.get(op,''))
|
||||
elif k in ('id', ):
|
||||
key = k + get_operator(op, 'int')
|
||||
else:
|
||||
key = '%s%s' % (k, {
|
||||
'==': '__iexact',
|
||||
'^': '__istartswith',
|
||||
'$': '__iendswith',
|
||||
}.get(op,'__icontains'))
|
||||
|
||||
key = k + get_operator(op, 'istr')
|
||||
key = str(key)
|
||||
if exclude:
|
||||
q = ~Q(**{key: v})
|
||||
|
|
|
@ -1,17 +1,22 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# vi:si:et:sw=4:sts=4:ts=4
|
||||
from django.db.models import Q, Manager
|
||||
from oxdjango.query import QuerySet
|
||||
from django.conf import settings
|
||||
|
||||
from oxdjango.managers import get_operator
|
||||
from oxdjango.query import QuerySet
|
||||
|
||||
keymap = {
|
||||
'email': 'user__email',
|
||||
'user': 'username',
|
||||
'group': 'user__groups__name',
|
||||
'groups': 'user__groups__name',
|
||||
}
|
||||
default_key = 'username'
|
||||
|
||||
def parseCondition(condition, user):
|
||||
k = condition.get('key', 'name')
|
||||
k = {
|
||||
'email': 'user__email',
|
||||
'user': 'username',
|
||||
'group': 'user__groups__name',
|
||||
'groups': 'user__groups__name',
|
||||
}.get(k, k)
|
||||
k = condition.get('key', default_key)
|
||||
k = keymap.get(k, k)
|
||||
v = condition['value']
|
||||
op = condition.get('operator')
|
||||
if not op:
|
||||
|
@ -28,18 +33,9 @@ def parseCondition(condition, user):
|
|||
v = levels.index(v) - 1
|
||||
else:
|
||||
v = 0
|
||||
key = '%s%s' % (k, {
|
||||
'==': '__exact',
|
||||
'<': '__lt',
|
||||
'>': '__gt',
|
||||
}.get(op,''))
|
||||
key = k + get_operator(op, 'int')
|
||||
else:
|
||||
key = '%s%s' % (k, {
|
||||
'==': '__iexact',
|
||||
'^': '__istartswith',
|
||||
'$': '__iendswith',
|
||||
}.get(op,'__icontains'))
|
||||
|
||||
key = k + get_operator(op, 'istr')
|
||||
key = str(key)
|
||||
|
||||
q = Q(**{key: v})
|
||||
|
|
Loading…
Reference in a new issue