pandora/pandora/clip/managers.py

210 lines
6.3 KiB
Python
Raw Normal View History

2011-10-02 20:18:42 +02:00
# -*- coding: utf-8 -*-
2012-11-18 20:34:43 +01:00
import unicodedata
from six import string_types
2011-10-02 20:18:42 +02:00
from django.db.models import Q, Manager
from django.conf import settings
2016-02-20 09:06:41 +00:00
from oxdjango.query import QuerySet
2011-11-06 20:36:35 +01:00
from item.utils import decode_id, get_by_id
from oxdjango.managers import get_operator
2011-11-06 20:36:35 +01:00
2011-10-02 20:18:42 +02:00
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'
2011-10-02 20:18:42 +02:00
def parseCondition(condition, user):
'''
condition: {
value: "war"
}
or
condition: {
key: "year",
value: "1970-1980,
operator: "!="
}
'''
k = condition.get('key', default_key)
k = keymap.get(k, k)
2011-10-02 20:18:42 +02:00
if not k:
k = default_key
2011-10-02 20:18:42 +02:00
v = condition['value']
op = condition.get('operator')
if not op:
op = ''
if get_by_id(settings.CONFIG['layers'], k):
2016-06-30 02:41:44 +02:00
return parseCondition({'key': 'annotations__layer',
2011-10-03 13:29:10 +02:00
'value': k,
2016-06-30 02:41:44 +02:00
'operator': '==='}, user) & \
parseCondition({'key': 'annotations__findvalue',
'value': v,
'operator': op}, user)
2011-10-03 13:29:10 +02:00
2011-10-02 20:18:42 +02:00
if op.startswith('!'):
op = op[1:]
exclude = True
else:
exclude = False
if op == '-':
q = parseCondition({'key': k, 'value': v[0], 'operator': '>='}, user) \
& parseCondition({'key': k, 'value': v[1], 'operator': '<'}, user)
2012-06-18 16:36:04 +02:00
return exclude and ~q or q
2011-11-07 12:09:38 +01:00
if (not exclude and op == '=' or op in ('$', '^', '>=', '<')) and v == '':
2011-11-10 12:09:43 +01:00
return Q()
2011-11-07 12:09:38 +01:00
2012-06-18 16:36:04 +02:00
if k == 'id':
2014-09-19 12:26:46 +00:00
public_id, points = v.split('/')
points = [float('%0.03f' % float(p)) for p in points.split('-')]
2014-09-19 12:26:46 +00:00
q = Q(item__public_id=public_id, start=points[0], end=points[1])
2012-06-18 16:36:04 +02:00
return exclude and ~q or q
elif k.endswith('__id'):
2011-11-06 20:36:35 +01:00
v = decode_id(v)
if isinstance(v, bool):
2011-10-02 20:18:42 +02:00
key = k
elif k in ('id', ) or k.endswith('__id'):
key = k + get_operator(op, 'int')
2011-10-02 20:18:42 +02:00
else:
key = k + get_operator(op, 'istr' if k in case_insensitive_keys else 'str')
2011-10-02 20:18:42 +02:00
key = str(key)
if isinstance(v, string_types) and op != '===':
2012-11-18 20:34:43 +01:00
v = unicodedata.normalize('NFKD', v).lower()
2011-10-02 20:18:42 +02:00
if exclude:
q = ~Q(**{key: v})
else:
q = Q(**{key: v})
return q
def parseConditions(conditions, operator, user):
'''
conditions: [
{
value: "war"
}
{
key: "year",
value: "1970-1980,
operator: "!="
},
{
key: "country",
value: "f",
operator: "^"
}
],
operator: "&"
'''
conn = []
for condition in conditions:
if 'conditions' in condition:
q = parseConditions(condition['conditions'],
condition.get('operator', '&'), user)
if q:
conn.append(q)
pass
else:
conn.append(parseCondition(condition, user))
if conn:
q = conn[0]
for c in conn[1:]:
if operator == '|':
q = q | c
else:
q = q & c
return q
return None
class ClipManager(Manager):
def get_query_set(self):
return QuerySet(self.model)
def filter_annotations(self, data, user):
layer_ids = [k['id'] for k in settings.CONFIG['layers']]
keys = layer_ids + ['annotations', 'text', '*']
conditions = data.get('query', {}).get('conditions', [])
2017-02-16 14:24:51 +01:00
conditions = list(filter(lambda c: c['key'] in keys, conditions))
operator = data.get('query', {}).get('operator', '&')
def parse(condition):
key = 'findvalue' + get_operator(condition.get('operator', ''))
2012-11-18 20:41:18 +01:00
v = condition['value']
if isinstance(v, string_types):
2012-11-18 20:41:18 +01:00
v = unicodedata.normalize('NFKD', v).lower()
q = Q(**{key: v})
if condition['key'] in layer_ids:
q = q & Q(layer=condition['key'])
return q
conditions = [parse(c) for c in conditions]
if conditions:
q = conditions[0]
for c in conditions[1:]:
if operator == '|':
q = q | c
else:
q = q & c
return q
return None
2011-10-02 20:18:42 +02:00
def find(self, data, user):
'''
query: {
conditions: [
{
value: "war"
}
{
key: "year",
value: "1970-1980,
operator: "!="
},
{
key: "country",
value: "f",
operator: "^"
}
],
operator: "&"
}
'''
#join query with operator
qs = self.get_query_set()
conditions = parseConditions(data.get('query', {}).get('conditions', []),
data.get('query', {}).get('operator', '&'),
user)
if conditions:
qs = qs.filter(conditions)
2017-01-20 13:00:04 +01:00
qs = qs.distinct()
2011-10-02 20:18:42 +02:00
if 'keys' in data:
layer_ids = [k['id'] for k in settings.CONFIG['layers']]
2017-02-16 14:24:51 +01:00
for l in list(filter(lambda k: k in layer_ids, data['keys'])):
qs = qs.filter(**{l: True})
2012-03-10 00:55:45 +01:00
#anonymous can only see public clips
if not user or user.is_anonymous():
allowed_level = settings.CONFIG['capabilities']['canSeeItem']['guest']
2012-03-22 22:33:42 +01:00
qs = qs.filter(sort__rightslevel__lte=allowed_level)
2012-03-10 00:55:45 +01:00
#users can see public clips, there own clips and clips of there groups
else:
2016-02-19 22:04:15 +05:30
allowed_level = settings.CONFIG['capabilities']['canSeeItem'][user.profile.get_level()]
2012-03-22 22:33:42 +01:00
q = Q(sort__rightslevel__lte=allowed_level)|Q(user=user.id)
if user.groups.count():
q |= Q(item__groups__in=user.groups.all())
qs = qs.filter(q)
2012-03-10 00:55:45 +01:00
#admins can see all available clips
2011-10-02 20:18:42 +02:00
return qs