added command to set the calling code for countries, modified setup_clean_db transfer, modified serializers

This commit is contained in:
Anatoly 2020-01-30 17:57:43 +03:00
parent ac606d14f8
commit ced3abe3cf
8 changed files with 100 additions and 9 deletions

View File

@ -1206,6 +1206,16 @@ class ContactPhone(models.Model):
def __str__(self):
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):
"""Contact email model."""

View File

@ -1,5 +1,4 @@
from functools import lru_cache
from pprint import pprint
from django.db.models import F
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.serializers import MenuDishSerializer
from gallery.models import Image
from location.models import Address
from location.serializers import AddressDetailSerializer, TranslatedField
from main.models import Currency
from main.serializers import AwardSerializer
@ -242,13 +240,17 @@ class PlatesSerializers(model_serializers.PlateSerializer):
class ContactPhoneBackSerializers(model_serializers.PlateSerializer):
"""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:
model = models.ContactPhone
fields = [
'id',
'establishment',
'phone'
'phone',
'country_calling_code',
'national_calling_number',
]

View 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'))

View 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'),
),
]

View File

@ -45,7 +45,10 @@ class Country(TranslatedFieldsMixin,
languages = models.ManyToManyField(Language, verbose_name=_('Languages'))
is_active = models.BooleanField(_('is active'), default=True)
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)
objects = CountryQuerySet.as_manager()
@ -71,8 +74,9 @@ class Country(TranslatedFieldsMixin,
return 'hh:mmA'
@property
def country_id(self):
return self.id
def display_calling_code(self):
"""Return formatted calling code."""
return f'+{self.calling_code}' if self.calling_code else None
class RegionQuerySet(models.QuerySet):

View File

@ -1,6 +1,7 @@
from rest_framework import serializers
from location import models
from location.serializers import common
from utils.serializers import TranslatedField
@ -12,6 +13,7 @@ class CountryBackSerializer(common.CountrySerializer):
"""Country back-office serializer."""
name_translated = TranslatedField()
display_calling_code = serializers.CharField(allow_null=True, read_only=True)
class Meta:
model = models.Country
@ -21,5 +23,8 @@ class CountryBackSerializer(common.CountrySerializer):
'svg_image',
'name',
'name_translated',
'country_id'
'display_calling_code',
]
extra_kwargs = {
'calling_code': {'write_only': True}
}

View File

@ -335,7 +335,12 @@ def add_correct_location_models(ruby_data):
for mysql_id, city_object in tqdm(ruby_data.items()):
country_data = city_object["country"]
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.save()
except Country.DoesNotExist:
@ -572,6 +577,7 @@ def add_fake_country():
country_data = {
"name": '{"en-GB": "Antilles Guyane West Indies"}',
"code": "aa",
"calling_code": "590",
"svg_image": "svg/country/11-02-2019/658714.svg",
"mysql_ids": []
}

View File

@ -538,3 +538,25 @@ ELASTICSEARCH_DSL = {}
ELASTICSEARCH_INDEX_NAMES = {}
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
}