def save(self, **kwargs): email = self.cleaned_data["email"] token_generator = kwargs.get("token_generator", default_token_generator) for user in User.objects.filter(email__iexact=email): temp_key = token_generator.make_token(user) # save it to the password reset model password_reset = PasswordReset(user=user, temp_key=temp_key) password_reset.save() current_site = Site.objects.get_current() domain = unicode(current_site.domain) # send the password reset email subject = _("Password reset email sent") message = render_to_string("account/password_reset_key_message.txt", { "user": user, "uid": int_to_base36(user.id), "temp_key": temp_key, "domain": domain, }) send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [user.email]) return self.cleaned_data["email"]
def save(self, **kwargs): email = self.cleaned_data["email"] token_generator = kwargs.get("token_generator", default_token_generator) for user in User.objects.filter(email__iexact=email): temp_key = token_generator.make_token(user) # save it to the password reset model password_reset = PasswordReset(user=user, temp_key=temp_key) password_reset.save() current_site = Site.objects.get_current() domain = unicode(current_site.domain) # send the password reset email subject = _("Password reset e-mail sent") message = render_to_string( "account/password_reset_key_message.txt", { "user": user, "uid": int_to_base36(user.id), "temp_key": temp_key, "domain": domain, }) send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [user.email]) return self.cleaned_data["email"]
def save(self): for user in User.objects.filter( email__iexact=self.cleaned_data["email"]): temp_key = sha_constructor("%s%s%s" % ( settings.SECRET_KEY, user.email, settings.SECRET_KEY, )).hexdigest() # save it to the password reset model password_reset = PasswordReset(user=user, temp_key=temp_key) password_reset.save() current_site = Site.objects.get_current() domain = unicode(current_site.domain) #send the password reset email subject = _(u"Email de redefinição de senha enviado") message = render_to_string( "account/password_reset_key_message.txt", { "user": user, "temp_key": temp_key, "domain": domain, }) send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [user.email], priority="high") return self.cleaned_data["email"]
def save(self, domain): for user in User.objects.filter(email_address__iexact=self.cleaned_data["email"]): temp_key = sha_constructor("%s%s%s" % ( settings.SECRET_KEY, user.email, settings.SECRET_KEY, )).hexdigest() # save it to the password reset model try: password_reset = PasswordReset.objects.get(user=user,temp_key=temp_key, reset=False) except PasswordReset.DoesNotExist: password_reset = PasswordReset(user=user, temp_key=temp_key) password_reset.save() #send the password reset email subject = _("Password reset email sent") link = 'http://'+domain+reverse('acct_passwd_reset_key',args=(temp_key,)) message = _(settings.PASSWORD_RESET_TEMPLATE) % { "display_name" : user.get_display_name(), "username": user.username, "link" : link, } send_mail(subject, message, settings.SUPPORT_EMAIL, [user.email_address],fail_silently=False) return self.cleaned_data["email"]
def save(self): for user in User.objects.filter(email__iexact=self.cleaned_data["email"]): temp_key = sha_constructor("%s%s%s" % ( settings.SECRET_KEY, user.email, settings.SECRET_KEY, )).hexdigest() # save it to the password reset model password_reset = PasswordReset(user=user, temp_key=temp_key) password_reset.save() current_site = Site.objects.get_current() domain = unicode(current_site.domain) # send the password reset email subject = _("Password reset email sent") message = render_to_string("account/password_reset_key_message.txt", { "user": user, "temp_key": temp_key, "domain": domain, }) send_mail( subject, message, settings.DEFAULT_FROM_EMAIL, [user.email], priority="high") return self.cleaned_data["email"]
def save(self, domain): for user in User.objects.filter( email_address__iexact=self.cleaned_data["email"]): temp_key = sha_constructor("%s%s%s" % ( settings.SECRET_KEY, user.email, settings.SECRET_KEY, )).hexdigest() # save it to the password reset model try: password_reset = PasswordReset.objects.get(user=user, temp_key=temp_key, reset=False) except PasswordReset.DoesNotExist: password_reset = PasswordReset(user=user, temp_key=temp_key) password_reset.save() #send the password reset email subject = _("Password reset email sent") link = 'http://' + domain + reverse('acct_passwd_reset_key', args=(temp_key, )) message = _(settings.PASSWORD_RESET_TEMPLATE) % { "display_name": user.get_display_name(), "username": user.username, "link": link, } send_mail(subject, message, settings.SUPPORT_EMAIL, [user.email_address], fail_silently=False) return self.cleaned_data["email"]
def save(self): for user in User.objects.filter(email__iexact=self.cleaned_data["email"]): temp_key = sha_constructor("%s%s%s" % ( settings.SECRET_KEY, user.email, settings.SECRET_KEY, )).hexdigest() domain = unicode(settings.ACTIVE_SITE) context = { "user": user, "temp_key": temp_key, "domain": domain, } # save it to the password reset model password_reset = PasswordReset(user=user, temp_key=temp_key) password_reset.save() subject = "Password Reset Request" message_txt = render_to_string("account/password_reset_key_message.txt", context) message_html = render_to_string("account/password_reset_key_message.html", context) message = EmailMultiAlternatives(subject, message_txt, settings.DEFAULT_FROM_EMAIL, [user.email[0]]) message.attach_alternative(message_html, "text/html") message.send() #send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [user.email], priority="high") return self.cleaned_data["email"]
def save(self, **kwargs): email = self.cleaned_data["email"] token_generator = kwargs.get("token_generator", default_token_generator) for user in User.objects.filter(email__iexact=email): temp_key = token_generator.make_token(user) # save it to the password reset model password_reset = PasswordReset(user=user, temp_key=temp_key) password_reset.save() current_site = Site.objects.get_current() domain = unicode(current_site.domain) # send the password reset email subject = settings.PASSWD_RESET_SUBJECT message = render_to_string(settings.PWD_RESET_MSG, { "user": user, "uid": int_to_base36(user.id), "temp_key": temp_key, "domain": domain, }) self.send_reset_email(subject, message, user.email) # launch a new thread to send email, don't let users wait for so long, by junn #startEmailSendThread(subject, message, settings.DEFAULT_FROM_EMAIL, [user.email]) return self.cleaned_data["email"]
def save(self): for user in User.objects.filter(email__iexact=self.cleaned_data["email"]): # make a random password so this account can't be accessed. new_password = User.objects.make_random_password() #user.set_password(new_password) #user.save() # Make the temp key by generating another random password. temp_key = User.objects.make_random_password() # save it to the password reset model password_reset = PasswordReset(user=user,temp_key=temp_key) password_reset.save() #send the password reset email subject = _("Password reset email sent") current_site = Site.objects.get(id=settings.SITE_ID) message = render_to_string("account/password_reset_key_message.txt", { "user": user, "temp_key": temp_key, "domain" : current_site.domain, }) send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [user.email], priority="high") return self.cleaned_data["email"]
def save(self): user = User.objects.get(email=self.cleaned_data['email']) key = generate_hash_key(user.username) reset = PasswordReset(key=key, user=user) reset.save() template_name = 'account/password_reset_mail.html' subject = 'Criar nova senha de acesso para o Active Gears' context = {'reset': reset} send_mail_template(subject, template_name, context, [user.email])
def form_valid(self, form): PasswordReset.create(form.user) response_kwargs = { 'request': self.request, 'template': self.template_name_sent, 'context': { 'email': form.user.email, } } html = self.response_class(**response_kwargs) html.render() return JsonResponse(status=1, data={'html': html.content})
def form_valid(self, form): PasswordReset.create(form.user) if self.request.is_ajax(): #t = get_template(self.template_name_sent_ajax) #t.render(RequestContext(self.request, # self.get_context_data(form=form))) return JsonResponse(status=1, msg=u'重置密码邮件已发送至该邮箱。') response_kwargs = { 'request': self.request, 'template': self.template_name_sent, 'context': self.get_context_data(form=form) } self.template_name = self.template_name_sent return self.response_class(**response_kwargs)
def test_pwdrst_process(db, account_objects): user = account_objects.user assert authenticate(username=user.username, password="******") pwdrst = PasswordReset.start(user) pwdrst.complete("newpassword") assert authenticate(username=user.username, password="******") assert pwdrst.id is None
def reset_password_instance(request): if request.method == 'POST': fromEmail = "*****@*****.**" toEmail = request.POST.get('email') try: reset_instance = PasswordReset.objects.get(email=toEmail, done=False) return HttpResponse(simplejson.dumps({'message':"There is a request. Please check again!"})) except PasswordReset.DoesNotExist: msg = MIMEMultipart('alternative') msg['Subject'] = "Outclan - reset password" msg['From'] = fromEmail msg['To'] = toEmail salt = sha_constructor(str(random.random())).hexdigest()[:5] token = sha_constructor(salt + toEmail).hexdigest() PasswordReset(email=toEmail, token=token).save() link = "http://www.outclan.com/password/"+token text = "Hi!\n"+link+"\n" html = """\ <html> <head></head> <body> <p>Hi!<br> """+link+"""<br> </p> </body> </html> """ username = '******' password = "******" part1 = MIMEText(text, 'plain') part2 = MIMEText(html, 'html') msg.attach(part1) msg.attach(part2) s = smtplib.SMTP('smtp.sendgrid.net', 587) s.login(username, password) s.sendmail(fromEmail, toEmail, msg.as_string()) s.quit() return HttpResponse(simplejson.dumps({'message':"An email has been sent to you!"})) return HttpResponse('Not here!')
def reset_password(request, key=None): context = {} if request.method == "POST" and request.POST.get("email", None): if request.user.is_authenticated(): return HttpResponseRedirect(reverse("conference_schedule")) email = request.POST.get("email", None) if User.objects.filter(email__iexact=email).count(): context["email"] = email else: context["email_error"] = email for user in User.objects.filter(email__iexact=email): temp_key = sha_constructor("%s%s%s" % (settings.SECRET_KEY, user.email, settings.SECRET_KEY)).hexdigest() # save it to the password reset model password_reset = PasswordReset(user=user, temp_key=temp_key) password_reset.save() current_site = Site.objects.get_current() domain = unicode(current_site.domain) # send the password reset email subject = "myEWB password reset" message = render_to_string( "conference/schedule/password_reset_message.txt", {"user": user, "temp_key": temp_key, "domain": domain} ) send_mail( subject=subject, txtMessage=message, fromemail=settings.DEFAULT_FROM_EMAIL, recipients=[user.email], priority="high", ) elif key: if PasswordReset.objects.filter(temp_key__exact=key, reset=False).count(): if request.method == "POST": form = ResetPasswordKeyForm(request.POST) if form.is_valid(): # get the password_reset object temp_key = form.cleaned_data.get("temp_key") password_reset = PasswordReset.objects.filter(temp_key__exact=temp_key, reset=False) password_reset = password_reset[0] # should always be safe, as form_clean checks this # now set the new user password user = User.objects.get(passwordreset__exact=password_reset) result = user.set_password(form.cleaned_data["password1"]) if not result: # unsuccessful form._errors[forms.forms.NON_FIELD_ERRORS] = ["Error (password is too simple maybe?)"] else: user.save() # change all the password reset records to this person to be true. for password_reset in PasswordReset.objects.filter(user=user): password_reset.reset = True password_reset.save() user = auth.authenticate(username=user.username, password=form.cleaned_data["password1"]) auth.login(request, user) return HttpResponseRedirect(reverse("conference_schedule")) else: form = ResetPasswordKeyForm(initial={"temp_key": key}) context["keyvalid"] = True context["form"] = form else: context["keyerror"] = True else: return HttpResponseRedirect(reverse("conference_schedule_login")) return render_to_response("conference/schedule/reset.html", context, context_instance=RequestContext(request))
def reset_password(request, key=None): context = {} if request.method == 'POST' and request.POST.get('email', None): if request.user.is_authenticated(): return HttpResponseRedirect(reverse('conference_schedule')) email = request.POST.get('email', None) if User.objects.filter(email__iexact=email).count(): context['email'] = email else: context['email_error'] = email for user in User.objects.filter(email__iexact=email): temp_key = sha_constructor("%s%s%s" % ( settings.SECRET_KEY, user.email, settings.SECRET_KEY, )).hexdigest() # save it to the password reset model password_reset = PasswordReset(user=user, temp_key=temp_key) password_reset.save() current_site = Site.objects.get_current() domain = unicode(current_site.domain) #send the password reset email subject = "myEWB password reset" message = render_to_string( "conference/schedule/password_reset_message.txt", { "user": user, "temp_key": temp_key, "domain": domain, }) send_mail(subject=subject, txtMessage=message, fromemail=settings.DEFAULT_FROM_EMAIL, recipients=[user.email], priority="high") elif key: if PasswordReset.objects.filter(temp_key__exact=key, reset=False).count(): if request.method == 'POST': form = ResetPasswordKeyForm(request.POST) if form.is_valid(): # get the password_reset object temp_key = form.cleaned_data.get("temp_key") password_reset = PasswordReset.objects.filter( temp_key__exact=temp_key, reset=False) password_reset = password_reset[ 0] # should always be safe, as form_clean checks this # now set the new user password user = User.objects.get( passwordreset__exact=password_reset) result = user.set_password(form.cleaned_data['password1']) if not result: # unsuccessful form._errors[forms.forms.NON_FIELD_ERRORS] = [ "Error (password is too simple maybe?)" ] else: user.save() # change all the password reset records to this person to be true. for password_reset in PasswordReset.objects.filter( user=user): password_reset.reset = True password_reset.save() user = auth.authenticate( username=user.username, password=form.cleaned_data['password1']) auth.login(request, user) return HttpResponseRedirect( reverse('conference_schedule')) else: form = ResetPasswordKeyForm(initial={'temp_key': key}) context['keyvalid'] = True context['form'] = form else: context['keyerror'] = True else: return HttpResponseRedirect(reverse('conference_schedule_login')) return render_to_response("conference/schedule/reset.html", context, context_instance=RequestContext(request))