Tuesday, February 5, 2019

reg: How to use Django Password validation in my existing views?

I have an existing django views.py where I have some password, username, and email validation logic applied. However, going forward I need to apply more advanced password validation. for e.g. password length limitation, uppercase sensitivity etc. I have a code written for advanced validation, but I am not able to apply them to my existing views.py. Below is the code from views.py

from django.shortcuts import render, redirect  from django.contrib.auth.models import User  from django.contrib import messages  from . import validator  def register(request):      if request.method == 'POST':          first_name = request.POST['first_name']          last_name = request.POST['last_name']          email = request.POST['email']          username = request.POST['username']          password = request.POST['password', validator.MinimumLengthValidator]          password2 = request.POST['password2']            # check if the password match          if password == password2:                if User.objects.filter(username=username).exists():                  messages.error(request, 'username already exist')                  return redirect('register')              else:                  if User.objects.filter(email=email).exists():                      messages.error(request, 'Registration Failed - Try different email address')                      return redirect('register')                  else:                      user = User.objects.create_user(username=username, password=password, email=email,                                                      first_name=first_name, last_name=last_name)                      user.save()                      messages.success(request, 'Registration complete, please proceed to login')                      return redirect('register')          else:              messages.error(request, 'password dose not match')              return redirect('register')      else:          return render(request, 'ACCOUNTS/register.html')

Below is the code for advanced password validation from validate.py

import re      from django.core.exceptions import ValidationError  from django.utils.translation import ugettext as _        class MinimumLengthValidator:      def __init__(self, min_length=8):          self.min_length = min_length        def validate(self, password, user=None):          if len(password) < self.min_length:              raise ValidationError(                  _("This password must contain at least %(min_length)d characters."),                  code='password_too_short',                  params={'min_length': self.min_length},              )        def get_help_text(self):          return _(              "Your password must contain at least %(self.min_length)d characters."              % {'min_length': self.min_length}          )      class NumberValidator(object):      def validate(self, password, user=None):          if not re.findall('\d', password):              raise ValidationError(                  _("The password must contain at least %(min_digits)d digit(s), 0-9."),                  code='password_no_number',              )        def get_help_text(self):          return _(              "Your password must contain at least 1 digit, 0-9."          )      class UppercaseValidator(object):      def validate(self, password, user=None):          if not re.findall('[A-Z]', password):              raise ValidationError(                  _("The password must contain at least 1 uppercase letter, A-Z."),                  code='password_no_upper',              )        def get_help_text(self):          return _(              "Your password must contain at least 1 uppercase letter, A-Z."          )

I have tried below steps.

I imported the validator.py in the views.py,and tried to call the module.function inside the password field as below    password = request.POST['password', validator.MinimumLengthValidator]

But that doesn't work. If I am right, I can write a mixin class and call it in my views.py. But I am using function based views. So, I am not sure if I can use mixin. Please suggest how can we achieve the desired result.

Regards,

Amitesh Sahay
91-750 797 8619

No comments:

Post a Comment