Skip to content

Commit 5b3e4e1

Browse files
refactor(business): Add handling of empty values ​​of the country parameter.
1 parent 75777df commit 5b3e4e1

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

promo_code/business/views.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -152,16 +152,15 @@ def get_queryset(self):
152152
company=self.request.user,
153153
)
154154

155-
countries = self.request.query_params.getlist('country', [])
156-
country_list = []
157-
158-
for country_group in countries:
159-
country_list.extend(country_group.split(','))
160-
161-
country_list = [c.strip() for c in country_list if c.strip()]
162-
163-
if country_list:
164-
regex_pattern = r'(' + '|'.join(map(re.escape, country_list)) + ')'
155+
countries = [
156+
country.strip()
157+
for group in self.request.query_params.getlist('country', [])
158+
for country in group.split(',')
159+
if country.strip()
160+
]
161+
162+
if countries:
163+
regex_pattern = r'(' + '|'.join(map(re.escape, countries)) + ')'
165164
queryset = queryset.filter(
166165
django.db.models.Q(target__country__iregex=regex_pattern)
167166
| django.db.models.Q(target__country__isnull=True),
@@ -210,9 +209,17 @@ def _validate_countries(self, errors):
210209
country_list = []
211210

212211
for country_group in countries:
213-
country_list.extend(country_group.split(','))
212+
parts = [part.strip() for part in country_group.split(',')]
213+
214+
if any(part == '' for part in parts):
215+
raise rest_framework.exceptions.ValidationError(
216+
'Invalid country format.',
217+
)
218+
219+
country_list.extend(parts)
214220

215221
country_list = [c.strip().upper() for c in country_list if c.strip()]
222+
216223
invalid_countries = []
217224

218225
for code in country_list:

0 commit comments

Comments
 (0)