added command to set the calling code for countries, modified setup_clean_db transfer, modified serializers
This commit is contained in:
parent
ac606d14f8
commit
ced3abe3cf
|
|
@ -1206,6 +1206,16 @@ class ContactPhone(models.Model):
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f'{self.phone.as_e164}'
|
return f'{self.phone.as_e164}'
|
||||||
|
|
||||||
|
@property
|
||||||
|
def country_calling_code(self):
|
||||||
|
"""Return phone code from PhonеNumberField."""
|
||||||
|
return f'+{self.phone.country_code}' if self.phone and hasattr(self, 'phone') else None
|
||||||
|
|
||||||
|
@property
|
||||||
|
def national_calling_number(self):
|
||||||
|
"""Return phone national number from from PhonеNumberField."""
|
||||||
|
return self.phone.national_number if self.phone and hasattr(self, 'phone') else None
|
||||||
|
|
||||||
|
|
||||||
class ContactEmail(models.Model):
|
class ContactEmail(models.Model):
|
||||||
"""Contact email model."""
|
"""Contact email model."""
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
from functools import lru_cache
|
from functools import lru_cache
|
||||||
from pprint import pprint
|
|
||||||
|
|
||||||
from django.db.models import F
|
from django.db.models import F
|
||||||
from django.shortcuts import get_object_or_404
|
from django.shortcuts import get_object_or_404
|
||||||
|
|
@ -11,7 +10,6 @@ from establishment import models, serializers as model_serializers
|
||||||
from establishment.models import ContactEmail, ContactPhone, EstablishmentEmployee
|
from establishment.models import ContactEmail, ContactPhone, EstablishmentEmployee
|
||||||
from establishment.serializers import MenuDishSerializer
|
from establishment.serializers import MenuDishSerializer
|
||||||
from gallery.models import Image
|
from gallery.models import Image
|
||||||
from location.models import Address
|
|
||||||
from location.serializers import AddressDetailSerializer, TranslatedField
|
from location.serializers import AddressDetailSerializer, TranslatedField
|
||||||
from main.models import Currency
|
from main.models import Currency
|
||||||
from main.serializers import AwardSerializer
|
from main.serializers import AwardSerializer
|
||||||
|
|
@ -242,13 +240,17 @@ class PlatesSerializers(model_serializers.PlateSerializer):
|
||||||
|
|
||||||
class ContactPhoneBackSerializers(model_serializers.PlateSerializer):
|
class ContactPhoneBackSerializers(model_serializers.PlateSerializer):
|
||||||
"""ContactPhone serializers."""
|
"""ContactPhone serializers."""
|
||||||
|
country_calling_code = serializers.CharField(read_only=True, allow_null=True)
|
||||||
|
national_calling_number = serializers.CharField(read_only=True, allow_null=True)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = models.ContactPhone
|
model = models.ContactPhone
|
||||||
fields = [
|
fields = [
|
||||||
'id',
|
'id',
|
||||||
'establishment',
|
'establishment',
|
||||||
'phone'
|
'phone',
|
||||||
|
'country_calling_code',
|
||||||
|
'national_calling_number',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
24
apps/location/management/commands/adding_calling_code.py
Normal file
24
apps/location/management/commands/adding_calling_code.py
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
from django.conf import settings
|
||||||
|
from django.core.management.base import BaseCommand
|
||||||
|
from tqdm import tqdm
|
||||||
|
|
||||||
|
from location.models import Country
|
||||||
|
|
||||||
|
|
||||||
|
class Command(BaseCommand):
|
||||||
|
help = """Added calling code to existed countries."""
|
||||||
|
|
||||||
|
def handle(self, *args, **kwarg):
|
||||||
|
country_qs = Country.objects.filter(
|
||||||
|
code__in=[
|
||||||
|
country_code.lower() for country_code in settings.COUNTRY_CALLING_CODES.keys()
|
||||||
|
])
|
||||||
|
|
||||||
|
to_update = []
|
||||||
|
for country in tqdm(country_qs, desc='Iterate over countries'):
|
||||||
|
if str(country.calling_code) != str(settings.COUNTRY_CALLING_CODES[country.code.lower()]):
|
||||||
|
country.calling_code = settings.COUNTRY_CALLING_CODES[country.code.lower()]
|
||||||
|
to_update.append(country)
|
||||||
|
|
||||||
|
Country.objects.bulk_update(to_update, ['calling_code', ])
|
||||||
|
self.stdout.write(self.style.WARNING(f'Updated {len(to_update)} countries'))
|
||||||
18
apps/location/migrations/0038_country_calling_code.py
Normal file
18
apps/location/migrations/0038_country_calling_code.py
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
# Generated by Django 2.2.7 on 2020-01-30 14:07
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('location', '0037_address_district_name'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='country',
|
||||||
|
name='calling_code',
|
||||||
|
field=models.CharField(blank=True, default=None, help_text='i.e. "1-809"', max_length=5, null=True, verbose_name='calling code'),
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
@ -45,7 +45,10 @@ class Country(TranslatedFieldsMixin,
|
||||||
languages = models.ManyToManyField(Language, verbose_name=_('Languages'))
|
languages = models.ManyToManyField(Language, verbose_name=_('Languages'))
|
||||||
is_active = models.BooleanField(_('is active'), default=True)
|
is_active = models.BooleanField(_('is active'), default=True)
|
||||||
old_id = models.IntegerField(null=True, blank=True, default=None)
|
old_id = models.IntegerField(null=True, blank=True, default=None)
|
||||||
|
calling_code = models.CharField(max_length=5,
|
||||||
|
blank=True, null=True, default=None,
|
||||||
|
verbose_name=_('calling code'),
|
||||||
|
help_text='i.e. "1-809"')
|
||||||
mysql_ids = ArrayField(models.IntegerField(), blank=True, null=True)
|
mysql_ids = ArrayField(models.IntegerField(), blank=True, null=True)
|
||||||
|
|
||||||
objects = CountryQuerySet.as_manager()
|
objects = CountryQuerySet.as_manager()
|
||||||
|
|
@ -71,8 +74,9 @@ class Country(TranslatedFieldsMixin,
|
||||||
return 'hh:mmA'
|
return 'hh:mmA'
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def country_id(self):
|
def display_calling_code(self):
|
||||||
return self.id
|
"""Return formatted calling code."""
|
||||||
|
return f'+{self.calling_code}' if self.calling_code else None
|
||||||
|
|
||||||
|
|
||||||
class RegionQuerySet(models.QuerySet):
|
class RegionQuerySet(models.QuerySet):
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
|
from rest_framework import serializers
|
||||||
|
|
||||||
from location import models
|
from location import models
|
||||||
from location.serializers import common
|
from location.serializers import common
|
||||||
|
|
||||||
from utils.serializers import TranslatedField
|
from utils.serializers import TranslatedField
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -12,6 +13,7 @@ class CountryBackSerializer(common.CountrySerializer):
|
||||||
"""Country back-office serializer."""
|
"""Country back-office serializer."""
|
||||||
|
|
||||||
name_translated = TranslatedField()
|
name_translated = TranslatedField()
|
||||||
|
display_calling_code = serializers.CharField(allow_null=True, read_only=True)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = models.Country
|
model = models.Country
|
||||||
|
|
@ -21,5 +23,8 @@ class CountryBackSerializer(common.CountrySerializer):
|
||||||
'svg_image',
|
'svg_image',
|
||||||
'name',
|
'name',
|
||||||
'name_translated',
|
'name_translated',
|
||||||
'country_id'
|
'display_calling_code',
|
||||||
]
|
]
|
||||||
|
extra_kwargs = {
|
||||||
|
'calling_code': {'write_only': True}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -335,7 +335,12 @@ def add_correct_location_models(ruby_data):
|
||||||
for mysql_id, city_object in tqdm(ruby_data.items()):
|
for mysql_id, city_object in tqdm(ruby_data.items()):
|
||||||
country_data = city_object["country"]
|
country_data = city_object["country"]
|
||||||
try:
|
try:
|
||||||
country = Country.objects.get(code=country_data['code'], mysql_ids__isnull=False)
|
country_code = country_data['code'] # i.e. fr
|
||||||
|
country = Country.objects.get(
|
||||||
|
code=country_code,
|
||||||
|
mysql_ids__isnull=False,
|
||||||
|
calling_code=settings.COUNTRY_CALLING_CODES.get(country_code)
|
||||||
|
)
|
||||||
country.mysql_ids.append(mysql_id)
|
country.mysql_ids.append(mysql_id)
|
||||||
country.save()
|
country.save()
|
||||||
except Country.DoesNotExist:
|
except Country.DoesNotExist:
|
||||||
|
|
@ -572,6 +577,7 @@ def add_fake_country():
|
||||||
country_data = {
|
country_data = {
|
||||||
"name": '{"en-GB": "Antilles Guyane West Indies"}',
|
"name": '{"en-GB": "Antilles Guyane West Indies"}',
|
||||||
"code": "aa",
|
"code": "aa",
|
||||||
|
"calling_code": "590",
|
||||||
"svg_image": "svg/country/11-02-2019/658714.svg",
|
"svg_image": "svg/country/11-02-2019/658714.svg",
|
||||||
"mysql_ids": []
|
"mysql_ids": []
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -538,3 +538,25 @@ ELASTICSEARCH_DSL = {}
|
||||||
ELASTICSEARCH_INDEX_NAMES = {}
|
ELASTICSEARCH_INDEX_NAMES = {}
|
||||||
|
|
||||||
THUMBNAIL_FORCE_OVERWRITE = True
|
THUMBNAIL_FORCE_OVERWRITE = True
|
||||||
|
|
||||||
|
COUNTRY_CALLING_CODES = {
|
||||||
|
"at": 43, # Austria
|
||||||
|
"au": 61, # Australia
|
||||||
|
"be": 32, # Belgium
|
||||||
|
"de": 49, # Germany
|
||||||
|
"gr": 30, # Greece
|
||||||
|
"ge": 995, # Georgia
|
||||||
|
"il": 972, # Israel
|
||||||
|
"it": 39, # Italy
|
||||||
|
"ca": 1, # Canada
|
||||||
|
"lu": 352, # Luxembourg
|
||||||
|
"ma": 212, # Morocco
|
||||||
|
"nl": 31, # Netherlands
|
||||||
|
"ru": 7, # Russia
|
||||||
|
"ro": 40, # Romania
|
||||||
|
"si": 386, # Slovenia
|
||||||
|
"fr": 33, # France
|
||||||
|
"hr": 385, # Croatia
|
||||||
|
"jp": 81, # Japan
|
||||||
|
"aa": 590 # Guyane West Indies
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user