Skip to content

Commit

Permalink
Merge pull request #481 from johannaengland/destination-is-deletable
Browse files Browse the repository at this point in the history
Make DestinationConfig.is_deletable behave according to name
  • Loading branch information
johannaengland authored Oct 6, 2022
2 parents 1de5d75 + 80d1543 commit adcaad7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
14 changes: 10 additions & 4 deletions src/argus/notificationprofile/media/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@


class NotificationMedium(ABC):
class NotDeletableError(Exception):
"""
Custom exception class that is raised when a destination cannot be
deleted
"""

@classmethod
@abstractmethod
def validate(cls, instance: RequestDestinationConfigSerializer, dict: dict) -> dict:
Expand All @@ -39,11 +45,11 @@ def send(event: Event, destinations: QuerySet[DestinationConfig], **kwargs) -> b
"""Sends message about a given event to the given destinations"""
pass

@staticmethod
def is_deletable(destination: DestinationConfig) -> Union[str, NoneType]:
@classmethod
def raise_if_not_deletable(cls, destination: DestinationConfig):
"""
Returns None if the given destination is able to be deleted and
returns an error message if not
Returns None if the given destination is deletable and raises an
NotDeletableError if not
"""
return None

Expand Down
23 changes: 10 additions & 13 deletions src/argus/notificationprofile/media/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,27 +80,24 @@ def validate(cls, instance: RequestDestinationConfigSerializer, email_dict: dict

return form.cleaned_data

@staticmethod
def is_deletable(destination: DestinationConfig) -> Union[str, NoneType]:
@classmethod
def raise_if_not_deletable(cls, destination: DestinationConfig) -> Union[str, NoneType]:
"""
Returns None if the given email destination is able to be deleted
and an error message if it was defined by an outside source or
is in use by any notification profiles
Raises a NotDeletableError if the given email destination is not able
to be deleted (if it was defined by an outside source or is in use by
any notification profiles)
"""
if destination.settings["synced"]:
return "Cannot delete this email destination since it was defined by an outside source."
raise cls.NotDeletableError(
"Cannot delete this email destination since it was defined by an outside source."
)

connected_profiles = destination.notification_profiles.all()
if connected_profiles:
profiles = ", ".join([str(profile) for profile in connected_profiles])
return "".join(
[
"Cannot delete this destination since it is in use in the notification profile(s): ",
profiles,
".",
]
raise cls.NotDeletableError(
f"Cannot delete this destination since it is in use in the notification profile(s): {profiles}."
)
return None

@staticmethod
def update(destination: DestinationConfig, validated_data: dict) -> Union[DestinationConfig, NoneType]:
Expand Down
10 changes: 6 additions & 4 deletions src/argus/notificationprofile/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from argus.drf.permissions import IsOwner
from argus.incident.serializers import IncidentSerializer
from argus.notificationprofile.media import MEDIA_CLASSES_DICT
from argus.notificationprofile.media.base import NotificationMedium
from .models import DestinationConfig, Filter, Media, NotificationProfile, Timeslot
from .serializers import (
FilterSerializer,
Expand Down Expand Up @@ -170,11 +171,12 @@ def destroy(self, request, *args, **kwargs):
except DestinationConfig.DoesNotExist:
raise ValidationError(f"Destination with pk={pk} does not exist.")

error_message = MEDIA_CLASSES_DICT[destination.media.slug].is_deletable(destination)
if not error_message:
return super().destroy(destination)
try:
MEDIA_CLASSES_DICT[destination.media.slug].raise_if_not_deletable(destination)
except NotificationMedium.NotDeletableError as e:
raise ValidationError(str(e))
else:
raise ValidationError(error_message)
return super().destroy(destination)


class TimeslotViewSet(viewsets.ModelViewSet):
Expand Down

0 comments on commit adcaad7

Please sign in to comment.