diff --git a/assets b/assets index 88c8391f4..e71ea53a6 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit 88c8391f43538cadef481b0cefcf9b211e154f99 +Subproject commit e71ea53a6c2d6c7c7dca3a6d77982f57e670d413 diff --git a/notifications/serializers.py b/notifications/serializers.py index 96781bd79..a8dcbbc11 100644 --- a/notifications/serializers.py +++ b/notifications/serializers.py @@ -1,3 +1,4 @@ +from django.utils.translation import gettext from rest_framework import serializers from api.serializers import ( @@ -270,6 +271,16 @@ class AlertSubscriptionSerialize(ModelSerializer): hazard_types_detail = DisasterTypeSerializer(source="hazard_types", many=True, read_only=True) alert_per_day_display = serializers.CharField(source="get_alert_per_day_display", read_only=True) + def validate(self, attrs): + countries = attrs.get("countries") + regions = attrs.get("regions") + + if not countries and not regions: + raise serializers.ValidationError( + {"non_field_errors": gettext("Please select at least one country or one region to create a subscription.")} + ) + return attrs + class Meta: model = AlertSubscription fields = ( diff --git a/notifications/tests.py b/notifications/tests.py index 725fda5af..1f3936cdd 100644 --- a/notifications/tests.py +++ b/notifications/tests.py @@ -277,6 +277,20 @@ def test_list_retrieve_subscription(self): self.assert_200(response) self.assertEqual(response.data["user"], self.user1.id) + # Test create Subscription without country and region + def test_create_subscription_without_country_and_region(self): + data = { + "title": "title-1-test", + "user": self.user1.id, + "hazard_types": [self.hazard_type1.id, self.hazard_type2.id], + "alert_per_day": AlertSubscription.AlertPerDay.TEN, + "source": AlertSubscription.AlertSource.MONTANDON, + } + url = "/api/v2/alert-subscription/" + self.authenticate(self.user1) + response = self.client.post(url, data=data, format="json") + self.assert_400(response) + # Test Create Subscription def test_create_subscription(self):