From b518faaf8b6dc56ad9e2e1383eaab03988d925ef Mon Sep 17 00:00:00 2001 From: Anatoly Date: Fri, 31 Jan 2020 14:52:22 +0300 Subject: [PATCH] added DEFAULT_CALLING_CODE_ANTILLES_GUYANE_WEST_INDIES param, refactored serializers --- apps/location/models.py | 18 +++++++++++++++--- apps/location/serializers/back.py | 1 + apps/location/serializers/common.py | 2 ++ project/settings/base.py | 1 + 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/apps/location/models.py b/apps/location/models.py index 5d5211a5..02e8c6b9 100644 --- a/apps/location/models.py +++ b/apps/location/models.py @@ -29,6 +29,7 @@ class Country(TranslatedFieldsMixin, """Country model.""" STR_FIELD_NAME = 'name' + CALLING_NUMBER_MASK = '+%s' TWELVE_HOURS_FORMAT_COUNTRIES = [ 'ca', # Canada @@ -74,13 +75,24 @@ class Country(TranslatedFieldsMixin, return 'hh:mmA' @property - def display_calling_code(self): + def display_calling_code(self) -> str: """Return formatted calling code.""" if self.code and self.calling_code: # hardcoded calling numbers for Antilles Guyane West Indies islands. if self.code.lower() == 'aa': - return f'{[f"+{i}" for i in set(settings.CALLING_CODES_ANTILLES_GUYANE_WEST_INDIES)]}' - return f'+{self.calling_code}' + return f"""{[self.CALLING_NUMBER_MASK % i + for i in set(settings.CALLING_CODES_ANTILLES_GUYANE_WEST_INDIES)]}""" + return self.CALLING_NUMBER_MASK % self.calling_code + + @property + def default_calling_code(self): + """Return default calling code based on phone number.""" + if self.code and self.calling_code: + # hardcoded default calling number for Antilles Guyane West Indies islands. + if self.code.lower() == 'aa': + return (self.CALLING_NUMBER_MASK % + settings.DEFAULT_CALLING_CODE_ANTILLES_GUYANE_WEST_INDIES) + return self.CALLING_NUMBER_MASK % self.calling_code class RegionQuerySet(models.QuerySet): diff --git a/apps/location/serializers/back.py b/apps/location/serializers/back.py index 51163d5a..dc23a727 100644 --- a/apps/location/serializers/back.py +++ b/apps/location/serializers/back.py @@ -21,6 +21,7 @@ class CountryBackSerializer(common.CountrySerializer): 'name', 'name_translated', 'display_calling_code', + 'default_calling_code', ] extra_kwargs = { 'calling_code': {'write_only': True} diff --git a/apps/location/serializers/common.py b/apps/location/serializers/common.py index 404e415e..5238e132 100644 --- a/apps/location/serializers/common.py +++ b/apps/location/serializers/common.py @@ -13,6 +13,7 @@ class CountrySerializer(serializers.ModelSerializer): name_translated = TranslatedField() display_calling_code = serializers.CharField(allow_null=True, read_only=True) + default_calling_code = serializers.CharField(allow_null=True, read_only=True) class Meta: model = models.Country @@ -22,6 +23,7 @@ class CountrySerializer(serializers.ModelSerializer): 'svg_image', 'name_translated', 'display_calling_code', + 'default_calling_code', ] diff --git a/project/settings/base.py b/project/settings/base.py index 8ae06fbc..b6888806 100644 --- a/project/settings/base.py +++ b/project/settings/base.py @@ -562,3 +562,4 @@ COUNTRY_CALLING_CODES = { } CALLING_CODES_ANTILLES_GUYANE_WEST_INDIES = [590, 594, 1758] +DEFAULT_CALLING_CODE_ANTILLES_GUYANE_WEST_INDIES = 590