def get(self, request, *args, **kwargs): if request.user.is_authenticated: add_messages(self.request, _("Already logged in"), [ _("You are already logged."), _("If you want to logout:"), _("- click on your username"), _("- click on {logout}") ]) return HttpResponseRedirect(reverse_lazy('app_new_index')) return super().get(request, *args, **kwargs)
def form_valid(self, form): # Copy/paste of code at site-packages/django/contrib/auth/views.py form.save() # Updating the password logs out all other sessions for the user # except the current one. update_session_auth_hash(self.request, form.user) add_messages(self.request, _("Password reset!"), [_("Your password has been reset!"), _("You can login with your new password.")]) return super(PasswordResetView, self).form_valid(form)
def get(self, request, *args, **kwargs): try: rand_str = kwargs.get('rand_str', '-') p = Person.objects.get(confirmation_code=rand_str) if request.user.is_authenticated: if p.user != self.request.user: # already logged, but not the same user add_messages(self.request, _("Already logged in"), [ _("You were logged as another user"), _("You have been disconnected"), _("Please reconnect") ]) return logout_then_login(request) # came here = People found # - if already activated, login + redirect to index # if p.user.is_active: # not logged in, , but already activated: login( request, p.user, # manually precise backend otherwise conflicts: backend='django.contrib.auth.backends.ModelBackend') add_messages(self.request, _("Account activated!"), [ _("You have been automatically logged"), _("(your account was already activated)"), ]) else: # not logged in, People found and not activated: login( request, p.user, # manually precise backend otherwise conflicts: backend='django.contrib.auth.backends.ModelBackend') p.user.is_active = True p.user.save() add_messages(self.request, _("Account activated!"), [ _("Welcome to {}!").format(settings.WEBSITE_NAME), _("Congratulations!"), _("Your account has been activated!"), ]) except Person.DoesNotExist: # hack -> no message, nothing: return HttpResponseRedirect(reverse_lazy('auth_login')) return HttpResponseRedirect(reverse_lazy('app_new_index'))
def form_valid(self, form): add_messages(self.request, _("Password reset!"), [ _("Your password has been successfully reset."), _("You can now login with your new password.") ]) return super().form_valid(form)
def form_valid(self, form): user = None email = form.cleaned_data.get('email') if email: try: # Check the form.send_mail()! I should use it, but it's longer # than copy/paste my (working code) to send a mail: user = User.objects.get(email__iexact=email, is_active=True) if not user.has_usable_password(): user = None except User.DoesNotExist: pass if user is not None: p = user.person if p is None: add_messages(self.request, _("You're a social person!"), [ _("You registered here via a social network."), _("Please connect via a social network " "available here.") ]) return HttpResponseRedirect(reverse_lazy('auth_login')) # Person exists: p.reset_code = str(uuid.uuid4()) # generate random str p.save() # he registered via classical registration: # region - send_mail - # used a lot copy/paste. there's room for improvement here: site_name = self.request.META['HTTP_HOST'] site_web = '{}://{}'.format(self.request.scheme, site_name) email_message = EmailMessage( subject=_("Password reset"), body='{}\n{}\n{}\n\n{}\n{}\n\n{}'.format( _("You've asked to reset your password on %(site_name)s") % {'site_name': site_name.split(':')[0]}, _("Please go to the following page " "and choose a new password:"******"{}{}".format( site_web, reverse_lazy('auth_password_reset', kwargs={'rand_str': p.reset_code})), _("Thanks for using our site!"), _("See you soon on %(site_name)s") % {'site_name': site_name.split(':')[0]}, _("The %(site_name)s's team") % {'site_name': site_name.split(':')[0]}, ), from_email=f'contact@{settings.WEBSITE_NAME}.com', reply_to=[f'contact@{settings.WEBSITE_NAME}.com'], to=[form.cleaned_data['email']], bcc=['*****@*****.**'], ) # email_message.attach('design.png', img_data, 'image/png') email_message.send() # endregion add_messages(self.request, _("Email sent!"), [ _("A reset link has been sent"), _("(if your email is in our database).") ]) return super(ForgotPasswordView, self).form_valid(form)