From bda2d6f3338fccdf6b85ce1dd45e8eb66303bd42 Mon Sep 17 00:00:00 2001 From: sudip-khanal Date: Wed, 20 May 2026 15:16:58 +0545 Subject: [PATCH] feat(notification): make countries field optional on alert subscription model --- assets | 2 +- .../0017_alter_alertsubscription_countries.py | 19 ++++++++ notifications/models.py | 1 + notifications/tests.py | 48 ++++++++++++++++++- 4 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 notifications/migrations/0017_alter_alertsubscription_countries.py diff --git a/assets b/assets index df833da38..da0353b6e 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit df833da38287a18dbdccbe50f623a91a609fa363 +Subproject commit da0353b6e2903072825743d77bf2e0ff79195a7e diff --git a/notifications/migrations/0017_alter_alertsubscription_countries.py b/notifications/migrations/0017_alter_alertsubscription_countries.py new file mode 100644 index 000000000..7008a38f8 --- /dev/null +++ b/notifications/migrations/0017_alter_alertsubscription_countries.py @@ -0,0 +1,19 @@ +# Generated by Django 4.2.30 on 2026-05-20 09:16 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0231_alter_export_export_type'), + ('notifications', '0016_alertsubscription'), + ] + + operations = [ + migrations.AlterField( + model_name='alertsubscription', + name='countries', + field=models.ManyToManyField(blank=True, related_name='alert_subscriptions_countries', to='api.country', verbose_name='Countries'), + ), + ] diff --git a/notifications/models.py b/notifications/models.py index 80c017159..b39707985 100644 --- a/notifications/models.py +++ b/notifications/models.py @@ -356,6 +356,7 @@ class AlertPerDay(models.IntegerChoices): Country, related_name="alert_subscriptions_countries", verbose_name=_("Countries"), + blank=True, ) regions = models.ManyToManyField( Region, diff --git a/notifications/tests.py b/notifications/tests.py index 1f3936cdd..0c1d6d865 100644 --- a/notifications/tests.py +++ b/notifications/tests.py @@ -277,7 +277,7 @@ 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 + # Test Create Subscription def test_create_subscription_without_country_and_region(self): data = { "title": "title-1-test", @@ -291,7 +291,38 @@ def test_create_subscription_without_country_and_region(self): response = self.client.post(url, data=data, format="json") self.assert_400(response) - # Test Create Subscription + def test_create_subscription_without_country(self): + data = { + "title": "title-2-test", + "user": self.user1.id, + "hazard_types": [self.hazard_type1.id, self.hazard_type2.id], + "source": AlertSubscription.AlertSource.MONTANDON, + "regions": [self.region.id], + } + url = "/api/v2/alert-subscription/" + self.authenticate(self.user1) + response = self.client.post(url, data=data, format="json") + self.assert_201(response) + subscription = AlertSubscription.objects.get(id=response.data["id"]) + self.assertEqual(subscription.user, self.user1) + + def test_create_subscription_without_region(self): + data = { + "title": "title-2-test", + "user": self.user1.id, + "hazard_types": [self.hazard_type1.id, self.hazard_type2.id], + "source": AlertSubscription.AlertSource.MONTANDON, + "countries": [self.country.id, self.country_1.id], + "alert_per_day": AlertSubscription.AlertPerDay.FIFTY, + } + url = "/api/v2/alert-subscription/" + self.authenticate(self.user1) + response = self.client.post(url, data=data, format="json") + self.assert_201(response) + subscription = AlertSubscription.objects.get(id=response.data["id"]) + self.assertEqual(subscription.user, self.user1) + self.assertEqual(subscription.alert_per_day, AlertSubscription.AlertPerDay.FIFTY) + def test_create_subscription(self): data = { @@ -328,3 +359,16 @@ def test_update_subscription(self): self.alert_subscription.refresh_from_db() self.assertEqual(self.alert_subscription.countries.first().id, self.country_1.id) self.assertEqual(self.alert_subscription.alert_per_day, AlertSubscription.AlertPerDay.TEN) + + def test_delete_subscription(self): + self.alert_subscription = AlertSubscriptionFactory.create( + title="Test title", + user=self.user1, + countries=self.countries, + regions=[self.region], + hazard_types=[self.hazard_type1, self.hazard_type2], + ) + self.authenticate(self.user1) + url = f"/api/v2/alert-subscription/{self.alert_subscription.id}/" + response = self.client.delete(url) + self.assert_204(response)