From fbcec1bf18f07110cadcf0130c7b6bc4a81a7f3c Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Thu, 10 Nov 2011 16:34:45 +0000 Subject: [PATCH] add time type, use sort table for numbers and dates in find api --- pandora/0xdb.jsonc | 2 +- pandora/item/managers.py | 22 +++++++++++++--------- pandora/item/models.py | 3 ++- pandora/item/utils.py | 12 ++++++++++++ 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/pandora/0xdb.jsonc b/pandora/0xdb.jsonc index d485c8b97..13f1b05ab 100644 --- a/pandora/0xdb.jsonc +++ b/pandora/0xdb.jsonc @@ -133,7 +133,7 @@ { "id": "runtime", "title": "Runtime", - "type": "integer", + "type": "time", "columnWidth": 60, "format": {"type": "duration", "args": [0, "short"]} }, diff --git a/pandora/item/managers.py b/pandora/item/managers.py index d719fe44c..15360861e 100644 --- a/pandora/item/managers.py +++ b/pandora/item/managers.py @@ -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 diff --git a/pandora/item/models.py b/pandora/item/models.py index 6d7a47f5e..c8f8b9d82 100644 --- a/pandora/item/models.py +++ b/pandora/item/models.py @@ -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]) diff --git a/pandora/item/utils.py b/pandora/item/utils.py index 360bd5390..55eca3ece 100644 --- a/pandora/item/utils.py +++ b/pandora/item/utils.py @@ -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 {