Python django.core.validators 模块,RegexValidator() 实例源码

我们从Python开源项目中,提取了以下47个代码示例,用于说明如何使用django.core.validators.RegexValidator()

项目:moore    作者:UTNkar    | 项目源码 | 文件源码
def to_python(self, value):
        if value in self.empty_values:
            return None, ''
        value = force_text(value)
        validators.RegexValidator(
            regex=r'^\d{8}\-?(T|\d)\d{3}$',
            message=_('Use the format YYYYMMDD-XXXX for your person number.')
        )(value)
        try:
            date = datetime.strptime(value[:8], '%Y%m%d').date()
        except ValueError:
            date = value[:4] + '-' + value[4:6] \
                   + '-' + value[6:8]
            raise ValidationError(
                _('%(date)s is an invalid date'),
                params={'date': date}
            )
        number = value[-4:]
        return date, number
项目:fakester    作者:pawelad    | 项目源码 | 文件源码
def test_model_local_path_field_validator(self):
        """Test model 'local_path' field `RegexValidator`"""
        instance = self.model()
        field = instance._meta.get_field('local_path')
        validator = field.validators[0]

        assert isinstance(validator, RegexValidator)
        assert validator.regex.pattern == '[a-zA-Z0-9/._-]+'
        assert (
            validator.message ==
            "Allowed characters: a-z, A-Z, 0-9, slash (/), dot (.), "
            "underscore (_) and hyphen (-)."
        )
项目:CodingDojo    作者:ComputerSocietyUNB    | 项目源码 | 文件源码
def _set_regex(self, regex):
        if isinstance(regex, six.string_types):
            regex = re.compile(regex, re.UNICODE)
        self._regex = regex
        if hasattr(self, '_regex_validator') and self._regex_validator in self.validators:
            self.validators.remove(self._regex_validator)
        self._regex_validator = validators.RegexValidator(regex=regex)
        self.validators.append(self._regex_validator)
项目:NarshaTech    作者:KimJangHyeon    | 项目源码 | 文件源码
def _set_regex(self, regex):
        if isinstance(regex, six.string_types):
            regex = re.compile(regex, re.UNICODE)
        self._regex = regex
        if hasattr(self, '_regex_validator') and self._regex_validator in self.validators:
            self.validators.remove(self._regex_validator)
        self._regex_validator = validators.RegexValidator(regex=regex)
        self.validators.append(self._regex_validator)
项目:Scrum    作者:prakharchoudhary    | 项目源码 | 文件源码
def _set_regex(self, regex):
        if isinstance(regex, six.string_types):
            regex = re.compile(regex, re.UNICODE)
        self._regex = regex
        if hasattr(self, '_regex_validator') and self._regex_validator in self.validators:
            self.validators.remove(self._regex_validator)
        self._regex_validator = validators.RegexValidator(regex=regex)
        self.validators.append(self._regex_validator)
项目:django    作者:alexsukhrin    | 项目源码 | 文件源码
def _set_regex(self, regex):
        if isinstance(regex, six.string_types):
            regex = re.compile(regex, re.UNICODE)
        self._regex = regex
        if hasattr(self, '_regex_validator') and self._regex_validator in self.validators:
            self.validators.remove(self._regex_validator)
        self._regex_validator = validators.RegexValidator(regex=regex)
        self.validators.append(self._regex_validator)
项目:xsd_to_django_model    作者:tuffnatty    | 项目源码 | 文件源码
def __init__(self, *args, **kwargs):
        if "max_length" not in kwargs: kwargs["max_length"] = 13
        if "validators" not in kwargs: kwargs["validators"] = [validators.MinLengthValidator(12), validators.RegexValidator(r"\d{2}:\d{2}:\d{6,7}")]
        super(CadastralBlockTypeField, self).__init__(*args, **kwargs)
项目:xsd_to_django_model    作者:tuffnatty    | 项目源码 | 文件源码
def __init__(self, *args, **kwargs):
        if "max_length" not in kwargs: kwargs["max_length"] = 40
        if "validators" not in kwargs: kwargs["validators"] = [validators.RegexValidator(r"\d{2}:\d{2}:\d{6,7}:\d{1,}")]
        super(CadastralNumberTypeField, self).__init__(*args, **kwargs)
项目:xsd_to_django_model    作者:tuffnatty    | 项目源码 | 文件源码
def __init__(self, *args, **kwargs):
        if "max_length" not in kwargs: kwargs["max_length"] = 100
        if "validators" not in kwargs: kwargs["validators"] = [validators.RegexValidator(r"[0-9a-zA-Z_.\-]{2,50}[@]{1}[0-9a-zA-Z_./-]{2,50}[.]{1}[a-zA-Z]{2,5}")]
        super(EmailAddressTypeField, self).__init__(*args, **kwargs)
项目:xsd_to_django_model    作者:tuffnatty    | 项目源码 | 文件源码
def __init__(self, *args, **kwargs):
        if "max_length" not in kwargs: kwargs["max_length"] = 10
        if "validators" not in kwargs: kwargs["validators"] = [validators.RegexValidator(r"\d{10}")]
        super(LegalPersonINNTypeField, self).__init__(*args, **kwargs)
项目:xsd_to_django_model    作者:tuffnatty    | 项目源码 | 文件源码
def __init__(self, *args, **kwargs):
        if "max_length" not in kwargs: kwargs["max_length"] = 13
        if "validators" not in kwargs: kwargs["validators"] = [validators.RegexValidator(r"[125]{1}\d{12}")]
        super(OGRNCompanyTypeField, self).__init__(*args, **kwargs)
项目:xsd_to_django_model    作者:tuffnatty    | 项目源码 | 文件源码
def __init__(self, *args, **kwargs):
        if "max_length" not in kwargs: kwargs["max_length"] = 6
        if "validators" not in kwargs: kwargs["validators"] = [validators.RegexValidator(r"\d{6}")]
        super(PostalCodeRFTypeField, self).__init__(*args, **kwargs)
项目:xsd_to_django_model    作者:tuffnatty    | 项目源码 | 文件源码
def __init__(self, *args, **kwargs):
        if "max_length" not in kwargs: kwargs["max_length"] = 11
        if "validators" not in kwargs: kwargs["validators"] = [validators.RegexValidator(r"\d{11}")]
        super(SNILSTypeField, self).__init__(*args, **kwargs)
项目:xsd_to_django_model    作者:tuffnatty    | 项目源码 | 文件源码
def __init__(self, *args, **kwargs):
        if "validators" not in kwargs: kwargs["validators"] = [validators.RegexValidator(r"(\s*[^\s]\s*)+")]
        super(nonEmptyStringField, self).__init__(*args, **kwargs)
项目:xsd_to_django_model    作者:tuffnatty    | 项目源码 | 文件源码
def __init__(self, *args, **kwargs):
        if "max_length" not in kwargs: kwargs["max_length"] = 100
        if "validators" not in kwargs: kwargs["validators"] = [validators.RegexValidator(r"[?-??-???\-0-9][?-??-???\-\s'',.]*")]
        super(rus_100Field, self).__init__(*args, **kwargs)
项目:xsd_to_django_model    作者:tuffnatty    | 项目源码 | 文件源码
def __init__(self, *args, **kwargs):
        if "max_length" not in kwargs: kwargs["max_length"] = 50
        if "validators" not in kwargs: kwargs["validators"] = [validators.RegexValidator(r"[0-9]{2}-[0-9]{2}-[0-9]{1,}")]
        super(sCertificate50Field, self).__init__(*args, **kwargs)
项目:xsd_to_django_model    作者:tuffnatty    | 项目源码 | 文件源码
def __init__(self, *args, **kwargs):
        if "max_length" not in kwargs: kwargs["max_length"] = 500
        if "validators" not in kwargs: kwargs["validators"] = [validators.RegexValidator(r".+\.[p|P][d|D][f|F]")]
        super(sName500PDFField, self).__init__(*args, **kwargs)
项目:xsd_to_django_model    作者:tuffnatty    | 项目源码 | 文件源码
def __init__(self, *args, **kwargs):
        if "validators" not in kwargs: kwargs["validators"] = [validators.MinValueValidator(2), validators.MaxValueValidator(18), validators.RegexValidator(r"\d{1,2}")]
        super(DressSizeTypeField, self).__init__(*args, **kwargs)
项目:xsd_to_django_model    作者:tuffnatty    | 项目源码 | 文件源码
def __init__(self, *args, **kwargs):
        if "validators" not in kwargs: kwargs["validators"] = [validators.MinValueValidator(2), validators.MaxValueValidator(6), validators.RegexValidator(r"\d{1}")]
        super(SmallDressSizeTypeField, self).__init__(*args, **kwargs)
项目:Gypsy    作者:benticarlos    | 项目源码 | 文件源码
def _set_regex(self, regex):
        if isinstance(regex, six.string_types):
            regex = re.compile(regex, re.UNICODE)
        self._regex = regex
        if hasattr(self, '_regex_validator') and self._regex_validator in self.validators:
            self.validators.remove(self._regex_validator)
        self._regex_validator = validators.RegexValidator(regex=regex)
        self.validators.append(self._regex_validator)
项目:DjangoBlog    作者:0daybug    | 项目源码 | 文件源码
def _set_regex(self, regex):
        if isinstance(regex, six.string_types):
            regex = re.compile(regex, re.UNICODE)
        self._regex = regex
        if hasattr(self, '_regex_validator') and self._regex_validator in self.validators:
            self.validators.remove(self._regex_validator)
        self._regex_validator = validators.RegexValidator(regex=regex)
        self.validators.append(self._regex_validator)
项目:wanblog    作者:wanzifa    | 项目源码 | 文件源码
def _set_regex(self, regex):
        if isinstance(regex, six.string_types):
            regex = re.compile(regex, re.UNICODE)
        self._regex = regex
        if hasattr(self, '_regex_validator') and self._regex_validator in self.validators:
            self.validators.remove(self._regex_validator)
        self._regex_validator = validators.RegexValidator(regex=regex)
        self.validators.append(self._regex_validator)
项目:tabmaster    作者:NicolasMinghetti    | 项目源码 | 文件源码
def _set_regex(self, regex):
        if isinstance(regex, six.string_types):
            regex = re.compile(regex, re.UNICODE)
        self._regex = regex
        if hasattr(self, '_regex_validator') and self._regex_validator in self.validators:
            self.validators.remove(self._regex_validator)
        self._regex_validator = validators.RegexValidator(regex=regex)
        self.validators.append(self._regex_validator)
项目:trydjango18    作者:lucifer-yqh    | 项目源码 | 文件源码
def _set_regex(self, regex):
        if isinstance(regex, six.string_types):
            regex = re.compile(regex, re.UNICODE)
        self._regex = regex
        if hasattr(self, '_regex_validator') and self._regex_validator in self.validators:
            self.validators.remove(self._regex_validator)
        self._regex_validator = validators.RegexValidator(regex=regex)
        self.validators.append(self._regex_validator)
项目:trydjango18    作者:wei0104    | 项目源码 | 文件源码
def _set_regex(self, regex):
        if isinstance(regex, six.string_types):
            regex = re.compile(regex, re.UNICODE)
        self._regex = regex
        if hasattr(self, '_regex_validator') and self._regex_validator in self.validators:
            self.validators.remove(self._regex_validator)
        self._regex_validator = validators.RegexValidator(regex=regex)
        self.validators.append(self._regex_validator)
项目:esdc-ce    作者:erigones    | 项目源码 | 文件源码
def _set_regex(self, regex):
        if isinstance(regex, six.string_types):
            regex = re.compile(regex)
        self._regex = regex
        if hasattr(self, '_regex_validator') and self._regex_validator in self.validators:
            self.validators.remove(self._regex_validator)
        self._regex_validator = validators.RegexValidator(regex=regex)
        self.validators.append(self._regex_validator)
项目:ims    作者:ims-team    | 项目源码 | 文件源码
def _set_regex(self, regex):
        if isinstance(regex, six.string_types):
            regex = re.compile(regex, re.UNICODE)
        self._regex = regex
        if hasattr(self, '_regex_validator') and self._regex_validator in self.validators:
            self.validators.remove(self._regex_validator)
        self._regex_validator = validators.RegexValidator(regex=regex)
        self.validators.append(self._regex_validator)
项目:lifesoundtrack    作者:MTG    | 项目源码 | 文件源码
def _set_regex(self, regex):
        if isinstance(regex, six.string_types):
            regex = re.compile(regex, re.UNICODE)
        self._regex = regex
        if hasattr(self, '_regex_validator') and self._regex_validator in self.validators:
            self.validators.remove(self._regex_validator)
        self._regex_validator = validators.RegexValidator(regex=regex)
        self.validators.append(self._regex_validator)
项目:django-open-lecture    作者:DmLitov4    | 项目源码 | 文件源码
def _set_regex(self, regex):
        if isinstance(regex, six.string_types):
            regex = re.compile(regex, re.UNICODE)
        self._regex = regex
        if hasattr(self, '_regex_validator') and self._regex_validator in self.validators:
            self.validators.remove(self._regex_validator)
        self._regex_validator = validators.RegexValidator(regex=regex)
        self.validators.append(self._regex_validator)
项目:travlr    作者:gauravkulkarni96    | 项目源码 | 文件源码
def _set_regex(self, regex):
        if isinstance(regex, six.string_types):
            regex = re.compile(regex, re.UNICODE)
        self._regex = regex
        if hasattr(self, '_regex_validator') and self._regex_validator in self.validators:
            self.validators.remove(self._regex_validator)
        self._regex_validator = validators.RegexValidator(regex=regex)
        self.validators.append(self._regex_validator)
项目:logo-gen    作者:jellene4eva    | 项目源码 | 文件源码
def _set_regex(self, regex):
        if isinstance(regex, six.string_types):
            regex = re.compile(regex, re.UNICODE)
        self._regex = regex
        if hasattr(self, '_regex_validator') and self._regex_validator in self.validators:
            self.validators.remove(self._regex_validator)
        self._regex_validator = validators.RegexValidator(regex=regex)
        self.validators.append(self._regex_validator)
项目:liberator    作者:libscie    | 项目源码 | 文件源码
def _set_regex(self, regex):
        if isinstance(regex, six.string_types):
            regex = re.compile(regex, re.UNICODE)
        self._regex = regex
        if hasattr(self, '_regex_validator') and self._regex_validator in self.validators:
            self.validators.remove(self._regex_validator)
        self._regex_validator = validators.RegexValidator(regex=regex)
        self.validators.append(self._regex_validator)
项目:gmail_scanner    作者:brandonhub    | 项目源码 | 文件源码
def _set_regex(self, regex):
        if isinstance(regex, six.string_types):
            regex = re.compile(regex, re.UNICODE)
        self._regex = regex
        if hasattr(self, '_regex_validator') and self._regex_validator in self.validators:
            self.validators.remove(self._regex_validator)
        self._regex_validator = validators.RegexValidator(regex=regex)
        self.validators.append(self._regex_validator)
项目:djanoDoc    作者:JustinChavez    | 项目源码 | 文件源码
def _set_regex(self, regex):
        if isinstance(regex, six.string_types):
            regex = re.compile(regex, re.UNICODE)
        self._regex = regex
        if hasattr(self, '_regex_validator') and self._regex_validator in self.validators:
            self.validators.remove(self._regex_validator)
        self._regex_validator = validators.RegexValidator(regex=regex)
        self.validators.append(self._regex_validator)
项目:edd    作者:JBEI    | 项目源码 | 文件源码
def get_form(self, request, obj=None, **kwargs):
        """
        Override default generated admin form to add specialized help text and verification for
        Uniprot accession ID's. This is a bit indirect since type_name is inherited from
        MeasurementTypeAdmin.
        """

        # override the type_name label to indicate it should be a UniProt accession ID
        if settings.REQUIRE_UNIPROT_ACCESSION_IDS:
            labels = kwargs.get('labels')
            if not labels:
                labels = {}
                kwargs['labels'] = labels
            labels['type_name'] = _('Protein Name')
            labels['accession_id'] = _('UniProt Accession ID')

        generated_form = super(ProteinAdmin, self).get_form(request, obj, **kwargs)

        # require that newly-created ProteinIdentifiers have an accession ID matching the
        # expected pattern. existing ID's that don't conform should still be editable
        new_identifier = not obj
        if new_identifier and settings.REQUIRE_UNIPROT_ACCESSION_IDS:
            generated_form.base_fields['type_name'].validators.append(RegexValidator(
                    regex=ProteinIdentifier.accession_pattern,
                    message=_('New entries must be valid UniProt accession IDs')))
        return generated_form
项目:CSCE482-WordcloudPlus    作者:ggaytan00    | 项目源码 | 文件源码
def _set_regex(self, regex):
        if isinstance(regex, six.string_types):
            regex = re.compile(regex, re.UNICODE)
        self._regex = regex
        if hasattr(self, '_regex_validator') and self._regex_validator in self.validators:
            self.validators.remove(self._regex_validator)
        self._regex_validator = validators.RegexValidator(regex=regex)
        self.validators.append(self._regex_validator)
项目:tissuelab    作者:VirtualPlants    | 项目源码 | 文件源码
def _set_regex(self, regex):
        if isinstance(regex, six.string_types):
            regex = re.compile(regex, re.UNICODE)
        self._regex = regex
        if hasattr(self, '_regex_validator') and self._regex_validator in self.validators:
            self.validators.remove(self._regex_validator)
        self._regex_validator = validators.RegexValidator(regex=regex)
        self.validators.append(self._regex_validator)
项目:producthunt    作者:davidgengler    | 项目源码 | 文件源码
def _set_regex(self, regex):
        if isinstance(regex, six.string_types):
            regex = re.compile(regex, re.UNICODE)
        self._regex = regex
        if hasattr(self, '_regex_validator') and self._regex_validator in self.validators:
            self.validators.remove(self._regex_validator)
        self._regex_validator = validators.RegexValidator(regex=regex)
        self.validators.append(self._regex_validator)
项目:django-rtc    作者:scifiswapnil    | 项目源码 | 文件源码
def _set_regex(self, regex):
        if isinstance(regex, six.string_types):
            regex = re.compile(regex, re.UNICODE)
        self._regex = regex
        if hasattr(self, '_regex_validator') and self._regex_validator in self.validators:
            self.validators.remove(self._regex_validator)
        self._regex_validator = validators.RegexValidator(regex=regex)
        self.validators.append(self._regex_validator)
项目:geekpoint    作者:Lujinghu    | 项目源码 | 文件源码
def _set_regex(self, regex):
        if isinstance(regex, six.string_types):
            regex = re.compile(regex, re.UNICODE)
        self._regex = regex
        if hasattr(self, '_regex_validator') and self._regex_validator in self.validators:
            self.validators.remove(self._regex_validator)
        self._regex_validator = validators.RegexValidator(regex=regex)
        self.validators.append(self._regex_validator)
项目:django-next-train    作者:bitpixdigital    | 项目源码 | 文件源码
def _set_regex(self, regex):
        if isinstance(regex, six.string_types):
            regex = re.compile(regex, re.UNICODE)
        self._regex = regex
        if hasattr(self, '_regex_validator') and self._regex_validator in self.validators:
            self.validators.remove(self._regex_validator)
        self._regex_validator = validators.RegexValidator(regex=regex)
        self.validators.append(self._regex_validator)
项目:LatinSounds_AppEnviaMail    作者:G3ek-aR    | 项目源码 | 文件源码
def _set_regex(self, regex):
        if isinstance(regex, six.string_types):
            regex = re.compile(regex, re.UNICODE)
        self._regex = regex
        if hasattr(self, '_regex_validator') and self._regex_validator in self.validators:
            self.validators.remove(self._regex_validator)
        self._regex_validator = validators.RegexValidator(regex=regex)
        self.validators.append(self._regex_validator)
项目:DjangoZeroToHero    作者:RayParra    | 项目源码 | 文件源码
def _set_regex(self, regex):
        if isinstance(regex, six.string_types):
            regex = re.compile(regex, re.UNICODE)
        self._regex = regex
        if hasattr(self, '_regex_validator') and self._regex_validator in self.validators:
            self.validators.remove(self._regex_validator)
        self._regex_validator = validators.RegexValidator(regex=regex)
        self.validators.append(self._regex_validator)
项目:Roboism    作者:markroxor    | 项目源码 | 文件源码
def _set_regex(self, regex):
        if isinstance(regex, six.string_types):
            regex = re.compile(regex, re.UNICODE)
        self._regex = regex
        if hasattr(self, '_regex_validator') and self._regex_validator in self.validators:
            self.validators.remove(self._regex_validator)
        self._regex_validator = validators.RegexValidator(regex=regex)
        self.validators.append(self._regex_validator)
项目:django-redditauth    作者:SkullTech    | 项目源码 | 文件源码
def validate_reddit_username(value):
    return RegexValidator(regex='^[-_0-9a-zA-Z]{3,20}$')
项目:django-wechat-api    作者:crazy-canux    | 项目源码 | 文件源码
def _set_regex(self, regex):
        if isinstance(regex, six.string_types):
            regex = re.compile(regex, re.UNICODE)
        self._regex = regex
        if hasattr(self, '_regex_validator') and self._regex_validator in self.validators:
            self.validators.remove(self._regex_validator)
        self._regex_validator = validators.RegexValidator(regex=regex)
        self.validators.append(self._regex_validator)
项目:djangorestframework-utils    作者:benzid-wael    | 项目源码 | 文件源码
def get_field_info(self, field):
        """
        Given an instance of a serializer field, return a dictionary
        of metadata about it.
        """
        field_info = collections.OrderedDict()
        field_info['type'] = self.label_lookup[field]
        field_info['required'] = getattr(field, 'required', False)

        attrs = [
            'read_only', 'label', 'help_text', 'allow_null',
            'min_length', 'max_length',
            'min_value', 'max_value',
        ]

        # Handle default attribute
        default_value = getattr(field, 'default')
        if (default_value is not empty):
            field_info['default'] = force_text(
                default_value, strings_only=True)

        for attr in attrs:
            value = getattr(field, attr, None)
            if value is not None and value != '':
                field_info[attr] = force_text(value, strings_only=True)

        if hasattr(field, 'choices'):
            field_info['choices'] = [
                {
                    'value': choice_value,
                    'display_name': force_text(choice_name, strings_only=True)
                }
                for choice_value, choice_name in field.choices.items()
            ]

        # handle RegexField
        if isinstance(field, serializers.RegexField):
            pattern = None
            for validator in field.validators:
                if isinstance(validator, RegexValidator):
                    pattern = validator.regex.pattern
                    break
            field_info['pattern'] = force_text(pattern, strings_only=True)

        # handle DecimalField
        if isinstance(field, serializers.DecimalField):
            for attr in ('max_digits', 'decimal_places'):
                field_info[attr] = force_text(
                    getattr(field, attr), strings_only=True)

        return field_info