From 80d15436345bc2b7f86ddf7afe6ab842560b2822 Mon Sep 17 00:00:00 2001 From: Johanna England Date: Mon, 19 Sep 2022 16:01:23 +0200 Subject: [PATCH] Change is_deletable to raise_if_not_deletable --- src/argus/notificationprofile/media/base.py | 14 ++++++++---- src/argus/notificationprofile/media/email.py | 23 +++++++++----------- src/argus/notificationprofile/views.py | 10 +++++---- 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/src/argus/notificationprofile/media/base.py b/src/argus/notificationprofile/media/base.py index b997d8271..1cf99fc10 100644 --- a/src/argus/notificationprofile/media/base.py +++ b/src/argus/notificationprofile/media/base.py @@ -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: @@ -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 diff --git a/src/argus/notificationprofile/media/email.py b/src/argus/notificationprofile/media/email.py index 4828e9899..831bc6adc 100644 --- a/src/argus/notificationprofile/media/email.py +++ b/src/argus/notificationprofile/media/email.py @@ -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]: diff --git a/src/argus/notificationprofile/views.py b/src/argus/notificationprofile/views.py index eb3940e68..45395b3a4 100644 --- a/src/argus/notificationprofile/views.py +++ b/src/argus/notificationprofile/views.py @@ -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, @@ -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):