コード例 #1
0
    def test_case_insensitive_validator(self):
        """
        Test the case-insensitive username validator.

        """
        validator = validators.CaseInsensitiveUnique(
            self.user_model,
            self.user_model.USERNAME_FIELD,
            validators.DUPLICATE_USERNAME,
        )
        for value in (123456, 1.7, uuid.uuid4()):
            self.assertTrue(validator(value) is None)

        base_creation_data = self.valid_data.copy()
        base_creation_data["password"] = base_creation_data["password1"]
        del base_creation_data["password1"]
        del base_creation_data["password2"]

        test_names = [("alice", "ALICE"), ("ALICE", "alice"),
                      ("Alice", "alice")]
        test_names.extend([("STRASSBURGER", "straßburger")])

        for name, conflict in test_names:
            creation_data = base_creation_data.copy()
            creation_data[self.user_model.USERNAME_FIELD] = name
            existing_user = self.user_model(**creation_data)
            existing_user.save()
            with self.assertRaisesMessage(ValidationError,
                                          str(validators.DUPLICATE_USERNAME)):
                validator(conflict)
            existing_user.delete()
コード例 #2
0
 def __init__(self, *args, **kwargs):
     super().__init__(*args, **kwargs)
     self.fields['email'].validators.extend(
         (validators.HTML5EmailValidator(),
          validators.validate_confusables_email,
          validators.CaseInsensitiveUnique(models.User, 'email',
                                           validators.DUPLICATE_EMAIL)))
     self.fields['email'].required = True
コード例 #3
0
ファイル: validators.py プロジェクト: aleffert/journaltown-be
 def __init__(self):
     self.validators = [
         validators.CaseInsensitiveUnique(
             get_user_model(), 'username',
             'Invalid username: Already taken'),
         validators.ReservedNameValidator(),
         validators.validate_confusables,
         UnicodeUsernameValidator()
     ]
コード例 #4
0
ファイル: forms.py プロジェクト: Pack144/py_packman
 def __init__(self, *args, **kwargs):
     super(RegistrationForm, self).__init__(*args, **kwargs)
     email_field = User.get_email_field_name()
     self.fields[email_field].validators.append(
         validators.validate_confusables_email)
     self.fields[email_field].required = True
     self.fields[email_field].validators.append(
         validators.CaseInsensitiveUnique(User, email_field,
                                          validators.DUPLICATE_EMAIL))
コード例 #5
0
    def test_case_insensitive_validator_eq(self):
        """
        Test CaseInsensitiveUnique __eq__ method.
        __eq__ is necessary for serializing custom user models that use
        the validator.

        """
        validator = validators.CaseInsensitiveUnique(
            self.user_model, self.user_model.USERNAME_FIELD,
            validators.DUPLICATE_USERNAME)
        validator_same = validators.CaseInsensitiveUnique(
            self.user_model, self.user_model.USERNAME_FIELD,
            validators.DUPLICATE_USERNAME)
        self.assertTrue(validator.__eq__(validator_same))

        validator_different = validators.CaseInsensitiveUnique(
            self.user_model, 'not username field',
            validators.DUPLICATE_USERNAME)
        self.assertFalse(validator.__eq__(validator_different))
コード例 #6
0
ファイル: forms.py プロジェクト: benallan/lovelace-website
    def __init__(self, *args, **kwargs):
        super(CustomRegistrationForm, self).__init__(*args, **kwargs)
        self.fields[User.USERNAME_FIELD].validators.append(
            MinLengthValidator(3))
        self.fields[User.USERNAME_FIELD].validators.append(
            MaxLengthValidator(20))

        self.fields[User.USERNAME_FIELD].validators.append(
            validators.CaseInsensitiveUnique(User, User.USERNAME_FIELD,
                                             validators.DUPLICATE_USERNAME))

        email_field = User.get_email_field_name()
        self.fields[email_field].validators.append(
            validators.CaseInsensitiveUnique(User, email_field,
                                             validators.DUPLICATE_EMAIL))

        self.fields[
            'username'].help_text = "Required. 3-20 characters. Letters, digits and @/./+/-/_ only."
        self.fields[
            'password1'].help_text = "Your password must contain at least 8 characters, cannot be entirely numeric, and cannot be too similar to your other personal information or a commonly used password."
コード例 #7
0
ファイル: forms.py プロジェクト: ieeeuoft/newhacks-site-2020
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        email_field = User.get_email_field_name()
        self.fields[email_field].validators.extend((
            validators.HTML5EmailValidator(),
            validators.validate_confusables_email,
            validators.CaseInsensitiveUnique(User, email_field,
                                             "This email is unavailable"),
        ))
        self.label_suffix = ""

        # This overrides the default labels set by UserCreationForm
        for field, label in self._meta.labels.items():
            self.fields[field].label = label

        for field in self._meta.fields:
            self.fields[field].required = True
コード例 #8
0
ファイル: forms.py プロジェクト: adamprada/ksicht
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        email_field = User.get_email_field_name()
        self.fields[email_field].validators.append(
            reg_validators.CaseInsensitiveUnique(
                User, email_field, reg_validators.DUPLICATE_EMAIL
            )
        )
        self.fields[email_field].label = "Emailová adresa"
        self.fields[
            email_field
        ].help_text = "Slouží jako uživatelské jméno pro přihlášení."

        self.fields["tos"] = forms.BooleanField(
            widget=forms.CheckboxInput,
            label=f"Přečetl/a jsem si a souhlasím s <a href='{webpack_static('attachments/zpracovani-osobnich-udaju-pro-web.pdf')}' target='_blank' rel='noopener'>podmínkami použití a zpracováním osobních údajů</a>",
        )

        self.helper = FormHelper()
        self.helper.layout = Layout(
            Row(Column(Field("email", autocomplete="email"))),
            Row(Column("password1"), Column("password2")),
            Row(
                Column("first_name"),
                Column("last_name"),
                Column(Field("phone", autocomplete="phone")),
            ),
            Row(Column("street")),
            Row(
                Column("zip_code", css_class="is-2"),
                Column("city", css_class="is-4"),
                Column("country", css_class="is-4 is-offset-2"),
            ),
            Row(Column("school", css_class="is-10"), Column("school_year")),
            Row(Column("school_alt_name"), Column("school_alt_street")),
            Row(
                Column("school_alt_zip_code", css_class="is-2"),
                Column("school_alt_city", css_class="is-4"),
            ),
            Row(Column("tos")),
            FormActions(
                FormControl(Submit("submit", "Pokračovat")),
                FormControl(Link("core:home", "Zpět", "button is-text")),
            ),
        )
コード例 #9
0
 def __init__(self, *args, **kwargs):
     super().__init__(*args, **kwargs)
     self.fields[User.USERNAME_FIELD].validators.append(
         validators.CaseInsensitiveUnique(User, User.USERNAME_FIELD,
                                          validators.DUPLICATE_USERNAME))
     self.fields["username"].help_text += (
         " You cannot change this without intervention from an admin, so "
         "consider carefully.")
     self.helper = FormHelper()
     self.helper.form_action = "django_registration_register"
     self.helper.layout = Layout(
         PrependedText("username", "https://wheretofind.me/@"),
         Field("email"),
         Field("password1"),
         Field("password2"),
         Field("tos"),
         Submit("save", "Sign up", css_class="btn-lg"),
     )
コード例 #10
0
    def test_case_insensitive_validator(self):
        """
        Test the case-insensitive username validator.

        """
        validator = validators.CaseInsensitiveUnique(
            self.user_model, self.user_model.USERNAME_FIELD,
            validators.DUPLICATE_USERNAME)
        for value in (123456, 1.7, uuid.uuid4()):
            self.assertTrue(validator(value) is None)

        base_creation_data = self.valid_data.copy()
        base_creation_data['password'] = base_creation_data['password1']
        del base_creation_data['password1']
        del base_creation_data['password2']

        test_names = [
            (u'alice', u'ALICE'),
            (u'ALICE', u'alice'),
            (u'Alice', u'alice'),
        ]
        if six.PY3:
            test_names.extend([
                (u'STRASSBURGER', u'straßburger'),
            ])

        for name, conflict in test_names:
            creation_data = base_creation_data.copy()
            creation_data[self.user_model.USERNAME_FIELD] = name
            existing_user = self.user_model(**creation_data)
            existing_user.save()
            with self.assertRaisesMessage(
                    ValidationError,
                    six.text_type(validators.DUPLICATE_USERNAME)):
                validator(conflict)
            existing_user.delete()