From fc226bd5df7a48b872163bc03fb55a51999dfd7b Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Wed, 18 Oct 2023 17:30:11 +0200 Subject: [PATCH] send email for put your password --- idhub/admin/views.py | 14 +- idhub/email/__init__.py | 0 idhub/email/views.py | 55 +++++ idhub/templates/auth/login.html | 204 +++++------------- idhub/templates/auth/login_base.html | 114 ++++++++++ idhub/templates/auth/password_reset.html | 27 +++ .../auth/password_reset_complete.html | 16 ++ .../auth/password_reset_confirm.html | 36 ++++ idhub/templates/auth/password_reset_done.html | 15 ++ .../registration/activate_user_email.html | 31 +++ .../registration/activate_user_email.txt | 19 ++ .../registration/activate_user_subject.txt | 4 + .../registration/password_reset_email.html | 30 +++ .../registration/password_reset_email.txt | 14 ++ .../registration/password_reset_subject.txt | 3 + idhub/urls.py | 29 +++ trustchain_idhub/settings.py | 2 + 17 files changed, 456 insertions(+), 157 deletions(-) create mode 100644 idhub/email/__init__.py create mode 100644 idhub/email/views.py create mode 100644 idhub/templates/auth/login_base.html create mode 100644 idhub/templates/auth/password_reset.html create mode 100644 idhub/templates/auth/password_reset_complete.html create mode 100644 idhub/templates/auth/password_reset_confirm.html create mode 100644 idhub/templates/auth/password_reset_done.html create mode 100644 idhub/templates/idhub/admin/registration/activate_user_email.html create mode 100644 idhub/templates/idhub/admin/registration/activate_user_email.txt create mode 100644 idhub/templates/idhub/admin/registration/activate_user_subject.txt create mode 100644 idhub/templates/idhub/admin/registration/password_reset_email.html create mode 100644 idhub/templates/idhub/admin/registration/password_reset_email.txt create mode 100644 idhub/templates/idhub/admin/registration/password_reset_subject.txt diff --git a/idhub/admin/views.py b/idhub/admin/views.py index 0e6f122..13031b0 100644 --- a/idhub/admin/views.py +++ b/idhub/admin/views.py @@ -1,4 +1,5 @@ import logging +from smtplib import SMTPException from django.utils.translation import gettext_lazy as _ from django.views.generic.base import TemplateView @@ -9,6 +10,7 @@ from django.urls import reverse_lazy from django.contrib import messages from idhub.models import Membership, Rol, Service, UserRol from idhub.mixins import AdminView +from idhub.email.views import NotifyActivateUserByEmail from idhub.admin.forms import ( ProfileForm, MembershipForm, @@ -121,7 +123,7 @@ class AdminPeopleEditView(AdminPeopleView, UpdateView): success_url = reverse_lazy('idhub:admin_people_list') -class AdminPeopleRegisterView(People, CreateView): +class AdminPeopleRegisterView(NotifyActivateUserByEmail, People, CreateView): template_name = "idhub/admin/people_register.html" subtitle = _('People Register') icon = 'bi bi-person' @@ -137,6 +139,16 @@ class AdminPeopleRegisterView(People, CreateView): ) return self.success_url + def form_valid(self, form): + user = form.save() + messages.success(self.request, _('The account is created successfully')) + if user.is_active: + try: + self.send_email(user) + except SMTPException as e: + messages.error(self.request, e) + return super().form_valid(form) + class AdminPeopleMembershipRegisterView(People, CreateView): template_name = "idhub/admin/people_membership_register.html" diff --git a/idhub/email/__init__.py b/idhub/email/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/idhub/email/views.py b/idhub/email/views.py new file mode 100644 index 0000000..ae73ebf --- /dev/null +++ b/idhub/email/views.py @@ -0,0 +1,55 @@ +from django.conf import settings +from django.template import loader +from django.core.mail import EmailMultiAlternatives +from django.contrib.auth.tokens import default_token_generator +from django.contrib.sites.shortcuts import get_current_site +from django.utils.encoding import force_bytes +from django.utils.http import urlsafe_base64_encode + + +class NotifyActivateUserByEmail: + def get_email_context(self, user): + """ + Define a new context with a token for put in a email + when send a email for add a new password + """ + protocol = 'https' if self.request.is_secure() else 'http' + current_site = get_current_site(self.request) + site_name = current_site.name + domain = current_site.domain + context = { + 'email': user.email, + 'domain': domain, + 'site_name': site_name, + 'uid': urlsafe_base64_encode(force_bytes(user.pk)), + 'user': user, + 'token': default_token_generator.make_token(user), + 'protocol': protocol, + } + return context + + def send_email(self, user): + """ + Send a email when a user is activated. + """ + context = self.get_email_context(user) + subject_template_name = 'idhub/admin/registration/activate_user_subject.txt' + email_template_name = 'idhub/admin/registration/activate_user_email.txt' + html_email_template_name = 'idhub/admin/registration/activate_user_email.html' + 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) + from_email = settings.DEFAULT_FROM_EMAIL + to_email = user.email + + email_message = EmailMultiAlternatives( + subject, body, from_email, [to_email]) + html_email = loader.render_to_string(html_email_template_name, context) + email_message.attach_alternative(html_email, 'text/html') + if settings.DEVELOPMENT: + print(to_email) + print(body) + return + + email_message.send() diff --git a/idhub/templates/auth/login.html b/idhub/templates/auth/login.html index 6f43260..28c199e 100644 --- a/idhub/templates/auth/login.html +++ b/idhub/templates/auth/login.html @@ -1,162 +1,54 @@ - +{% extends "auth/login_base.html" %} {% load i18n static %} - - - - {% block head %} - {% block meta %} - - - - - - {% endblock %} - {% block title %}{% if title %}{{ title }} – {% endif %}IdHub{% endblock %} +{% block login_content %} +
+ {% csrf_token %} + - - {% block style %} - - - - - - - - - - {% endblock %} - {% endblock %} - - - - -
-
-
- {% block messages %} - {% for message in messages %} - - {% endfor %} - {% endblock messages %} - -
-
-
-
- {% block branding %} -

- Pangea.org - Internet etic i solidari -

- {% endblock %} -
-
- - {% csrf_token %} - - -
-
- - {% if form.username.errors %} -

- {{ form.username.errors|striptags }} -

- {% endif %} -
-
- -
-
- - {% if form.password.errors %} -

- {{ form.password.errors|striptags }} -

- {% endif %} -
-
- - {% if form.non_field_errors %} - {% for error in form.non_field_errors %} -
{{ error }}
- {% endfor %} - {% endif %} - - - -
- -
- -
-
- - -
-
- - -
-
- - - - - - +
+
+ + {% if form.password.errors %} +

+ {{ form.password.errors|striptags }} +

+ {% endif %} +
+
+ + {% if form.non_field_errors %} + {% for error in form.non_field_errors %} +
{{ error }}
+ {% endfor %} + {% endif %} + + + +
+ +
+ + +{% endblock %} diff --git a/idhub/templates/auth/login_base.html b/idhub/templates/auth/login_base.html new file mode 100644 index 0000000..c7c5db1 --- /dev/null +++ b/idhub/templates/auth/login_base.html @@ -0,0 +1,114 @@ +{% load i18n static %} + + + + + {% block head %} + {% block meta %} + + + + + + {% endblock %} + {% block title %}{% if title %}{{ title }} – {% endif %}IdHub{% endblock %} + + + {% block style %} + + + + + + + + + + {% endblock %} + {% endblock %} + + + + +
+
+
+ {% block messages %} + {% for message in messages %} + + {% endfor %} + {% endblock messages %} + +
+
+
+
+ {% block branding %} +

+ Pangea.org - Internet etic i solidari +

+ {% endblock %} +
+
+ {% block login_content %} + {% endblock %} +
+
+ +
+
+ + + + +
+
+
+ + + + + + + diff --git a/idhub/templates/auth/password_reset.html b/idhub/templates/auth/password_reset.html new file mode 100644 index 0000000..5555c06 --- /dev/null +++ b/idhub/templates/auth/password_reset.html @@ -0,0 +1,27 @@ +{% extends "auth/login_base.html" %} +{% load i18n django_bootstrap5 %} + +{% block login_content %} + +
+
+

{% trans 'Password reset' %}

+ {% trans "Forgotten your password? Enter your email address below, and we'll email instructions for setting a new one." %} +
+
+ +
+
+
+
+ {% csrf_token %} + {% bootstrap_form form %} + {% bootstrap_form_errors form type='non_fields' %} +
+ +
+
+
+
+
+{% endblock %} diff --git a/idhub/templates/auth/password_reset_complete.html b/idhub/templates/auth/password_reset_complete.html new file mode 100644 index 0000000..51ad149 --- /dev/null +++ b/idhub/templates/auth/password_reset_complete.html @@ -0,0 +1,16 @@ +{% extends "auth/login_base.html" %} +{% load i18n %} + +{% block login_content %} +
+
+
+
+

{% trans 'Password reset complete' %}

+

{% trans 'Your password has been set. You may go ahead and log in now.' %}

+ {% trans 'Login' %} +
+
+
+
+{% endblock %} diff --git a/idhub/templates/auth/password_reset_confirm.html b/idhub/templates/auth/password_reset_confirm.html new file mode 100644 index 0000000..30b103e --- /dev/null +++ b/idhub/templates/auth/password_reset_confirm.html @@ -0,0 +1,36 @@ +{% extends "auth/login_base.html" %} +{% load i18n django_bootstrap5 %} + +{% block login_content %} +
+
+ {% if validlink %} +
+

{% trans 'Enter new password' %}

+

{% trans 'Please enter your new password twice so we can verify you typed it in correctly.' %}

+
+ +
+
+
+
+ {% csrf_token %} + {% bootstrap_form form %} + {% bootstrap_form_errors form type='non_fields' %} +
+ +
+
+
+
+
+ {% else %} +
+

{% trans 'Password reset unsuccessful' %}

+

{% trans 'The password reset link was invalid, possibly because it has already been used.' %}
+ {% trans 'Please request a new password reset.' %}

+
+ {% endif %} +
+
+{% endblock %} diff --git a/idhub/templates/auth/password_reset_done.html b/idhub/templates/auth/password_reset_done.html new file mode 100644 index 0000000..1b18de7 --- /dev/null +++ b/idhub/templates/auth/password_reset_done.html @@ -0,0 +1,15 @@ +{% extends "auth/login_base.html" %} +{% load i18n %} + +{% block login_content %} +
+
+

{% trans 'Password reset sent' %}

+ +

{% trans "We've emailed you instructions for setting your password, if an account exists with the email you entered. You should receive them shortly." %}

+ +

{% trans "If you don't receive an email, please make sure you've entered the address you registered with, and check your spam folder." %}

+ +
+
+{% endblock %} diff --git a/idhub/templates/idhub/admin/registration/activate_user_email.html b/idhub/templates/idhub/admin/registration/activate_user_email.html new file mode 100644 index 0000000..54370b8 --- /dev/null +++ b/idhub/templates/idhub/admin/registration/activate_user_email.html @@ -0,0 +1,31 @@ +{% load i18n %}{% autoescape off %} +{% trans "IdHub" as site %} +

+ {% blocktrans %}You're receiving this email because your user account at {{site}} has been activated.{% endblocktrans %} +

+ +

+{% trans "Your username is:" %} {{ user.username }} +

+ +

+{% trans "Please go to the following page and choose a password:" %} +

+ +

+{% block reset_link %} + +{{ protocol }}://{{ domain }}{% url 'idhub:password_reset_confirm' uidb64=uid token=token %} + +{% endblock %} +

+ +

+{% trans "Thanks for using our site!" %} +

+ +

+{% blocktrans %}The {{site}} team{% endblocktrans %} +

+ +{% endautoescape %} diff --git a/idhub/templates/idhub/admin/registration/activate_user_email.txt b/idhub/templates/idhub/admin/registration/activate_user_email.txt new file mode 100644 index 0000000..e3204b1 --- /dev/null +++ b/idhub/templates/idhub/admin/registration/activate_user_email.txt @@ -0,0 +1,19 @@ +{% load i18n %}{% autoescape off %} + +{% trans "Idhub" as site %} + +{% blocktrans %}You're receiving this email because your user account at {{site}} has been activated.{% endblocktrans %} + +{% trans "Your username is:" %} {{ user.username }} + +{% trans "Please go to the following page and choose a password:" %} +{% block reset_link %} +{{ protocol }}://{{ domain }}{% url 'idhub:password_reset_confirm' uidb64=uid token=token %} +{% endblock %} + + +{% trans "Thanks for using our site!" %} + +{% blocktrans %}The {{site}} team{% endblocktrans %} + +{% endautoescape %} diff --git a/idhub/templates/idhub/admin/registration/activate_user_subject.txt b/idhub/templates/idhub/admin/registration/activate_user_subject.txt new file mode 100644 index 0000000..d28a515 --- /dev/null +++ b/idhub/templates/idhub/admin/registration/activate_user_subject.txt @@ -0,0 +1,4 @@ +{% load i18n %}{% autoescape off %} +{% trans "IdHub" as site %} +{% blocktrans %}User activation on {{site}}{% endblocktrans %} +{% endautoescape %} diff --git a/idhub/templates/idhub/admin/registration/password_reset_email.html b/idhub/templates/idhub/admin/registration/password_reset_email.html new file mode 100644 index 0000000..062025a --- /dev/null +++ b/idhub/templates/idhub/admin/registration/password_reset_email.html @@ -0,0 +1,30 @@ +{% 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 }}://{{ domain }}{% url 'idhub:password_reset_confirm' uidb64=uid token=token %} + +{% endblock %} +

+ +

+{% trans "Your username, in case you've forgotten:" %} {{ user.username }} +

+ +

+{% trans "Thanks for using our site!" %} +

+ +

+{% blocktrans %}The {{ site_name }} team{% endblocktrans %} +

+ +{% endautoescape %} diff --git a/idhub/templates/idhub/admin/registration/password_reset_email.txt b/idhub/templates/idhub/admin/registration/password_reset_email.txt new file mode 100644 index 0000000..a4313fe --- /dev/null +++ b/idhub/templates/idhub/admin/registration/password_reset_email.txt @@ -0,0 +1,14 @@ +{% 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 }}://{{ domain }}{% url 'idhub:password_reset_confirm' uidb64=uid token=token %} +{% endblock %} +{% trans "Your username, in case you've forgotten:" %} {{ user.username }} + +{% trans "Thanks for using our site!" %} + +{% blocktrans %}The {{ site_name }} team{% endblocktrans %} + +{% endautoescape %} diff --git a/idhub/templates/idhub/admin/registration/password_reset_subject.txt b/idhub/templates/idhub/admin/registration/password_reset_subject.txt new file mode 100644 index 0000000..45a354b --- /dev/null +++ b/idhub/templates/idhub/admin/registration/password_reset_subject.txt @@ -0,0 +1,3 @@ +{% load i18n %}{% autoescape off %} +{% blocktrans %}Password reset on {{ site_name }}{% endblocktrans %} +{% endautoescape %} \ No newline at end of file diff --git a/idhub/urls.py b/idhub/urls.py index 02f7625..61e8e99 100644 --- a/idhub/urls.py +++ b/idhub/urls.py @@ -28,6 +28,35 @@ urlpatterns = [ permanent=False)), path('login/', LoginView.as_view(), name='login'), path('logout/', auth_views.LogoutView.as_view(), name='logout'), + path('auth/password_reset/', + auth_views.PasswordResetView.as_view( + template_name='auth/password_reset.html', + email_template_name='auth/registration/password_reset_email.txt', + html_email_template_name='auth/registration/password_reset_email.html', + subject_template_name='auth/registration/password_reset_subject.txt', + success_url=reverse_lazy('auth:password_reset_done') + ), + name='password_reset' + ), + path('auth/password_reset/done/', + auth_views.PasswordResetDoneView.as_view( + template_name='auth/password_reset_done.html' + ), + name='password_reset_done' + ), + path('auth/reset///', + auth_views.PasswordResetConfirmView.as_view( + template_name='auth/password_reset_confirm.html', + success_url=reverse_lazy('idhub:password_reset_complete') + ), + name='password_reset_confirm' + ), + path('auth/reset/done/', + auth_views.PasswordResetCompleteView.as_view( + template_name='auth/password_reset_complete.html' + ), + name='password_reset_complete' + ), # User path('user/dashboard/', views_user.UserDashboardView.as_view(), diff --git a/trustchain_idhub/settings.py b/trustchain_idhub/settings.py index c0af4ff..731b03f 100644 --- a/trustchain_idhub/settings.py +++ b/trustchain_idhub/settings.py @@ -30,6 +30,7 @@ SECRET_KEY = config('SECRET_KEY') # SECURITY WARNING: don't run with debug turned on in production! DEBUG = config('DEBUG', default=False, cast=bool) +DEVELOPMENT = config('DEVELOPMENT', default=False, cast=bool) ALLOWED_HOSTS = config('ALLOWED_HOSTS', default=[], cast=Csv()) @@ -173,3 +174,4 @@ MESSAGE_TAGS = { messages.WARNING: 'alert-warning', messages.ERROR: 'alert-danger', } +