Skip to content

Commit

Permalink
Merge pull request #411 from praekeltfoundation/cms-assessment-locale…
Browse files Browse the repository at this point in the history
…-filter

Add CMS locale filtering to assessment API
  • Loading branch information
DevChima authored Feb 12, 2025
2 parents 04f2fae + c97f125 commit 9ec0dc4
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Language_code field on imports and exports
- Remove whatsapp body hidden characters
- Fix for assessment import for multiple languages
- Add locale filtering to assessments API
### Removed
- Locale field on exports
-->
Expand Down
5 changes: 5 additions & 0 deletions home/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ class AssessmentViewSet(BaseAPIViewSet):

def get_queryset(self):
qa = self.request.query_params.get("qa")
locale_code = self.request.query_params.get("locale")

if qa:
# return the latest revision for each Assessment
Expand Down Expand Up @@ -355,12 +356,16 @@ def get_queryset(self):
"last_published_at"
)

if locale_code:
queryset = queryset.filter(locale__language_code=locale_code)

tag = self.request.query_params.get("tag")
if tag is not None:
ids = []
for t in AssessmentTag.objects.filter(tag__name__iexact=tag):
ids.append(t.content_object_id)
queryset = queryset.filter(id__in=ids)

return queryset


Expand Down
51 changes: 51 additions & 0 deletions home/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2810,3 +2810,54 @@ def test_assessment_detail_endpoint_filter_by_tag(self, uclient):
response = uclient.get("/api/v2/assessment/?tag=tag3")
content = json.loads(response.content)
assert content["count"] == 0


@pytest.mark.django_db
class TestAssessmentLocaleFilterAPI:
@pytest.fixture(autouse=True)
def create_test_data(self):
"""
Create the content that the tests in this class will use.
"""
self.locale_fr, _ = Locale.objects.get_or_create(language_code="fr")
self.locale_en, _ = Locale.objects.get_or_create(language_code="en")

self.assessment_fr = Assessment.objects.create(
title="French Assessment",
slug="french-assessment",
version="1.0",
locale=self.locale_fr,
live=True,
)

self.assessment_en = Assessment.objects.create(
title="English Assessment",
slug="english-assessment",
version="1.0",
locale=self.locale_en,
live=True,
)

def test_assessment_locale_filter_fr(self, admin_client):
"""
Ensure that only French assessment is returned
"""
response_fr = admin_client.get("/api/v2/assessment/", {"locale": "fr"})

assert response_fr.status_code == 200
response_data_fr = response_fr.json()

assert len(response_data_fr["results"]) == 1
assert response_data_fr["results"][0]["locale"] == "fr"
assert response_data_fr["results"][0]["title"] == "French Assessment"

def test_assessment_locale_filter_en(self, admin_client):
"""
Ensure that only English assessment is returned
"""
response_en = admin_client.get("/api/v2/assessment/", {"locale": "en"})
assert response_en.status_code == 200
response_data_en = response_en.json()
assert len(response_data_en["results"]) == 1
assert response_data_en["results"][0]["locale"] == "en"
assert response_data_en["results"][0]["title"] == "English Assessment"

0 comments on commit 9ec0dc4

Please sign in to comment.