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