Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice clarification 😅

return [
{
"model": self.model,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand Down
1 change: 1 addition & 0 deletions requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ python-dateutil>=2.8.1
jsonschema>=3.2.0
django-celery-results
packaging>=21.0
pycountry
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down