2010-11-26 15:07:24 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vi:si:et:sw=4:sts=4:ts=4
|
|
|
|
from __future__ import division
|
|
|
|
|
|
|
|
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
|
2010-12-22 07:45:37 +00:00
|
|
|
from api.actions import actions
|
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
|
2011-05-28 11:35:57 +00:00
|
|
|
def addEvent(request):
|
2011-05-28 12:10:28 +00:00
|
|
|
'''
|
|
|
|
param data
|
|
|
|
{
|
|
|
|
'name': '',
|
|
|
|
'start': ''
|
|
|
|
'end': ''
|
|
|
|
}
|
|
|
|
required keys: name, start, end
|
|
|
|
'''
|
2010-11-26 15:07:24 +00:00
|
|
|
data = json.loads(request.POST['data'])
|
2011-05-28 12:10:28 +00:00
|
|
|
#FIXME: check for alternativeNames too!
|
2011-05-28 11:35:57 +00:00
|
|
|
if models.Event.filter(name=data['name']).count() == 0:
|
2011-05-28 12:10:28 +00:00
|
|
|
event = models.Event(name = data['name'])
|
|
|
|
for key in ('start', 'end', 'alternativeNames'):
|
|
|
|
if key in data and data[key]:
|
|
|
|
setattr(event, key, data[key])
|
|
|
|
event.save()
|
2010-11-26 15:07:24 +00:00
|
|
|
response = json_response(status=200, text='created')
|
|
|
|
else:
|
2011-05-28 12:10:28 +00:00
|
|
|
response = json_response(status=403, text='event name exists')
|
2010-11-26 15:07:24 +00:00
|
|
|
return render_to_json_response(response)
|
2011-05-28 11:35:57 +00:00
|
|
|
actions.register(addEvent, 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
|
2011-05-28 11:35:57 +00:00
|
|
|
def editEvent(request):
|
2010-11-26 15:07:24 +00:00
|
|
|
'''
|
|
|
|
param data
|
|
|
|
{
|
2011-05-28 12:10:28 +00:00
|
|
|
'id': event id,
|
|
|
|
'name': ''
|
|
|
|
...
|
2010-11-26 15:07:24 +00:00
|
|
|
}
|
2011-05-28 12:10:28 +00:00
|
|
|
update provides keys of event with id
|
2010-11-26 15:07:24 +00:00
|
|
|
'''
|
|
|
|
data = json.loads(request.POST['data'])
|
2011-05-28 12:10:28 +00:00
|
|
|
event = get_object_or_404_json(models.Event, pk=data['id'])
|
|
|
|
if event.editable(request.user):
|
2010-11-26 15:07:24 +00:00
|
|
|
conflict = False
|
2011-05-28 12:10:28 +00:00
|
|
|
names = [data.get('name', event.name)] + data.get('alternativeNames', [])
|
2010-11-26 15:07:24 +00:00
|
|
|
for name in names: #FIXME: also check aliases!
|
2011-05-28 12:10:28 +00:00
|
|
|
if models.Event.filter(name=data['name']).exclude(id=event.id).count() != 0:
|
2010-11-26 15:07:24 +00:00
|
|
|
conflict = True
|
|
|
|
if not conflict:
|
2011-05-28 12:10:28 +00:00
|
|
|
for key in ('start', 'end', 'alternativeNames'):
|
|
|
|
if key in data:
|
|
|
|
setattr(event, key, data[key])
|
|
|
|
event.save()
|
2010-11-26 15:07:24 +00:00
|
|
|
response = json_response(status=200, text='updated')
|
|
|
|
else:
|
2011-05-28 12:10:28 +00:00
|
|
|
response = json_response(status=403, text='Event name conflict')
|
2010-11-26 15:07:24 +00:00
|
|
|
else:
|
|
|
|
response = json_response(status=403, text='permission denied')
|
|
|
|
return render_to_json_response(response)
|
2011-05-28 11:35:57 +00:00
|
|
|
actions.register(editEvent, 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
|
2011-05-28 11:35:57 +00:00
|
|
|
def removeEvent(request):
|
2011-05-28 12:10:28 +00:00
|
|
|
'''
|
|
|
|
param data {
|
|
|
|
id: event id
|
|
|
|
}
|
|
|
|
remove Event with given id
|
|
|
|
|
|
|
|
'''
|
|
|
|
data = json.loads(request.POST['data'])
|
|
|
|
event = get_object_or_404_json(models.Event, pk=data['id'])
|
|
|
|
if event.editable(request.user):
|
|
|
|
event.delete()
|
|
|
|
response = json_response(status=200, text='removed')
|
|
|
|
else:
|
|
|
|
response = json_response(status=403, text='permission denied')
|
2010-11-26 15:07:24 +00:00
|
|
|
return render_to_json_response(response)
|
2011-05-28 11:35:57 +00:00
|
|
|
actions.register(removeEvent, cache=False)
|
2010-11-26 15:07:24 +00:00
|
|
|
|
2011-01-01 11:44:42 +00:00
|
|
|
|
2011-05-28 13:57:47 +00:00
|
|
|
def findEvents(request):
|
2010-11-26 15:07:24 +00:00
|
|
|
'''
|
|
|
|
param data
|
|
|
|
{'query': query, 'sort': array, 'range': 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]
|
|
|
|
|
|
|
|
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
|
2011-05-28 11:35:57 +00:00
|
|
|
ids: ids of events for which positions are required
|
2010-11-26 15:07:24 +00:00
|
|
|
'''
|
|
|
|
data = json.loads(request.POST['data'])
|
|
|
|
response = json_response(status=200, text='ok')
|
2011-05-28 11:35:57 +00:00
|
|
|
response['data']['events'] = []
|
2010-11-26 15:07:24 +00:00
|
|
|
#FIXME: add coordinates to limit search
|
2011-05-28 11:35:57 +00:00
|
|
|
for p in models.Event.objects.find(data['query']):
|
|
|
|
response['data']['events'].append(p.json())
|
2010-11-26 15:07:24 +00:00
|
|
|
return render_to_json_response(response)
|
2011-05-28 13:57:47 +00:00
|
|
|
actions.register(findEvents)
|