perf: country code api (#13672)

* perf: remove notification migrations

* perf: country code api

---------

Co-authored-by: ibuler <ibuler@qq.com>
This commit is contained in:
fit2bot
2024-07-09 19:23:41 +08:00
committed by GitHub
parent 5c1d0238e1
commit e36d51cc0b
7 changed files with 76 additions and 24 deletions

View File

@@ -1,11 +1,58 @@
import phonenumbers
import pycountry
from django.db import models
from django.utils.translation import gettext_lazy as _
from phonenumbers import PhoneMetadata
ADMIN = 'Admin'
USER = 'User'
AUDITOR = 'Auditor'
def get_country_phone_codes():
phone_codes = []
for region_code in phonenumbers.SUPPORTED_REGIONS:
phone_metadata = PhoneMetadata.metadata_for_region(region_code)
if phone_metadata:
phone_codes.append((region_code, phone_metadata.country_code))
return phone_codes
def get_country(region_code):
country = pycountry.countries.get(alpha_2=region_code)
if country:
return country
else:
return None
def get_country_phone_choices():
codes = get_country_phone_codes()
choices = []
for code, phone in codes:
country = get_country(code)
if not country:
continue
country_name = country.name
flag = country.flag
if country.name == 'China':
country_name = _('China')
if code == 'TW':
country_name = 'Taiwan'
flag = get_country('CN').flag
choices.append({
'name': country_name,
'phone_code': f'+{phone}',
'flag': flag,
'code': code,
})
choices.sort(key=lambda x: x['name'])
return choices
class Trigger(models.TextChoices):
manual = 'manual', _('Manual trigger')
timing = 'timing', _('Timing trigger')
@@ -28,17 +75,4 @@ class Language(models.TextChoices):
jp = 'ja', '日本語',
COUNTRY_CALLING_CODES = [
{'name': 'China(中国)', 'value': '+86'},
{'name': 'HongKong(中国香港)', 'value': '+852'},
{'name': 'Macao(中国澳门)', 'value': '+853'},
{'name': 'Taiwan(中国台湾)', 'value': '+886'},
{'name': 'America(America)', 'value': '+1'},
{'name': 'Russia(Россия)', 'value': '+7'},
{'name': 'France(français)', 'value': '+33'},
{'name': 'Britain(Britain)', 'value': '+44'},
{'name': 'Germany(Deutschland)', 'value': '+49'},
{'name': 'Japan(日本)', 'value': '+81'},
{'name': 'Korea(한국)', 'value': '+82'},
{'name': 'India(भारत)', 'value': '+91'}
]
COUNTRY_CALLING_CODES = get_country_phone_choices()