Skip to content
Snippets Groups Projects
Commit 4642abd6 authored by GraceDB's avatar GraceDB
Browse files

Update SupereventEventViewSet deletion handling

We now use ValidateDestroyMixin rather than SafeDeleteMixin for
handling removal of events from a superevent.  Some additional
logic in other places was no longer needed.
parent 8142fc5f
No related branches found
No related tags found
No related merge requests found
...@@ -107,7 +107,7 @@ class SupereventViewSet(SafeCreateMixin, viewsets.ModelViewSet): ...@@ -107,7 +107,7 @@ class SupereventViewSet(SafeCreateMixin, viewsets.ModelViewSet):
return Response(serializer.data) return Response(serializer.data)
class SupereventEventViewSet(SafeDestroyMixin, class SupereventEventViewSet(ValidateDestroyMixin,
SupereventNestedViewSet): SupereventNestedViewSet):
"""View for events attached to a superevent""" """View for events attached to a superevent"""
serializer_class = SupereventEventSerializer serializer_class = SupereventEventSerializer
...@@ -115,8 +115,6 @@ class SupereventEventViewSet(SafeDestroyMixin, ...@@ -115,8 +115,6 @@ class SupereventEventViewSet(SafeDestroyMixin,
permission_classes = (permissions.IsAuthenticatedOrReadOnly, permission_classes = (permissions.IsAuthenticatedOrReadOnly,
EventParentSupereventPermissions,) EventParentSupereventPermissions,)
lookup_url_kwarg = 'graceid' lookup_url_kwarg = 'graceid'
destroy_error_classes = (Superevent.PreferredEventRemovalError,)
destroy_error_response_status = status.HTTP_400_BAD_REQUEST
list_view_order_by = ('pk',) list_view_order_by = ('pk',)
def get_object(self): def get_object(self):
...@@ -130,6 +128,21 @@ class SupereventEventViewSet(SafeDestroyMixin, ...@@ -130,6 +128,21 @@ class SupereventEventViewSet(SafeDestroyMixin,
return event return event
def validate_destroy(self, request, instance):
# Don't allow removal of preferred events
# NOTE: instance should be an event attached to the superevent
# in question. All other events are filtered out when we get
# and filter the queryset. So if instance has the
# superevent_preferred_for attribute, it must be the preferred event.
if hasattr(instance, 'superevent_preferred_for'):
err_msg = ("Event {gid} can't be removed from superevent {sid} "
"because it is the preferred event").format(
gid=instance.graceid, sid=instance.superevent.graceid)
return False, err_msg
else:
return True, None
def perform_destroy(self, instance): def perform_destroy(self, instance):
remove_event_from_superevent(instance.superevent, instance, remove_event_from_superevent(instance.superevent, instance,
self.request.user, add_superevent_log=True, self.request.user, add_superevent_log=True,
......
...@@ -410,10 +410,6 @@ class Superevent(CleanSaveModel, AutoIncrementModel): ...@@ -410,10 +410,6 @@ class Superevent(CleanSaveModel, AutoIncrementModel):
def __unicode__(self): def __unicode__(self):
return self.superevent_id return self.superevent_id
class PreferredEventRemovalError(Exception):
# To be raised when an attempt is made to remove the preferred event.
pass
class DateIdError(Exception): class DateIdError(Exception):
# To be raised when the superevent date ID is in a bad format; i.e., # To be raised when the superevent date ID is in a bad format; i.e.,
# one that datetime can't parse or that the regex won't match # one that datetime can't parse or that the regex won't match
......
...@@ -326,7 +326,6 @@ def remove_tag_from_log(log, tag, user, add_log_message=True, ...@@ -326,7 +326,6 @@ def remove_tag_from_log(log, tag, user, add_log_message=True,
log_for_tag_removal = create_log(user, comment, event_or_superevent, log_for_tag_removal = create_log(user, comment, event_or_superevent,
issue_alert=False, autogenerated=True) issue_alert=False, autogenerated=True)
return log_for_tag_removal return log_for_tag_removal
...@@ -377,11 +376,6 @@ def remove_event_from_superevent(superevent, event, user, add_event_log=True, ...@@ -377,11 +376,6 @@ def remove_event_from_superevent(superevent, event, user, add_event_log=True,
This function should be within a try-except block to catch exceptions and This function should be within a try-except block to catch exceptions and
convert them to the appropriate response. convert them to the appropriate response.
""" """
# Throw error if this is the preferred event
if event == superevent.preferred_event:
raise Superevent.PreferredEventRemovalError("Can't remove a "
"superevent's preferred event without setting a new one.")
# Remove event from superevent # Remove event from superevent
superevent.events.remove(event) superevent.events.remove(event)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment