Guest User

Untitled

a guest
Apr 12th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. email=forms.EmailField(label='Email',max_length=254)
  2.  
  3. password = forms.CharField(label='Password',
  4. widget=forms.PasswordInput())
  5.  
  6. def clean(self,*args,**kwargs):
  7. email=self.self.cleaned_data.get("email")
  8. password=self.cleaned_data.get("password")
  9. if email and password:
  10. user=authenticate(email=email,password=password)
  11. if not user:
  12. raise forms.ValidationError("This email id is not registered")
  13. if not user.check_password(password):
  14. raise forms.ValidationError("Incorrect password")
  15. return super(UserLoginForm,self).clean(*args,**kwargs)
  16.  
  17. error_messages = {
  18. 'duplicate_emailid': "This email id is already exists.",
  19. 'duplicate_mobile': "This mobile no is already exists.",
  20. 'password_mismatch': "The two password fields didn't match.",
  21. 'too_short': "Passwords must be at least 6 characters long.",
  22. }
  23.  
  24. username= forms.CharField(label='Username', max_length=30)
  25. email = forms.EmailField(label='Email address',max_length=254)
  26. mobile =forms.CharField(label='Mobile No',max_length=10)
  27. password = forms.CharField(label='Password',
  28. widget=forms.PasswordInput())
  29. confirm_password = forms.CharField(label='Confirm Password',
  30. widget=forms.PasswordInput())
  31. class Meta:
  32. model = User
  33. fields = ['username', 'email', 'mobile', 'password','confirm_password']
  34. def username(self):
  35. username=self.cleaned_data.get("username")
  36. if username is None:
  37. msg="Plese Enter the User name"
  38. raise forms.ValidationError(msg)
  39. return username
  40.  
  41. def clean_password(self):
  42. password = self.cleaned_data.get("password")
  43. if len(password) < 6:
  44. raise forms.ValidationError(
  45. self.error_messages['too_short'],
  46. )
  47. return password
  48. def clean_confirm_password(self):
  49. password1 = self.cleaned_data.get('password')
  50. confirm_password = self.cleaned_data.get('confirm_password')
  51. if password and confirm_password and password != confirm_password:
  52. raise forms.ValidationError(
  53. self.error_messages['password_mismatch']
  54. )
  55. return confirm_password
  56. def clean_email(self):
  57. email = self.cleaned_data.get('email')
  58. check_duplicate_email = User.objects.filter (email=email).exists()
  59. if check_duplicate_email:
  60. raise forms.ValidationError(
  61. self.error_messages['duplicate_emailid']
  62. )
  63. return email
  64. def clean_mobile(self):
  65. mobile=self.cleaned_data.get('mobile')
  66. check_duplicate_mobile=User.objects.filter(mobile=mobile).exists()
  67. if check_duplicate_mobile:
  68. raise forms.ValidationError(
  69. self.error_messages['duplicate_mobile']
  70. )
  71. return mobile
  72.  
  73. title="Login"
  74.  
  75. form=UserLoginForm(request.POST or None)
  76.  
  77. if form.is_valid():
  78. email=form.cleaned_data.get("email")
  79. password = form.cleaned_data.get("password")
  80. user=authenticate(email=email,password=password)
  81. login(request,user)
  82. return render(request,"registration/signup.html",{"form":form,"title"=title})
  83.  
  84. title="Signup"
  85.  
  86. form = RegistrationForm(request.POST or None)
  87.  
  88. if form.is_valid():
  89. user=form.save(commit=False)
  90. email = form.cleaned_data.get("email")
  91. password = form.cleaned_data.get("password")
  92. user=set_password(password)
  93. user.save()
  94. new_user = authenticate(email=user.email, password=password)
  95. login(request, new_user)
  96. return redirect("registration/register.html")
  97. return render(request, "registration/signup.html", {"form": form,"title"=title})
Add Comment
Please, Sign In to add comment