Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion assets
Submodule assets updated 0 files
11 changes: 11 additions & 0 deletions notifications/serializers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.utils.translation import gettext
from rest_framework import serializers

from api.serializers import (
Expand Down Expand Up @@ -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 = (
Expand Down
14 changes: 14 additions & 0 deletions notifications/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down
Loading