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