From cd039a8d21240088b087cdd849c6c6346d776bf3 Mon Sep 17 00:00:00 2001
From: Dmitriy Lyalyuev <dmitriy@lyalyuev.info>
Date: Thu, 27 Oct 2022 11:50:41 +0300
Subject: [PATCH] feat: custom forms for accounts

---
 later42/settings.py                           |  4 ++-
 later42/templates/profile.html                |  7 ++++
 .../{ => registration}/email_activation.html  |  1 +
 .../{ => registration}/email_sent.html        |  0
 later42/templates/registration/login.html     | 10 ++++--
 .../registration/password_change_done.html    |  6 ++++
 .../registration/password_change_form.html    | 14 ++++++++
 .../registration/password_reset_complete.html |  6 ++++
 .../registration/password_reset_confirm.html  | 32 +++++++++++++++++++
 .../registration/password_reset_done.html     |  6 ++++
 .../registration/password_reset_form.html     | 14 ++++++++
 .../templates/{ => registration}/signup.html  |  0
 later42/urls.py                               |  8 ++---
 later42/views/signup.py                       | 10 ++----
 14 files changed, 103 insertions(+), 15 deletions(-)
 rename later42/templates/{ => registration}/email_activation.html (99%)
 rename later42/templates/{ => registration}/email_sent.html (100%)
 create mode 100644 later42/templates/registration/password_change_done.html
 create mode 100644 later42/templates/registration/password_change_form.html
 create mode 100644 later42/templates/registration/password_reset_complete.html
 create mode 100644 later42/templates/registration/password_reset_confirm.html
 create mode 100644 later42/templates/registration/password_reset_done.html
 create mode 100644 later42/templates/registration/password_reset_form.html
 rename later42/templates/{ => registration}/signup.html (100%)

diff --git a/later42/settings.py b/later42/settings.py
index 2432381..1067fa8 100644
--- a/later42/settings.py
+++ b/later42/settings.py
@@ -33,13 +33,13 @@ CSRF_TRUSTED_ORIGINS = ['https://' + os.getenv('DOMAIN', 'localhost')]
 # Application definition
 
 INSTALLED_APPS = [
+    'later42',
     'django.contrib.admin',
     'django.contrib.auth',
     'django.contrib.contenttypes',
     'django.contrib.sessions',
     'django.contrib.messages',
     'django.contrib.staticfiles',
-    'later42',
     'rest_framework',
     'rest_framework.authtoken',
 ]
@@ -162,6 +162,8 @@ URLS_PER_PAGE = 20
 READABILITY_HOST = os.getenv('READABILITY_HOST', None)
 
 EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
+if DEBUG:
+    EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
 EMAIL_USE_TLS = os.getenv('EMAIL_USE_TLS', False)
 EMAIL_HOST = os.getenv('EMAIL_HOST', 'smtp')
 EMAIL_HOST_USER = os.getenv('EMAIL_HOST_USER', None)
diff --git a/later42/templates/profile.html b/later42/templates/profile.html
index 26b2546..60e9562 100644
--- a/later42/templates/profile.html
+++ b/later42/templates/profile.html
@@ -24,6 +24,13 @@
           {{ user.email }}
         </div>
       </div>
+      <div class="row align-items-start">
+        <div class="col">
+        </div>
+        <div class="col">
+          <a href="{% url 'password_change' %}">Сменить пароль</a>
+        </div>
+      </div>
     </div>
     <p> </p>
     <div class="container">
diff --git a/later42/templates/email_activation.html b/later42/templates/registration/email_activation.html
similarity index 99%
rename from later42/templates/email_activation.html
rename to later42/templates/registration/email_activation.html
index 392ba21..986d3bf 100644
--- a/later42/templates/email_activation.html
+++ b/later42/templates/registration/email_activation.html
@@ -1,5 +1,6 @@
 {% autoescape off %}
 Привет, {{ user.username }}.
+
 Ссылка для активации аккаунта:
 https://{{ domain }}{% url 'activate' uidb64=uid token=token %}
 {% endautoescape %}
\ No newline at end of file
diff --git a/later42/templates/email_sent.html b/later42/templates/registration/email_sent.html
similarity index 100%
rename from later42/templates/email_sent.html
rename to later42/templates/registration/email_sent.html
diff --git a/later42/templates/registration/login.html b/later42/templates/registration/login.html
index 4872c8b..9218197 100644
--- a/later42/templates/registration/login.html
+++ b/later42/templates/registration/login.html
@@ -5,8 +5,14 @@
 <form method="post">
   {% csrf_token %}
   {{ form.as_p }}
-  <button class="btn btn-primary text-uppercase" type="submit">Войти</button>
+      <div class="d-flex justify-content-between">
+        <div class="p-2">
+          <button class="btn btn-primary text-uppercase" type="submit">Войти</button>
+        </div>
+        <div class="p-2">
+          <a href={% url 'password_reset' %} class="btn btn-primary text-uppercase">Забыл пароль</a>
+        </div>
+      </div>
 </form>
 <br />
 {% endblock %}
-
diff --git a/later42/templates/registration/password_change_done.html b/later42/templates/registration/password_change_done.html
new file mode 100644
index 0000000..eb2eb63
--- /dev/null
+++ b/later42/templates/registration/password_change_done.html
@@ -0,0 +1,6 @@
+{% extends "base.html" %}
+
+{% block content %}
+<p>Пароль успешно изменен.</p>
+<p>Перейти в <a href="{% url 'profile' %}">Профиль</a>.</p>
+{% endblock %}
\ No newline at end of file
diff --git a/later42/templates/registration/password_change_form.html b/later42/templates/registration/password_change_form.html
new file mode 100644
index 0000000..3968b76
--- /dev/null
+++ b/later42/templates/registration/password_change_form.html
@@ -0,0 +1,14 @@
+{% extends 'base.html' %}
+{% block content %}
+<div  class="card">
+<div  class="card-body">
+<h4  class="card-title">Смена пароля</h4>
+<form  method="post">
+{% csrf_token %}
+{{ form.as_p }}
+<button  type="submit"  class="btn btn-primary btn-block">Сменить</button>
+</form>
+</div>
+</div>
+</div>
+{% endblock %}
diff --git a/later42/templates/registration/password_reset_complete.html b/later42/templates/registration/password_reset_complete.html
new file mode 100644
index 0000000..c58b876
--- /dev/null
+++ b/later42/templates/registration/password_reset_complete.html
@@ -0,0 +1,6 @@
+{% extends "base.html" %}
+
+{% block content %}
+<p>Пароль успешно изменен.</p>
+<p>Вы можете <a href="{% url 'login' %}">войти</a>.</p>
+{% endblock %}
\ No newline at end of file
diff --git a/later42/templates/registration/password_reset_confirm.html b/later42/templates/registration/password_reset_confirm.html
new file mode 100644
index 0000000..8daf8bc
--- /dev/null
+++ b/later42/templates/registration/password_reset_confirm.html
@@ -0,0 +1,32 @@
+{% extends "base.html" %}
+
+{% block content %}
+    {% if validlink %}
+        <p>Введите новый пароль.</p>
+        <form action="" method="post">
+            <div style="display:none">
+                <input type="hidden" value="{{ csrf_token }}" name="csrfmiddlewaretoken">
+            </div>
+            <table>
+                <tr>
+                    <td>{{ form.new_password1.errors }}
+                        <label for="id_new_password1">Новый пароль:</label></td>
+                    <td>{{ form.new_password1 }}</td>
+                </tr>
+                <tr>
+                    <td>{{ form.new_password2.errors }}
+                        <label for="id_new_password2">Подтверждение:</label></td>
+                    <td>{{ form.new_password2 }}</td>
+                </tr>
+                <tr>
+                    <td></td>
+                    <td><input type="submit" value="Сменить" /></td>
+                </tr>
+            </table>
+        </form>
+    {% else %}
+        <h1>Сброс пароля прошел неудачно</h1>
+        <p>Ссылка неверная или уже была использована.<br />
+        Попробуйте сбросить пароль еще раз.</p>
+    {% endif %}
+{% endblock %}
\ No newline at end of file
diff --git a/later42/templates/registration/password_reset_done.html b/later42/templates/registration/password_reset_done.html
new file mode 100644
index 0000000..1790614
--- /dev/null
+++ b/later42/templates/registration/password_reset_done.html
@@ -0,0 +1,6 @@
+{% extends "base.html" %}
+
+{% block content %}
+<p>Сообщение успешно отправлено.</p>
+<p>Вы должны получить его в скором времени.</p>
+{% endblock %}
\ No newline at end of file
diff --git a/later42/templates/registration/password_reset_form.html b/later42/templates/registration/password_reset_form.html
new file mode 100644
index 0000000..5eb7dd2
--- /dev/null
+++ b/later42/templates/registration/password_reset_form.html
@@ -0,0 +1,14 @@
+{% extends 'base.html' %}
+{% block content %}
+<div  class="card">
+<div  class="card-body">
+<h4  class="card-title">Reset your password</h4>
+<form  method="post">
+{% csrf_token %}
+{{ form.as_p }}
+<button  type="submit"  class="btn btn-primary btn-block">Reset</button>
+</form>
+</div>
+</div>
+</div>
+{% endblock %}
diff --git a/later42/templates/signup.html b/later42/templates/registration/signup.html
similarity index 100%
rename from later42/templates/signup.html
rename to later42/templates/registration/signup.html
diff --git a/later42/urls.py b/later42/urls.py
index 39d2929..fd49e48 100644
--- a/later42/urls.py
+++ b/later42/urls.py
@@ -17,11 +17,11 @@ from django.contrib import admin
 
 from django.urls import path, include
 from django.contrib.auth.models import User
-from django.contrib.auth.views import LoginView
+# from django.contrib.auth import views
 
 from rest_framework import routers, serializers, viewsets
 
-from later42.forms import CustomLoginForm
+# from later42.forms import CustomLoginForm
 from later42.views import account_activation, index, profile, api, api_token, reader, signup, about
 
 
@@ -44,9 +44,7 @@ urlpatterns = [
     path('signup/', signup.register, name='signup'),
     path(r'^activate/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
          account_activation.activate, name='activate'),
-    path("accounts/login/",
-         LoginView.as_view(authentication_form=CustomLoginForm), name="login"),
-    path("accounts/", include("django.contrib.auth.urls")),
+    path('accounts/', include('django.contrib.auth.urls')),
     path('profile/', profile.get, name='profile'),
     path('api/url/', api.URL.as_view(), name='urls'),
     path('delete/<int:url_id>', index.delete, name='delete'),
diff --git a/later42/views/signup.py b/later42/views/signup.py
index 8902789..a303bee 100644
--- a/later42/views/signup.py
+++ b/later42/views/signup.py
@@ -15,18 +15,14 @@ def register(request):
         form = SignUpForm(request.POST)
         if form.is_valid():
             user = form.save(commit=False)
-            # user.refresh_from_db()
             user.is_active = False
             user.save()
             raw_password = form.cleaned_data.get('password1')
 
             current_site = get_current_site(request)
 
-            # user = authenticate(username=user.username, password=raw_password)
-            # login(request, user)
-
             mail_subject = 'Later42: Активация аккаунта'
-            message = render_to_string('email_activation.html', {
+            message = render_to_string('registration/email_activation.html', {
                 'user': user,
                 'domain': current_site.domain,
                 'uid': urlsafe_base64_encode(force_bytes(user.pk)),
@@ -39,7 +35,7 @@ def register(request):
             )
             email.send()
 
-            return render(request, 'email_sent.html')
+            return render(request, 'registration/email_sent.html')
     else:
         form = SignUpForm()
-    return render(request, 'signup.html', {'form': form})
+    return render(request, 'registration/signup.html', {'form': form})