From e9572e23c90242fa039977a470cde4d66361781b Mon Sep 17 00:00:00 2001 From: Branson Stephens <stephenb@uwm.edu> Date: Thu, 10 Jan 2013 14:02:51 -0600 Subject: [PATCH] fixed slot duplication. template filter now returns list of slot dicts.' --- gracedb/api.py | 15 +++++++++++---- gracedb/templatetags/slot.py | 13 ++++++++----- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/gracedb/api.py b/gracedb/api.py index 496d62fd5..146c721a8 100644 --- a/gracedb/api.py +++ b/gracedb/api.py @@ -854,10 +854,17 @@ class EventSlot(APIView): if not os.path.exists(filePath): return Response("No slot created because file does not exist", status=status.HTTP_404_NOT_FOUND) - # Create the slot. - slot = Slot(event=event,name=slotname,value=filename) - slot.save() - return Response("Slot created.",status=status.HTTP_201_CREATED) + # Check for existence of the slot. If it exists, simply update the + # existing slot. + try: + slot = Slot.objects.filter(event=event).filter(name=slotname)[0] + slot.value = filename + slot.save() + except: + # Create the slot. + slot = Slot(event=event,name=slotname,value=filename) + slot.save() + return Response("Slot created or updated.",status=status.HTTP_201_CREATED) # Delete a slot. def delete(self, request, graceid, slotname): diff --git a/gracedb/templatetags/slot.py b/gracedb/templatetags/slot.py index fff26f11f..97c9524bd 100644 --- a/gracedb/templatetags/slot.py +++ b/gracedb/templatetags/slot.py @@ -6,13 +6,16 @@ from ..models import Slot, EventLog register = template.Library() @register.filter("slot") -def slot(event,slotname): +def slot(event,pattern): if event is None: - return mark_safe("") + return None try: - slot = Slot.objects.filter(event=event).filter(name=slotname)[0] - out = slot.value + # This returns a list of dictionary objects, like + # [{'event': event, 'name': 'skymap', 'value':'skymap.png'}, ... ] + # XXX doesn't the template need the full path? + return Slot.objects.filter(event=event).filter(name__regex=pattern).values() except: + # Either there is no such slot or something went wrong. + # In either case, we want the template to just ignore it. return None - return mark_safe(out) -- GitLab