我们从Python开源项目中,提取了以下47个代码示例,用于说明如何使用django.core.validators.RegexValidator()。
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
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 (-)." )
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)
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)
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)
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)
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)
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)
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)
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)
def __init__(self, *args, **kwargs): if "validators" not in kwargs: kwargs["validators"] = [validators.RegexValidator(r"(\s*[^\s]\s*)+")] super(nonEmptyStringField, self).__init__(*args, **kwargs)
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)
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)
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)
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)
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)
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)
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
def validate_reddit_username(value): return RegexValidator(regex='^[-_0-9a-zA-Z]{3,20}$')
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