forked from 0x2620/pandora
conflict management
This commit is contained in:
parent
4027dea8b0
commit
78e29d254f
2 changed files with 29 additions and 14 deletions
|
@ -24,8 +24,13 @@ def addEvent(request):
|
|||
required keys: name, start, end
|
||||
'''
|
||||
data = json.loads(request.POST['data'])
|
||||
#FIXME: check for alternativeNames too!
|
||||
if models.Event.filter(name=data['name']).count() == 0:
|
||||
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:
|
||||
event = models.Event(name = data['name'])
|
||||
for key in ('start', 'end', 'alternativeNames'):
|
||||
if key in data and data[key]:
|
||||
|
@ -33,7 +38,8 @@ def addEvent(request):
|
|||
event.save()
|
||||
response = json_response(status=200, text='created')
|
||||
else:
|
||||
response = json_response(status=403, text='event name exists')
|
||||
response = json_response(status=403, text='name exists')
|
||||
response['data']['names'] = existing_names
|
||||
return render_to_json_response(response)
|
||||
actions.register(addEvent, cache=False)
|
||||
|
||||
|
@ -53,10 +59,12 @@ def editEvent(request):
|
|||
event = get_object_or_404_json(models.Event, pk=data['id'])
|
||||
if event.editable(request.user):
|
||||
conflict = False
|
||||
conflict_names = []
|
||||
names = [data.get('name', event.name)] + data.get('alternativeNames', [])
|
||||
for name in names: #FIXME: also check aliases!
|
||||
if models.Event.filter(name=data['name']).exclude(id=event.id).count() != 0:
|
||||
for name in names:
|
||||
if models.Event.objects.filter(name_find__icontains=u'|%s|'%name).exclude(id=event.id).count() != 0:
|
||||
conflict = True
|
||||
conflict_names.append(name)
|
||||
if not conflict:
|
||||
for key in ('start', 'end', 'alternativeNames'):
|
||||
if key in data:
|
||||
|
@ -65,6 +73,7 @@ def editEvent(request):
|
|||
response = json_response(status=200, text='updated')
|
||||
else:
|
||||
response = json_response(status=403, text='Event name conflict')
|
||||
response['data']['names'] = conflict_names
|
||||
else:
|
||||
response = json_response(status=403, text='permission denied')
|
||||
return render_to_json_response(response)
|
||||
|
|
|
@ -37,12 +37,13 @@ def addPlace(request):
|
|||
'''
|
||||
data = json.loads(request.POST['data'])
|
||||
exists = False
|
||||
existing_name = existing_geoname = ''
|
||||
existing_names = []
|
||||
existing_geoname = ''
|
||||
names = data.pop('name')
|
||||
for name in [names] + data.get('alternativeNames', []):
|
||||
if models.Place.objects.filter(name_find__icontains=u'|%s|'%name).count() != 0:
|
||||
exists = True
|
||||
existing_name = name
|
||||
existing_names.append(name)
|
||||
if 'geoname' in data:
|
||||
if models.Place.objects.filter(geoname=data['geoname']).count() > 0:
|
||||
exists = True
|
||||
|
@ -62,8 +63,10 @@ def addPlace(request):
|
|||
response = json_response(place.json())
|
||||
else:
|
||||
response = json_response(status=403,
|
||||
text='%s "%s" exists'%(existing_name and 'Name' or 'Geoname',
|
||||
existing_name or existing_geoname))
|
||||
text='%s exists'%(existing_names and 'Name' or 'Geoname'))
|
||||
response['data']['names'] = existing_names
|
||||
if existing_geoname:
|
||||
response['data']['geoname'] = existing_geoname
|
||||
return render_to_json_response(response)
|
||||
actions.register(addPlace, cache=False)
|
||||
|
||||
|
@ -85,15 +88,16 @@ def editPlace(request):
|
|||
names = [names]
|
||||
if place.editable(request.user):
|
||||
conflict = False
|
||||
confict_name = confict_geoname = ''
|
||||
conflict_names = []
|
||||
conflict_geoname = ''
|
||||
for name in names + data.get('alternativeNames', []):
|
||||
if models.Place.objects.filter(name_find__icontains=u'|%s|'%name).exclude(id=place.id).count() != 0:
|
||||
conflict = True
|
||||
confict_name = name
|
||||
conflict_names.append(name)
|
||||
if 'geoname' in data:
|
||||
if models.Place.objects.filter(geoname=data['geoname']).exclude(id=place.id).count() != 0:
|
||||
conflict = True
|
||||
confict_geoname = data['geoname']
|
||||
conflict_geoname = data['geoname']
|
||||
if not conflict:
|
||||
for key in data:
|
||||
if key != 'id':
|
||||
|
@ -106,8 +110,10 @@ def editPlace(request):
|
|||
response = json_response(place.json())
|
||||
else:
|
||||
response = json_response(status=403,
|
||||
text='%s "%s" exists'%(confict_name and 'Name' or 'Geoname',
|
||||
confict_name or confict_geoname))
|
||||
text='%s exists'%(conflict_names and 'Name' or 'Geoname'))
|
||||
response['data']['names'] = conflict_names
|
||||
if conflict_geoname:
|
||||
response['data']['geoname'] = conflict_geoname
|
||||
else:
|
||||
response = json_response(status=403, text='permission denied')
|
||||
return render_to_json_response(response)
|
||||
|
|
Loading…
Reference in a new issue