Package: django2-rest-passwordreset
Django Rest Password ResetThis python package provides a simple password reset strategy for django rest framework, where users can request password
reset tokens via their registered e-mail address.The main idea behind this package is to not make any assumptions about how the token is delivered to the end-user (e-mail, text-message, etc...).
Instead, this package provides a signal that can be reacted on (e.g., by sending an e-mail or a text message).This package basically provides two REST endpoints:Request a tokenVerify (confirm) a token (and change the password)QuickstartInstall the package from pypi using pip:pipinstalldjango-rest-passwordresetAdddjango_rest_passwordresetto yourINSTALLED_APPS(afterrest_framework) within your Django settings file:INSTALLED_APPS=(...'django.contrib.auth',...'rest_framework',...'django_rest_passwordreset',...)This package provides two endpoints, which can be included by includingdjango_rest_passwordreset.urlsin yoururls.pyas follows:fromdjango.conf.urlsimporturl,includeurlpatterns=[...url(r'^api/password_reset/',include('django_rest_passwordreset.urls',namespace='password_reset')),...]Note: You can adapt the url to your needs.EndpointsThe following endpoints are provided:reset_password- request a reset password token by using theemailparameterreset_password/confirm- using a validtoken, the users password is set to the providedpasswordSignalsreset_password_token_created(reset_password_token)Fired when a reset password token is generatedpre_password_reset(user)- fired just before a password is being resetpost_password_reset(user)- fired after a password has been resetExample for sending an e-mailCreate two new django templates:email/user_reset_password.htmlandemail/user_reset_password.txt. Those templates will contain the e-mail message sent to the user, aswell as the password reset link (or token).
Within the templates, you can access the following context variables:current_user,username,email,reset_password_url. Feel free to adapt this to your needs.Add the following code, which contains a Django Signal, to your application (seethis part of the django documentationfor more information on where to put signals).fromdjango.dispatchimportreceiverfromdjango_rest_passwordreset.signalsimportreset_password_token_created@receiver(reset_password_token_created)defpassword_reset_token_created(sender,reset_password_token,*args,**kwargs):"""Handles password reset tokensWhen a token is created, an e-mail needs to be sent to the user:param sender::param reset_password_token::param args::param kwargs::return:"""# send an e-mail to the usercontext={'current_user':reset_password_token.user,'username':reset_password_token.user.username,'email':reset_password_token.user.email,# ToDo: The URL can (and should) be constructed using pythons built-in `reverse` method.'reset_password_url':"http://some_url/reset/?token={token}".format(token=reset_password_token.key)}# render email textemail_html_message=render_to_string('email/user_reset_password.html',context)email_plaintext_message=render_to_string('email/user_reset_password.txt',context)msg=EmailMultiAlternatives(# title:_("Password Reset for{title}".format(title="Some website title")),# message:email_plaintext_message,# from:"noreply@somehost.local",# to:[reset_password_token.user.email])msg.attach_alternative(email_html_message,"text/html")msg.send()You should now be able to use the endpoints to request a password reset token via your e-mail address.
If you want to test this locally, I recommend using some kind of fake mailserver (such as maildump).TestsSee foldertests/. Basically, all endpoints are covered with multiple
unit tests.Use this code snippet to run tests:pipinstall-rrequirements_test.txt
pythonsetup.pyinstallcdtests
pythonmanage.pytest