def test_regexfield_2(self): f = RegexField('^[0-9][A-F][0-9]$', required=False) self.assertEqual('2A2', f.clean('2A2')) self.assertEqual('3F3', f.clean('3F3')) with self.assertRaisesMessage(ValidationError, "'Enter a valid value.'"): f.clean('3G3') self.assertEqual('', f.clean(''))
def test_regexfield_3(self): f = RegexField(re.compile('^[0-9][A-F][0-9]$')) self.assertEqual('2A2', f.clean('2A2')) self.assertEqual('3F3', f.clean('3F3')) with self.assertRaisesMessage(ValidationError, "'Enter a valid value.'"): f.clean('3G3') with self.assertRaisesMessage(ValidationError, "'Enter a valid value.'"): f.clean(' 2A2') with self.assertRaisesMessage(ValidationError, "'Enter a valid value.'"): f.clean('2A2 ')
def test_regexfield_4(self): f = RegexField('^[0-9]+$', min_length=5, max_length=10) with self.assertRaisesMessage(ValidationError, "'Ensure this value has at least 5 characters (it has 3).'"): f.clean('123') six.assertRaisesRegex( self, ValidationError, r"'Ensure this value has at least 5 characters \(it has 3\)\.'," r" u?'Enter a valid value\.'", f.clean, 'abc' ) self.assertEqual('12345', f.clean('12345')) self.assertEqual('1234567890', f.clean('1234567890')) with self.assertRaisesMessage(ValidationError, "'Ensure this value has at most 10 characters (it has 11).'"): f.clean('12345678901') with self.assertRaisesMessage(ValidationError, "'Enter a valid value.'"): f.clean('12345a')
def test_change_regex_after_init(self): f = RegexField('^[a-z]+$') f.regex = '^[0-9]+$' self.assertEqual('1234', f.clean('1234')) with self.assertRaisesMessage(ValidationError, "'Enter a valid value.'"): f.clean('abcd')
def test_regexfield_unicode_characters(self): f = RegexField(r'^\w+$') self.assertEqual('éèøçÎÎ你好', f.clean('éèøçÎÎ你好'))
def test_regexfield_unicode_characters(self): f = RegexField(r"^\w+$") self.assertEqual("éèøçÎÎ你好", f.clean("éèøçÎÎ你好"))
def test_regexfield_1(self): f = RegexField('^[0-9][A-F][0-9]$') self.assertEqual('2A2', f.clean('2A2')) self.assertEqual('3F3', f.clean('3F3')) with self.assertRaisesMessage(ValidationError, "'Enter a valid value.'"): f.clean('3G3') with self.assertRaisesMessage(ValidationError, "'Enter a valid value.'"): f.clean(' 2A2') with self.assertRaisesMessage(ValidationError, "'Enter a valid value.'"): f.clean('2A2 ') with self.assertRaisesMessage(ValidationError, "'This field is required.'"): f.clean('')
def test_regexfield_4(self): f = RegexField("^[0-9]+$", min_length=5, max_length=10) with self.assertRaisesMessage( ValidationError, "'Ensure this value has at least 5 characters (it has 3).'"): f.clean("123") with self.assertRaisesMessage( ValidationError, "'Ensure this value has at least 5 characters (it has 3).', " "'Enter a valid value.'", ): f.clean("abc") self.assertEqual("12345", f.clean("12345")) self.assertEqual("1234567890", f.clean("1234567890")) with self.assertRaisesMessage( ValidationError, "'Ensure this value has at most 10 characters (it has 11).'", ): f.clean("12345678901") with self.assertRaisesMessage(ValidationError, "'Enter a valid value.'"): f.clean("12345a")
def test_regexfield_4(self): f = RegexField('^[0-9]+$', min_length=5, max_length=10) with self.assertRaisesMessage( ValidationError, "'Ensure this value has at least 5 characters (it has 3).'"): f.clean('123') with self.assertRaisesMessage( ValidationError, "'Ensure this value has at least 5 characters (it has 3).', " "'Enter a valid value.'", ): f.clean('abc') self.assertEqual('12345', f.clean('12345')) self.assertEqual('1234567890', f.clean('1234567890')) with self.assertRaisesMessage( ValidationError, "'Ensure this value has at most 10 characters (it has 11).'"): f.clean('12345678901') with self.assertRaisesMessage(ValidationError, "'Enter a valid value.'"): f.clean('12345a')
class CustomUserFormBase(ModelForm): username = RegexField(re.compile('^[\w.@+-]{1,30}$'), label=_('username').capitalize()) last_name = RegexField(re.compile(r"^[\w '-]+$", re.U), label=_('last name').capitalize(), required=True, max_length=30) first_name = RegexField(re.compile(r"^[\w '-]+$", re.U), label=_('first name').capitalize(), required=True, max_length=30) def clean_username(self): username = self.cleaned_data['username'] user_pk = self.instance.user.pk if self.instance.pk else None if User.objects.filter(username=username).exclude(pk=user_pk).exists(): raise ValidationError(_('This username is already used.')) return username def clean(self): cleaned_data = super(CustomUserFormBase, self).clean() custom_user = self.instance or None striping_fields = ('first_name', 'last_name') for striping_field in striping_fields: cleaned_data[striping_field] = (cleaned_data.get(striping_field, '').strip()) kw_to_exists = {'user__first_name': cleaned_data.get('first_name'), 'user__last_name': cleaned_data.get('last_name')} if not isinstance(self, CustomUserUpdateForm): existed_user = CustomUser.objects.filter(**kw_to_exists).exists() if existed_user and CustomUser.objects.filter(**kw_to_exists)[0]\ .pk: raise ValidationError(_('Full name must be unique')) else: if custom_user: existed_user = CustomUser.objects.filter(**kw_to_exists)\ .exclude(pk=custom_user.pk) if existed_user: raise ValidationError(_('Full name must be unique')) return cleaned_data def __init__(self, *args, **kwargs): super(CustomUserFormBase, self).__init__(*args, **kwargs) first_field_names = ['username', 'last_name', 'first_name'] addition_fields = [field_name for field_name in self.fields if field_name not in first_field_names] field_names = first_field_names + addition_fields fields = OrderedDict((field_name, self.fields[field_name]) for field_name in field_names) self.fields = fields # adding an username initial value if self.instance and self.instance.pk: custom_user = self.instance self.fields['username'].initial = custom_user.user.username self.fields['last_name'].initial = custom_user.user.last_name self.fields['first_name'].initial = custom_user.user.first_name def save(self, commit=True): with transaction.atomic(): custom_user = ModelForm.save(self, commit) custom_user.user.username = self.cleaned_data['username'] custom_user.user.last_name = self.cleaned_data['last_name'] custom_user.user.first_name = self.cleaned_data['first_name'] if commit: custom_user.user.save() return custom_user
def test_regexfield_strip(self): f = RegexField('^[a-z]+$', strip=True) self.assertEqual(f.clean(' a'), 'a') self.assertEqual(f.clean('a '), 'a')
def test_get_regex(self): f = RegexField('^[a-z]+$') self.assertEqual(f.regex, re.compile('^[a-z]+$'))
class SIPURIForm(Form): uri = RegexField(label='', regex=_sip_uri_regex, error_messages={'invalid': 'Invalid SIP URI'})