From 0264ead88d2628050a6a486ae74d21a96562287a Mon Sep 17 00:00:00 2001 From: Jakub Komarek Date: Thu, 10 Jul 2025 11:24:45 +0200 Subject: [PATCH] Populate Country table --- .../shared/views/form/CountryField.vue | 3 ++ .../management/commands/loadconstants.py | 28 +++++++++++++++++-- requirements.in | 1 + requirements.txt | 2 ++ 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/contentcuration/contentcuration/frontend/shared/views/form/CountryField.vue b/contentcuration/contentcuration/frontend/shared/views/form/CountryField.vue index 84e3dcd53a..8254a4f8bb 100644 --- a/contentcuration/contentcuration/frontend/shared/views/form/CountryField.vue +++ b/contentcuration/contentcuration/frontend/shared/views/form/CountryField.vue @@ -30,6 +30,9 @@ import DropdownWrapper from 'shared/views/form/DropdownWrapper'; + // NOTE that this list MUST stay in sync with the list of countries + // generated on the backend in contentcuration/management/commands/loadconstants.py, + // and special care should be taken when updating the i18n-iso-countries library. var countries = require('i18n-iso-countries'); countries.registerLocale(require('i18n-iso-countries/langs/en.json')); countries.registerLocale(require('i18n-iso-countries/langs/es.json')); diff --git a/contentcuration/contentcuration/management/commands/loadconstants.py b/contentcuration/contentcuration/management/commands/loadconstants.py index 3c54ac2025..33b3907d5b 100644 --- a/contentcuration/contentcuration/management/commands/loadconstants.py +++ b/contentcuration/contentcuration/management/commands/loadconstants.py @@ -1,5 +1,6 @@ import logging as logmodule +import pycountry from django.conf import settings from django.contrib.sites.models import Site from django.core.cache import cache @@ -15,12 +16,20 @@ logging = logmodule.getLogger(__name__) +# Kosovo is supported by the i18n-iso-countries library that is used to get the +# list of countries on the frontend in +# contentcuration/frontend/shared/views/form/CountryField.vue, so we add it here +# as well to ensure that the lists generated by both libraries are the same. +# NOTE that it MUST be ensured that the lists stay in sync, and special care +# should be taken when these libraries are updated. +pycountry.countries.add_entry(alpha_2="XK", alpha_3="XXK", name="Kosovo", numeric="926") + class ConstantGenerator(object): id_field = "id" def generate_list(self): - # Get constants from subclass' default_list (from le-utils pkg) + # Get constants from subclass' default_list (usually from le-utils pkg) return [ { "model": self.model, @@ -117,6 +126,20 @@ def get_dict(self, constant): } +class CountryGenerator(ConstantGenerator): + # This does NOT use the le-utils constants module, but instead uses + # the pycountry library to get the country list directly. + id_field = "code" + default_list = pycountry.countries + model = models.Country + + def get_dict(self, constant): + return { + "code": constant.alpha_2, + "name": constant.name, + } + + SITES = [ { "model": Site, @@ -161,8 +184,9 @@ def get_dict(self, constant): KINDS = KindGenerator().generate_list() PRESETS = PresetGenerator().generate_list() LANGUAGES = LanguageGenerator().generate_list() +COUNTRIES = CountryGenerator().generate_list() -CONSTANTS = [SITES, LICENSES, KINDS, FILE_FORMATS, PRESETS, LANGUAGES] +CONSTANTS = [SITES, LICENSES, KINDS, FILE_FORMATS, PRESETS, LANGUAGES, COUNTRIES] class EarlyExit(BaseException): diff --git a/requirements.in b/requirements.in index cffdacedad..daf71b4140 100644 --- a/requirements.in +++ b/requirements.in @@ -33,3 +33,4 @@ python-dateutil>=2.8.1 jsonschema>=3.2.0 django-celery-results packaging>=21.0 +pycountry diff --git a/requirements.txt b/requirements.txt index 3ff533e5ba..761f1f8da5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -188,6 +188,8 @@ pyasn1==0.4.8 # rsa pyasn1-modules==0.2.8 # via google-auth +pycountry==24.6.1 + # via -r requirements.in pyparsing==2.4.7 # via httplib2 python-dateutil==2.9.0.post0