2010-11-26 15:07:24 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vi:si:et:sw=4:sts=4:ts=4
|
2016-08-23 10:27:06 +00:00
|
|
|
from __future__ import division, print_function, absolute_import
|
2010-11-26 15:07:24 +00:00
|
|
|
|
2013-02-21 13:28:36 +00:00
|
|
|
from django.db.models import Count
|
2012-01-14 12:43:40 +00:00
|
|
|
from django.conf import settings
|
|
|
|
|
2016-08-23 10:27:06 +00:00
|
|
|
from six import string_types
|
2011-10-09 15:53:18 +00:00
|
|
|
import ox
|
2010-11-26 15:07:24 +00:00
|
|
|
from ox.utils import json
|
2016-02-20 09:06:41 +00:00
|
|
|
from oxdjango.decorators import login_required_json
|
|
|
|
from oxdjango.shortcuts import render_to_json_response, get_object_or_404_json, json_response
|
2010-11-26 15:07:24 +00:00
|
|
|
|
2016-02-20 09:06:41 +00:00
|
|
|
from oxdjango.api import actions
|
2011-09-24 23:09:48 +00:00
|
|
|
from item import utils
|
2014-12-17 13:45:46 +00:00
|
|
|
from changelog.models import add_changelog
|
2011-09-24 23:09:48 +00:00
|
|
|
|
2016-08-23 10:27:06 +00:00
|
|
|
from . import models
|
2011-01-01 11:44:42 +00:00
|
|
|
|
2010-11-26 15:07:24 +00:00
|
|
|
@login_required_json
|
2014-10-06 08:26:43 +00:00
|
|
|
def addEvent(request, data):
|
2011-05-28 12:10:28 +00:00
|
|
|
'''
|
2014-12-19 12:59:10 +00:00
|
|
|
Adds a new event to the calendar
|
2014-12-18 19:26:37 +00:00
|
|
|
takes {
|
2014-12-18 20:16:41 +00:00
|
|
|
end: string, // 'YYYY-MM-DD HH:MM:SS', arbitrary precision
|
|
|
|
name: string, // name
|
|
|
|
start: string // 'YYYY-MM-DD HH:MM:SS', arbitrary precision
|
2014-12-18 19:26:37 +00:00
|
|
|
}
|
|
|
|
returns {
|
2014-12-18 20:16:41 +00:00
|
|
|
id: string // event id
|
2014-12-18 19:26:37 +00:00
|
|
|
}
|
|
|
|
see: editEvent, findEvents, removeEvents
|
2011-05-28 12:10:28 +00:00
|
|
|
'''
|
2011-10-04 00:47:48 +00:00
|
|
|
existing_names = []
|
|
|
|
exists = False
|
2012-02-01 15:25:18 +00:00
|
|
|
names = [data['name']] + data.get('alternativeNames', [])
|
|
|
|
for name in names:
|
2012-05-27 11:52:12 +00:00
|
|
|
name = ox.decode_html(name)
|
2012-02-01 15:25:18 +00:00
|
|
|
if models.Event.objects.filter(defined=True,
|
|
|
|
name_find__icontains=u'|%s|'%name).count() != 0:
|
2011-10-04 00:47:48 +00:00
|
|
|
exists = True
|
|
|
|
existing_names.append(name)
|
|
|
|
if not exists:
|
2012-02-01 15:25:18 +00:00
|
|
|
models.Event.objects.filter(defined=False, name__in=names).delete()
|
2012-02-21 16:26:06 +00:00
|
|
|
data['name'] = ox.escape_html(data['name'])
|
|
|
|
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]
|
2016-08-23 10:27:06 +00:00
|
|
|
if isinstance(value, string_types):
|
2012-02-21 16:26:06 +00:00
|
|
|
value = ox.escape_html(value)
|
2011-10-09 21:12:50 +00:00
|
|
|
if key == 'alternativeNames':
|
2012-02-21 16:26:06 +00:00
|
|
|
value = tuple([ox.escape_html(v) for v in value])
|
2011-10-09 21:12:50 +00:00
|
|
|
setattr(event, key, value)
|
|
|
|
if 'nameSort' in data:
|
2012-02-21 16:26:06 +00:00
|
|
|
value = ox.escape_html(data['nameSort'])
|
|
|
|
event.set_name_sort(value)
|
2012-02-20 14:09:26 +00:00
|
|
|
event.matches = 0
|
2011-05-28 12:10:28 +00:00
|
|
|
event.save()
|
2012-02-20 17:42:13 +00:00
|
|
|
event.update_matches()
|
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()
|
2014-12-17 13:45:46 +00:00
|
|
|
add_changelog(request, data, event.get_id())
|
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
|
2014-10-06 08:26:43 +00:00
|
|
|
def editEvent(request, data):
|
2010-11-26 15:07:24 +00:00
|
|
|
'''
|
2014-12-18 19:26:37 +00:00
|
|
|
Edits a calendar event
|
|
|
|
takes {
|
2014-12-18 20:16:41 +00:00
|
|
|
end: string, // 'YYYY-MM-DD HH:MM:SS', arbitrary precision
|
|
|
|
id: string, // event id
|
|
|
|
name: string, // event name
|
|
|
|
start: string // 'YYYY-MM-DD HH:MM:SS', arbitrary precision
|
2014-12-18 19:26:37 +00:00
|
|
|
}
|
|
|
|
returns {
|
2014-12-18 20:16:41 +00:00
|
|
|
id: string, // event id
|
|
|
|
key: value, // property id and value
|
|
|
|
... // more key/value pairs
|
2014-12-18 19:26:37 +00:00
|
|
|
}
|
|
|
|
see: addEvent, findEvents, removeEvent
|
2010-11-26 15:07:24 +00:00
|
|
|
'''
|
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:
|
2012-02-01 15:25:18 +00:00
|
|
|
if models.Event.objects.filter(defined=True,
|
|
|
|
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:
|
2012-02-01 15:25:18 +00:00
|
|
|
models.Event.objects.filter(defined=False, name__in=names).delete()
|
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]
|
2016-08-23 10:27:06 +00:00
|
|
|
if isinstance(value, string_types):
|
2012-02-21 16:26:06 +00:00
|
|
|
value = ox.escape_html(value)
|
2011-10-09 21:12:50 +00:00
|
|
|
if key == 'alternativeNames':
|
2012-02-21 16:26:06 +00:00
|
|
|
value = tuple([ox.escape_html(v) for v in value])
|
2011-10-09 21:12:50 +00:00
|
|
|
setattr(event, key, value)
|
2012-02-21 16:26:06 +00:00
|
|
|
if 'name' in data:
|
|
|
|
event.set_name_sort(ox.escape_html(data['name']))
|
2011-10-09 21:12:50 +00:00
|
|
|
if 'nameSort' in data:
|
2012-02-21 16:26:06 +00:00
|
|
|
event.set_name_sort(ox.escape_html(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:
|
2012-02-20 14:09:26 +00:00
|
|
|
event.update_matches()
|
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()
|
2014-12-17 13:45:46 +00:00
|
|
|
add_changelog(request, data)
|
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
|
2014-10-06 08:26:43 +00:00
|
|
|
def removeEvent(request, data):
|
2011-05-28 12:10:28 +00:00
|
|
|
'''
|
2014-12-19 12:59:10 +00:00
|
|
|
Removes an event from the calendar
|
2014-12-18 19:26:37 +00:00
|
|
|
takes {
|
2014-12-18 20:56:28 +00:00
|
|
|
id: string // event id
|
2014-12-18 19:26:37 +00:00
|
|
|
}
|
|
|
|
returns {}
|
|
|
|
see: addEvent, editEvent, findEvents
|
2011-05-28 12:10:28 +00:00
|
|
|
'''
|
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):
|
2014-12-17 13:45:46 +00:00
|
|
|
add_changelog(request, data)
|
2011-05-28 12:10:28 +00:00
|
|
|
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
|
|
|
|
2014-10-06 08:26:43 +00:00
|
|
|
def findEvents(request, data):
|
2010-11-26 15:07:24 +00:00
|
|
|
'''
|
2014-12-19 12:59:10 +00:00
|
|
|
Finds calendar events for a given query
|
2014-12-18 19:26:37 +00:00
|
|
|
takes {
|
2014-12-18 20:16:41 +00:00
|
|
|
query: object, // query object, see `find`
|
|
|
|
sort: [object], // list of sort objects, see `find`
|
|
|
|
range: [int, int] // range of results to return
|
2014-12-18 19:26:37 +00:00
|
|
|
}
|
2014-12-18 20:16:41 +00:00
|
|
|
returns {
|
|
|
|
items: [object] // list of items
|
|
|
|
}
|
2014-12-19 15:05:08 +00:00
|
|
|
see: addEvent, editEvent, find, removeEvent
|
2010-11-26 15:07:24 +00:00
|
|
|
'''
|
|
|
|
response = json_response(status=200, text='ok')
|
2011-09-24 23:09:48 +00:00
|
|
|
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:
|
2014-09-19 12:26:46 +00:00
|
|
|
response['data']['position'] = utils.get_positions(ids, [qs[0].public_id])[0]
|
2011-09-24 23:09:48 +00:00
|
|
|
elif 'positions' in data:
|
2016-06-30 14:18:07 +00:00
|
|
|
ids = list(qs.values_list('id', flat=True))
|
2016-06-30 14:42:56 +00:00
|
|
|
response['data']['positions'] = utils.get_positions(ids, data['positions'], decode_id=True)
|
2011-09-24 23:09:48 +00:00
|
|
|
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)
|
2012-01-14 12:43:40 +00:00
|
|
|
|
2014-12-22 10:35:39 +00:00
|
|
|
def getEvents(request, data):
|
2012-01-14 12:43:40 +00:00
|
|
|
'''
|
2014-12-19 12:59:10 +00:00
|
|
|
Gets event names and matches
|
|
|
|
takes {}
|
2014-12-18 19:26:37 +00:00
|
|
|
returns {
|
2014-12-19 12:59:10 +00:00
|
|
|
items: [
|
|
|
|
{
|
|
|
|
name: string, // event name
|
|
|
|
matches: int // number of matches in annotations
|
|
|
|
},
|
|
|
|
... // more events
|
|
|
|
]
|
2014-12-18 19:26:37 +00:00
|
|
|
}
|
2014-12-22 10:35:39 +00:00
|
|
|
see: getPlaces
|
2012-01-14 12:43:40 +00:00
|
|
|
'''
|
|
|
|
response = json_response({})
|
|
|
|
layers = [l['id'] for l in filter(lambda l: l['type'] == 'event',
|
|
|
|
settings.CONFIG['layers'])]
|
|
|
|
items = models.Annotation.objects.filter(layer__in=layers,
|
|
|
|
events__id=None).order_by('value')
|
|
|
|
items = items.values('value').annotate(Count('value'))
|
|
|
|
response['data']['items'] = [{
|
|
|
|
'name': i['value'],
|
|
|
|
'matches': i['value__count']
|
|
|
|
} for i in items]
|
|
|
|
return render_to_json_response(response)
|
2014-12-22 10:35:39 +00:00
|
|
|
actions.register(getEvents)
|