Skip to content

Commit db9b7a4

Browse files
sudip-khanalsandeshit
authored andcommitted
feat(notification): make countries field optional on alert subscription model
1 parent 8ba2cf4 commit db9b7a4

3 files changed

Lines changed: 66 additions & 2 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Generated by Django 4.2.30 on 2026-05-20 09:16
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('api', '0231_alter_export_export_type'),
10+
('notifications', '0016_alertsubscription'),
11+
]
12+
13+
operations = [
14+
migrations.AlterField(
15+
model_name='alertsubscription',
16+
name='countries',
17+
field=models.ManyToManyField(blank=True, related_name='alert_subscriptions_countries', to='api.country', verbose_name='Countries'),
18+
),
19+
]

notifications/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ class AlertPerDay(models.IntegerChoices):
356356
Country,
357357
related_name="alert_subscriptions_countries",
358358
verbose_name=_("Countries"),
359+
blank=True,
359360
)
360361
regions = models.ManyToManyField(
361362
Region,

notifications/tests.py

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ def test_list_retrieve_subscription(self):
277277
self.assert_200(response)
278278
self.assertEqual(response.data["user"], self.user1.id)
279279

280-
# Test create Subscription without country and region
280+
# Test Create Subscription
281281
def test_create_subscription_without_country_and_region(self):
282282
data = {
283283
"title": "title-1-test",
@@ -291,7 +291,38 @@ def test_create_subscription_without_country_and_region(self):
291291
response = self.client.post(url, data=data, format="json")
292292
self.assert_400(response)
293293

294-
# Test Create Subscription
294+
def test_create_subscription_without_country(self):
295+
data = {
296+
"title": "title-2-test",
297+
"user": self.user1.id,
298+
"hazard_types": [self.hazard_type1.id, self.hazard_type2.id],
299+
"source": AlertSubscription.AlertSource.MONTANDON,
300+
"regions": [self.region.id],
301+
}
302+
url = "/api/v2/alert-subscription/"
303+
self.authenticate(self.user1)
304+
response = self.client.post(url, data=data, format="json")
305+
self.assert_201(response)
306+
subscription = AlertSubscription.objects.get(id=response.data["id"])
307+
self.assertEqual(subscription.user, self.user1)
308+
309+
def test_create_subscription_without_region(self):
310+
data = {
311+
"title": "title-2-test",
312+
"user": self.user1.id,
313+
"hazard_types": [self.hazard_type1.id, self.hazard_type2.id],
314+
"source": AlertSubscription.AlertSource.MONTANDON,
315+
"countries": [self.country.id, self.country_1.id],
316+
"alert_per_day": AlertSubscription.AlertPerDay.FIFTY,
317+
}
318+
url = "/api/v2/alert-subscription/"
319+
self.authenticate(self.user1)
320+
response = self.client.post(url, data=data, format="json")
321+
self.assert_201(response)
322+
subscription = AlertSubscription.objects.get(id=response.data["id"])
323+
self.assertEqual(subscription.user, self.user1)
324+
self.assertEqual(subscription.alert_per_day, AlertSubscription.AlertPerDay.FIFTY)
325+
295326
def test_create_subscription(self):
296327

297328
data = {
@@ -328,3 +359,16 @@ def test_update_subscription(self):
328359
self.alert_subscription.refresh_from_db()
329360
self.assertEqual(self.alert_subscription.countries.first().id, self.country_1.id)
330361
self.assertEqual(self.alert_subscription.alert_per_day, AlertSubscription.AlertPerDay.TEN)
362+
363+
def test_delete_subscription(self):
364+
self.alert_subscription = AlertSubscriptionFactory.create(
365+
title="Test title",
366+
user=self.user1,
367+
countries=self.countries,
368+
regions=[self.region],
369+
hazard_types=[self.hazard_type1, self.hazard_type2],
370+
)
371+
self.authenticate(self.user1)
372+
url = f"/api/v2/alert-subscription/{self.alert_subscription.id}/"
373+
response = self.client.delete(url)
374+
self.assert_204(response)

0 commit comments

Comments
 (0)