Python django.core.validators.RegexValidator() Examples
The following are 20
code examples of django.core.validators.RegexValidator().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
django.core.validators
, or try the search function
.
Example #1
Source File: base.py From python-opencivicdata with BSD 3-Clause "New" or "Revised" License | 6 votes |
def __init__(self, *args, **kwargs): self.ocd_type = kwargs.pop("ocd_type") if self.ocd_type != "jurisdiction": kwargs["default"] = lambda: "ocd-{}/{}".format(self.ocd_type, uuid.uuid4()) # len('ocd-') + len(ocd_type) + len('/') + len(uuid) # = 4 + len(ocd_type) + 1 + 36 # = len(ocd_type) + 41 kwargs["max_length"] = 41 + len(self.ocd_type) regex = ( "^ocd-" + self.ocd_type + "/[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}$" ) else: kwargs["max_length"] = 300 regex = common.JURISDICTION_ID_REGEX kwargs["primary_key"] = True # get pattern property if it exists, otherwise just return the object (hopefully a string) msg = "ID must match " + getattr(regex, "pattern", regex) kwargs["validators"] = [RegexValidator(regex=regex, message=msg, flags=re.U)] super(OCDIDField, self).__init__(*args, **kwargs)
Example #2
Source File: models.py From memex-explorer with BSD 2-Clause "Simplified" License | 5 votes |
def alphanumeric_validator(): return RegexValidator(r'^[a-zA-Z0-9-_ ]+$', 'Only numbers, letters, underscores, dashes and spaces are allowed.')
Example #3
Source File: fields.py From esdc-ce with Apache License 2.0 | 5 votes |
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)
Example #4
Source File: test_validators.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_all_errors_get_reported(self): class UserForm(forms.Form): full_name = forms.CharField( max_length=50, validators=[ validators.validate_integer, validators.validate_email, ] ) string = forms.CharField( max_length=50, validators=[ validators.RegexValidator( regex='^[a-zA-Z]*$', message="Letters only.", ) ] ) ignore_case_string = forms.CharField( max_length=50, validators=[ validators.RegexValidator( regex='^[a-z]*$', message="Letters only.", flags=re.IGNORECASE, ) ] ) form = UserForm({ 'full_name': 'not int nor mail', 'string': '2 is not correct', 'ignore_case_string': "IgnORE Case strIng", }) with self.assertRaises(ValidationError) as e: form.fields['full_name'].clean('not int nor mail') self.assertEqual(2, len(e.exception.messages)) self.assertFalse(form.is_valid()) self.assertEqual(form.errors['string'], ["Letters only."]) self.assertEqual(form.errors['string'], ["Letters only."])
Example #5
Source File: test_validators.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_all_errors_get_reported(self): class UserForm(forms.Form): full_name = forms.CharField( max_length=50, validators=[ validators.validate_integer, validators.validate_email, ] ) string = forms.CharField( max_length=50, validators=[ validators.RegexValidator( regex='^[a-zA-Z]*$', message="Letters only.", ) ] ) ignore_case_string = forms.CharField( max_length=50, validators=[ validators.RegexValidator( regex='^[a-z]*$', message="Letters only.", flags=re.IGNORECASE, ) ] ) form = UserForm({ 'full_name': 'not int nor mail', 'string': '2 is not correct', 'ignore_case_string': "IgnORE Case strIng", }) with self.assertRaises(ValidationError) as e: form.fields['full_name'].clean('not int nor mail') self.assertEqual(2, len(e.exception.messages)) self.assertFalse(form.is_valid()) self.assertEqual(form.errors['string'], ["Letters only."]) self.assertEqual(form.errors['string'], ["Letters only."])
Example #6
Source File: fields.py From python2017 with MIT License | 5 votes |
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)
Example #7
Source File: fields.py From openhgsenti with Apache License 2.0 | 5 votes |
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)
Example #8
Source File: models.py From memex-explorer with BSD 2-Clause "Simplified" License | 5 votes |
def zipped_file_validator(): return RegexValidator(r'.*\.(ZIP|zip)$', 'Only compressed archive (.zip) files are allowed.')
Example #9
Source File: fields.py From GTDWeb with GNU General Public License v2.0 | 5 votes |
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)
Example #10
Source File: fields.py From luscan-devel with GNU General Public License v2.0 | 5 votes |
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)
Example #11
Source File: fields.py From python with Apache License 2.0 | 5 votes |
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)
Example #12
Source File: forms.py From SchoolIdolAPI with Apache License 2.0 | 5 votes |
def __init__(self, *args, **kwargs): self.request = kwargs.pop('request', None) initial = kwargs.get('initial', {}) initial['right_picture'] = '' kwargs['initial'] = initial super(_Activity, self).__init__(*args, **kwargs) self.fields['right_picture'].help_text = _('Use a valid imgur URL such as http://i.imgur.com/6oHYT4B.png') self.fields['right_picture'].label = _('Picture') self.fields['right_picture'].validators.append(RegexValidator( regex=settings.IMGUR_REGEXP, message='Invalid Imgur URL', code='invalid_url', ))
Example #13
Source File: fields.py From Hands-On-Application-Development-with-PyCharm with MIT License | 5 votes |
def _set_regex(self, regex): if isinstance(regex, str): 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)
Example #14
Source File: validators.py From lego with MIT License | 5 votes |
def validate_email_address_content(email_address): """Make sure we only create valid emails.""" regex_validator = RegexValidator(regex=EmailValidator.user_regex) reserved_valdator = ReservedNameValidator() regex_validator(email_address.email) reserved_valdator(email_address.email)
Example #15
Source File: models.py From opencraft with GNU Affero General Public License v3.0 | 5 votes |
def validate_color(color): """ Check the color is either #123 or #123456 """ validators.RegexValidator( r'^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$', '{} is not a valid color, it must be either #123 or #123456'.format(color), )(color)
Example #16
Source File: fields.py From bioforum with MIT License | 5 votes |
def _set_regex(self, regex): if isinstance(regex, str): 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)
Example #17
Source File: project.py From KubeOperator with Apache License 2.0 | 5 votes |
def get_fields(self): fields = super().get_fields() name_field = fields.get('name') for validator in name_field.validators: if isinstance(validator, RegexValidator): if validator.regex.pattern == r'^[-a-zA-Z0-9_]+$': validator.regex = re.compile(r'^[-\w]+\Z') return fields
Example #18
Source File: test_writer.py From django-sqlserver with MIT License | 4 votes |
def test_serialize_class_based_validators(self): """ Ticket #22943: Test serialization of class-based validators, including compiled regexes. """ validator = RegexValidator(message="hello") string = MigrationWriter.serialize(validator)[0] self.assertEqual(string, "django.core.validators.RegexValidator(message='hello')") self.serialize_round_trip(validator) # Test with a compiled regex. validator = RegexValidator(regex=re.compile(r'^\w+$', re.U)) string = MigrationWriter.serialize(validator)[0] self.assertEqual(string, "django.core.validators.RegexValidator(regex=re.compile('^\\\\w+$', 32))") self.serialize_round_trip(validator) # Test a string regex with flag validator = RegexValidator(r'^[0-9]+$', flags=re.U) string = MigrationWriter.serialize(validator)[0] if PY36: self.assertEqual(string, "django.core.validators.RegexValidator('^[0-9]+$', flags=re.RegexFlag(32))") else: self.assertEqual(string, "django.core.validators.RegexValidator('^[0-9]+$', flags=32)") self.serialize_round_trip(validator) # Test message and code validator = RegexValidator('^[-a-zA-Z0-9_]+$', 'Invalid', 'invalid') string = MigrationWriter.serialize(validator)[0] self.assertEqual(string, "django.core.validators.RegexValidator('^[-a-zA-Z0-9_]+$', 'Invalid', 'invalid')") self.serialize_round_trip(validator) # Test with a subclass. validator = EmailValidator(message="hello") string = MigrationWriter.serialize(validator)[0] self.assertEqual(string, "django.core.validators.EmailValidator(message='hello')") self.serialize_round_trip(validator) validator = deconstructible(path="migrations.test_writer.EmailValidator")(EmailValidator)(message="hello") string = MigrationWriter.serialize(validator)[0] self.assertEqual(string, "migrations.test_writer.EmailValidator(message='hello')") validator = deconstructible(path="custom.EmailValidator")(EmailValidator)(message="hello") with self.assertRaisesRegex(ImportError, "No module named '?custom'?"): MigrationWriter.serialize(validator) validator = deconstructible(path="django.core.validators.EmailValidator2")(EmailValidator)(message="hello") with self.assertRaisesMessage(ValueError, "Could not find object EmailValidator2 in django.core.validators."): MigrationWriter.serialize(validator)
Example #19
Source File: test_writer.py From djongo with GNU Affero General Public License v3.0 | 4 votes |
def test_serialize_class_based_validators(self): """ Ticket #22943: Test serialization of class-based validators, including compiled regexes. """ validator = RegexValidator(message="hello") string = MigrationWriter.serialize(validator)[0] self.assertEqual(string, "django.core.validators.RegexValidator(message='hello')") self.serialize_round_trip(validator) # Test with a compiled regex. validator = RegexValidator(regex=re.compile(r'^\w+$')) string = MigrationWriter.serialize(validator)[0] self.assertEqual(string, "django.core.validators.RegexValidator(regex=re.compile('^\\\\w+$'))") self.serialize_round_trip(validator) # Test a string regex with flag validator = RegexValidator(r'^[0-9]+$', flags=re.S) string = MigrationWriter.serialize(validator)[0] if PY36: self.assertEqual(string, "django.core.validators.RegexValidator('^[0-9]+$', flags=re.RegexFlag(16))") else: self.assertEqual(string, "django.core.validators.RegexValidator('^[0-9]+$', flags=16)") self.serialize_round_trip(validator) # Test message and code validator = RegexValidator('^[-a-zA-Z0-9_]+$', 'Invalid', 'invalid') string = MigrationWriter.serialize(validator)[0] self.assertEqual(string, "django.core.validators.RegexValidator('^[-a-zA-Z0-9_]+$', 'Invalid', 'invalid')") self.serialize_round_trip(validator) # Test with a subclass. validator = EmailValidator(message="hello") string = MigrationWriter.serialize(validator)[0] self.assertEqual(string, "django.core.validators.EmailValidator(message='hello')") self.serialize_round_trip(validator) validator = deconstructible(path="migrations.test_writer.EmailValidator")(EmailValidator)(message="hello") string = MigrationWriter.serialize(validator)[0] self.assertEqual(string, "migrations.test_writer.EmailValidator(message='hello')") validator = deconstructible(path="custom.EmailValidator")(EmailValidator)(message="hello") with self.assertRaisesMessage(ImportError, "No module named 'custom'"): MigrationWriter.serialize(validator) validator = deconstructible(path="django.core.validators.EmailValidator2")(EmailValidator)(message="hello") with self.assertRaisesMessage(ValueError, "Could not find object EmailValidator2 in django.core.validators."): MigrationWriter.serialize(validator)
Example #20
Source File: test_writer.py From djongo with GNU Affero General Public License v3.0 | 4 votes |
def test_serialize_class_based_validators(self): """ Ticket #22943: Test serialization of class-based validators, including compiled regexes. """ validator = RegexValidator(message="hello") string = MigrationWriter.serialize(validator)[0] self.assertEqual(string, "django.core.validators.RegexValidator(message='hello')") self.serialize_round_trip(validator) # Test with a compiled regex. validator = RegexValidator(regex=re.compile(r'^\w+$')) string = MigrationWriter.serialize(validator)[0] self.assertEqual(string, "django.core.validators.RegexValidator(regex=re.compile('^\\\\w+$'))") self.serialize_round_trip(validator) # Test a string regex with flag validator = RegexValidator(r'^[0-9]+$', flags=re.S) string = MigrationWriter.serialize(validator)[0] if PY36: self.assertEqual(string, "django.core.validators.RegexValidator('^[0-9]+$', flags=re.RegexFlag(16))") else: self.assertEqual(string, "django.core.validators.RegexValidator('^[0-9]+$', flags=16)") self.serialize_round_trip(validator) # Test message and code validator = RegexValidator('^[-a-zA-Z0-9_]+$', 'Invalid', 'invalid') string = MigrationWriter.serialize(validator)[0] self.assertEqual(string, "django.core.validators.RegexValidator('^[-a-zA-Z0-9_]+$', 'Invalid', 'invalid')") self.serialize_round_trip(validator) # Test with a subclass. validator = EmailValidator(message="hello") string = MigrationWriter.serialize(validator)[0] self.assertEqual(string, "django.core.validators.EmailValidator(message='hello')") self.serialize_round_trip(validator) validator = deconstructible(path="migrations.test_writer.EmailValidator")(EmailValidator)(message="hello") string = MigrationWriter.serialize(validator)[0] self.assertEqual(string, "migrations.test_writer.EmailValidator(message='hello')") validator = deconstructible(path="custom.EmailValidator")(EmailValidator)(message="hello") with self.assertRaisesMessage(ImportError, "No module named 'custom'"): MigrationWriter.serialize(validator) validator = deconstructible(path="django.core.validators.EmailValidator2")(EmailValidator)(message="hello") with self.assertRaisesMessage(ValueError, "Could not find object EmailValidator2 in django.core.validators."): MigrationWriter.serialize(validator)