Skip to content

Commit 282265b

Browse files
committed
Added DoesNotExist handling.
1 parent 4627641 commit 282265b

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/signals/filters.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from typing import Any
2+
import logging
23

34
import django_filters
45
from django.db.models import Q
@@ -20,6 +21,9 @@
2021
)
2122

2223

24+
logger = logging.getLogger(__name__)
25+
26+
2327
class NumberInFilter(BaseInFilter, NumberFilter):
2428
pass
2529

@@ -50,7 +54,10 @@ class SignalFilter(django_filters.FilterSet):
5054

5155
def __init__(self, data, *args, **kwargs):
5256
data = data.copy()
53-
data.setdefault('geographic_scope', GeographicScope.objects.get(name='USA').id)
57+
try:
58+
data.setdefault('geographic_scope', GeographicScope.objects.get(name='USA').id)
59+
except GeographicScope.DoesNotExist:
60+
logger.warning("Default Geographic Scope was not found in the database. Using an empty list.")
5461
super().__init__(data, *args, **kwargs)
5562

5663
class Meta:

src/signals/views.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from typing import Any, Dict
2+
import logging
23

34
from django.conf import settings
45
from django.views.generic import DetailView, ListView
@@ -12,6 +13,9 @@
1213
from signals.serializers import SignalSerializer
1314

1415

16+
logger = logging.getLogger(__name__)
17+
18+
1519
class SignalsListView(ListView):
1620
"""
1721
ListView for displaying a list of Signal objects.
@@ -76,7 +80,12 @@ def get_context_data(self, **kwargs) -> Dict[str, Any]:
7680
context: Dict[str, Any] = super().get_context_data(**kwargs)
7781
url_params_dict, url_params_str = self.get_url_params()
7882
if not url_params_dict.get("geographic_scope"):
79-
url_params_dict["geographic_scope"] = [GeographicScope.objects.get(name="USA").id]
83+
default_geographic_scope = []
84+
try:
85+
default_geographic_scope = [GeographicScope.objects.get(name="USA").id]
86+
except GeographicScope.DoesNotExist:
87+
logger.warning("Default Geographic Scope was not found in the database. Using an empty list.")
88+
url_params_dict["geographic_scope"] = default_geographic_scope
8089
context["url_params_dict"] = url_params_dict
8190
context["form"] = SignalFilterForm(initial=url_params_dict)
8291
context["url_params_str"] = url_params_str

0 commit comments

Comments
 (0)