From f62f254f988efbae88ec9aaa783e796c93863f5a Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Thu, 24 Feb 2011 19:39:58 +0100 Subject: [PATCH] places --- pandora/itemlist/views.py | 6 +- pandora/place/managers.py | 10 +-- pandora/place/models.py | 56 ++++++++++------ pandora/place/views.py | 137 ++++++++++++++++++++++++++++++-------- 4 files changed, 153 insertions(+), 56 deletions(-) diff --git a/pandora/itemlist/views.py b/pandora/itemlist/views.py index ffc494370..3ad4024df 100644 --- a/pandora/itemlist/views.py +++ b/pandora/itemlist/views.py @@ -31,7 +31,7 @@ def _order_query(qs, sort): qs = qs.order_by(*order_by) return qs -def _parse_query(data, user): +def parse_query(data, user): query = {} query['range'] = [0, 100] query['sort'] = [{'key':'user', 'operator':'+'}, {'key':'name', 'operator':'+'}] @@ -69,14 +69,14 @@ def findLists(request): } return {status: {code: int, text: string}, data: { - lists: [ + items: [ {name:, user:, featured:, public...} ] } } ''' data = json.loads(request.POST['data']) - query = _parse_query(data, request.user) + query = parse_query(data, request.user) #order is_section_request = query['sort'] == [{u'operator': u'+', u'key': u'position'}] diff --git a/pandora/place/managers.py b/pandora/place/managers.py index bef67bb77..4cdf181de 100644 --- a/pandora/place/managers.py +++ b/pandora/place/managers.py @@ -8,13 +8,13 @@ class PlaceManager(Manager): def get_query_set(self): return super(PlaceManager, self).get_query_set() - def find(self, q='', f="globe", sw_lat=-180.0, sw_lng=-180.0, ne_lat=180.0, ne_lng=180.0): + def find(self, q='', south=-180.0, west=-180.0, north=180.0, east=180.0): qs = self.get_query_set() qs = qs.filter(Q( - Q(Q(sw_lat__gt=sw_lat)|Q(sw_lat__lt=ne_lat)|Q(sw_lng__gt=sw_lng)|Q(sw_lng__lt=ne_lng)) & - Q(Q(sw_lat__gt=sw_lat)|Q(sw_lat__lt=ne_lat)|Q(sw_lng__lt=ne_lng)|Q(ne_lng__gt=ne_lng)) & - Q(Q(ne_lat__gt=sw_lat)|Q(ne_lat__lt=ne_lat)|Q(sw_lng__gt=sw_lng)|Q(sw_lng__lt=ne_lng)) & - Q(Q(ne_lat__gt=sw_lat)|Q(ne_lat__lt=ne_lat)|Q(ne_lng__gt=sw_lng)|Q(ne_lng__lt=ne_lng)) + Q(Q(south__gt=south)|Q(south__lt=north)|Q(west__gt=west)|Q(west__lt=east)) & + Q(Q(south__gt=south)|Q(south__lt=north)|Q(west__lt=east)|Q(east__gt=east)) & + Q(Q(north__gt=south)|Q(north__lt=north)|Q(west__gt=west)|Q(west__lt=east)) & + Q(Q(north__gt=south)|Q(north__lt=north)|Q(east__gt=west)|Q(east__lt=east)) )) if q: qs = qs.filter(name_find__icontains=q) diff --git a/pandora/place/models.py b/pandora/place/models.py index c0780502f..f0a80b9dd 100644 --- a/pandora/place/models.py +++ b/pandora/place/models.py @@ -6,6 +6,7 @@ from django.db import models import ox from ox.django import fields +from django.contrib.auth.models import User, Group import managers @@ -14,24 +15,26 @@ class Place(models.Model): ''' Places are named locations, they should have geographical information attached to them. ''' + created = models.DateTimeField(auto_now_add=True) + modified = models.DateTimeField(auto_now=True) + user = models.ForeignKey(User, null=True, related_name='items') - name = models.CharField(max_length=200, unique=True) + name = fields.TupleField(default=[]) name_sort = models.CharField(max_length=200) name_find = models.TextField(default='', editable=False) geoname = models.CharField(max_length=1024, unique=True) - geoname_reverse = models.CharField(max_length=1024, unique=True) + geoname_sort = models.CharField(max_length=1024, unique=True) wikipediaId = models.CharField(max_length=1000, blank=True) - aliases = fields.TupleField(default=[]) - sw_lat = models.FloatField(default=0) - sw_lng = models.FloatField(default=0) - ne_lat = models.FloatField(default=0) - ne_lng = models.FloatField(default=0) - center_lat = models.FloatField(default=0) - center_lng = models.FloatField(default=0) - area = models.FloatField(default=-1) + south = models.FloatField(default=0) + west = models.FloatField(default=0) + north = models.FloatField(default=0) + east = models.FloatField(default=0) + lat = models.FloatField(default=0) + lng = models.FloatField(default=0) + size = models.FloatField(default=-1) objects = managers.PlaceManager() @@ -41,25 +44,38 @@ class Place(models.Model): def __unicode__(self): return self.name + def editable(self, user): + if user.is_staff or self.user == user: + return True + return False + + def get_id(self): + return ox.to32(self.id) + def json(self): - j = {} - for key in ('name', 'name_sort', 'aliases', 'geoname', 'geoname_reversed', - 'sw_lat', 'sw_lng', 'ne_lat', 'ne_lng', - 'center_lat', 'center_lng'): + j = { + 'id': self.get_id(), + 'user': self.user.username, + } + for key in ('created', 'modified', + 'name', 'geoname', + 'south', 'west', 'north', 'east', + 'lat', 'lng', 'size'): j[key] = getattr(self, key) + return j def save(self, *args, **kwargs): if not self.name_sort: - self.name_sort = self.name - self.geoname_reverse = ', '.join(reversed(self.geoname.split(', '))) + self.name_sort = ', '.join(self.name) + self.geoname_sort = ', '.join(reversed(self.geoname.split(', '))) - self.name_find = '|%s|'%'|'.join([self.name] + self.aliases) + self.name_find = '|%s|'%'|'.join(self.name) #update center - self.lat_center = ox.location.center(self.lat_sw, self.lat_ne) - self.lng_center = ox.location.center(self.lng_sw, self.lng_ne) + #self.lat = ox.location.center(self.south, self.north) + #self.lng = ox.location.center(self.east, self.west) #update area - self.area = ox.location.area(self.lat_sw, self.lng_sw, self.lat_ne, self.lng_ne) + #self.size = ox.location.area(self.south, self.west, self.north, self.east) super(Place, self).save(*args, **kwargs) diff --git a/pandora/place/views.py b/pandora/place/views.py index 880b670d5..5a64e0c79 100644 --- a/pandora/place/views.py +++ b/pandora/place/views.py @@ -2,35 +2,47 @@ # vi:si:et:sw=4:sts=4:ts=4 from __future__ import division +import ox from ox.utils import json from ox.django.decorators import login_required_json from ox.django.shortcuts import render_to_json_response, get_object_or_404_json, json_response -import models from api.actions import actions +from item import utils + +import models @login_required_json def addPlace(request): #FIXME: require admin ''' - param data - { - 'place': dict - } - place contains key/value pairs with place propterties + param data { + name: "", + geoname: "", + south: float, + west: float, + north: float, + east: float, + lat: float, + lng: float, + size: float, + } ''' data = json.loads(request.POST['data']) exists = False - names = [data['place']['name']] + data['place']['aliases'] + names = data['name'] + if isinstance(names, basestring): + names = [names] for name in names: - if models.Place.objects.filter(name_find__icontains=u'|%s|'%data['name']).count() != 0: + if models.Place.objects.filter(name_find__icontains=u'|%s|'%name).count() != 0: exists = True if not exists: place = models.Place() - for key in data['place']: - setattr(place, key, data['place'][key]) + place.user = request.user + for key in data: + setattr(place, key, data[key]) place.save() response = json_response(status=200, text='created') else: @@ -42,24 +54,27 @@ actions.register(addPlace, cache=False) @login_required_json def editPlace(request): ''' - param data - { - 'id': placeid, - 'place': dict - } - place contains key/value pairs with place propterties + param data { + 'id': placeid, + 'name': ... + 'north': 0... + } + can contain any of the allowed keys for place ''' data = json.loads(request.POST['data']) - place = get_object_or_404_json(models.Place, pk=data['id']) + place = get_object_or_404_json(models.Place, pk=ox.from32(data['id'])) + names = data.get('name', []) + if isinstance(names, basestring): + names = [names] if place.editable(request.user): conflict = False - names = [data['place']['name']] + data['place']['aliases'] for name in names: - if models.Place.objects.filter(name_find__icontains=u'|%s|'%data['name']).exclude(id=place.id).count() != 0: + if models.Place.objects.filter(name_find__icontains=u'|%s|'%name).exclude(id=place.id).count() != 0: conflict = True if not conflict: - for key in data['place']: - setattr(place, key, data['place'][key]) + for key in data: + if key != 'id': + setattr(place, key, data[key]) place.save() response = json_response(status=200, text='updated') else: @@ -72,13 +87,68 @@ actions.register(editPlace, cache=False) @login_required_json def removePlace(request): - response = json_response(status=501, text='not implemented') + ''' + param data { + 'id': placeid, + } + ''' + 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)) + if place.editable(request.user): + place.delete() + response = json_response(status=200, text='deleted') + else: + response = json_response(status=403, text='permission denied') return render_to_json_response(response) actions.register(removePlace, cache=False) +def parse_query(data, user): + query = {} + query['range'] = [0, 100] + query['sort'] = [{'key':'user', 'operator':'+'}, {'key':'name', 'operator':'+'}] + for key in ('keys', 'group', 'list', 'range', 'ids', 'sort'): + if key in data: + query[key] = data[key] + query['qs'] = models.Place.objects.all() + return query -def findPlace(request): +def findPlaces(request): ''' + param data { + query: { + conditions: [ + { + key: 'user', + value: 'something', + operator: '=' + } + ] + operator: "," + }, + sort: [{key: 'name', operator: '+'}], + range: [0, 100] + keys: [] + } + + possible query keys: + name, geoname, user + + possible keys: + name, geoname, user + + return { + status: { + code: int, + text: string + }, + data: { + items: [ + {name:, user:, featured:, public...} + ] + } + } param data {'query': query, 'sort': array, 'range': array, 'area': array} @@ -96,7 +166,7 @@ def findPlace(request): } ] range: result range, array [from, to] - area: [sw_lat, sw_lng, ne_lat, ne_lng] only return places in that square + area: [south, west, north, east] only return places in that square with keys, items is list of dicts with requested properties: return {'status': {'code': int, 'text': string}, @@ -111,10 +181,21 @@ Positions ids: ids of places for which positions are required ''' data = json.loads(request.POST['data']) - response = json_response(status=200, text='ok') - response['data']['places'] = [] + response = json_response() + + query = parse_query(data, request.user) + qs = query['qs'] + if 'keys' in data: + qs = qs[query['range'][0]:query['range'][1]] + response['data']['items'] = [p.json(request.user) for p in qs] + elif 'ids' in data: + ids = [i.get_id() for i in qs] + response['data']['positions'] = utils.get_positions(ids, query['ids']) + else: + response['data']['items'] = qs.count() + response['data']['items'] = [] #FIXME: add coordinates to limit search for p in models.Place.objects.find(data['query']): - response['data']['places'].append(p.json()) + response['data']['items'].append(p.json()) return render_to_json_response(response) -actions.register(findPlace) +actions.register(findPlaces)