Source code for intranet.apps.preferences.fields

from django import forms
from django.core import validators
from django.db import models
from django.utils.encoding import force_text

[docs]class PhoneField(models.Field): """Model field for a phone number""" def __init__(self, *args, **kwargs): kwargs["max_length"] = 17 super().__init__(*args, **kwargs)
[docs] def deconstruct(self): name, path, args, kwargs = super().deconstruct() del kwargs["max_length"] return name, path, args, kwargs
[docs] def get_internal_type(self): return "CharField"
[docs] def formfield(self, **kwargs): # pylint: disable=arguments-differ # This is a fairly standard way to set up some defaults # while letting the caller override them. defaults = {"form_class": PhoneFormField} defaults.update(kwargs) return super().formfield(**defaults)
[docs]class PhoneFormField(forms.Field): widget = forms.TextInput default_error_messages = {"incomplete": "Please enter a phone number.", "invalid": "Please enter a valid phone number."} def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.validators.append(validators.RegexValidator(r"^[\dA-Z]{3}-?[\dA-Z]{3}-?[\dA-Z]{4}$", "Please enter a valid phone number."))
[docs] def to_python(self, value): """Returns a Unicode object.""" if value in self.empty_values: return "" value = force_text(value).strip() return value
[docs] @staticmethod def prepare_value(value): return "" if value == "None" else value
[docs] @staticmethod def widget_attrs(_): # Max phone number is 15, and US numbers can start with +1, so max length is 17 return {"maxlength": "17"}