Add country ordering

This commit is contained in:
Christian Cueni 2024-02-15 12:58:41 +01:00
parent 4ab5df0753
commit 3e270e97ff
4 changed files with 68 additions and 3 deletions

View File

@ -3,7 +3,7 @@ from rest_framework import status
from rest_framework.test import APITestCase
from vbv_lernwelt.core.model_utils import add_countries, add_organisations
from vbv_lernwelt.core.models import Organisation, User
from vbv_lernwelt.core.models import Country, Organisation, User
class EntitiesViewTest(APITestCase):
@ -15,7 +15,7 @@ class EntitiesViewTest(APITestCase):
add_organisations()
add_countries()
def test_list_entities(self) -> None:
def test_list_organisation_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()
@ -51,3 +51,49 @@ class EntitiesViewTest(APITestCase):
"name": "Afghanistan",
},
)
def test_list_country_entities_ordered_by_country_id(self) -> None:
# GIVEN
url = reverse("list_entities")
first_country = Country.objects.get(country_id=1)
# WHEN
response = self.client.get(url)
# THEN
self.assertEqual(response.status_code, status.HTTP_200_OK)
countries = response.data["countries"]
self.assertEqual(
countries[0],
{
"id": first_country.country_id,
"name": first_country.name_de,
},
)
def test_list_country_entities_ordered_by_order_id(self) -> None:
# GIVEN
url = reverse("list_entities")
switzerland = Country.objects.get(name_de="Schweiz")
switzerland.order_id = 1
switzerland.save()
# WHEN
response = self.client.get(url)
# THEN
self.assertEqual(response.status_code, status.HTTP_200_OK)
countries = response.data["countries"]
self.assertEqual(
countries[0],
{
"id": switzerland.country_id,
"name": switzerland.name_de,
},
)

View File

@ -122,6 +122,7 @@ class OrganisationAdmin(admin.ModelAdmin):
@admin.register(Country)
class CountryAdmin(admin.ModelAdmin):
list_display = (
"order_id",
"country_id",
"name_de",
"name_fr",

View File

@ -0,0 +1,17 @@
# Generated by Django 3.2.20 on 2024-02-15 10:38
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("core", "0006_auto_20240125_0915"),
]
operations = [
migrations.AddField(
model_name="country",
name="order_id",
field=models.PositiveIntegerField(default=20),
),
]

View File

@ -26,6 +26,7 @@ class Country(models.Model):
name_de = models.CharField(max_length=255)
name_fr = models.CharField(max_length=255)
name_it = models.CharField(max_length=255)
order_id = models.PositiveIntegerField(default=10)
def __str__(self):
return f"{self.name_de} ({self.country_id})"
@ -33,7 +34,7 @@ class Country(models.Model):
class Meta:
verbose_name = "Country"
verbose_name_plural = "Countries"
ordering = ["country_id"]
ordering = ["order_id", "country_id"]
class User(AbstractUser):