some work on advanced find

This commit is contained in:
rolux 2011-11-10 13:55:55 +00:00
parent b6f5b45154
commit 4f61bf466e
2 changed files with 50 additions and 54 deletions

View file

@ -115,7 +115,7 @@
{ {
"id": "year", "id": "year",
"title": "Year", "title": "Year",
"type": "year", // fixme: do we need this? "type": "year",
"autocomplete": true, "autocomplete": true,
"columnWidth": 60, "columnWidth": 60,
"filter": true, "filter": true,

View file

@ -37,21 +37,6 @@ def parseCondition(condition, user):
else: else:
exclude = False exclude = False
if isinstance(v, list):
q = parseCondition({'key': k, 'value': v[0], 'operator': '>='}, user) \
& parseCondition({'key': k, 'value': v[1], 'operator': '<'}, user)
if exclude:
return ~q
else:
return q
if (not exclude and op == '=' or op in ('$', '^')) and v == '':
return Q()
if k == 'filename' and (user.is_anonymous() or \
not user.get_profile().capability('canSeeFiles')):
return Q(id=0)
key_type = settings.CONFIG['keys'].get(k, {'type':'string'}).get('type') key_type = settings.CONFIG['keys'].get(k, {'type':'string'}).get('type')
if isinstance(key_type, list): if isinstance(key_type, list):
key_type = key_type[0] key_type = key_type[0]
@ -66,10 +51,19 @@ def parseCondition(condition, user):
}.get(key_type, key_type) }.get(key_type, key_type)
if k == 'list': if k == 'list':
key_type = 'list' key_type = 'list'
if k in ('isSeries', ):
key_type = 'bool'
if k in ('canPlayVideo', 'canPlayClips'): if (not exclude and op == '=' or op in ('$', '^')) and v == '':
return Q()
elif k == 'filename' and (user.is_anonymous() or \
not user.get_profile().capability('canSeeFiles')):
return Q(id=0)
elif isinstance(v, list) and len(v) == 2:
q = parseCondition({'key': k, 'value': v[0], 'operator': '>='}, user) \
& parseCondition({'key': k, 'value': v[1], 'operator': '<'}, user)
if exclude:
q = ~q
return q
elif k in ('canPlayVideo', 'canPlayClips'):
level = user.is_anonymous() and 'guest' or user.get_profile().get_level() level = user.is_anonymous() and 'guest' or user.get_profile().get_level()
allowed_level = settings.CONFIG['capabilities'][k][level] allowed_level = settings.CONFIG['capabilities'][k][level]
if v: if v:
@ -79,7 +73,7 @@ def parseCondition(condition, user):
if exclude: if exclude:
q = ~q q = ~q
return q return q
elif key_type == 'bool': elif key_type == 'boolean':
q = Q(**{'find__key': k, 'find__value': v and '1' or '0'}) q = Q(**{'find__key': k, 'find__value': v and '1' or '0'})
if exclude: if exclude:
q = ~q q = ~q
@ -116,20 +110,14 @@ def parseCondition(condition, user):
k = str(k) k = str(k)
value_key = str(value_key) value_key = str(value_key)
if exclude: if k == '*':
if k == '*': q = Q(**{value_key: v})
q = ~Q(**{value_key: v}) elif in_find:
elif in_find: q = Q(**{'find__key': k, value_key: v})
q = ~Q(**{'find__key': k, value_key: v})
else:
q = ~Q(**{value_key: v})
else: else:
if k == '*': q = Q(**{value_key: v})
q = Q(**{value_key: v}) if exclude:
elif in_find: q = ~q
q = Q(**{'find__key': k, value_key: v})
else:
q = Q(**{value_key: v})
return q return q
elif key_type == 'list': elif key_type == 'list':
q = Q(id=0) q = Q(id=0)
@ -151,33 +139,41 @@ def parseCondition(condition, user):
else: else:
q = Q(id=0) q = Q(id=0)
return q return q
else: #number or date elif key_type == 'date':
def parseDate(d): def parseDate(d):
while len(d) < 3: while len(d) < 3:
d.append(1) d.append(1)
return datetime(*[int(i) for i in d]) return datetime(*[int(i) for i in d])
if key_type == "date": #using sort here since find only contains strings
v = parseDate(v.split('.')) v = parseDate(v.split('-'))
else: vk = 'sort__%s%s' % (k, {
vk = 'value%s' % ({ '==': '__exact',
'==': '__exact', '>': '__gt',
'>': '__gt', '>=': '__gte',
'>=': '__gte', '<': '__lt',
'<': '__lt', '<=': '__lte',
'<=': '__lte', }.get(op,''))
'^': '__istartswith', q = Q(**{vk: v})
'$': '__iendswith', if exclude:
}.get(op,'__exact')) q = ~q
return q
vk = str('find__%s' % vk) else: #number
vk = 'find__value%s' % ({
if exclude: #!1960 '==': '__exact',
return ~Q(**{'find__key': k, vk: v}) '>': '__gt',
else: #1960 '>=': '__gte',
return Q(**{'find__key': k, vk: v}) '<': '__lt',
'<=': '__lte',
'^': '__istartswith',
'$': '__iendswith',
}.get(op,'__exact'))
vk = str(vk)
q = Q(**{'find__key': k, vk: v})
if exclude:
q = ~q
return q
def parseConditions(conditions, operator, user): def parseConditions(conditions, operator, user):
''' '''