more vents

This commit is contained in:
j 2011-05-28 14:10:28 +02:00
parent 6886c76255
commit fe2163064d

View file

@ -12,13 +12,26 @@ from api.actions import actions
@login_required_json @login_required_json
def addEvent(request): def addEvent(request):
'''
param data
{
'name': '',
'start': ''
'end': ''
}
required keys: name, start, end
'''
data = json.loads(request.POST['data']) data = json.loads(request.POST['data'])
#FIXME: check for alternativeNames too!
if models.Event.filter(name=data['name']).count() == 0: if models.Event.filter(name=data['name']).count() == 0:
place = models.Event(name = data['name']) event = models.Event(name = data['name'])
place.save() for key in ('start', 'end', 'alternativeNames'):
if key in data and data[key]:
setattr(event, key, data[key])
event.save()
response = json_response(status=200, text='created') response = json_response(status=200, text='created')
else: else:
response = json_response(status=403, text='place name exists') response = json_response(status=403, text='event name exists')
return render_to_json_response(response) return render_to_json_response(response)
actions.register(addEvent, cache=False) actions.register(addEvent, cache=False)
@ -28,26 +41,28 @@ def editEvent(request):
''' '''
param data param data
{ {
'id': dateid, 'id': event id,
'date': dict 'name': ''
...
} }
date contains key/value pairs with place propterties update provides keys of event with id
''' '''
data = json.loads(request.POST['data']) data = json.loads(request.POST['data'])
Event = get_object_or_404_json(models.Event, pk=data['id']) event = get_object_or_404_json(models.Event, pk=data['id'])
if Event.editable(request.user): if event.editable(request.user):
conflict = False conflict = False
names = [data['date']['name']] + data['date']['aliases'] names = [data.get('name', event.name)] + data.get('alternativeNames', [])
for name in names: #FIXME: also check aliases! for name in names: #FIXME: also check aliases!
if models.Event.filter(name=data['name']).exclude(id=Event.id).count() != 0: if models.Event.filter(name=data['name']).exclude(id=event.id).count() != 0:
conflict = True conflict = True
if not conflict: if not conflict:
for key in data['date']: for key in ('start', 'end', 'alternativeNames'):
setattr(Event, key, data['date'][key]) if key in data:
Event.save() setattr(event, key, data[key])
event.save()
response = json_response(status=200, text='updated') response = json_response(status=200, text='updated')
else: else:
response = json_response(status=403, text='Event name/alias conflict') response = json_response(status=403, text='Event name conflict')
else: else:
response = json_response(status=403, text='permission denied') response = json_response(status=403, text='permission denied')
return render_to_json_response(response) return render_to_json_response(response)
@ -56,7 +71,20 @@ actions.register(editEvent, cache=False)
@login_required_json @login_required_json
def removeEvent(request): def removeEvent(request):
response = json_response(status=501, text='not implemented') '''
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')
return render_to_json_response(response) return render_to_json_response(response)
actions.register(removeEvent, cache=False) actions.register(removeEvent, cache=False)