conflict management

This commit is contained in:
j 2011-10-04 00:47:48 +00:00
parent 4027dea8b0
commit 78e29d254f
2 changed files with 29 additions and 14 deletions

View file

@ -24,8 +24,13 @@ def addEvent(request):
required keys: 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! existing_names = []
if models.Event.filter(name=data['name']).count() == 0: 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']) event = models.Event(name = data['name'])
for key in ('start', 'end', 'alternativeNames'): for key in ('start', 'end', 'alternativeNames'):
if key in data and data[key]: if key in data and data[key]:
@ -33,7 +38,8 @@ def addEvent(request):
event.save() event.save()
response = json_response(status=200, text='created') response = json_response(status=200, text='created')
else: 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) return render_to_json_response(response)
actions.register(addEvent, cache=False) actions.register(addEvent, cache=False)
@ -53,10 +59,12 @@ def editEvent(request):
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
conflict_names = []
names = [data.get('name', event.name)] + data.get('alternativeNames', []) names = [data.get('name', event.name)] + data.get('alternativeNames', [])
for name in names: #FIXME: also check aliases! for name in names:
if models.Event.filter(name=data['name']).exclude(id=event.id).count() != 0: if models.Event.objects.filter(name_find__icontains=u'|%s|'%name).exclude(id=event.id).count() != 0:
conflict = True conflict = True
conflict_names.append(name)
if not conflict: if not conflict:
for key in ('start', 'end', 'alternativeNames'): for key in ('start', 'end', 'alternativeNames'):
if key in data: if key in data:
@ -65,6 +73,7 @@ def editEvent(request):
response = json_response(status=200, text='updated') response = json_response(status=200, text='updated')
else: else:
response = json_response(status=403, text='Event name conflict') response = json_response(status=403, text='Event name conflict')
response['data']['names'] = conflict_names
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)

View file

@ -37,12 +37,13 @@ def addPlace(request):
''' '''
data = json.loads(request.POST['data']) data = json.loads(request.POST['data'])
exists = False exists = False
existing_name = existing_geoname = '' existing_names = []
existing_geoname = ''
names = data.pop('name') names = data.pop('name')
for name in [names] + data.get('alternativeNames', []): for name in [names] + data.get('alternativeNames', []):
if models.Place.objects.filter(name_find__icontains=u'|%s|'%name).count() != 0: if models.Place.objects.filter(name_find__icontains=u'|%s|'%name).count() != 0:
exists = True exists = True
existing_name = name existing_names.append(name)
if 'geoname' in data: if 'geoname' in data:
if models.Place.objects.filter(geoname=data['geoname']).count() > 0: if models.Place.objects.filter(geoname=data['geoname']).count() > 0:
exists = True exists = True
@ -62,8 +63,10 @@ def addPlace(request):
response = json_response(place.json()) response = json_response(place.json())
else: else:
response = json_response(status=403, response = json_response(status=403,
text='%s "%s" exists'%(existing_name and 'Name' or 'Geoname', text='%s exists'%(existing_names and 'Name' or 'Geoname'))
existing_name or existing_geoname)) response['data']['names'] = existing_names
if existing_geoname:
response['data']['geoname'] = existing_geoname
return render_to_json_response(response) return render_to_json_response(response)
actions.register(addPlace, cache=False) actions.register(addPlace, cache=False)
@ -85,15 +88,16 @@ def editPlace(request):
names = [names] names = [names]
if place.editable(request.user): if place.editable(request.user):
conflict = False conflict = False
confict_name = confict_geoname = '' conflict_names = []
conflict_geoname = ''
for name in names + data.get('alternativeNames', []): for name in names + data.get('alternativeNames', []):
if models.Place.objects.filter(name_find__icontains=u'|%s|'%name).exclude(id=place.id).count() != 0: if models.Place.objects.filter(name_find__icontains=u'|%s|'%name).exclude(id=place.id).count() != 0:
conflict = True conflict = True
confict_name = name conflict_names.append(name)
if 'geoname' in data: if 'geoname' in data:
if models.Place.objects.filter(geoname=data['geoname']).exclude(id=place.id).count() != 0: if models.Place.objects.filter(geoname=data['geoname']).exclude(id=place.id).count() != 0:
conflict = True conflict = True
confict_geoname = data['geoname'] conflict_geoname = data['geoname']
if not conflict: if not conflict:
for key in data: for key in data:
if key != 'id': if key != 'id':
@ -106,8 +110,10 @@ def editPlace(request):
response = json_response(place.json()) response = json_response(place.json())
else: else:
response = json_response(status=403, response = json_response(status=403,
text='%s "%s" exists'%(confict_name and 'Name' or 'Geoname', text='%s exists'%(conflict_names and 'Name' or 'Geoname'))
confict_name or confict_geoname)) response['data']['names'] = conflict_names
if conflict_geoname:
response['data']['geoname'] = conflict_geoname
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)