From 1bf2c1558d0b8418e8530069ab6f2a6f09349dca Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Mon, 14 Oct 2024 17:02:40 +0200 Subject: [PATCH] email system and reset password --- admin/views.py | 10 ++++++- dashboard/templates/unassigned_devices.html | 2 +- dhub/settings.py | 24 ++++++++++++++++ login/templates/activate_user_email.html | 31 +++++++++++++++++++++ login/templates/activate_user_email.txt | 19 +++++++++++++ login/templates/activate_user_subject.txt | 4 +++ login/templates/password_reset_email.html | 4 +-- login/templates/password_reset_email.txt | 2 +- login/views.py | 4 ++- 9 files changed, 94 insertions(+), 6 deletions(-) create mode 100644 login/templates/activate_user_email.html create mode 100644 login/templates/activate_user_email.txt create mode 100644 login/templates/activate_user_subject.txt diff --git a/admin/views.py b/admin/views.py index 14627cb..9ce25c7 100644 --- a/admin/views.py +++ b/admin/views.py @@ -1,3 +1,4 @@ +from smtplib import SMTPException from django.urls import reverse_lazy from django.shortcuts import get_object_or_404 from django.utils.translation import gettext_lazy as _ @@ -9,6 +10,7 @@ from django.views.generic.edit import ( ) from dashboard.mixins import DashboardView, Http403 from user.models import User, Institution +from admin.email import NotifyActivateUserByEmail class AdminView(DashboardView): @@ -42,7 +44,7 @@ class UsersView(AdminView, TemplateView): return context -class CreateUserView(AdminView, CreateView): +class CreateUserView(AdminView, NotifyActivateUserByEmail, CreateView): template_name = "user.html" title = _("User") breadcrumb = _("admin / User") + " /" @@ -58,6 +60,12 @@ class CreateUserView(AdminView, CreateView): form.instance.institution = self.request.user.institution form.instance.set_password(form.instance.password) response = super().form_valid(form) + + try: + self.send_email(form.instance) + except SMTPException as e: + messages.error(self.request, e) + return response diff --git a/dashboard/templates/unassigned_devices.html b/dashboard/templates/unassigned_devices.html index af12820..369edd2 100644 --- a/dashboard/templates/unassigned_devices.html +++ b/dashboard/templates/unassigned_devices.html @@ -15,7 +15,7 @@ {% trans 'Documents' %} {% endif %} - + {% trans 'Exports' %} diff --git a/dhub/settings.py b/dhub/settings.py index 8b77fd7..3b48d2c 100644 --- a/dhub/settings.py +++ b/dhub/settings.py @@ -39,6 +39,30 @@ assert DOMAIN in ALLOWED_HOSTS, "DOMAIN is not ALLOWED_HOST" CSRF_TRUSTED_ORIGINS = config('CSRF_TRUSTED_ORIGINS', default=f'https://{DOMAIN}', cast=Csv()) + +INITIAL_ADMIN_EMAIL = config("INITIAL_ADMIN_EMAIL", default='admin@example.org') +INITIAL_ADMIN_PASSWORD = config("INITIAL_ADMIN_PASSWORD", default='1234') + +DEFAULT_FROM_EMAIL = config( + 'DEFAULT_FROM_EMAIL', default='webmaster@localhost') + +EMAIL_HOST = config('EMAIL_HOST', default='localhost') + +EMAIL_HOST_USER = config('EMAIL_HOST_USER', default='') + +EMAIL_HOST_PASSWORD = config('EMAIL_HOST_PASSWORD', default='') + +EMAIL_PORT = config('EMAIL_PORT', default=25, cast=int) + +EMAIL_USE_TLS = config('EMAIL_USE_TLS', default=False, cast=bool) + +EMAIL_BACKEND = config('EMAIL_BACKEND', default='django.core.mail.backends.smtp.EmailBackend') + +EMAIL_FILE_PATH = config('EMAIL_FILE_PATH', default='/tmp/app-messages') + +ENABLE_EMAIL = config("ENABLE_EMAIL", default=True, cast=bool) + + # Application definition INSTALLED_APPS = [ diff --git a/login/templates/activate_user_email.html b/login/templates/activate_user_email.html new file mode 100644 index 0000000..20aa2a6 --- /dev/null +++ b/login/templates/activate_user_email.html @@ -0,0 +1,31 @@ +{% load i18n %}{% autoescape off %} +{% trans "DeviceHub" 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 'login:password_reset_confirm' uidb64=uid token=token %} + +{% endblock %} +

+ +

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

+ +

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

+ +{% endautoescape %} diff --git a/login/templates/activate_user_email.txt b/login/templates/activate_user_email.txt new file mode 100644 index 0000000..2c4ff0c --- /dev/null +++ b/login/templates/activate_user_email.txt @@ -0,0 +1,19 @@ +{% load i18n %}{% autoescape off %} + +{% trans "DeviceHub" 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 'login:password_reset_confirm' uidb64=uid token=token %} +{% endblock %} + + +{% trans "Thanks for using our site!" %} + +{% blocktrans %}The {{site}} team{% endblocktrans %} + +{% endautoescape %} diff --git a/login/templates/activate_user_subject.txt b/login/templates/activate_user_subject.txt new file mode 100644 index 0000000..d28a515 --- /dev/null +++ b/login/templates/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/login/templates/password_reset_email.html b/login/templates/password_reset_email.html index 062025a..903cc2e 100644 --- a/login/templates/password_reset_email.html +++ b/login/templates/password_reset_email.html @@ -9,8 +9,8 @@

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

diff --git a/login/templates/password_reset_email.txt b/login/templates/password_reset_email.txt index a4313fe..8dbf945 100644 --- a/login/templates/password_reset_email.txt +++ b/login/templates/password_reset_email.txt @@ -3,7 +3,7 @@ {% 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 %} +{{ protocol }}://{{ domain }}{% url 'login:password_reset_confirm' uidb64=uid token=token %} {% endblock %} {% trans "Your username, in case you've forgotten:" %} {{ user.username }} diff --git a/login/views.py b/login/views.py index 7cd9156..5bf48a3 100644 --- a/login/views.py +++ b/login/views.py @@ -65,8 +65,10 @@ class PasswordResetView(auth_views.PasswordResetView): success_url = reverse_lazy('login:password_reset_done') def form_valid(self, form): + import pdb; pdb.set_trace() try: - return super().form_valid(form) + response = super().form_valid(form) + return response except Exception as err: logger.error(err) return HttpResponseRedirect(self.success_url)