Reformatted utils/views.py and added a condition to DCL_TEXT in passoword reset email

This commit is contained in:
PCoder 2017-09-02 16:36:20 +05:30
parent 1c42623964
commit 2276533185

View file

@ -1,16 +1,16 @@
from django.views.generic import FormView, CreateView
from django.utils.http import urlsafe_base64_encode, urlsafe_base64_decode
from django.conf import settings from django.conf import settings
from django.contrib import messages from django.contrib import messages
from django.contrib.auth.tokens import default_token_generator
from django.utils.encoding import force_bytes
from django.http import HttpResponseRedirect
from django.contrib.auth import authenticate, login from django.contrib.auth import authenticate, login
from django.contrib.auth.tokens import default_token_generator
from django.http import HttpResponseRedirect
from django.utils.encoding import force_bytes
from django.utils.http import urlsafe_base64_encode, urlsafe_base64_decode
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from membership.models import CustomUser from django.views.generic import FormView, CreateView
from .mailer import BaseEmail from membership.models import CustomUser
from .forms import SetPasswordForm from .forms import SetPasswordForm
from .mailer import BaseEmail
class SignupViewMixin(CreateView): class SignupViewMixin(CreateView):
@ -18,8 +18,8 @@ class SignupViewMixin(CreateView):
success_url = None success_url = None
def get_success_url(self): def get_success_url(self):
next_url = self.request.POST.get('next') if self.request.POST.get(
next_url = self.request.POST.get('next') if self.request.POST.get('next')\ 'next') \
else self.success_url else self.success_url
return next_url return next_url
@ -66,7 +66,8 @@ class LoginViewMixin(FormView):
class PasswordResetViewMixin(FormView): class PasswordResetViewMixin(FormView):
# template_name = 'hosting/reset_password.html' # template_name = 'hosting/reset_password.html'
# form_class = PasswordResetRequestForm # form_class = PasswordResetRequestForm
success_message = _("The link to reset your email has been sent to your email") success_message = _(
"The link to reset your email has been sent to your email")
site = '' site = ''
def test_generate_email_context(self, user): def test_generate_email_context(self, user):
@ -74,8 +75,9 @@ class PasswordResetViewMixin(FormView):
'user': user, 'user': user,
'token': default_token_generator.make_token(user), 'token': default_token_generator.make_token(user),
'uid': urlsafe_base64_encode(force_bytes(user.pk)), 'uid': urlsafe_base64_encode(force_bytes(user.pk)),
'site_name': 'ungleich', 'site_name': 'ungleich' if self.site != 'dcl' else settings.DCL_TEXT,
'base_url': "{0}://{1}".format(self.request.scheme, self.request.get_host()) 'base_url': "{0}://{1}".format(self.request.scheme,
self.request.get_host())
} }
return context return context
@ -83,7 +85,8 @@ class PasswordResetViewMixin(FormView):
def form_valid(self, form): def form_valid(self, form):
email = form.cleaned_data.get('email') email = form.cleaned_data.get('email')
user = CustomUser.objects.get(email=email) user = CustomUser.objects.get(email=email)
messages.add_message(self.request, messages.SUCCESS, self.success_message) messages.add_message(self.request, messages.SUCCESS,
self.success_message)
context = self.test_generate_email_context(user) context = self.test_generate_email_context(user)
email_data = { email_data = {
'subject': _('Password Reset'), 'subject': _('Password Reset'),
@ -103,6 +106,7 @@ class PasswordResetViewMixin(FormView):
class PasswordResetConfirmViewMixin(FormView): class PasswordResetConfirmViewMixin(FormView):
# template_name = 'hosting/confirm_reset_password.html' # template_name = 'hosting/confirm_reset_password.html'
form_class = SetPasswordForm form_class = SetPasswordForm
# success_url = reverse_lazy('hosting:login') # success_url = reverse_lazy('hosting:login')
def post(self, request, uidb64=None, token=None, *arg, **kwargs): def post(self, request, uidb64=None, token=None, *arg, **kwargs):
@ -114,7 +118,8 @@ class PasswordResetConfirmViewMixin(FormView):
form = self.form_class(request.POST) form = self.form_class(request.POST)
if user is not None and default_token_generator.check_token(user, token): if user is not None and default_token_generator.check_token(user,
token):
if form.is_valid(): if form.is_valid():
new_password = form.cleaned_data['new_password2'] new_password = form.cleaned_data['new_password2']
user.set_password(new_password) user.set_password(new_password)
@ -122,11 +127,13 @@ class PasswordResetConfirmViewMixin(FormView):
messages.success(request, 'Password has been reset.') messages.success(request, 'Password has been reset.')
return self.form_valid(form) return self.form_valid(form)
else: else:
messages.error(request, 'Password reset has not been successful.') messages.error(request,
'Password reset has not been successful.')
form.add_error(None, 'Password reset has not been successful.') form.add_error(None, 'Password reset has not been successful.')
return self.form_invalid(form) return self.form_invalid(form)
else: else:
messages.error(request, 'The reset password link is no longer valid.') messages.error(request,
'The reset password link is no longer valid.')
form.add_error(None, 'The reset password link is no longer valid.') form.add_error(None, 'The reset password link is no longer valid.')
return self.form_invalid(form) return self.form_invalid(form)