From 2bd9741d28206de6015797f92958af89066b05c1 Mon Sep 17 00:00:00 2001 From: Pablo Silva Date: Thu, 21 Nov 2024 16:54:25 +0000 Subject: [PATCH] Create new reset-password view --- .envs/.example | 2 + src/config/settings.py | 14 ++-- .../jinja2/users/html/password_reset.jinja2 | 59 +++++++++++++++ .../users/html/password_reset_complete.jinja2 | 15 ++++ .../users/html/password_reset_confirm.jinja2 | 71 +++++++++++++++++++ .../users/html/password_reset_email.html | 11 +++ .../users/html/password_reset_subject.txt | 1 + src/users/jinja2/users/login.jinja2 | 2 +- src/users/urls.py | 34 +++++---- src/users/views.py | 21 ++++-- 10 files changed, 205 insertions(+), 25 deletions(-) create mode 100644 src/users/jinja2/users/html/password_reset.jinja2 create mode 100644 src/users/jinja2/users/html/password_reset_complete.jinja2 create mode 100644 src/users/jinja2/users/html/password_reset_confirm.jinja2 create mode 100644 src/users/jinja2/users/html/password_reset_email.html create mode 100644 src/users/jinja2/users/html/password_reset_subject.txt diff --git a/.envs/.example b/.envs/.example index 98e5726d..01605030 100644 --- a/.envs/.example +++ b/.envs/.example @@ -37,6 +37,8 @@ POSTGRES_PASSWORD=secret # Email server variables SMTP_SERVER=mailpit SMTP_PORT=1025 +SMTP_USE_TLS=False +SMTP_USE_SSL=False SMTP_USER= SMTP_PASSWORD= SMTP_SENDER_MAIL="jandig@memelab.com.br" diff --git a/src/config/settings.py b/src/config/settings.py index dc8c79b7..8ec6666d 100644 --- a/src/config/settings.py +++ b/src/config/settings.py @@ -194,11 +194,15 @@ def debug(request): # Sphinx docs DOCS_ROOT = "/jandig/build/" -SMTP_SERVER = env("SMTP_SERVER", default="mailpit") -SMTP_PORT = env("SMTP_PORT", default=1025) -SMTP_USER = env("SMTP_USER", default="jandig@jandig.com") -SMTP_PASSWORD = env("SMTP_PASSWORD", default="password") -SMTP_SENDER_MAIL = env("SMTP_SENDER_MAIL", default="jandig@memelab.com.br") + +DEFAULT_FROM_EMAIL = env("SMTP_SENDER_MAIL", default="jandig@memelab.com.br") +EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' +EMAIL_HOST = env("SMTP_SERVER", default="mailpit") +EMAIL_USE_TLS = env("SMTP_USE_TLS", default=False) +EMAIL_PORT = env("SMTP_PORT", default=1025) +EMAIL_HOST_USER = env("SMTP_USER", default="jandig@jandig.com") +EMAIL_HOST_PASSWORD = env("SMTP_PASSWORD", default="password") +EMAIL_USE_SSL = False # Recaptcha RECAPTCHA_ENABLED = env("RECAPTCHA_ENABLED", default=False) diff --git a/src/users/jinja2/users/html/password_reset.jinja2 b/src/users/jinja2/users/html/password_reset.jinja2 new file mode 100644 index 00000000..18146e6a --- /dev/null +++ b/src/users/jinja2/users/html/password_reset.jinja2 @@ -0,0 +1,59 @@ +{% extends '/core/arviewer.jinja2' %} +{% block content %} +
+
+
+
+
+
+
+

Forgot Password?

+
+ {% if form.errors %} + + {% endif %} +
+
+ {{ csrf_input }} +
+
+
+ + +
+
+
+
+
+
+ +
+
+
+
+
+ +
+
+
+
+ +
+{% endblock content %} \ No newline at end of file diff --git a/src/users/jinja2/users/html/password_reset_complete.jinja2 b/src/users/jinja2/users/html/password_reset_complete.jinja2 new file mode 100644 index 00000000..d878ed47 --- /dev/null +++ b/src/users/jinja2/users/html/password_reset_complete.jinja2 @@ -0,0 +1,15 @@ +{% extends '/core/arviewer.jinja2' %} +{% block title %} Password Reset {% endblock title%} +{% block content %} +
+
+
+
+
+ Your password has been set. You may go ahead and Login Here +
+
+
+
+
+{% endblock content %} \ No newline at end of file diff --git a/src/users/jinja2/users/html/password_reset_confirm.jinja2 b/src/users/jinja2/users/html/password_reset_confirm.jinja2 new file mode 100644 index 00000000..ccdef32d --- /dev/null +++ b/src/users/jinja2/users/html/password_reset_confirm.jinja2 @@ -0,0 +1,71 @@ +{% extends '/core/arviewer.jinja2' %} +{% block title %} Password Reset {% endblock title%} +{% block content %} +
+
+
+
+ {% if validlink %} +
+
+

Reset Your Password

+
+ {% if form.errors %} + + {% endif %} + +
+
+ {{ csrf_input }} +
+
+
+ + + + +
+
+
+
+
+
+ + +
+
+
+
+
+
+ +
+
+
+
+
+
+ {% else %} +
+ The password reset link was invalid, possibly because it has already been used. + Please request a new password reset. +
+ {% endif %} +
+
+
+
+{% endblock content %} \ No newline at end of file diff --git a/src/users/jinja2/users/html/password_reset_email.html b/src/users/jinja2/users/html/password_reset_email.html new file mode 100644 index 00000000..e261c607 --- /dev/null +++ b/src/users/jinja2/users/html/password_reset_email.html @@ -0,0 +1,11 @@ + + To initiate the password reset process for your {{ user.email }} Django Registration/Login App Account, + click the link below: + + {{ protocol }}://{{ domain }}{{ url('password_reset_confirm',args=[uid,token]) }} + + If clicking the link above doesn't work, please copy and paste the URL in a new browser + window instead. + + Sincerely, + Jandig Team diff --git a/src/users/jinja2/users/html/password_reset_subject.txt b/src/users/jinja2/users/html/password_reset_subject.txt new file mode 100644 index 00000000..5cb9df8e --- /dev/null +++ b/src/users/jinja2/users/html/password_reset_subject.txt @@ -0,0 +1 @@ +Jandig Password Reset \ No newline at end of file diff --git a/src/users/jinja2/users/login.jinja2 b/src/users/jinja2/users/login.jinja2 index 2b720ba0..451a8114 100644 --- a/src/users/jinja2/users/login.jinja2 +++ b/src/users/jinja2/users/login.jinja2 @@ -46,7 +46,7 @@
- {{ _('Recover password') }} + {{ _('Recover password') }}
diff --git a/src/users/urls.py b/src/users/urls.py index a4fcb038..99804ef9 100644 --- a/src/users/urls.py +++ b/src/users/urls.py @@ -27,6 +27,7 @@ related_content, signup, wrong_verification_code, + ResetPasswordView, ) urlpatterns = [ @@ -40,22 +41,29 @@ name="login", ), path("logout/", auth_views.LogoutView.as_view(), name="logout"), - path("recover/", recover_password, name="recover"), - path("recover-code/", recover_code, name="recover-code"), + path('reset-password/', ResetPasswordView.as_view(), name='reset-password'), + path('password-reset-confirm///', + auth_views.PasswordResetConfirmView.as_view(template_name='users/html/password_reset_confirm.jinja2'), + name='password_reset_confirm'), + path('password-reset-complete/', + auth_views.PasswordResetCompleteView.as_view(template_name='users/html/password_reset_complete.jinja2'), + name='password_reset_complete'), + # path("recover/", recover_password, name="recover"), + # path("recover-code/", recover_code, name="recover-code"), + # path( + # "wrong-verification-code", + # wrong_verification_code, + # name="wrong-verification-code", + # ), + # path( + # "invalid-recovering-email", + # invalid_recovering_email_or_username, + # name="invalid_recovering_email_or_username", + # ), + # path("recover-edit-password", recover_edit_password, name="recover-edit-password"), path("profile/", profile, name="profile"), path("profile/edit/", edit_profile, name="edit-profile"), path("profile/edit-password/", edit_password, name="edit-password"), - path( - "wrong-verification-code", - wrong_verification_code, - name="wrong-verification-code", - ), - path( - "invalid-recovering-email", - invalid_recovering_email_or_username, - name="invalid_recovering_email_or_username", - ), - path("recover-edit-password", recover_edit_password, name="recover-edit-password"), path("markers/upload/", marker_upload, name="marker-upload"), path("objects/upload/", object_upload, name="object-upload"), path("element/get/", element_get, name="element-get"), diff --git a/src/users/views.py b/src/users/views.py index fb60ecf7..80658a18 100644 --- a/src/users/views.py +++ b/src/users/views.py @@ -29,13 +29,15 @@ UploadObjectForm, ) from .models import Profile -from .services.email_service import EmailService -from .services.encrypt_service import EncryptService -from .services.recaptcha_service import BOT_SCORE, create_assessment -from .services.user_service import UserService +from .services import ( EmailService,EncryptService ,BOT_SCORE, create_assessment, UserService) +from django.urls import reverse_lazy +from django.contrib.auth.views import PasswordResetView +from django.contrib.messages.views import SuccessMessageMixin log = logging.getLogger(__file__) +User = get_user_model() + def signup(request): if request.method == "POST": @@ -72,8 +74,15 @@ def signup(request): ) -User = get_user_model() - +class ResetPasswordView(SuccessMessageMixin, PasswordResetView): + template_name = 'users/html/password_reset.jinja2' + email_template_name = 'users/html/password_reset_email.html' + subject_template_name = 'users/html/password_reset_subject.txt' + success_message = _("We've emailed you instructions for setting your password, " \ + "if an account exists with the email you entered. You should receive them shortly." \ + " If you don't receive an email, " \ + "please make sure you've entered the address you registered with, and check your spam folder.") + success_url = reverse_lazy('home') def recover_password(request): if request.method == "POST":