Server IP : 85.214.239.14 / Your IP : 3.17.181.181 Web Server : Apache/2.4.62 (Debian) System : Linux h2886529.stratoserver.net 4.9.0 #1 SMP Tue Jan 9 19:45:01 MSK 2024 x86_64 User : www-data ( 33) PHP Version : 7.4.18 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : OFF | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : OFF Directory : /srv/modoboa/env/lib64/python3.5/site-packages/phonenumber_field/ |
Upload File : |
from babel import Locale from django.conf import settings from django.forms import Select, TextInput from django.forms.widgets import MultiWidget from django.utils import translation from phonenumbers import PhoneNumberFormat from phonenumbers.data import _COUNTRY_CODE_TO_REGION_CODE from phonenumbers.phonenumberutil import ( national_significant_number, region_code_for_number, ) from phonenumber_field.phonenumber import PhoneNumber class PhonePrefixSelect(Select): initial = None def __init__(self, initial=None): choices = [("", "---------")] language = translation.get_language() or settings.LANGUAGE_CODE if language: locale = Locale(translation.to_locale(language)) for prefix, values in _COUNTRY_CODE_TO_REGION_CODE.items(): prefix = "+%d" % prefix if initial and initial in values: self.initial = prefix for country_code in values: country_name = locale.territories.get(country_code) if country_name: choices.append((prefix, "{} {}".format(country_name, prefix))) super().__init__(choices=sorted(choices, key=lambda item: item[1])) def render(self, name, value, *args, **kwargs): return super().render(name, value or self.initial, *args, **kwargs) class PhoneNumberPrefixWidget(MultiWidget): """ A Widget that splits phone number input into: - a country select box for phone prefix - an input for local phone number """ def __init__(self, attrs=None, initial=None): widgets = (PhonePrefixSelect(initial), TextInput()) super().__init__(widgets, attrs) def decompress(self, value): if value: if type(value) == PhoneNumber: if value.country_code and value.national_number: return [ "+%d" % value.country_code, national_significant_number(value), ] else: return value.split(".") return [None, ""] def value_from_datadict(self, data, files, name): values = super().value_from_datadict(data, files, name) if all(values): return "%s.%s" % tuple(values) return "" class PhoneNumberInternationalFallbackWidget(TextInput): """ A Widget that allows a phone number in a national format, but if given an international number will fall back to international format """ def __init__(self, region=None, attrs=None): if region is None: region = getattr(settings, "PHONENUMBER_DEFAULT_REGION", None) self.region = region super().__init__(attrs) def format_value(self, value): if isinstance(value, PhoneNumber): number_region = region_code_for_number(value) if self.region != number_region: formatter = PhoneNumberFormat.INTERNATIONAL else: formatter = PhoneNumberFormat.NATIONAL return value.format_as(formatter) return super().format_value(value)