Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow overriding of messages #5

Merged
merged 3 commits into from May 31, 2018
Merged

Conversation

mic159
Copy link
Contributor

@mic159 mic159 commented May 16, 2018

Add code for use as a field validator

Set the code of the ValidationError for pwned passwords to pwned_password, to allow the use of error_messages when used in a field as a regular validator.

See: https://docs.djangoproject.com/en/2.0/ref/forms/validation/#raising-validationerror
See: https://docs.djangoproject.com/en/2.0/ref/forms/fields/#error-messages

Example:

def pwned_validator(password):
    PwnedPasswordsValidator().validate(password)

class ChangePassword(forms.Form):
    forms.CharField(
        validators=[pwned_validator],
        error_messages={
            'pwned_password': 'Example message',
        }
    )

Override messages in Django settings

Unfortunately you cannot override messages like the above when used in a clean_* method (like the default Django change password form).
So I also added a way to override it in the OPTIONS of the validator in the settings.
You can also put the number of times the password appeared in breaches with the %(amount)d syntax, and even an optional plural form.

For example:

{
    'NAME': 'pwned_passwords_django.validators.PwnedPasswordsValidator',
    'OPTIONS': {
        'error_message': 'That password was pwned',
        'help_message': 'Cannot be pwned password',
    },
}

With count (and plural form):

{
    'NAME': 'pwned_passwords_django.validators.PwnedPasswordsValidator',
    'OPTIONS': {
        'error_message': (
                'Pwned %(amount)d time',
                'Pwned %(amount)d times',
        ),
    },
}

@ubernostrum
Copy link
Owner

I need to think a bit on this one. Still digging out from under post-PyCon pile of stuff to read.

@ubernostrum
Copy link
Owner

ubernostrum commented May 25, 2018

Now that the package reorg branch has landed and the code lives in src/, do you mind rebasing this to have the files in the correct locations?

@mic159
Copy link
Contributor Author

mic159 commented May 25, 2018

@ubernostrum It is now rebased

@ubernostrum ubernostrum merged commit e98f5a7 into ubernostrum:master May 31, 2018
@jessexoc
Copy link

jessexoc commented Jun 1, 2018

@ubernostrum Are there plans to push a new release to PyPI soon?

@mic159 mic159 deleted the message_code branch June 1, 2018 10:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants