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.""" """Country model."""
STR_FIELD_NAME = 'name' STR_FIELD_NAME = 'name'
CALLING_NUMBER_MASK = '+%s'
TWELVE_HOURS_FORMAT_COUNTRIES = [ TWELVE_HOURS_FORMAT_COUNTRIES = [
'ca', # Canada 'ca', # Canada
@ -74,13 +75,24 @@ class Country(TranslatedFieldsMixin,
return 'hh:mmA' return 'hh:mmA'
@property @property
def display_calling_code(self): def display_calling_code(self) -> str:
"""Return formatted calling code.""" """Return formatted calling code."""
if self.code and self.calling_code: if self.code and self.calling_code:
# hardcoded calling numbers for Antilles Guyane West Indies islands. # hardcoded calling numbers for Antilles Guyane West Indies islands.
if self.code.lower() == 'aa': if self.code.lower() == 'aa':
return f'{[f"+{i}" for i in set(settings.CALLING_CODES_ANTILLES_GUYANE_WEST_INDIES)]}' return f"""{[self.CALLING_NUMBER_MASK % i
return f'+{self.calling_code}' 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): class RegionQuerySet(models.QuerySet):

View File

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

View File

@ -13,6 +13,7 @@ class CountrySerializer(serializers.ModelSerializer):
name_translated = TranslatedField() name_translated = TranslatedField()
display_calling_code = serializers.CharField(allow_null=True, read_only=True) display_calling_code = serializers.CharField(allow_null=True, read_only=True)
default_calling_code = serializers.CharField(allow_null=True, read_only=True)
class Meta: class Meta:
model = models.Country model = models.Country
@ -22,6 +23,7 @@ class CountrySerializer(serializers.ModelSerializer):
'svg_image', 'svg_image',
'name_translated', 'name_translated',
'display_calling_code', 'display_calling_code',
'default_calling_code',
] ]

View File

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