forked from 0x2620/pandora
add time type, use sort table for numbers and dates in find api
This commit is contained in:
parent
c3b5d19e9f
commit
fbcec1bf18
4 changed files with 28 additions and 11 deletions
|
@ -133,7 +133,7 @@
|
|||
{
|
||||
"id": "runtime",
|
||||
"title": "Runtime",
|
||||
"type": "integer",
|
||||
"type": "time",
|
||||
"columnWidth": 60,
|
||||
"format": {"type": "duration", "args": [0, "short"]}
|
||||
},
|
||||
|
|
|
@ -7,9 +7,11 @@ from django.conf import settings
|
|||
|
||||
from itemlist.models import List
|
||||
import models
|
||||
import utils
|
||||
|
||||
from ox.django.query import QuerySet
|
||||
|
||||
|
||||
def parseCondition(condition, user):
|
||||
'''
|
||||
condition: {
|
||||
|
@ -143,13 +145,13 @@ def parseCondition(condition, user):
|
|||
q = Q(id=0)
|
||||
return q
|
||||
elif key_type == 'date':
|
||||
def parseDate(d):
|
||||
def parse_date(d):
|
||||
while len(d) < 3:
|
||||
d.append(1)
|
||||
return datetime(*[int(i) for i in d])
|
||||
|
||||
#using sort here since find only contains strings
|
||||
v = parseDate(v.split('-'))
|
||||
v = parse_date(v.split('-'))
|
||||
vk = 'sort__%s%s' % (k, {
|
||||
'==': '__exact',
|
||||
'>': '__gt',
|
||||
|
@ -157,23 +159,25 @@ def parseCondition(condition, user):
|
|||
'<': '__lt',
|
||||
'<=': '__lte',
|
||||
}.get(op,''))
|
||||
vk = str(vk)
|
||||
q = Q(**{vk: v})
|
||||
if exclude:
|
||||
q = ~q
|
||||
return q
|
||||
else: #number
|
||||
vk = 'find__value%s' % ({
|
||||
else: #numbers
|
||||
#use sort table here
|
||||
if key_type == 'time':
|
||||
v = int(utils.parse_time(v))
|
||||
|
||||
vk = 'sort__%s%s' % (k, {
|
||||
'==': '__exact',
|
||||
'>': '__gt',
|
||||
'>=': '__gte',
|
||||
'<': '__lt',
|
||||
'<=': '__lte',
|
||||
'^': '__istartswith',
|
||||
'$': '__iendswith',
|
||||
}.get(op,'__exact'))
|
||||
}.get(op,''))
|
||||
vk = str(vk)
|
||||
|
||||
q = Q(**{'find__key': k, vk: v})
|
||||
q = Q(**{vk: v})
|
||||
if exclude:
|
||||
q = ~q
|
||||
return q
|
||||
|
|
|
@ -1156,7 +1156,7 @@ attrs = {
|
|||
'item': models.OneToOneField('Item', related_name='sort', primary_key=True),
|
||||
'duration': models.FloatField(null=True, blank=True, db_index=True),
|
||||
}
|
||||
for key in filter(lambda k: 'columnWidth' in k, settings.CONFIG['itemKeys']):
|
||||
for key in filter(lambda k: 'columnWidth' in k or k['type'] in ('integer', 'time', 'float', 'data'), settings.CONFIG['itemKeys']):
|
||||
name = key['id']
|
||||
name = {'id': 'itemId'}.get(name, name)
|
||||
sort_type = key.get('sort', key['type'])
|
||||
|
@ -1177,6 +1177,7 @@ for key in filter(lambda k: 'columnWidth' in k, settings.CONFIG['itemKeys']):
|
|||
'length': 'integer',
|
||||
'date': 'date',
|
||||
'hue': 'float',
|
||||
'time': 'integer',
|
||||
'label': 'integer',
|
||||
}.get(sort_type, sort_type)]
|
||||
attrs[name] = model[0](**model[1])
|
||||
|
|
|
@ -20,6 +20,18 @@ def parse_decimal(string):
|
|||
d = string.split('/')
|
||||
return Decimal(d[0]) / Decimal(d[1])
|
||||
|
||||
def parse_time(t):
|
||||
'''
|
||||
parse time string and return seconds as float
|
||||
'''
|
||||
s = 0.0
|
||||
p = t.split(':')
|
||||
for i in range(len(p)):
|
||||
_p = p[i]
|
||||
if _p.endswith('.'):
|
||||
_p =_p[:-1]
|
||||
s = s * 60 + float(_p)
|
||||
return s
|
||||
|
||||
def plural_key(term):
|
||||
return {
|
||||
|
|
Loading…
Reference in a new issue