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
|
|
|
|
|
|
|
|
from django.db.models import Q, Manager
|
2011-01-01 11:44:42 +00:00
|
|
|
|
2011-01-11 20:12:35 +00:00
|
|
|
from itemlist.models import List
|
2009-08-16 12:23:29 +00:00
|
|
|
import models
|
2009-08-01 14:14:54 +00:00
|
|
|
|
2010-07-14 14:35:10 +00:00
|
|
|
def parseCondition(condition):
|
|
|
|
'''
|
|
|
|
condition: {
|
|
|
|
value: "war"
|
|
|
|
}
|
|
|
|
or
|
|
|
|
condition: {
|
|
|
|
key: "year",
|
|
|
|
value: "1970-1980,
|
|
|
|
operator: "!="
|
|
|
|
}
|
|
|
|
...
|
2011-01-01 11:44:42 +00:00
|
|
|
'''
|
2010-07-14 14:35:10 +00:00
|
|
|
k = condition.get('key', 'all')
|
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:
|
|
|
|
k = 'all'
|
2010-07-14 14:35:10 +00:00
|
|
|
v = condition['value']
|
|
|
|
op = condition.get('operator', None)
|
2011-01-01 11:44:42 +00:00
|
|
|
if not op:
|
2011-01-03 14:14:54 +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-01-03 08:45:31 +00:00
|
|
|
key_type = models.site_config['keys'].get(k, {'type':'string'}).get('type')
|
|
|
|
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-01-11 20:12:35 +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':
|
|
|
|
key_type = 'list'
|
2011-01-03 14:14:54 +00:00
|
|
|
if key_type == "string":
|
2010-07-15 15:55:10 +00:00
|
|
|
in_find=True
|
2010-11-06 16:14:00 +00:00
|
|
|
value_key = 'find__value'
|
2011-01-03 14:14:54 +00:00
|
|
|
if k in models.Item.facet_keys:
|
|
|
|
in_find = False
|
|
|
|
if op == '=':
|
2010-09-23 16:01:48 +00:00
|
|
|
v = models.Item.objects.filter(facets__key=k, facets__value=v)
|
2011-01-03 14:14:54 +00:00
|
|
|
elif op == '^':
|
|
|
|
v = models.Item.objects.filter(facets__key=k, facets__value__istartswith=v)
|
|
|
|
elif op == '$':
|
|
|
|
v = models.Item.objects.filter(facets__key=k, facets__value__iendswith=v)
|
2011-01-04 10:37:45 +00:00
|
|
|
else:
|
|
|
|
v = models.Item.objects.filter(facets__key=k, facets__value__icontains=v)
|
2011-01-03 14:14:54 +00:00
|
|
|
k = 'id__in'
|
|
|
|
elif op == '=':
|
|
|
|
value_key = 'find__value__iexact'
|
2010-07-14 14:35:10 +00:00
|
|
|
elif op == '^':
|
|
|
|
v = v[1:]
|
2010-11-06 16:14:00 +00:00
|
|
|
value_key = 'find__value__istartswith'
|
2010-07-14 14:35:10 +00:00
|
|
|
elif op == '$':
|
|
|
|
v = v[:-1]
|
2010-11-06 16:14:00 +00:00
|
|
|
value_key = 'find__value__iendswith'
|
2011-01-03 14:14:54 +00:00
|
|
|
else: # default
|
2010-11-06 16:14:00 +00:00
|
|
|
value_key = 'find__value__icontains'
|
2010-07-14 14:35:10 +00:00
|
|
|
k = str(k)
|
|
|
|
if exclude:
|
2010-11-06 16:14:00 +00:00
|
|
|
if in_find and not k.startswith('itemId'):
|
2011-01-01 11:44:42 +00:00
|
|
|
q = ~Q(**{'find__key': k, value_key: v})
|
2010-11-06 16:14:00 +00:00
|
|
|
else:
|
2011-01-01 11:44:42 +00:00
|
|
|
q = ~Q(**{k: v})
|
2010-07-14 14:35:10 +00:00
|
|
|
else:
|
2010-11-06 16:14:00 +00:00
|
|
|
if in_find and not k.startswith('itemId'):
|
2011-01-01 11:44:42 +00:00
|
|
|
q = Q(**{'find__key': k, value_key: v})
|
2010-11-06 16:14:00 +00:00
|
|
|
else:
|
2011-01-01 11:44:42 +00:00
|
|
|
q = Q(**{k: v})
|
2010-11-06 16:14:00 +00:00
|
|
|
return q
|
2011-01-11 20:12:35 +00:00
|
|
|
elif key_type == 'list':
|
2011-01-13 15:32:14 +00:00
|
|
|
q = Q(itemId=False)
|
2011-01-11 20:12:35 +00:00
|
|
|
l = v.split(".")
|
2011-01-13 15:32:14 +00:00
|
|
|
if len(l) == 2:
|
2011-01-14 06:24:40 +00:00
|
|
|
lqs = list(List.objects.filter(name=l[1], user__username=l[0]))
|
|
|
|
if len(lqs) == 1:
|
2011-01-13 19:40:50 +00:00
|
|
|
l = lqs[0]
|
|
|
|
if l.query.get('static', False) == False:
|
|
|
|
data = l.query
|
|
|
|
q = parseConditions(data.get('conditions', []),
|
2011-01-13 15:32:14 +00:00
|
|
|
data.get('operator', '&'))
|
|
|
|
else:
|
2011-01-13 19:40:50 +00:00
|
|
|
q = Q(id__in=l.items.all())
|
2011-01-11 20:12:35 +00:00
|
|
|
return q
|
2010-07-14 14:35:10 +00:00
|
|
|
else: #number or date
|
2011-01-01 11:44:42 +00:00
|
|
|
|
2010-07-14 14:35:10 +00:00
|
|
|
def parseDate(d):
|
|
|
|
while len(d) < 3:
|
|
|
|
d.append(1)
|
|
|
|
return datetime(*[int(i) for i in d])
|
|
|
|
if op == '-':
|
|
|
|
v1 = v[1]
|
|
|
|
v2 = v[2]
|
2011-01-03 14:14:54 +00:00
|
|
|
if key_type == "date":
|
2010-07-14 14:35:10 +00:00
|
|
|
v1 = parseDate(v1.split('.'))
|
|
|
|
v2 = parseDate(v2.split('.'))
|
|
|
|
|
|
|
|
if exclude: #!1960-1970
|
2010-12-06 23:31:41 +00:00
|
|
|
k1 = 'value__lt'
|
|
|
|
k2 = 'value__gte'
|
2011-01-01 11:44:42 +00:00
|
|
|
return Q(**{'find__key': k, k1: v1})|Q(**{'find__key': k, k2: v2})
|
2010-07-14 14:35:10 +00:00
|
|
|
else: #1960-1970
|
2010-12-06 23:31:41 +00:00
|
|
|
k1 = 'value__gte'
|
|
|
|
k2 = 'value__lt'
|
2011-01-01 11:44:42 +00:00
|
|
|
return Q(**{'find__key': k, k1: v1})&Q(**{'find__key': k, k2: v2})
|
2010-07-14 14:35:10 +00:00
|
|
|
else:
|
2011-01-03 14:14:54 +00:00
|
|
|
if key_type == "date":
|
2010-07-14 14:35:10 +00:00
|
|
|
v = parseDate(v.split('.'))
|
|
|
|
if op == '=':
|
2010-12-06 23:31:41 +00:00
|
|
|
vk = 'value__exact'
|
2010-07-14 14:35:10 +00:00
|
|
|
elif op == '>':
|
2010-12-06 23:31:41 +00:00
|
|
|
vk = 'value__gt'
|
2010-07-14 14:35:10 +00:00
|
|
|
elif op == '>=':
|
2010-12-06 23:31:41 +00:00
|
|
|
vk = 'value__gte'
|
2010-07-14 14:35:10 +00:00
|
|
|
elif op == '<':
|
2010-12-06 23:31:41 +00:00
|
|
|
vk = 'value__lt'
|
2010-07-14 14:35:10 +00:00
|
|
|
elif op == '<=':
|
2010-12-06 23:31:41 +00:00
|
|
|
vk = 'value__lte'
|
2011-01-03 14:14:54 +00:00
|
|
|
elif op == '':
|
|
|
|
vk = 'value__exact'
|
2010-12-06 23:31:41 +00:00
|
|
|
|
|
|
|
vk = 'find__%s' % vk
|
|
|
|
vk = str(vk)
|
2010-07-14 14:35:10 +00:00
|
|
|
|
|
|
|
if exclude: #!1960
|
2011-01-01 11:44:42 +00:00
|
|
|
return ~Q(**{'find__key': k, vk: v})
|
2010-07-14 14:35:10 +00:00
|
|
|
else: #1960
|
2011-01-01 11:44:42 +00:00
|
|
|
return Q(**{'find__key': k, vk: v})
|
|
|
|
|
2010-07-14 14:35:10 +00:00
|
|
|
|
|
|
|
def parseConditions(conditions, operator):
|
|
|
|
'''
|
|
|
|
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'],
|
|
|
|
condition.get('operator', '&'))
|
2011-01-01 11:44:42 +00:00
|
|
|
if q:
|
|
|
|
conn.append(q)
|
2010-07-14 14:35:10 +00:00
|
|
|
pass
|
|
|
|
else:
|
2011-01-01 11:44:42 +00:00
|
|
|
if condition.get('value', '') != '' or \
|
|
|
|
condition.get('operator', '') == '=':
|
2010-07-15 12:12:04 +00:00
|
|
|
conn.append(parseCondition(condition))
|
2010-07-14 14:35:10 +00:00
|
|
|
if conn:
|
|
|
|
q = conn[0]
|
|
|
|
for c in conn[1:]:
|
|
|
|
if operator == '|':
|
|
|
|
q = q | c
|
|
|
|
else:
|
|
|
|
q = q & c
|
|
|
|
return q
|
|
|
|
return None
|
|
|
|
|
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):
|
2010-09-23 16:01:48 +00:00
|
|
|
return super(ItemManager, self).get_query_set()
|
2009-08-01 14:14:54 +00:00
|
|
|
|
2010-02-22 09:25:29 +00:00
|
|
|
def filter_list(self, qs, l, user):
|
|
|
|
if l != "all":
|
|
|
|
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'],
|
|
|
|
data['query'].get('operator', '&'))
|
|
|
|
if conditions:
|
|
|
|
qs = qs.filter(conditions)
|
|
|
|
else:
|
|
|
|
qs = qs.filter(id__in=lqs[0].items.all())
|
2010-02-22 09:25:29 +00:00
|
|
|
return qs
|
|
|
|
|
2010-07-07 22:11:34 +00:00
|
|
|
def find(self, data, user):
|
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
|
2010-02-22 09:25:29 +00:00
|
|
|
qs = qs.filter(available=True)
|
2011-01-13 19:40:50 +00:00
|
|
|
conditions = parseConditions(data['query'].get('conditions', []),
|
2010-07-14 14:35:10 +00:00
|
|
|
data['query'].get('operator', '&'))
|
2010-02-22 09:25:29 +00:00
|
|
|
if conditions:
|
2010-07-14 14:35:10 +00:00
|
|
|
qs = qs.filter(conditions)
|
2010-02-22 09:25:29 +00:00
|
|
|
|
2010-07-14 14:35:10 +00:00
|
|
|
#FIXME: lists are part of query now
|
2010-02-22 09:25:29 +00:00
|
|
|
# filter list, works for own or public lists
|
|
|
|
l = data.get('list', 'all')
|
|
|
|
qs = self.filter_list(qs, l, user)
|
|
|
|
return qs
|