Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow xx_XX lang in parameter #93

Merged
merged 2 commits into from
Aug 5, 2024
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
8 changes: 8 additions & 0 deletions qgisfeedproject/qgisfeed/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ def test_lang_filter(self):
self.assertTrue("Null Island QGIS Meeting" in titles)
self.assertTrue("QGIS acquired by ESRI" in titles)

response = c.get('/?lang=en_US')
data = json.loads(response.content)
titles = [d['title'] for d in data]
self.assertTrue("Null Island QGIS Meeting" in titles)
self.assertTrue("QGIS acquired by ESRI" in titles)

def test_lat_lon_filter(self):
c = Client(HTTP_USER_AGENT='Mozilla/5.0 QGIS/32400/Fedora '
'Linux (Workstation Edition)')
Expand Down Expand Up @@ -175,6 +181,8 @@ def test_invalid_parameters(self):
self.assertEqual(response.status_code, 400)
response = c.get('/?lang=KK')
self.assertEqual(response.status_code, 400)
response = c.get('/?lang=english')
self.assertEqual(response.status_code, 400)

def test_image_link(self):
c = Client(HTTP_USER_AGENT='Mozilla/5.0 QGIS/32400/Fedora '
Expand Down
11 changes: 10 additions & 1 deletion qgisfeedproject/qgisfeed/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
from user_visit.models import UserVisit
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

import re


QGISFEED_MAX_RECORDS=getattr(settings, 'QGISFEED_MAX_RECORDS', 20)

Expand Down Expand Up @@ -67,7 +69,14 @@ def get_filters(self, request):
filters = {}
if request.GET.get('lang'):
lang = request.GET.get('lang')
if not lang in LANGUAGE_KEYS:
# Define the regular expression pattern for 'xx' or 'xx_XX'
pattern = re.compile(r'^[a-z]{2}(_[A-Z]{2})?$')
if pattern.match(lang):
# Get the first two letters of the language code
lang = lang[:2]
if lang not in LANGUAGE_KEYS:
raise BadRequestException("Invalid language parameter.")
else:
raise BadRequestException("Invalid language parameter.")
filters['lang'] = lang
if request.GET.get('lat') and request.GET.get('lon'):
Expand Down
Loading