Forms

django-password-policies-iplweb ships two django.forms.Form classes that handles the validation of new passwords:

PasswordPoliciesForm

class password_policies.forms.PasswordPoliciesForm(user, *args, **kwargs)

A form that lets a user set his/her password without entering the old password.

Has the following fields and methods:

new_password1

PasswordPoliciesField (Required)

Parameters:
new_password2

CharField (Required)

Parameters:
  • error_messages – {‘required’: ‘This field is required.’}

  • localize – False

  • validators – [’ProhibitNullCharactersValidator’]

  • widget – PasswordInput

error_messages = {'password_mismatch': "The two password fields didn't match.", 'password_used': 'The new password was used before. Please enter another one.'}

This forms error messages.

__init__(user, *args, **kwargs)

Initializes the form.

Parameters:

user – A User instance.

clean_new_password1()

Validates that a given password was not used before.

clean_new_password2()

Validates that the two new passwords match.

save(commit=True)

Sets the user’s password to the new one and creates an entry in the user’s password history, if PASSWORD_USE_HISTORY is set to True.

property media

Return all media required to render the widgets on this form.

PasswordPoliciesChangeForm

class password_policies.forms.PasswordPoliciesChangeForm(user, *args, **kwargs)

Bases: PasswordPoliciesForm

A form that lets a user change his/her password by entering their old password.

Has the following fields and methods:

old_password

CharField (Required)

Parameters:
  • error_messages – {‘required’: ‘This field is required.’}

  • localize – False

  • validators – [’ProhibitNullCharactersValidator’]

  • widget – PasswordInput

new_password1

PasswordPoliciesField (Required)

Parameters:
new_password2

CharField (Required)

Parameters:
  • error_messages – {‘required’: ‘This field is required.’}

  • localize – False

  • validators – [’ProhibitNullCharactersValidator’]

  • widget – PasswordInput

error_messages = {'password_identical': 'The old and the new password are the same.', 'password_incorrect': 'Your old password was entered incorrectly. Please enter it again.', 'password_mismatch': "The two password fields didn't match.", 'password_similar': 'The old and the new password are too similar.', 'password_used': 'The new password was used before. Please enter another one.'}

This forms error messages.

clean_old_password()

Validates the current password.

clean()

Validates that old and new password are not too similar.

save(commit=True)

Sets the user’s password to the new one and creates an entry in the user’s password history, if PASSWORD_USE_HISTORY is set to True.

property media

Return all media required to render the widgets on this form.

PasswordPoliciesRegistrationForm

class password_policies.forms.PasswordPoliciesRegistrationForm(data=None, files=None, auto_id='id_%s', prefix=None, initial=None, error_class=<class 'django.forms.utils.ErrorList'>, label_suffix=None, empty_permitted=False, field_order=None, use_required_attribute=None, renderer=None, bound_field_class=None)

A form to support user registration with password policies.

Has the following fields and methods:

username

RegexField (Required)

Parameters:
  • error_messages – {‘required’: ‘This field is required.’, ‘invalid’: ‘This value may contain only letters, numbers and @/./+/-/_ characters.’}

  • help_text – Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only.

  • localize – False

  • validators – [’MaxLengthValidator’, ‘ProhibitNullCharactersValidator’, ‘RegexValidator’]

  • widget – TextInput

password1

PasswordPoliciesField (Required)

Parameters:
password2

CharField (Required)

Parameters:
  • error_messages – {‘required’: ‘This field is required.’}

  • help_text – Enter the same password as above, for verification.

  • localize – False

  • validators – [’ProhibitNullCharactersValidator’]

  • widget – PasswordInput

error_messages = {'duplicate_username': 'A user with that username already exists.', 'password_mismatch': "The two password fields didn't match."}

This forms error messages.

clean_username()

Validates that the username is not already taken.

clean_password2()

Validates that the two passwords are identical.

property media

Return all media required to render the widgets on this form.

PasswordResetForm

class password_policies.forms.PasswordResetForm(data=None, files=None, auto_id='id_%s', prefix=None, initial=None, error_class=<class 'django.forms.utils.ErrorList'>, label_suffix=None, empty_permitted=False, field_order=None, use_required_attribute=None, renderer=None, bound_field_class=None)

A form to let a user reset his/her password.

Has the following fields and methods:

email

EmailField (Required)

Parameters:
error_messages = {'unknown': "That e-mail address doesn't have an associated user account. Are you sure you've registered?", 'unusable': 'The user account associated with this e-mail address cannot reset the password.'}

This forms error messages.

clean_email()

Validates that an active user exists with the given email address.

save(domain_override=None, subject_template_name='registration/password_reset_subject.txt', email_template_name='registration/password_reset_email.txt', email_html_template_name='registration/password_reset_email.html', use_https=False, from_email=None, request=None)

Generates a one-use only link for resetting password and sends to the user.

Parameters:
  • domain_override (str) – A string that changes the site name and domain if needed.

  • email_template_name (str) – A relative path to a template in the root of a template directory to generate the body of the mail.

  • email_html_template_name (str) – A relative path to a template in the root of a template directory to generate the HTML attachment of the mail.

  • from_email (str) – The email address to use as sender of the email.

  • request – A HttpRequest instance.

  • subject_template_name (str) – A relative path to a template in the root of a template directory to generate the subject of the mail.

  • use_https (bool) – Determines wether to use HTTPS while generating the one-use only link for resetting passwords.

get_context_data(request, domain_override, use_https)

Returns a dictionary with common context items.

Parameters:
  • request – A HttpRequest instance.

  • domain_override (str) – A string that changes the site name and domain if needed.

  • use_https (bool) – Determines wether to use HTTPS while generating the one-use only link for resetting passwords.

property media

Return all media required to render the widgets on this form.