From bc118c67ee48bf64bc5db3e7bb5a86bda88d7fb1 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Sat, 1 Oct 2011 00:28:35 +0200 Subject: [PATCH] switch to base26 ids --- pandora/annotation/managers.py | 14 ++++++++++---- pandora/annotation/models.py | 6 +++++- pandora/annotation/views.py | 18 ++++++++++-------- pandora/event/models.py | 2 +- pandora/item/models.py | 2 +- pandora/place/managers.py | 2 +- pandora/place/models.py | 7 ++++--- pandora/place/views.py | 6 +++--- pandora/user/views.py | 2 +- 9 files changed, 36 insertions(+), 23 deletions(-) diff --git a/pandora/annotation/managers.py b/pandora/annotation/managers.py index 1949115d..89600584 100644 --- a/pandora/annotation/managers.py +++ b/pandora/annotation/managers.py @@ -21,6 +21,9 @@ def parseCondition(condition, user): 'user': 'user__username', 'place': 'places__id', 'event': 'events__id', + 'in': 'start', + 'out': 'end', + 'id': 'public_id', }.get(k, k) if not k: k = 'name' @@ -40,10 +43,8 @@ def parseCondition(condition, user): return ~q else: return q - if k == 'id': - v = ox.from32(v.split('/')[-1]) - elif k in ('places__id', 'events__id'): - v = ox.from32(v) + if k in ('places__id', 'events__id'): + v = ox.from26(v) if isinstance(v, bool): #featured and public flag key = k elif k in ('lat', 'lng', 'area', 'south', 'west', 'north', 'east', 'matches', @@ -56,7 +57,12 @@ def parseCondition(condition, user): }.get(op, '')) else: key = "%s%s" % (k, { + '>': '__gt', + '>=': '__gte', + '<': '__lt', + '<=': '__lte', '==': '__iexact', + '=': '__icontains', '^': '__istartswith', '$': '__iendswith', }.get(op, '__icontains')) diff --git a/pandora/annotation/models.py b/pandora/annotation/models.py index 145edc9e..f048828b 100644 --- a/pandora/annotation/models.py +++ b/pandora/annotation/models.py @@ -117,10 +117,14 @@ class Annotation(models.Model): self.hue = self.saturation = self.lightness = 0 #FIXME: set volume here + def set_public_id(self): + public_id = Annotation.objects.filter(item=self.item, id__lt=self.id).count() + self.public_id = "%s/%s" % ( self.item.itemId, ox.to26(public_id)) + def save(self, *args, **kwargs): if not self.id: super(Annotation, self).save(*args, **kwargs) - self.public_id = '%s/%s' % (self.item.itemId, ox.to32(self.id)) + self.set_public_id() if self.duration != self.end - self.start: self.update_calculated_values() super(Annotation, self).save(*args, **kwargs) diff --git a/pandora/annotation/views.py b/pandora/annotation/views.py index f8cc9f20..66f663f9 100644 --- a/pandora/annotation/views.py +++ b/pandora/annotation/views.py @@ -45,11 +45,15 @@ def order_query(qs, sort): operator = e['operator'] if operator != '-': operator = '' - if e['key'].startswith('clip:'): + key = { + 'in': 'start', + 'out': 'end', + }.get(e['key'], e['key']) + if key.startswith('clip:'): key = annotation_sort_key(e['key'][len('clip:'):]) - else: + elif key not in ('start', 'end', 'value'): #key mgith need to be changed, see order_sort in item/views.py - key = "item__sort__%s" % e['key'] + key = "item__sort__%s" % key order = '%s%s' % (operator, key) order_by.append(order) if order_by: @@ -155,13 +159,12 @@ def removeAnnotations(request): ''' response = json_response({}) data = json.loads(request.POST['data']) - ids = map(ox.from32, data['ids']) failed = [] - for a in models.Annotation.objects.filter(id__in=ids): + for a in models.Annotation.objects.filter(public_id__in=data['ids']): if a.editable(request.user): a.delete() else: - failed.append(a.get_id) + failed.append(a.public_id) if failed: response = json_response(status=403, text='permission denied') response['data']['ids'] = failed @@ -187,8 +190,7 @@ def editAnnotation(request): ''' response = json_response({}) data = json.loads(request.POST['data']) - itemId, annotationId = data['id'].split('/') - a = get_object_or_404_json(models.Annotation, pk=ox.from32(annotationId)) + a = get_object_or_404_json(models.Annotation, public_id=data['id']) if a.editable(request.user): a.value = data['value'] a.start = data['in'] diff --git a/pandora/event/models.py b/pandora/event/models.py index e3d94b1a..f9244403 100644 --- a/pandora/event/models.py +++ b/pandora/event/models.py @@ -83,7 +83,7 @@ class Event(models.Model): super(Event, self).save(*args, **kwargs) def get_id(self): - return ox.to32(self.id) + return ox.to26(self.id) def json(self, user=None): j = { diff --git a/pandora/item/models.py b/pandora/item/models.py index 7e00d403..d480ab9d 100644 --- a/pandora/item/models.py +++ b/pandora/item/models.py @@ -291,7 +291,7 @@ class Item(models.Model): self.itemId = str(uuid.uuid1()) super(Item, self).save(*args, **kwargs) if not settings.USE_IMDB: - self.itemId = ox.to32(self.id) + self.itemId = ox.to26(self.id) oxdbId = self.oxdb_id() if oxdbId: diff --git a/pandora/place/managers.py b/pandora/place/managers.py index a44defc5..48bb8280 100644 --- a/pandora/place/managers.py +++ b/pandora/place/managers.py @@ -40,7 +40,7 @@ def parseCondition(condition, user): else: return q if k == 'id': - v = ox.from32(v) + v = ox.from26(v) if isinstance(v, bool): #featured and public flag key = k elif k in ('lat', 'lng', 'area', 'south', 'west', 'north', 'east', 'matches', 'id'): diff --git a/pandora/place/models.py b/pandora/place/models.py index 6baed313..40523c0a 100644 --- a/pandora/place/models.py +++ b/pandora/place/models.py @@ -59,9 +59,9 @@ class Place(models.Model): return False def get_id(self): - return ox.to32(self.id) + return ox.to26(self.id) - def json(self, user=None): + def json(self, keys=None, user=None): j = { 'id': self.get_id(), 'user': self.user.username, @@ -71,7 +71,8 @@ class Place(models.Model): 'name', 'alternativeNames', 'geoname', 'countryCode', 'south', 'west', 'north', 'east', 'lat', 'lng', 'area', 'matches', 'type'): - j[key] = getattr(self, key) + if not keys or key in keys: + j[key] = getattr(self, key) return j def get_matches(self): diff --git a/pandora/place/views.py b/pandora/place/views.py index 2c41670d..b78cbeed 100644 --- a/pandora/place/views.py +++ b/pandora/place/views.py @@ -71,7 +71,7 @@ def editPlace(request): can contain any of the allowed keys for place ''' data = json.loads(request.POST['data']) - place = get_object_or_404_json(models.Place, pk=ox.from32(data['id'])) + place = get_object_or_404_json(models.Place, pk=ox.from26(data['id'])) names = data.get('name', []) if isinstance(names, basestring): names = [names] @@ -111,7 +111,7 @@ def removePlace(request): data = json.loads(request.POST['data']) if isinstance(data, dict): data = data['id'] - place = get_object_or_404_json(models.Place, pk=ox.from32(data)) + place = get_object_or_404_json(models.Place, pk=ox.from26(data)) if place.editable(request.user): place.delete() response = json_response(status=200, text='deleted') @@ -227,7 +227,7 @@ Positions qs = order_query(query['qs'], query['sort']) if 'keys' in data: qs = qs[query['range'][0]:query['range'][1]] - response['data']['items'] = [p.json(request.user) for p in qs] + response['data']['items'] = [p.json(data['keys'], request.user) for p in qs] elif 'position' in query: ids = [i.get_id() for i in qs] data['conditions'] = data['conditions'] + { diff --git a/pandora/user/views.py b/pandora/user/views.py index 8a4c2f7d..b7d25318 100644 --- a/pandora/user/views.py +++ b/pandora/user/views.py @@ -274,7 +274,7 @@ def requestToken(request): user = None if user: while True: - token = ox.to32(random.randint(32768, 1048575)) + token = ox.to26(random.randint(32768, 1048575)) if models.UserProfile.objects.filter(reset_token=token).count() == 0: break user_profile = user.get_profile()