2009-08-01 14:14:54 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vi:si:et:sw=4:sts=4:ts=4
|
2009-08-16 12:23:29 +00:00
|
|
|
from datetime import datetime
|
2012-11-18 19:26:13 +00:00
|
|
|
import unicodedata
|
2009-08-16 12:23:29 +00:00
|
|
|
|
|
|
|
from django.db.models import Q, Manager
|
2011-09-06 12:06:59 +00:00
|
|
|
from django.conf import settings
|
2011-01-01 11:44:42 +00:00
|
|
|
|
2012-01-09 12:17:55 +00:00
|
|
|
from archive.models import Volume
|
2011-01-11 20:12:35 +00:00
|
|
|
from itemlist.models import List
|
2009-08-16 12:23:29 +00:00
|
|
|
import models
|
2011-11-10 16:34:45 +00:00
|
|
|
import utils
|
2011-09-06 12:06:59 +00:00
|
|
|
|
2011-01-24 22:46:16 +00:00
|
|
|
from ox.django.query import QuerySet
|
2011-01-24 13:44:38 +00:00
|
|
|
|
2011-11-10 16:34:45 +00:00
|
|
|
|
2011-10-22 16:07:05 +00:00
|
|
|
def parseCondition(condition, user):
|
2010-07-14 14:35:10 +00:00
|
|
|
'''
|
|
|
|
condition: {
|
|
|
|
value: "war"
|
|
|
|
}
|
|
|
|
or
|
|
|
|
condition: {
|
|
|
|
key: "year",
|
2011-09-24 23:09:48 +00:00
|
|
|
value: [1970, 1980],
|
|
|
|
operator: "="
|
2010-07-14 14:35:10 +00:00
|
|
|
}
|
|
|
|
...
|
2011-01-01 11:44:42 +00:00
|
|
|
'''
|
2011-09-24 23:09:48 +00:00
|
|
|
k = condition.get('key', '*')
|
2010-09-23 16:01:48 +00:00
|
|
|
k = {'id': 'itemId'}.get(k, k)
|
2011-01-01 11:44:42 +00:00
|
|
|
if not k:
|
2011-09-24 23:09:48 +00:00
|
|
|
k = '*'
|
2010-07-14 14:35:10 +00:00
|
|
|
v = condition['value']
|
2011-04-22 23:34:01 +00:00
|
|
|
op = condition.get('operator')
|
2011-01-01 11:44:42 +00:00
|
|
|
if not op:
|
2011-09-22 04:01:26 +00:00
|
|
|
op = '='
|
2010-07-14 14:35:10 +00:00
|
|
|
if op.startswith('!'):
|
|
|
|
op = op[1:]
|
|
|
|
exclude = True
|
|
|
|
else:
|
|
|
|
exclude = False
|
2011-01-03 14:14:54 +00:00
|
|
|
|
2011-12-26 20:30:21 +00:00
|
|
|
facet_keys = models.Item.facet_keys + ['title']
|
|
|
|
for f in settings.CONFIG['filters']:
|
|
|
|
if f['id'] not in facet_keys:
|
|
|
|
facet_keys.append(f['id'])
|
2011-09-16 16:45:25 +00:00
|
|
|
key_type = settings.CONFIG['keys'].get(k, {'type':'string'}).get('type')
|
2011-01-24 09:38:46 +00:00
|
|
|
if isinstance(key_type, list):
|
|
|
|
key_type = key_type[0]
|
2011-01-03 08:45:31 +00:00
|
|
|
key_type = {
|
2011-01-03 14:14:54 +00:00
|
|
|
'title': 'string',
|
2011-01-03 16:02:33 +00:00
|
|
|
'person': 'string',
|
|
|
|
'text': 'string',
|
|
|
|
'year': 'string',
|
2011-01-05 13:06:09 +00:00
|
|
|
'length': 'string',
|
2011-11-08 17:43:12 +00:00
|
|
|
'layer': 'string',
|
2011-12-26 20:30:21 +00:00
|
|
|
'list': 'list',
|
2011-01-03 14:14:54 +00:00
|
|
|
}.get(key_type, key_type)
|
2011-01-11 20:12:35 +00:00
|
|
|
if k == 'list':
|
2011-11-10 17:53:12 +00:00
|
|
|
key_type = ''
|
2011-06-02 08:19:15 +00:00
|
|
|
|
2011-11-10 13:55:55 +00:00
|
|
|
if (not exclude and op == '=' or op in ('$', '^')) and v == '':
|
|
|
|
return Q()
|
|
|
|
elif k == 'filename' and (user.is_anonymous() or \
|
2013-05-31 13:47:11 +00:00
|
|
|
not user.get_profile().capability('canSeeMedia')):
|
2011-11-10 13:55:55 +00:00
|
|
|
return Q(id=0)
|
2012-01-12 08:49:08 +00:00
|
|
|
elif k == 'oshash':
|
|
|
|
return Q(files__oshash=v)
|
2012-05-29 09:26:18 +00:00
|
|
|
elif k == 'rendered':
|
|
|
|
return Q(rendered=v)
|
2011-11-10 21:22:58 +00:00
|
|
|
elif k == 'resolution':
|
|
|
|
q = parseCondition({'key': 'width', 'value': v[0], 'operator': op}, user) \
|
|
|
|
& parseCondition({'key': 'height', 'value': v[1], 'operator': op}, user)
|
|
|
|
if exclude:
|
|
|
|
q = ~q
|
|
|
|
return q
|
2012-04-02 15:33:23 +00:00
|
|
|
elif isinstance(v, list) and len(v) == 2 and op == '=':
|
2011-11-10 13:55:55 +00:00
|
|
|
q = parseCondition({'key': k, 'value': v[0], 'operator': '>='}, user) \
|
|
|
|
& parseCondition({'key': k, 'value': v[1], 'operator': '<'}, user)
|
|
|
|
if exclude:
|
|
|
|
q = ~q
|
|
|
|
return q
|
2011-11-10 14:59:12 +00:00
|
|
|
elif k in ('canplayvideo', 'canplayclips'):
|
2011-11-08 17:43:12 +00:00
|
|
|
level = user.is_anonymous() and 'guest' or user.get_profile().get_level()
|
2011-11-10 14:59:12 +00:00
|
|
|
allowed_level = settings.CONFIG['capabilities'][{
|
|
|
|
'canplayvideo': 'canPlayVideo',
|
|
|
|
'canplayclips': 'canPlayClips'
|
|
|
|
}[k]][level]
|
2011-11-08 17:43:12 +00:00
|
|
|
if v:
|
|
|
|
q = Q(level__lte=allowed_level)
|
|
|
|
else:
|
|
|
|
q = Q(level__gt=allowed_level)
|
|
|
|
if exclude:
|
|
|
|
q = ~q
|
|
|
|
return q
|
2011-11-10 13:55:55 +00:00
|
|
|
elif key_type == 'boolean':
|
2011-11-10 14:59:12 +00:00
|
|
|
q = Q(**{'find__key': k, 'find__value': v})
|
2011-11-08 17:43:12 +00:00
|
|
|
if exclude:
|
|
|
|
q = ~q
|
|
|
|
return q
|
|
|
|
elif key_type == "string":
|
2011-10-25 08:22:57 +00:00
|
|
|
in_find = not k.startswith('itemId')
|
|
|
|
if in_find:
|
|
|
|
value_key = 'find__value'
|
|
|
|
else:
|
|
|
|
value_key = k
|
2011-12-26 20:30:21 +00:00
|
|
|
if k in facet_keys:
|
2011-01-03 14:14:54 +00:00
|
|
|
in_find = False
|
2011-09-24 23:09:48 +00:00
|
|
|
facet_value = 'facets__value%s' % {
|
|
|
|
'==': '__iexact',
|
2011-09-29 17:13:07 +00:00
|
|
|
'>': '__gt',
|
|
|
|
'>=': '__gte',
|
|
|
|
'<': '__lt',
|
|
|
|
'<=': '__lte',
|
2011-09-24 23:09:48 +00:00
|
|
|
'^': '__istartswith',
|
|
|
|
'$': '__iendswith',
|
|
|
|
}.get(op, '__icontains')
|
|
|
|
v = models.Item.objects.filter(**{'facets__key':k, facet_value:v})
|
2011-10-25 08:31:58 +00:00
|
|
|
value_key = 'id__in'
|
2011-09-24 23:09:48 +00:00
|
|
|
else:
|
2011-10-25 08:22:57 +00:00
|
|
|
value_key = '%s%s' % (value_key, {
|
2011-09-24 23:09:48 +00:00
|
|
|
'==': '__iexact',
|
2011-09-29 17:13:07 +00:00
|
|
|
'>': '__gt',
|
|
|
|
'>=': '__gte',
|
|
|
|
'<': '__lt',
|
|
|
|
'<=': '__lte',
|
2011-09-24 23:09:48 +00:00
|
|
|
'^': '__istartswith',
|
|
|
|
'$': '__iendswith',
|
2011-10-25 08:22:57 +00:00
|
|
|
}.get(op, '__icontains'))
|
2011-09-24 23:09:48 +00:00
|
|
|
|
2010-07-14 14:35:10 +00:00
|
|
|
k = str(k)
|
2011-10-25 08:31:58 +00:00
|
|
|
value_key = str(value_key)
|
2012-11-18 19:26:13 +00:00
|
|
|
if isinstance(v, unicode):
|
|
|
|
v = unicodedata.normalize('NFKD', v).lower()
|
2011-11-10 13:55:55 +00:00
|
|
|
if k == '*':
|
|
|
|
q = Q(**{value_key: v})
|
|
|
|
elif in_find:
|
|
|
|
q = Q(**{'find__key': k, value_key: v})
|
2010-07-14 14:35:10 +00:00
|
|
|
else:
|
2011-11-10 13:55:55 +00:00
|
|
|
q = Q(**{value_key: v})
|
|
|
|
if exclude:
|
|
|
|
q = ~q
|
2010-11-06 16:14:00 +00:00
|
|
|
return q
|
2011-11-10 17:53:12 +00:00
|
|
|
elif k == 'list':
|
2011-11-06 13:12:15 +00:00
|
|
|
q = Q(id=0)
|
2011-09-22 04:01:26 +00:00
|
|
|
l = v.split(":")
|
2012-01-09 12:17:55 +00:00
|
|
|
if len(l) == 1:
|
|
|
|
vqs = Volume.objects.filter(name=v, user=user)
|
|
|
|
if vqs.count() == 1:
|
|
|
|
v = vqs[0]
|
|
|
|
q = Q(files__instances__volume__id=v.id)
|
|
|
|
elif len(l) >= 2:
|
2011-09-28 17:32:03 +00:00
|
|
|
l = (l[0], ":".join(l[1:]))
|
2011-01-14 06:24:40 +00:00
|
|
|
lqs = list(List.objects.filter(name=l[1], user__username=l[0]))
|
2011-10-22 16:07:05 +00:00
|
|
|
if len(lqs) == 1 and lqs[0].accessible(user):
|
|
|
|
l = lqs[0]
|
|
|
|
if l.query.get('static', False) == False:
|
|
|
|
data = l.query
|
|
|
|
q = parseConditions(data.get('conditions', []),
|
|
|
|
data.get('operator', '&'),
|
|
|
|
user)
|
|
|
|
else:
|
|
|
|
q = Q(id__in=l.items.all())
|
2011-11-02 19:19:42 +00:00
|
|
|
if exclude:
|
|
|
|
q = ~q
|
2011-10-22 16:07:05 +00:00
|
|
|
else:
|
|
|
|
q = Q(id=0)
|
2011-01-11 20:12:35 +00:00
|
|
|
return q
|
2011-11-10 13:55:55 +00:00
|
|
|
elif key_type == 'date':
|
2011-11-10 16:34:45 +00:00
|
|
|
def parse_date(d):
|
2010-07-14 14:35:10 +00:00
|
|
|
while len(d) < 3:
|
|
|
|
d.append(1)
|
|
|
|
return datetime(*[int(i) for i in d])
|
|
|
|
|
2011-11-10 13:55:55 +00:00
|
|
|
#using sort here since find only contains strings
|
2011-11-10 16:34:45 +00:00
|
|
|
v = parse_date(v.split('-'))
|
2011-11-10 13:55:55 +00:00
|
|
|
vk = 'sort__%s%s' % (k, {
|
|
|
|
'==': '__exact',
|
|
|
|
'>': '__gt',
|
|
|
|
'>=': '__gte',
|
|
|
|
'<': '__lt',
|
|
|
|
'<=': '__lte',
|
|
|
|
}.get(op,''))
|
2011-11-10 16:34:45 +00:00
|
|
|
vk = str(vk)
|
2011-11-10 13:55:55 +00:00
|
|
|
q = Q(**{vk: v})
|
|
|
|
if exclude:
|
|
|
|
q = ~q
|
|
|
|
return q
|
2011-11-10 17:53:12 +00:00
|
|
|
else: #integer, float, list, time
|
2011-11-10 16:34:45 +00:00
|
|
|
#use sort table here
|
|
|
|
if key_type == 'time':
|
|
|
|
v = int(utils.parse_time(v))
|
|
|
|
|
|
|
|
vk = 'sort__%s%s' % (k, {
|
2011-11-10 13:55:55 +00:00
|
|
|
'==': '__exact',
|
|
|
|
'>': '__gt',
|
|
|
|
'>=': '__gte',
|
|
|
|
'<': '__lt',
|
|
|
|
'<=': '__lte',
|
2011-11-10 16:34:45 +00:00
|
|
|
}.get(op,''))
|
2011-11-10 13:55:55 +00:00
|
|
|
vk = str(vk)
|
2011-11-10 16:34:45 +00:00
|
|
|
q = Q(**{vk: v})
|
2011-11-10 13:55:55 +00:00
|
|
|
if exclude:
|
|
|
|
q = ~q
|
|
|
|
return q
|
2010-07-14 14:35:10 +00:00
|
|
|
|
2011-10-22 16:07:05 +00:00
|
|
|
def parseConditions(conditions, operator, user):
|
2010-07-14 14:35:10 +00:00
|
|
|
'''
|
|
|
|
conditions: [
|
|
|
|
{
|
|
|
|
value: "war"
|
|
|
|
}
|
|
|
|
{
|
|
|
|
key: "year",
|
|
|
|
value: "1970-1980,
|
|
|
|
operator: "!="
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "country",
|
|
|
|
value: "f",
|
|
|
|
operator: "^"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
operator: "&"
|
2011-01-01 11:44:42 +00:00
|
|
|
'''
|
2010-07-14 14:35:10 +00:00
|
|
|
conn = []
|
|
|
|
for condition in conditions:
|
|
|
|
if 'conditions' in condition:
|
|
|
|
q = parseConditions(condition['conditions'],
|
2011-10-22 16:07:05 +00:00
|
|
|
condition.get('operator', '&'), user)
|
2010-07-14 14:35:10 +00:00
|
|
|
else:
|
2013-03-03 14:46:37 +00:00
|
|
|
q = parseCondition(condition, user)
|
|
|
|
if isinstance(q, list):
|
|
|
|
conn += q
|
|
|
|
elif q:
|
|
|
|
conn.append(q)
|
2010-07-14 14:35:10 +00:00
|
|
|
if conn:
|
2013-03-03 14:34:50 +00:00
|
|
|
if operator == '|':
|
|
|
|
q = conn[0]
|
|
|
|
for c in conn[1:]:
|
2010-07-14 14:35:10 +00:00
|
|
|
q = q | c
|
2013-03-03 14:34:50 +00:00
|
|
|
q = [q]
|
|
|
|
else:
|
|
|
|
q = conn
|
2010-07-14 14:35:10 +00:00
|
|
|
return q
|
2013-03-03 14:38:10 +00:00
|
|
|
return [Q()]
|
2011-01-01 11:44:42 +00:00
|
|
|
|
2010-09-23 16:01:48 +00:00
|
|
|
class ItemManager(Manager):
|
2011-01-01 11:44:42 +00:00
|
|
|
|
2009-08-01 14:14:54 +00:00
|
|
|
def get_query_set(self):
|
2011-01-24 22:46:16 +00:00
|
|
|
#return super(ItemManager, self).get_query_set()
|
|
|
|
return QuerySet(self.model)
|
2009-08-01 14:14:54 +00:00
|
|
|
|
2010-02-22 09:25:29 +00:00
|
|
|
def filter_list(self, qs, l, user):
|
2011-09-24 23:09:48 +00:00
|
|
|
if l != "*":
|
2010-02-22 09:25:29 +00:00
|
|
|
l = l.split(":")
|
|
|
|
only_public = True
|
|
|
|
if not user.is_anonymous():
|
2011-01-01 11:44:42 +00:00
|
|
|
if len(l) == 1:
|
|
|
|
l = [user.username] + l
|
|
|
|
if user.username == l[0]:
|
2010-02-22 09:25:29 +00:00
|
|
|
only_public = False
|
|
|
|
if len(l) == 2:
|
|
|
|
lqs = models.List.objects.filter(name=l[1], user__username=l[0])
|
|
|
|
if only_public:
|
2011-01-11 20:12:35 +00:00
|
|
|
lqs = lqs.filter(Q(status='public')|Q(status='featured'))
|
2010-02-22 09:25:29 +00:00
|
|
|
if lqs.count() == 1:
|
2011-01-06 07:04:10 +00:00
|
|
|
if lqs[0].query:
|
|
|
|
data = lqs[0].query
|
|
|
|
conditions = parseConditions(data['query']['conditions'],
|
2011-10-22 16:07:05 +00:00
|
|
|
data['query'].get('operator', '&'),
|
|
|
|
user)
|
2013-03-03 14:34:50 +00:00
|
|
|
for c in conditions:
|
|
|
|
qs = qs.filter(c)
|
2011-01-06 07:04:10 +00:00
|
|
|
else:
|
|
|
|
qs = qs.filter(id__in=lqs[0].items.all())
|
2010-02-22 09:25:29 +00:00
|
|
|
return qs
|
|
|
|
|
2012-02-14 16:14:04 +00:00
|
|
|
def find(self, data, user=None):
|
2010-02-22 09:25:29 +00:00
|
|
|
'''
|
|
|
|
query: {
|
|
|
|
conditions: [
|
|
|
|
{
|
2010-07-12 14:56:14 +00:00
|
|
|
value: "war"
|
2010-02-22 09:25:29 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
key: "year",
|
|
|
|
value: "1970-1980,
|
|
|
|
operator: "!="
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "country",
|
|
|
|
value: "f",
|
|
|
|
operator: "^"
|
|
|
|
}
|
|
|
|
],
|
2010-07-14 14:35:10 +00:00
|
|
|
operator: "&"
|
2010-02-22 09:25:29 +00:00
|
|
|
}
|
2011-01-01 11:44:42 +00:00
|
|
|
'''
|
2010-02-22 09:25:29 +00:00
|
|
|
|
|
|
|
#join query with operator
|
|
|
|
qs = self.get_query_set()
|
2010-09-23 16:01:48 +00:00
|
|
|
#only include items that have hard metadata
|
2011-06-01 11:03:43 +00:00
|
|
|
conditions = parseConditions(data.get('query', {}).get('conditions', []),
|
2011-10-22 16:07:05 +00:00
|
|
|
data.get('query', {}).get('operator', '&'),
|
|
|
|
user)
|
2013-03-03 14:34:50 +00:00
|
|
|
for c in conditions:
|
|
|
|
qs = qs.filter(c)
|
|
|
|
|
2012-04-02 10:56:39 +00:00
|
|
|
#FIXME: can this be avoided? needed for grid/accessed/foo
|
|
|
|
qs = qs.distinct()
|
2011-01-21 09:31:49 +00:00
|
|
|
|
|
|
|
#anonymous can only see public items
|
2012-02-14 16:14:04 +00:00
|
|
|
if not user or user.is_anonymous():
|
2013-07-23 11:38:50 +00:00
|
|
|
level = 'guest'
|
|
|
|
allowed_level = settings.CONFIG['capabilities']['canSeeItem'][level]
|
2011-09-06 12:06:59 +00:00
|
|
|
qs = qs.filter(level__lte=allowed_level)
|
2013-07-23 11:38:50 +00:00
|
|
|
rendered_q = Q(rendered=True)
|
2011-01-21 09:31:49 +00:00
|
|
|
#users can see public items, there own items and items of there groups
|
2011-09-06 12:06:59 +00:00
|
|
|
else:
|
2013-07-23 11:38:50 +00:00
|
|
|
level = user.get_profile().get_level()
|
|
|
|
allowed_level = settings.CONFIG['capabilities']['canSeeItem'][level]
|
2012-03-23 15:52:20 +00:00
|
|
|
q = Q(level__lte=allowed_level)|Q(user=user)
|
2013-07-23 11:38:50 +00:00
|
|
|
rendered_q = Q(rendered=True)|Q(user=user)
|
2012-03-23 15:52:20 +00:00
|
|
|
if user.groups.count():
|
|
|
|
q |= Q(groups__in=user.groups.all())
|
2013-07-23 11:38:50 +00:00
|
|
|
rendered_q |= Q(groups__in=user.groups.all())
|
2012-03-23 15:52:20 +00:00
|
|
|
qs = qs.filter(q)
|
2013-07-23 11:38:50 +00:00
|
|
|
if settings.CONFIG.get('itemRequiresVideo') and level != 'admin':
|
|
|
|
qs = qs.filter(rendered_q)
|
2010-02-22 09:25:29 +00:00
|
|
|
return qs
|