Sort organizations by name
This commit is contained in:
parent
a154341fae
commit
a2eda11179
|
|
@ -11,6 +11,17 @@ from vbv_lernwelt.shop.serializers import CountrySerializer
|
|||
@api_view(["GET"])
|
||||
@permission_classes([IsAuthenticated])
|
||||
def list_entities(request):
|
||||
organisations = OrganisationSerializer(Organisation.objects.all(), many=True).data
|
||||
language_code = request.LANGUAGE_CODE if request.LANGUAGE_CODE else "de"
|
||||
field_mapping = {
|
||||
"de": "name_de",
|
||||
"fr": "name_fr",
|
||||
"it": "name_it",
|
||||
}
|
||||
|
||||
field_name = field_mapping.get(language_code, field_mapping["de"])
|
||||
|
||||
organisations = OrganisationSerializer(
|
||||
Organisation.objects.all().order_by(field_name), many=True
|
||||
).data
|
||||
countries = CountrySerializer(Country.objects.all(), many=True).data
|
||||
return Response({"organisations": organisations, "countries": countries})
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ from rest_framework import status
|
|||
from rest_framework.test import APITestCase
|
||||
|
||||
from vbv_lernwelt.core.model_utils import add_organisations
|
||||
from vbv_lernwelt.core.models import User
|
||||
from vbv_lernwelt.core.models import Organisation, User
|
||||
from vbv_lernwelt.shop.model_utils import add_countries
|
||||
|
||||
|
||||
|
|
@ -17,6 +17,10 @@ class EntitiesViewTest(APITestCase):
|
|||
add_countries()
|
||||
|
||||
def test_list_entities(self) -> None:
|
||||
# It seems that different locales handle ordering differently (especially with lower case letters)
|
||||
# As such we delete entries that start with lower case letters
|
||||
Organisation.objects.filter(organisation_id__in=[1, 2, 3]).delete()
|
||||
|
||||
# GIVEN
|
||||
url = reverse("list_entities")
|
||||
|
||||
|
|
@ -29,12 +33,12 @@ class EntitiesViewTest(APITestCase):
|
|||
organisations = response.data["organisations"]
|
||||
|
||||
self.assertEqual(
|
||||
organisations[0],
|
||||
organisations[-1],
|
||||
{
|
||||
"organisation_id": 1,
|
||||
"name_de": "andere Broker",
|
||||
"name_fr": "autres Broker",
|
||||
"name_it": "altre Broker",
|
||||
"organisation_id": 28,
|
||||
"name_de": "Zürich",
|
||||
"name_fr": "Zurich",
|
||||
"name_it": "Zurigo",
|
||||
},
|
||||
)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue