cablegates/pandora/place/views.py

247 lines
7.6 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
from __future__ import division
2011-05-28 13:57:47 +00:00
from django.db.models import Max, Min
2011-02-24 18:39:58 +00:00
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
from api.actions import actions
2011-02-24 18:39:58 +00:00
from item import utils
import models
2011-05-30 13:40:16 +00:00
import tasks
2011-01-01 11:44:42 +00:00
@login_required_json
def addPlace(request):
2011-01-01 11:44:42 +00:00
#FIXME: require admin
'''
2011-02-24 18:39:58 +00:00
param data {
name: "",
2011-05-29 20:35:49 +00:00
alternativeNames: [],
2011-02-24 18:39:58 +00:00
geoname: "",
2011-02-25 10:23:46 +00:00
countryCode: '',
2011-02-24 18:39:58 +00:00
south: float,
west: float,
north: float,
east: float,
lat: float,
lng: float,
2011-05-28 13:57:47 +00:00
area: float,
2011-05-30 11:23:49 +00:00
type: ""
2011-02-24 18:39:58 +00:00
}
'''
data = json.loads(request.POST['data'])
exists = False
2011-02-24 19:22:20 +00:00
names = data.pop('name')
2011-05-29 20:35:49 +00:00
for name in [names] + data.get('alternativeNames', []):
2011-02-24 18:39:58 +00:00
if models.Place.objects.filter(name_find__icontains=u'|%s|'%name).count() != 0:
exists = True
if not exists:
place = models.Place()
2011-02-24 18:39:58 +00:00
place.user = request.user
2011-03-05 14:31:56 +00:00
place.name = names
2011-05-29 20:35:49 +00:00
place.alternativeNames = tuple(data.pop('alternativeNames', []))
2011-02-24 18:39:58 +00:00
for key in data:
2011-03-05 14:31:56 +00:00
value = data[key]
if isinstance(value, list):
value = tuple(value)
setattr(place, key, value)
place.save()
2011-05-30 13:40:16 +00:00
tasks.update_matches.delay(place.id)
2011-02-25 10:23:46 +00:00
response = json_response(place.json())
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)
2011-01-01 11:44:42 +00:00
@login_required_json
def editPlace(request):
'''
2011-02-24 18:39:58 +00:00
param data {
'id': placeid,
'name': ...
'north': 0...
}
can contain any of the allowed keys for place
'''
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]
if place.editable(request.user):
conflict = False
2011-05-29 20:35:49 +00:00
for name in names + data.get('alternativeNames', []):
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:
conflict = True
2011-02-25 10:23:46 +00:00
if 'geoname' in data:
if models.Place.objects.filter(geoname=data['geoname']).exclude(id=place.id).count() != 0:
conflict = True
if not conflict:
2011-02-24 18:39:58 +00:00
for key in data:
if key != 'id':
2011-03-05 14:31:56 +00:00
value = data[key]
if isinstance(value, list):
value = tuple(value)
setattr(place, key, value)
place.save()
2011-05-30 13:40:16 +00:00
tasks.update_matches.delay(place.id)
2011-02-25 10:23:46 +00:00
response = json_response(place.json())
else:
2011-02-25 10:23:46 +00:00
response = json_response(status=403, text='place name/geoname 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)
2011-01-01 11:44:42 +00:00
@login_required_json
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')
return render_to_json_response(response)
2011-01-13 08:33:14 +00:00
actions.register(removePlace, cache=False)
2011-02-24 18:39:58 +00:00
def parse_query(data, user):
query = {}
query['range'] = [0, 100]
2011-02-25 10:23:46 +00:00
query['sort'] = [{'key':'name', 'operator':'+'}]
2011-06-01 11:03:43 +00:00
for key in ('keys', 'group', 'list', 'range', 'position', 'positions', 'sort', 'query'):
2011-02-24 18:39:58 +00:00
if key in data:
query[key] = data[key]
2011-02-25 10:23:46 +00:00
query['qs'] = models.Place.objects.find(query, user)
2011-02-24 18:39:58 +00:00
return query
2011-01-01 11:44:42 +00:00
2011-02-25 10:23:46 +00:00
def order_query(qs, sort):
order_by = []
for e in sort:
operator = e['operator']
if operator != '-':
operator = ''
key = {
'name': 'name_sort',
'geoname': 'geoname_sort',
}.get(e['key'], e['key'])
order = '%s%s' % (operator, key)
order_by.append(order)
if order_by:
qs = qs.order_by(*order_by, nulls_last=True)
return qs
2011-02-24 18:39:58 +00:00
def findPlaces(request):
'''
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...}
]
}
}
param data
2011-06-01 11:03:43 +00:00
{'query': query, 'sort': array, 'range': array}
2011-01-01 11:44:42 +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]
with keys, items is list of dicts with requested properties:
return {'status': {'code': int, 'text': string},
'data': {items: array}}
Positions
param data
2011-06-01 11:03:43 +00:00
{'query': query, 'positions': []}
2011-01-01 11:44:42 +00:00
query: query object, more on query syntax at
https://wiki.0x2620.org/wiki/pandora/QuerySyntax
2011-06-01 11:03:43 +00:00
positions: ids of places for which positions are required
'''
data = json.loads(request.POST['data'])
2011-05-30 13:41:56 +00:00
print data
2011-02-24 18:39:58 +00:00
response = json_response()
query = parse_query(data, request.user)
2011-02-25 10:23:46 +00:00
qs = order_query(query['qs'], query['sort'])
2011-02-24 18:39:58 +00:00
if 'keys' in data:
qs = qs[query['range'][0]:query['range'][1]]
response['data']['items'] = [p.json(request.user) for p in qs]
2011-06-01 11:03:43 +00:00
elif 'position' in query:
2011-02-24 18:39:58 +00:00
ids = [i.get_id() for i in qs]
2011-06-01 11:03:43 +00:00
data['conditions'] = data['conditions'] + {
'value': query['position'],
'key': query['sort'][0]['key'],
'operator': '^'
}
query = parse_query(data, request.user)
qs = order_query(query['qs'], query['sort'])
if qs.count() > 0:
response['data']['position'] = utils.get_positions(ids, [qs[0].itemId])[0]
elif 'positions' in data:
ids = [i.get_id() for i in qs]
response['data']['positions'] = utils.get_positions(ids, query['positions'])
2011-02-24 18:39:58 +00:00
else:
response['data']['items'] = qs.count()
2011-05-29 20:35:49 +00:00
response['data']['area'] = qs.aggregate(
south=Min('south'),
west=Min('west'),
north=Max('north'),
east=Max('east'),
2011-05-28 13:57:47 +00:00
)
return render_to_json_response(response)
2011-02-24 18:39:58 +00:00
actions.register(findPlaces)