jumpserver/apps/common/const/choices.py

79 lines
1.9 KiB
Python
Raw Normal View History

import phonenumbers
import pycountry
2022-09-07 11:49:42 +00:00
from django.db import models
2023-07-24 03:52:25 +00:00
from django.utils.translation import gettext_lazy as _
from phonenumbers import PhoneMetadata
2020-07-20 02:42:22 +00:00
ADMIN = 'Admin'
USER = 'User'
AUDITOR = 'Auditor'
2022-09-07 11:49:42 +00:00
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
2022-09-07 11:49:42 +00:00
class Trigger(models.TextChoices):
manual = 'manual', _('Manual trigger')
timing = 'timing', _('Timing trigger')
2022-11-01 03:52:51 +00:00
class Status(models.TextChoices):
2022-11-02 11:07:07 +00:00
ready = 'ready', _('Ready')
2022-11-01 03:52:51 +00:00
pending = 'pending', _("Pending")
running = 'running', _("Running")
success = 'success', _("Success")
failed = 'failed', _("Failed")
error = 'error', _("Error")
canceled = 'canceled', _("Canceled")
2024-02-02 08:52:57 +00:00
2024-06-26 11:26:03 +00:00
class Language(models.TextChoices):
en = 'en', 'English'
zh_hans = 'zh-hans', '中文(简体)'
zh_hant = 'zh-hant', '中文(繁體)'
jp = 'ja', '日本語',
COUNTRY_CALLING_CODES = get_country_phone_choices()