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-10-09 15:53:18 +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
|
|
|
|
|
2012-01-03 20:18:47 +00:00
|
|
|
from ox.django.api import actions
|
2011-09-24 23:09:48 +00:00
|
|
|
from item import utils
|
|
|
|
|
|
|
|
import models
|
2011-10-09 15:53:18 +00:00
|
|
|
import tasks
|
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-10-04 00:47:48 +00:00
|
|
|
existing_names = []
|
|
|
|
exists = False
|
|
|
|
for name in [data['name']] + data.get('alternativeNames', []):
|
|
|
|
if models.Event.objects.filter(name_find__icontains=u'|%s|'%name).count() != 0:
|
|
|
|
exists = True
|
|
|
|
existing_names.append(name)
|
|
|
|
if not exists:
|
2011-05-28 12:10:28 +00:00
|
|
|
event = models.Event(name = data['name'])
|
2011-10-09 15:40:18 +00:00
|
|
|
for key in ('start', 'startTime', 'end', 'endTime', 'duration', 'durationTime',
|
2011-10-09 21:12:50 +00:00
|
|
|
'type', 'alternativeNames'):
|
2011-05-28 12:10:28 +00:00
|
|
|
if key in data and data[key]:
|
2011-10-09 21:12:50 +00:00
|
|
|
value = data[key]
|
|
|
|
if key == 'alternativeNames':
|
|
|
|
value = tuple(value)
|
|
|
|
setattr(event, key, value)
|
|
|
|
if 'nameSort' in data:
|
2011-10-11 10:41:08 +00:00
|
|
|
event.set_name_sort(data['nameSort'])
|
2011-05-28 12:10:28 +00:00
|
|
|
event.save()
|
2011-10-09 15:53:18 +00:00
|
|
|
tasks.update_matches.delay(event.id)
|
2010-11-26 15:07:24 +00:00
|
|
|
response = json_response(status=200, text='created')
|
2011-10-09 15:53:18 +00:00
|
|
|
response['data'] = event.json()
|
2010-11-26 15:07:24 +00:00
|
|
|
else:
|
2011-10-31 16:06:12 +00:00
|
|
|
response = json_response(status=409, text='name exists')
|
2011-10-04 00:47:48 +00:00
|
|
|
response['data']['names'] = existing_names
|
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-12-18 09:35:49 +00:00
|
|
|
event = get_object_or_404_json(models.Event, pk=ox.fromAZ(data['id']))
|
2011-05-28 12:10:28 +00:00
|
|
|
if event.editable(request.user):
|
2010-11-26 15:07:24 +00:00
|
|
|
conflict = False
|
2011-10-04 00:47:48 +00:00
|
|
|
conflict_names = []
|
2011-05-28 12:10:28 +00:00
|
|
|
names = [data.get('name', event.name)] + data.get('alternativeNames', [])
|
2011-10-04 00:47:48 +00:00
|
|
|
for name in names:
|
|
|
|
if models.Event.objects.filter(name_find__icontains=u'|%s|'%name).exclude(id=event.id).count() != 0:
|
2010-11-26 15:07:24 +00:00
|
|
|
conflict = True
|
2011-10-04 00:47:48 +00:00
|
|
|
conflict_names.append(name)
|
2010-11-26 15:07:24 +00:00
|
|
|
if not conflict:
|
2011-10-11 10:41:08 +00:00
|
|
|
if 'name' in data:
|
|
|
|
event.set_name_sort(data['name'])
|
2011-10-09 21:12:50 +00:00
|
|
|
for key in ('name', 'start', 'startTime', 'end', 'endTime', 'duration', 'durationTime',
|
|
|
|
'type', 'alternativeNames'):
|
2011-05-28 12:10:28 +00:00
|
|
|
if key in data:
|
2011-10-09 21:12:50 +00:00
|
|
|
value = data[key]
|
|
|
|
if key == 'alternativeNames':
|
|
|
|
value = tuple(value)
|
|
|
|
setattr(event, key, value)
|
|
|
|
if 'nameSort' in data:
|
2011-10-11 10:41:08 +00:00
|
|
|
event.set_name_sort(data['nameSort'])
|
2011-05-28 12:10:28 +00:00
|
|
|
event.save()
|
2011-10-11 11:00:33 +00:00
|
|
|
if 'name' in data or 'alternativeNames' in data:
|
|
|
|
tasks.update_matches.delay(event.id)
|
2010-11-26 15:07:24 +00:00
|
|
|
response = json_response(status=200, text='updated')
|
2011-10-09 21:12:50 +00:00
|
|
|
response['data'] = event.json()
|
2010-11-26 15:07:24 +00:00
|
|
|
else:
|
2011-10-31 16:06:12 +00:00
|
|
|
response = json_response(status=409, text='Event name conflict')
|
2011-10-04 00:47:48 +00:00
|
|
|
response['data']['names'] = conflict_names
|
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'])
|
2011-12-18 09:35:49 +00:00
|
|
|
event = get_object_or_404_json(models.Event, pk=ox.fromAZ(data['id']))
|
2011-05-28 12:10:28 +00:00
|
|
|
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-09-24 23:09:48 +00:00
|
|
|
def parse_query(data, user):
|
|
|
|
query = {}
|
|
|
|
query['range'] = [0, 100]
|
|
|
|
query['sort'] = [{'key':'name', 'operator':'+'}]
|
|
|
|
for key in ('keys', 'group', 'list', 'range', 'sort', 'query'):
|
|
|
|
if key in data:
|
|
|
|
query[key] = data[key]
|
|
|
|
query['qs'] = models.Event.objects.find(query, user)
|
2011-10-19 16:20:12 +00:00
|
|
|
if 'itemsQuery' in data:
|
|
|
|
item_query = models.Item.objects.find({'query': data['itemsQuery']}, user)
|
2011-09-24 23:09:48 +00:00
|
|
|
query['qs'] = query['qs'].filter(items__in=item_query)
|
|
|
|
return query
|
|
|
|
|
|
|
|
def order_query(qs, sort):
|
|
|
|
order_by = []
|
|
|
|
for e in sort:
|
|
|
|
operator = e['operator']
|
|
|
|
if operator != '-':
|
|
|
|
operator = ''
|
|
|
|
key = {
|
|
|
|
'name': 'name_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-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
|
2011-10-19 16:20:12 +00:00
|
|
|
itemsQuery: {
|
2011-06-05 16:30:30 +00:00
|
|
|
//see find request
|
|
|
|
},
|
2010-11-26 15:07:24 +00:00
|
|
|
sort: array of key, operator dics
|
|
|
|
[
|
|
|
|
{
|
|
|
|
key: "year",
|
|
|
|
operator: "-"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "director",
|
|
|
|
operator: ""
|
|
|
|
}
|
|
|
|
]
|
|
|
|
range: result range, array [from, to]
|
|
|
|
|
2011-10-19 16:20:12 +00:00
|
|
|
itemsQuery can be used to limit the resuts to matches in those items.
|
2011-06-05 16:30:30 +00:00
|
|
|
|
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
|
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
|
|
|
'''
|
|
|
|
response = json_response(status=200, text='ok')
|
2011-09-24 23:09:48 +00:00
|
|
|
|
|
|
|
data = json.loads(request.POST['data'])
|
|
|
|
query = parse_query(data, request.user)
|
2011-10-09 21:12:50 +00:00
|
|
|
qs = order_query(query['qs'], query['sort'])
|
|
|
|
qs = qs.distinct()
|
2011-09-24 23:09:48 +00:00
|
|
|
if 'keys' in data:
|
|
|
|
qs = qs[query['range'][0]:query['range'][1]]
|
2011-10-09 13:49:21 +00:00
|
|
|
qs = qs.select_related()
|
2011-09-24 23:09:48 +00:00
|
|
|
response['data']['items'] = [p.json(request.user) for p in qs]
|
|
|
|
elif 'position' in query:
|
|
|
|
ids = [i.get_id() for i in qs]
|
|
|
|
data['conditions'] = data['conditions'] + {
|
|
|
|
'value': data['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, data['positions'])
|
|
|
|
else:
|
|
|
|
response['data']['items'] = qs.count()
|
|
|
|
|
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)
|