added DEFAULT_CALLING_CODE_ANTILLES_GUYANE_WEST_INDIES param, refactored serializers

This commit is contained in:
Anatoly 2020-01-31 14:52:22 +03:00
parent e008e7f2a1
commit b518faaf8b
4 changed files with 19 additions and 3 deletions

View File

@ -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):

View File

@ -21,6 +21,7 @@ class CountryBackSerializer(common.CountrySerializer):
'name',
'name_translated',
'display_calling_code',
'default_calling_code',
]
extra_kwargs = {
'calling_code': {'write_only': True}

View File

@ -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',
]

View File

@ -562,3 +562,4 @@ COUNTRY_CALLING_CODES = {
}
CALLING_CODES_ANTILLES_GUYANE_WEST_INDIES = [590, 594, 1758]
DEFAULT_CALLING_CODE_ANTILLES_GUYANE_WEST_INDIES = 590