python - Django deafult_token_generator creates token tha is almost immediately expired -
for set/reset password in django app use built-in django resetpasswordrequestview , resetpasswordconfirmview. them working fine. i've created function that's sending email user when administrator creates account (there no registration "common users" on site). here's code:
class resetpasswordrequestview(formview): template_name = "account/password_reset.html" #code template given below view's code success_url = '/account/reset_password' form_class = passwordresetrequestform @staticmethod def validate_email_address(email): """ method here validates if input email address or not. returns boolean. """ try: validate_email(email) return true except validationerror: return false def post(self, request, *args, **kwargs): """ normal post request takes input field "email_or_username" (in resetpasswordrequestform). """ form = self.form_class(request.post) if form.is_valid(): data= form.cleaned_data["email_or_username"] if self.validate_email_address(data) true: #uses method written above """ if input valid email address, following code lookup users associated email address. if found email sent address, else error message printed on screen. """ associated_users= user.objects.filter(q(email=data)|q(username=data)) if associated_users.exists(): user in associated_users: c = { 'email': user.email, 'domain': request.meta['http_host'], 'site_name': 'classplanner', 'uid': urlsafe_base64_encode(force_bytes(user.pk)), 'user': user, 'token': default_token_generator.make_token(user), 'protocol': 'http', } email_template_name='account/password_reset_email.html' # copied django/contrib/admin/templates/registration/password_reset_email.html templates directory # email subject *must not* contain newlines subject = _(u'password reset request classplanner account') email = loader.render_to_string(email_template_name, c) send_mail(subject, email, default_from_email , [user.email], fail_silently=false) result = self.form_valid(form) messages.success(request, _(u'an email has been sent given address. please check inbox continue resetting password.')) return result result = self.form_invalid(form) messages.error(request, _(u'no user associated email address')) return result else: """ if input username, following code lookup users associated user. if found email sent user's address, else error message printed on screen. """ associated_users= user.objects.filter(username=data) if associated_users.exists(): user in associated_users: c = { 'email': user.email, 'domain': request.meta['http_host'], 'site_name': 'classplanner', 'uid': urlsafe_base64_encode(force_bytes(user.pk)), 'user': user, 'token': default_token_generator.make_token(user), 'protocol': 'http', } email_template_name='account/password_reset_email.html' # email subject *must not* contain newlines subject = _(u'password reset request classplanner account') email = loader.render_to_string(email_template_name, c) send_mail(subject, email, default_from_email , [user.email], fail_silently=false) result = self.form_valid(form) messages.success(request, _(u'email has been sent user\'s email address. please check inbox continue resetting password.')) return result result = self.form_invalid(form) messages.error(request, _(u'this username not exist in system.')) return result messages.error(request, _(u'invalid input')) return self.form_invalid(form) class resetpasswordconfirmview(formview): template_name = "account/set_new_password.html" success_url = '/account/reset_password' form_class = setnewpasswordform def post(self, request, uidb64=none, token=none, *arg, **kwargs): """ view checks hash in password reset link , presents form entering new password. """ # usermodel = get_user_model() form = self.form_class(request.post) assert uidb64 not none , token not none # checked urlconf try: uid = urlsafe_base64_decode(uidb64) user = user.objects.get(pk=uid) except (typeerror, valueerror, overflowerror, usermodel.doesnotexist): user = none if user not none: print user, token if default_token_generator.check_token(user, token): if form.is_valid(): new_password= form.cleaned_data['new_password2'] user.set_password(new_password) user.save() messages.success(request, _(u'password has been reset.')) return self.form_valid(form) else: messages.error(request, _(u'password reset has been unsuccessful.')) return self.form_invalid(form) else: messages.error(request,_(u'the reset password link no longer valid.')) return self.form_invalid(form) else: messages.error(request, _(u'user not exists.')) return self.form_invalid(form) def sendresettingpasswordmail(user): c = { 'email': user.email, 'domain': hostname, 'site_name': 'classplanner', 'uid': urlsafe_base64_encode(force_bytes(user.pk)), 'user': user, 'token': default_token_generator.make_token(user), 'protocol': 'http', } email_template_name='account/new_account_created_mail.html' subject = _(u'creation of new classplanner account - please set password') email = loader.render_to_string(email_template_name, c) send_mail(subject, email, default_from_email , [user.email], fail_silently=true)
first 2 views works great, function (using same token generator) creates urls token, not valid. almost, because if check them inside function (right after creating , sending mail) valid, then, when try set password via url given in mail, , got 'the reset password link no longer valid.' in settings have password_reset_timeout_days = 1
shouldn't problem. ideas?
Comments
Post a Comment