pandora/pandora/itemlist/managers.py

133 lines
3.3 KiB
Python
Raw Normal View History

2011-01-11 10:19:49 +00:00
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
from django.db.models import Q, Manager
2011-01-11 14:56:08 +00:00
def parseCondition(condition, user):
2011-01-11 10:19:49 +00:00
'''
'''
k = condition.get('key', 'name')
2011-01-11 20:12:35 +00:00
k = {
'user': 'user__username',
'position': 'position__position',
2011-09-29 13:05:34 +00:00
'posterFrames': 'poster_frames',
2011-01-11 20:12:35 +00:00
}.get(k, k)
2011-01-11 10:19:49 +00:00
if not k:
k = 'name'
v = condition['value']
2011-04-22 23:34:01 +00:00
op = condition.get('operator')
2011-01-11 10:19:49 +00:00
if not op:
2011-09-28 18:00:13 +00:00
op = '='
2011-01-11 10:19:49 +00:00
if op.startswith('!'):
op = op[1:]
exclude = True
else:
exclude = False
2011-01-17 21:12:52 +00:00
if k == 'id':
2011-09-28 17:32:03 +00:00
v = v.split(":")
if len(v) >= 2:
v = (v[0], ":".join(v[1:]))
2011-01-17 21:12:52 +00:00
q = Q(user__username=v[0], name=v[1])
else:
q = Q(id__in=[])
return q
2011-01-11 10:21:23 +00:00
if k == 'subscribed':
2011-01-11 14:56:08 +00:00
key = 'subscribed_users__username'
v = user.username
elif isinstance(v, bool): #featured and public flag
2011-01-11 10:19:49 +00:00
key = k
else:
key = "%s%s" % (k, {
'==': '__iexact',
'^': '__istartswith',
'$': '__iendswith',
}.get(op, '__icontains'))
2011-02-23 10:34:24 +00:00
key = str(key)
2011-01-11 10:19:49 +00:00
if exclude:
q = ~Q(**{key: v})
else:
q = Q(**{key: v})
return q
2011-01-11 14:56:08 +00:00
def parseConditions(conditions, operator, user):
2011-01-11 10:19:49 +00:00
'''
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'],
2011-01-11 14:56:08 +00:00
condition.get('operator', '&'), user)
2011-01-11 10:19:49 +00:00
if q:
conn.append(q)
pass
else:
2011-09-28 18:00:13 +00:00
conn.append(parseCondition(condition, user))
2011-01-11 10:19:49 +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
class ListManager(Manager):
def get_query_set(self):
return super(ListManager, self).get_query_set()
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()
2011-01-11 14:56:08 +00:00
conditions = parseConditions(data['query'].get('conditions', []),
data['query'].get('operator', '&'),
user)
2011-01-11 10:19:49 +00:00
if conditions:
qs = qs.filter(conditions)
2011-01-11 16:19:27 +00:00
if user.is_anonymous():
2011-01-11 11:55:43 +00:00
qs = qs.filter(Q(status='public') | Q(status='featured'))
else:
qs = qs.filter(Q(status='public') | Q(status='featured') | Q(user=user))
2011-09-29 13:05:34 +00:00
return qs