Commit 1d378ade authored by Luis García's avatar Luis García
Browse files

Overwrite reset password email

parent d83e6d9c
SECRET_KEY=YOUR_SECRET_KEY
DEBUG=FALSE
ALLOWED_HOSTS=.YOUR_HOST, .YOUR_OTHER_HOST
FRONTEND_URL = localhost
DB_NAME=YOUR_DB_NAME
DB_USER=YOUR_DB_USER
DB_PASSWORD=YOUR_DB_PASSWORD
......
......@@ -202,6 +202,11 @@ REST_FRAMEWORK = {
),
}
REST_AUTH_SERIALIZERS = {
'PASSWORD_RESET_SERIALIZER':
'core.serializers.PasswordResetSerializer',
}
JWT_AUTH = {
'JWT_ENCODE_HANDLER':
'rest_framework_jwt.utils.jwt_encode_handler',
......
......@@ -28,6 +28,7 @@ from api.urls import api_patterns
#from pages.urls import pages_patterns
from django.views.i18n import JavaScriptCatalog
from registration.views import CustomActivationView
from rest_auth.views import PasswordResetConfirmView
from core.views.social_auth import FacebookLogin, TwitterLogin, FacebookConnect, TwitterConnect
......@@ -55,6 +56,9 @@ urlpatterns += [
url(r'^token-verify/', verify_jwt_token),
url(r'^oauth/', include('social_django.urls', namespace='social')),
url(r'^rest-auth/', include('rest_auth.urls')),
url(r'^reset-password/confirm/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})$',
PasswordResetConfirmView.as_view(),
name='password_reset_confirm'),
url(r'^rest-auth/facebook/$', FacebookLogin.as_view(), name='fb_login'),
url(r'^rest-auth/twitter/$', TwitterLogin.as_view(), name='twitter_login'),
url(r'^rest-auth/facebook/connect/$', FacebookConnect.as_view(), name='fb_connect'),
......
import unicodedata
from django import forms
from django.contrib.auth import (
authenticate, get_user_model, password_validation,
)
from django.contrib.auth.hashers import (
UNUSABLE_PASSWORD_PREFIX, identify_hasher,
)
from django.contrib.auth.models import User
from django.contrib.auth.tokens import default_token_generator
from django.contrib.sites.shortcuts import get_current_site
from django.core.mail import EmailMultiAlternatives
from django.template import loader
from django.utils.encoding import force_bytes
from django.utils.http import urlsafe_base64_encode
from django.utils.text import capfirst
from django.utils.translation import gettext, gettext_lazy as _
from decouple import config
UserModel = get_user_model()
class PasswordResetForm(forms.Form):
email = forms.EmailField(label=_("Email"), max_length=254)
def send_mail(self, subject_template_name, email_template_name,
context, from_email, to_email, html_email_template_name=None):
"""
Send a django.core.mail.EmailMultiAlternatives to `to_email`.
"""
subject = loader.render_to_string(subject_template_name, context)
# Email subject *must not* contain newlines
subject = ''.join(subject.splitlines())
body = loader.render_to_string(email_template_name, context)
email_message = EmailMultiAlternatives(subject, body, from_email, [to_email])
if html_email_template_name is not None:
html_email = loader.render_to_string(html_email_template_name, context)
email_message.attach_alternative(html_email, 'text/html')
email_message.send()
def get_users(self, email):
"""Given an email, return matching user(s) who should receive a reset.
This allows subclasses to more easily customize the default policies
that prevent inactive users and users with unusable passwords from
resetting their password.
"""
active_users = UserModel._default_manager.filter(**{
'%s__iexact' % UserModel.get_email_field_name(): email,
'is_active': True,
})
return (u for u in active_users if u.has_usable_password())
def save(self, domain_override=None,
subject_template_name='registration/password_reset_subject.txt',
email_template_name='registration/password_reset_email.html',
use_https=False, token_generator=default_token_generator,
from_email=None, request=None, html_email_template_name=None,
extra_email_context=None):
"""
Generate a one-use only link for resetting password and send it to the
user.
"""
email = self.cleaned_data["email"]
for user in self.get_users(email):
if not domain_override:
current_site = get_current_site(request)
site_name = current_site.name
domain = current_site.domain
else:
site_name = domain = domain_override
context = {
'email': email,
'domain': config('FRONTEND_URL'),
'site_name': site_name,
'uid': urlsafe_base64_encode(force_bytes(user.pk)).decode(),
'user': user,
'token': token_generator.make_token(user),
'protocol': 'https' if use_https else 'http',
**(extra_email_context or {}),
}
self.send_mail(
subject_template_name, email_template_name, context, from_email,
email, html_email_template_name=html_email_template_name,
)
\ No newline at end of file
from django.contrib.auth.models import User
from django.contrib.auth.hashers import make_password
from core.forms import PasswordResetForm
from django.conf import settings
from django.utils.translation import gettext as _
from rest_framework import serializers
from drf_extra_fields.geo_fields import PointField
from core.models import Profile, Map
......@@ -72,3 +75,28 @@ class MapSerializer(serializers.ModelSerializer):
class Meta:
model = Map
fields = ('id', 'name', 'file')
class PasswordResetSerializer(serializers.Serializer):
email = serializers.EmailField()
password_reset_form_class = PasswordResetForm
def validate_email(self, value):
self.reset_form = self.password_reset_form_class(data=self.initial_data)
if not self.reset_form.is_valid():
raise serializers.ValidationError(_('Error'))
if not User.objects.filter(email=value).exists():
raise serializers.ValidationError(_('Invalid e-mail address'))
return value
def save(self):
request = self.context.get('request')
opts = {
'use_https': request.is_secure(),
'from_email': getattr(settings, 'DEFAULT_FROM_EMAIL'),
'email_template_name': 'auth/password_reset_email.html',
'request': request,
}
self.reset_form.save(**opts)
\ No newline at end of file
{% load i18n %}{% autoescape off %}
{% blocktrans %}You're receiving this email because you requested a password reset
for your user account at {{ site_name }}.{% endblocktrans %}
{% trans "Please go to the following page and choose a new password:" %}
{% block reset_link %}
{{ protocol }}://paraguay.collabmap.in{% url 'password_reset_confirm' uidb64=uid token=token %}
{% endblock %}
{% trans "Your username, in case you've forgotten:" %} {{ user.get_username }}
{% trans "Thanks for using our site!" %}
{% blocktrans %}The {{ site_name }} team{% endblocktrans %}
{% endautoescape %}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment