Skip to content

Commit adcaad7

Browse files
Merge pull request #481 from johannaengland/destination-is-deletable
Make DestinationConfig.is_deletable behave according to name
2 parents 1de5d75 + 80d1543 commit adcaad7

File tree

3 files changed

+26
-21
lines changed

3 files changed

+26
-21
lines changed

src/argus/notificationprofile/media/base.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616

1717

1818
class NotificationMedium(ABC):
19+
class NotDeletableError(Exception):
20+
"""
21+
Custom exception class that is raised when a destination cannot be
22+
deleted
23+
"""
24+
1925
@classmethod
2026
@abstractmethod
2127
def validate(cls, instance: RequestDestinationConfigSerializer, dict: dict) -> dict:
@@ -39,11 +45,11 @@ def send(event: Event, destinations: QuerySet[DestinationConfig], **kwargs) -> b
3945
"""Sends message about a given event to the given destinations"""
4046
pass
4147

42-
@staticmethod
43-
def is_deletable(destination: DestinationConfig) -> Union[str, NoneType]:
48+
@classmethod
49+
def raise_if_not_deletable(cls, destination: DestinationConfig):
4450
"""
45-
Returns None if the given destination is able to be deleted and
46-
returns an error message if not
51+
Returns None if the given destination is deletable and raises an
52+
NotDeletableError if not
4753
"""
4854
return None
4955

src/argus/notificationprofile/media/email.py

+10-13
Original file line numberDiff line numberDiff line change
@@ -80,27 +80,24 @@ def validate(cls, instance: RequestDestinationConfigSerializer, email_dict: dict
8080

8181
return form.cleaned_data
8282

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

9395
connected_profiles = destination.notification_profiles.all()
9496
if connected_profiles:
9597
profiles = ", ".join([str(profile) for profile in connected_profiles])
96-
return "".join(
97-
[
98-
"Cannot delete this destination since it is in use in the notification profile(s): ",
99-
profiles,
100-
".",
101-
]
98+
raise cls.NotDeletableError(
99+
f"Cannot delete this destination since it is in use in the notification profile(s): {profiles}."
102100
)
103-
return None
104101

105102
@staticmethod
106103
def update(destination: DestinationConfig, validated_data: dict) -> Union[DestinationConfig, NoneType]:

src/argus/notificationprofile/views.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from argus.drf.permissions import IsOwner
1515
from argus.incident.serializers import IncidentSerializer
1616
from argus.notificationprofile.media import MEDIA_CLASSES_DICT
17+
from argus.notificationprofile.media.base import NotificationMedium
1718
from .models import DestinationConfig, Filter, Media, NotificationProfile, Timeslot
1819
from .serializers import (
1920
FilterSerializer,
@@ -170,11 +171,12 @@ def destroy(self, request, *args, **kwargs):
170171
except DestinationConfig.DoesNotExist:
171172
raise ValidationError(f"Destination with pk={pk} does not exist.")
172173

173-
error_message = MEDIA_CLASSES_DICT[destination.media.slug].is_deletable(destination)
174-
if not error_message:
175-
return super().destroy(destination)
174+
try:
175+
MEDIA_CLASSES_DICT[destination.media.slug].raise_if_not_deletable(destination)
176+
except NotificationMedium.NotDeletableError as e:
177+
raise ValidationError(str(e))
176178
else:
177-
raise ValidationError(error_message)
179+
return super().destroy(destination)
178180

179181

180182
class TimeslotViewSet(viewsets.ModelViewSet):

0 commit comments

Comments
 (0)