2010-11-26 15:07:24 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vi:si:et:sw=4:sts=4:ts=4
|
|
|
|
from __future__ import division
|
|
|
|
|
2011-02-24 18:39:58 +00:00
|
|
|
import ox
|
2010-11-26 15:07:24 +00:00
|
|
|
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
|
|
|
|
|
2010-12-22 07:45:37 +00:00
|
|
|
from api.actions import actions
|
2011-02-24 18:39:58 +00:00
|
|
|
from item import utils
|
|
|
|
|
|
|
|
import models
|
2010-11-26 15:07:24 +00:00
|
|
|
|
2011-01-01 11:44:42 +00:00
|
|
|
|
2010-11-26 15:07:24 +00:00
|
|
|
@login_required_json
|
2010-12-22 07:45:37 +00:00
|
|
|
def addPlace(request):
|
2011-01-01 11:44:42 +00:00
|
|
|
#FIXME: require admin
|
2010-11-26 15:07:24 +00:00
|
|
|
'''
|
2011-02-24 18:39:58 +00:00
|
|
|
param data {
|
|
|
|
name: "",
|
|
|
|
geoname: "",
|
|
|
|
south: float,
|
|
|
|
west: float,
|
|
|
|
north: float,
|
|
|
|
east: float,
|
|
|
|
lat: float,
|
|
|
|
lng: float,
|
|
|
|
size: float,
|
|
|
|
}
|
2010-11-26 15:07:24 +00:00
|
|
|
'''
|
|
|
|
data = json.loads(request.POST['data'])
|
|
|
|
exists = False
|
2011-02-24 18:39:58 +00:00
|
|
|
names = data['name']
|
|
|
|
if isinstance(names, basestring):
|
|
|
|
names = [names]
|
2010-11-26 15:07:24 +00:00
|
|
|
for name in names:
|
2011-02-24 18:39:58 +00:00
|
|
|
if models.Place.objects.filter(name_find__icontains=u'|%s|'%name).count() != 0:
|
2010-11-26 15:07:24 +00:00
|
|
|
exists = True
|
|
|
|
if not exists:
|
|
|
|
place = models.Place()
|
2011-02-24 18:39:58 +00:00
|
|
|
place.user = request.user
|
|
|
|
for key in data:
|
|
|
|
setattr(place, key, data[key])
|
2010-11-26 15:07:24 +00:00
|
|
|
place.save()
|
|
|
|
response = json_response(status=200, text='created')
|
|
|
|
else:
|
|
|
|
response = json_response(status=403, text='place name exists')
|
|
|
|
return render_to_json_response(response)
|
2011-01-13 08:33:14 +00:00
|
|
|
actions.register(addPlace, cache=False)
|
2010-11-26 15:07:24 +00:00
|
|
|
|
2011-01-01 11:44:42 +00:00
|
|
|
|
2010-11-26 15:07:24 +00:00
|
|
|
@login_required_json
|
2010-12-22 07:45:37 +00:00
|
|
|
def editPlace(request):
|
2010-11-26 15:07:24 +00:00
|
|
|
'''
|
2011-02-24 18:39:58 +00:00
|
|
|
param data {
|
|
|
|
'id': placeid,
|
|
|
|
'name': ...
|
|
|
|
'north': 0...
|
|
|
|
}
|
|
|
|
can contain any of the allowed keys for place
|
2010-11-26 15:07:24 +00:00
|
|
|
'''
|
|
|
|
data = json.loads(request.POST['data'])
|
2011-02-24 18:39:58 +00:00
|
|
|
place = get_object_or_404_json(models.Place, pk=ox.from32(data['id']))
|
|
|
|
names = data.get('name', [])
|
|
|
|
if isinstance(names, basestring):
|
|
|
|
names = [names]
|
2010-11-26 15:07:24 +00:00
|
|
|
if place.editable(request.user):
|
|
|
|
conflict = False
|
|
|
|
for name in names:
|
2011-02-24 18:39:58 +00:00
|
|
|
if models.Place.objects.filter(name_find__icontains=u'|%s|'%name).exclude(id=place.id).count() != 0:
|
2010-11-26 15:07:24 +00:00
|
|
|
conflict = True
|
|
|
|
if not conflict:
|
2011-02-24 18:39:58 +00:00
|
|
|
for key in data:
|
|
|
|
if key != 'id':
|
|
|
|
setattr(place, key, data[key])
|
2010-11-26 15:07:24 +00:00
|
|
|
place.save()
|
|
|
|
response = json_response(status=200, text='updated')
|
|
|
|
else:
|
|
|
|
response = json_response(status=403, text='place name/alias conflict')
|
|
|
|
else:
|
|
|
|
response = json_response(status=403, text='permission denied')
|
|
|
|
return render_to_json_response(response)
|
2011-01-13 08:33:14 +00:00
|
|
|
actions.register(editPlace, cache=False)
|
2010-11-26 15:07:24 +00:00
|
|
|
|
2011-01-01 11:44:42 +00:00
|
|
|
|
2010-11-26 15:07:24 +00:00
|
|
|
@login_required_json
|
2010-12-22 07:45:37 +00:00
|
|
|
def removePlace(request):
|
2011-02-24 18:39:58 +00:00
|
|
|
'''
|
|
|
|
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')
|
2010-11-26 15:07:24 +00:00
|
|
|
return render_to_json_response(response)
|
2011-01-13 08:33:14 +00:00
|
|
|
actions.register(removePlace, cache=False)
|
2010-11-26 15:07:24 +00:00
|
|
|
|
2011-02-24 18:39:58 +00:00
|
|
|
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
|
2011-01-01 11:44:42 +00:00
|
|
|
|
2011-02-24 18:39:58 +00:00
|
|
|
def findPlaces(request):
|
2010-11-26 15:07:24 +00:00
|
|
|
'''
|
2011-02-24 18:39:58 +00:00
|
|
|
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...}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
2010-11-26 15:07:24 +00:00
|
|
|
param data
|
|
|
|
{'query': query, 'sort': array, 'range': array, 'area': array}
|
2011-01-01 11:44:42 +00:00
|
|
|
|
2010-11-26 15:07:24 +00:00
|
|
|
query: query object, more on query syntax at
|
|
|
|
https://wiki.0x2620.org/wiki/pandora/QuerySyntax
|
|
|
|
sort: array of key, operator dics
|
|
|
|
[
|
|
|
|
{
|
|
|
|
key: "year",
|
|
|
|
operator: "-"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "director",
|
|
|
|
operator: ""
|
|
|
|
}
|
|
|
|
]
|
|
|
|
range: result range, array [from, to]
|
2011-02-24 18:39:58 +00:00
|
|
|
area: [south, west, north, east] only return places in that square
|
2010-11-26 15:07:24 +00:00
|
|
|
|
|
|
|
with keys, items is list of dicts with requested properties:
|
|
|
|
return {'status': {'code': int, 'text': string},
|
|
|
|
'data': {items: array}}
|
|
|
|
|
|
|
|
Positions
|
|
|
|
param data
|
|
|
|
{'query': query, 'ids': []}
|
2011-01-01 11:44:42 +00:00
|
|
|
|
2010-11-26 15:07:24 +00:00
|
|
|
query: query object, more on query syntax at
|
|
|
|
https://wiki.0x2620.org/wiki/pandora/QuerySyntax
|
|
|
|
ids: ids of places for which positions are required
|
|
|
|
'''
|
|
|
|
data = json.loads(request.POST['data'])
|
2011-02-24 18:39:58 +00:00
|
|
|
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'] = []
|
2010-11-26 15:07:24 +00:00
|
|
|
#FIXME: add coordinates to limit search
|
2011-01-01 11:44:42 +00:00
|
|
|
for p in models.Place.objects.find(data['query']):
|
2011-02-24 18:39:58 +00:00
|
|
|
response['data']['items'].append(p.json())
|
2010-11-26 15:07:24 +00:00
|
|
|
return render_to_json_response(response)
|
2011-02-24 18:39:58 +00:00
|
|
|
actions.register(findPlaces)
|