From ed37cd69242c6880fda2704230f3ea0e3c6a7b0d Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Sat, 14 Jan 2012 18:13:40 +0530 Subject: [PATCH] add getPlaceNames/getEventNames api --- pandora/event/views.py | 32 ++++++++++++++++++++++++++++++++ pandora/place/views.py | 32 +++++++++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/pandora/event/views.py b/pandora/event/views.py index 173c0331b..fd2964d98 100644 --- a/pandora/event/views.py +++ b/pandora/event/views.py @@ -2,6 +2,9 @@ # vi:si:et:sw=4:sts=4:ts=4 from __future__ import division +from django.db.models import Max, Min, Count +from django.conf import settings + import ox from ox.utils import json from ox.django.decorators import login_required_json @@ -213,3 +216,32 @@ Positions return render_to_json_response(response) actions.register(findEvents) + +def getEventNames(request): + ''' + param data { + } + return { + status: { + code: int, + text: string + }, + data: { + items: [ + {name:, matches} + ] + } + } + ''' + response = json_response({}) + layers = [l['id'] for l in filter(lambda l: l['type'] == 'event', + settings.CONFIG['layers'])] + items = models.Annotation.objects.filter(layer__in=layers, + events__id=None).order_by('value') + items = items.values('value').annotate(Count('value')) + response['data']['items'] = [{ + 'name': i['value'], + 'matches': i['value__count'] + } for i in items] + return render_to_json_response(response) +actions.register(getEventNames) diff --git a/pandora/place/views.py b/pandora/place/views.py index 7eb73f711..e32366617 100644 --- a/pandora/place/views.py +++ b/pandora/place/views.py @@ -2,7 +2,8 @@ # vi:si:et:sw=4:sts=4:ts=4 from __future__ import division -from django.db.models import Max, Min +from django.db.models import Max, Min, Count +from django.conf import settings import ox from ox.utils import json @@ -278,3 +279,32 @@ Positions ) return render_to_json_response(response) actions.register(findPlaces) + +def getPlaceNames(request): + ''' + param data { + } + return { + status: { + code: int, + text: string + }, + data: { + items: [ + {name:, matches} + ] + } + } + ''' + response = json_response({}) + layers = [l['id'] for l in filter(lambda l: l['type'] == 'place', + settings.CONFIG['layers'])] + items = models.Annotation.objects.filter(layer__in=layers, + places__id=None).order_by('value') + items = items.values('value').annotate(Count('value')) + response['data']['items'] = [{ + 'name': i['value'], + 'matches': i['value__count'] + } for i in items] + return render_to_json_response(response) +actions.register(getPlaceNames)