From 93557fa03ca8da7fa7c105803a9021017320a37d Mon Sep 17 00:00:00 2001 From: Tore Brede <Tore.Brede@uib.no> Date: Wed, 16 Feb 2022 12:57:54 +0100 Subject: [PATCH] GREG-214: Adding nno and eng acronyms --- frontend/src/hooks/useOus/index.tsx | 2 ++ .../sponsor/register/stepPersonForm.tsx | 19 ++++++++++++++++--- greg/api/serializers/organizational_unit.py | 10 +++++++++- greg/models.py | 18 ++++++++++++++++++ 4 files changed, 45 insertions(+), 4 deletions(-) diff --git a/frontend/src/hooks/useOus/index.tsx b/frontend/src/hooks/useOus/index.tsx index ba1a2119..a2f39ecd 100644 --- a/frontend/src/hooks/useOus/index.tsx +++ b/frontend/src/hooks/useOus/index.tsx @@ -7,6 +7,8 @@ type OuData = { en: string orgreg_id?: string acronym_nob?: string + acronym_nno?: string + acronym_eng?: string } function useOus(): OuData[] | undefined { diff --git a/frontend/src/routes/sponsor/register/stepPersonForm.tsx b/frontend/src/routes/sponsor/register/stepPersonForm.tsx index c018bf8a..c0b75136 100644 --- a/frontend/src/routes/sponsor/register/stepPersonForm.tsx +++ b/frontend/src/routes/sponsor/register/stepPersonForm.tsx @@ -18,7 +18,7 @@ import React, { } from 'react' import { addDays } from 'date-fns/fp' import { useTranslation } from 'react-i18next' -import useOus, { enSort, nbSort } from 'hooks/useOus' +import useOus, { enSort, nbSort, OuData } from 'hooks/useOus' import useRoleTypes, { RoleTypeData } from 'hooks/useRoleTypes' import { getOuName, isValidEmail } from 'utils' import { FeatureContext } from 'contexts' @@ -143,6 +143,20 @@ const StepPersonForm = forwardRef( return true } + function getAcronym(ouData: OuData) { + switch (i18n.language) { + case 'en': + return ouData.acronym_eng ?? ouData.acronym_nob + + case 'nn': + return ouData.acronym_nno ?? ouData.acronym_nob + + default: + // There should always be a Norwegian bokmaal acronym set + return ouData.acronym_nob + } + } + return ( <> <Typography @@ -243,8 +257,7 @@ const StepPersonForm = forwardRef( .sort(i18n.language === 'en' ? enSort : nbSort) .map((ou) => ( <MenuItem key={ou.id.toString()} value={ou.id}> - {getOuName(ou)} ({ou.acronym_nob ?? ''}) ( - {ou.orgreg_id ?? ''}) + {getOuName(ou)} ({getAcronym(ou)}) ({ou.orgreg_id ?? ''}) </MenuItem> ))} </TextField> diff --git a/greg/api/serializers/organizational_unit.py b/greg/api/serializers/organizational_unit.py index c9339ccf..e5de710e 100644 --- a/greg/api/serializers/organizational_unit.py +++ b/greg/api/serializers/organizational_unit.py @@ -29,4 +29,12 @@ class SponsorOrgUnitsSerializer(ModelSerializer): class Meta: model = OrganizationalUnit - fields = ["id", "nb", "en", "orgreg_id", "acronym_nob"] + fields = [ + "id", + "nb", + "en", + "orgreg_id", + "acronym_nob", + "acronym_nno", + "acronym_eng", + ] diff --git a/greg/models.py b/greg/models.py index d1fa4b78..a13668b9 100644 --- a/greg/models.py +++ b/greg/models.py @@ -491,6 +491,24 @@ class OrganizationalUnit(BaseModel): .first() ) + @property + def acronym_nno(self) -> Optional[str]: + """Gets the Norwegian nynorsk acronym if it exists""" + return ( + self.identifiers.filter(name="acronym_nno") + .values_list("value", flat=True) + .first() + ) + + @property + def acronym_eng(self) -> Optional[str]: + """Gets the English acronym if it exists""" + return ( + self.identifiers.filter(name="acronym_eng") + .values_list("value", flat=True) + .first() + ) + def __repr__(self) -> str: return "{}(id={!r}, name_en={!r}, parent={!r})".format( self.__class__.__name__, self.pk, self.name_en, self.parent -- GitLab