From 364bad2047fa417cb6ddbd0ddcc871939a36f80e Mon Sep 17 00:00:00 2001
From: Sofiane Lasri <alasri250@gmail.com>
Date: Wed, 7 Jun 2023 11:55:36 +0200
Subject: [PATCH] =?UTF-8?q?TP11=20termin=C3=A9?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../applipizza/templates/applipizza/base.html |  3 +
 .../applipizza/formulaireCreationPizza.html   |  2 +-
 webpizza/applipizza/views.py                  |  3 +-
 webpizza/connexion/forms.py                   | 24 +++++
 webpizza/connexion/templates/login.html       |  6 +-
 webpizza/connexion/templates/logout.html      |  4 +-
 .../templates/password-reset-complete.html    | 15 ++++
 .../templates/password-reset-confirm.html     | 88 +++++++++++++++++++
 .../templates/password-reset-done.html        | 15 ++++
 .../templates/password-reset-email.html       |  5 ++
 .../connexion/templates/password-reset.html   | 88 +++++++++++++++++++
 webpizza/connexion/templates/register.html    | 86 ++++++++++++++++++
 webpizza/connexion/urls.py                    | 16 +++-
 webpizza/connexion/views.py                   | 13 ++-
 webpizza/webpizza/settings.py                 |  3 +-
 15 files changed, 360 insertions(+), 11 deletions(-)
 create mode 100644 webpizza/connexion/forms.py
 create mode 100644 webpizza/connexion/templates/password-reset-complete.html
 create mode 100644 webpizza/connexion/templates/password-reset-confirm.html
 create mode 100644 webpizza/connexion/templates/password-reset-done.html
 create mode 100644 webpizza/connexion/templates/password-reset-email.html
 create mode 100644 webpizza/connexion/templates/password-reset.html
 create mode 100644 webpizza/connexion/templates/register.html

diff --git a/webpizza/applipizza/templates/applipizza/base.html b/webpizza/applipizza/templates/applipizza/base.html
index 663fbfa..206e37c 100644
--- a/webpizza/applipizza/templates/applipizza/base.html
+++ b/webpizza/applipizza/templates/applipizza/base.html
@@ -47,6 +47,9 @@
                     <li class="nav-link">
                         <a href="{% url 'login' %}">Connexion</a>
                     </li>
+                    <li class="nav-link">
+                        <a href="{% url 'register' %}">S'inscrire</a>
+                    </li>
                 {% endif %}
             </ul>
         </nav>
diff --git a/webpizza/applipizza/templates/applipizza/formulaireCreationPizza.html b/webpizza/applipizza/templates/applipizza/formulaireCreationPizza.html
index fc52264..cd87dda 100644
--- a/webpizza/applipizza/templates/applipizza/formulaireCreationPizza.html
+++ b/webpizza/applipizza/templates/applipizza/formulaireCreationPizza.html
@@ -25,7 +25,7 @@
         <h2>Ajouter un ingrédient</h2>
         <p>Renseigner le nom de l'ingrédient à ajouter.</p>
 
-        <form method="post" class="form-stacked">
+        <form method="post" enctype="multipart/form-data" class="form-stacked">
             {% csrf_token %}
             {% for field in form %}
                 {% if field.errors %}
diff --git a/webpizza/applipizza/views.py b/webpizza/applipizza/views.py
index ae9f44d..aec11c4 100644
--- a/webpizza/applipizza/views.py
+++ b/webpizza/applipizza/views.py
@@ -131,7 +131,8 @@ def create_pizza(request):
         form = PizzaForm(request.POST, request.FILES)
         if form.is_valid():
             form.save()
-            return redirect('pizzas')
+            return redirect('home')
+        print(form.errors)
     else:
         form = PizzaForm()
     return render(
diff --git a/webpizza/connexion/forms.py b/webpizza/connexion/forms.py
new file mode 100644
index 0000000..f7f297b
--- /dev/null
+++ b/webpizza/connexion/forms.py
@@ -0,0 +1,24 @@
+from django import forms
+from django.contrib.auth.models import User
+
+class UserRegistrationForm(forms.ModelForm):
+    password = forms.CharField(label='Mot de passe', widget=forms.PasswordInput)
+    confirm_password = forms.CharField(label='Confirmer le mot de passe', widget=forms.PasswordInput)
+
+    class Meta:
+        model = User
+        fields = ['username', 'password', 'confirm_password', 'first_name', 'last_name', 'email']
+
+    def clean_confirm_password(self):
+        password = self.cleaned_data.get('password')
+        confirm_password = self.cleaned_data.get('confirm_password')
+        if password and confirm_password and password != confirm_password:
+            raise forms.ValidationError('Les mots de passe ne correspondent pas.')
+        return confirm_password
+
+    def save(self, commit=True):
+        user = super().save(commit=False)
+        user.set_password(self.cleaned_data['password'])
+        if commit:
+            user.save()
+        return user
diff --git a/webpizza/connexion/templates/login.html b/webpizza/connexion/templates/login.html
index 5a0d4e7..4a62fca 100644
--- a/webpizza/connexion/templates/login.html
+++ b/webpizza/connexion/templates/login.html
@@ -3,7 +3,7 @@
 {% load static %}
 
 {% block title %}
-    Lasri Del Arte - Ajouter un ingrédient
+    Lasri Del Arte - Connexion
 {% endblock %}
 
 {% block content %}
@@ -51,8 +51,8 @@
             {% endfor %}
             <div class="actions">
                 <input type="submit" value="Se connecter" class="btn success">
-                <a href="/forgotten-password" class="btn danger">Mot de passe oublié ?</a>
-                <a href="/register">Pas encore inscrit ?</a>
+                <a href="{% url 'password_reset' %}" class="btn danger">Mot de passe oublié ?</a>
+                <a href="{% url 'register' %}">Pas encore inscrit ?</a>
             </div>
         </form>
     </div>
diff --git a/webpizza/connexion/templates/logout.html b/webpizza/connexion/templates/logout.html
index 38a4775..663b370 100644
--- a/webpizza/connexion/templates/logout.html
+++ b/webpizza/connexion/templates/logout.html
@@ -3,12 +3,12 @@
 {% load static %}
 
 {% block title %}
-    Lasri Del Arte - Ajouter un ingrédient
+    Lasri Del Arte - Déconnexion
 {% endblock %}
 
 {% block content %}
     <div class="container">
         <h2>Ne partez pas si vite !</h2>
-        <p>Voici 50 % de réduction sur tout ce dont vous n'avez pas besoin ! <a href="/login">Se reconnecter</a></p>
+        <p>Voici 50 % de réduction sur tout ce dont vous n'avez pas besoin ! <a href="{% url 'login' %}">Se reconnecter</a></p>
     </div>
 {% endblock %}
\ No newline at end of file
diff --git a/webpizza/connexion/templates/password-reset-complete.html b/webpizza/connexion/templates/password-reset-complete.html
new file mode 100644
index 0000000..641233f
--- /dev/null
+++ b/webpizza/connexion/templates/password-reset-complete.html
@@ -0,0 +1,15 @@
+{% extends 'base.html' %}
+
+{% load static %}
+
+{% block title %}
+    Lasri Del Arte - Mot de passe oublié
+{% endblock %}
+
+{% block content %}
+    <div class="container">
+        <h2>Réinitialisation réussie !</h2>
+        <p>Votre mot de passe a été réinitialisé avec succès !</p>
+        <a class="btn success" href="{% url 'login' %}">Retourner à la page de connexion</a>
+    </div>
+{% endblock %}
\ No newline at end of file
diff --git a/webpizza/connexion/templates/password-reset-confirm.html b/webpizza/connexion/templates/password-reset-confirm.html
new file mode 100644
index 0000000..37db101
--- /dev/null
+++ b/webpizza/connexion/templates/password-reset-confirm.html
@@ -0,0 +1,88 @@
+{% extends 'base.html' %}
+
+{% load static %}
+
+{% block title %}
+    Lasri Del Arte - Mot de passe oublié
+{% endblock %}
+
+{% block content %}
+    <div class="container">
+        {% if form.non_field_error %}
+            {% if status == 'success' %}
+                <div class="alert-message success" data-alert>
+                    <a class="close" href="#">×</a>
+                    <p><strong>Connecté !</p>
+                </div>
+            {% endif %}
+            {% if form.non_field_error %}
+                {% for error in form.non_field_errors %}
+                    <div class="alert-message error" data-alert>
+                        <a class="close" href="#">×</a>
+                        <p>{{ error }}</p>
+                    </div>
+                {% endfor %}
+            {% endif %}
+        {% endif %}
+        <h2>Mot de passe oublié</h2>
+        <p>Veuillez saisir vote nouveau mot de passe.</p>
+
+        <form method="post" class="form-stacked" id="form_login">
+            {% csrf_token %}
+            {% for field in form %}
+                {% if field.errors %}
+                    <div class="clearfix error">
+                    <label for="{{ field.id_for_label }}">{{ field.label }}</label>
+                    <div class="input">
+                        {{ field }}
+                        <span class="help-inline">
+                                {% for error in field.errors %}
+                                    {{ error }}
+                                {% endfor %}
+                            </span>
+                    </div>
+                {% else %}
+                    <div class="clearfix">
+                        <label for="{{ field.id_for_label }}">{{ field.label }}</label>
+                        <div class="input">
+                            {{ field }}
+                        </div>
+                    </div>
+                {% endif %}
+            {% endfor %}
+            <div class="actions">
+                <input type="submit" value="Se connecter" class="btn success">
+                <a href="{ url 'forgotten-password' }" class="btn danger">Mot de passe oublié ?</a>
+                <a href="{ url 'register' }">Pas encore inscrit ?</a>
+            </div>
+        </form>
+    </div>
+{% endblock %}
+{% block javascripts %}
+    <script type="text/javascript" src="{% static 'applipizza/js/bootstrap-alerts.js' %}"></script>
+    <script type="text/javascript">
+        var form = $('form');
+        var formRequiredFields = form.find('input[required]');
+        var formSubmitButton = form.find('input[type=submit]');
+
+        // We disable form validation
+        form.attr('novalidate', 'novalidate');
+        form.submit(function (e) {
+            formRequiredFields.each(function () {
+                if ($(this).val() == '') {
+                    console.log('empty');
+                    e.preventDefault();
+                    $(this).parent().append('<span class="help-inline">Ce champ est obligatoire</span>');
+                    $(this).parent().parent().addClass('error');
+                } else {
+                    if ($(this).parent().find('span.help-inline').length > 0) {
+                        $(this).parent().find('span.help-inline').remove();
+                        $(this).parent().parent().removeClass('error');
+                    }
+                }
+            });
+        });
+        // We disable the submit button
+        //formSubmitButton.attr('disabled', 'disabled');
+    </script>
+{% endblock %}
\ No newline at end of file
diff --git a/webpizza/connexion/templates/password-reset-done.html b/webpizza/connexion/templates/password-reset-done.html
new file mode 100644
index 0000000..24d572d
--- /dev/null
+++ b/webpizza/connexion/templates/password-reset-done.html
@@ -0,0 +1,15 @@
+{% extends 'base.html' %}
+
+{% load static %}
+
+{% block title %}
+    Lasri Del Arte - Mot de passe oublié
+{% endblock %}
+
+{% block content %}
+    <div class="container">
+        <h2>Vérifiez votre boite de réception !</h2>
+        <p>Si l'adresse que vous avez renseigné correspond à celle associée à votre compte, vous allez bientôt recevoir un e-mail de réinitialisation du mot de passe.</p>
+        <a class="btn success" href="{% url 'login' %}">Retourner à la page de connexion</a>
+    </div>
+{% endblock %}
\ No newline at end of file
diff --git a/webpizza/connexion/templates/password-reset-email.html b/webpizza/connexion/templates/password-reset-email.html
new file mode 100644
index 0000000..86c19a4
--- /dev/null
+++ b/webpizza/connexion/templates/password-reset-email.html
@@ -0,0 +1,5 @@
+Bonjour,
+Vous avez demandé la réinitialisation de votre mot de passe en indiquant ce courriel {{ email }}.
+
+Cliquez sur le lien ci-dessous pour définir un nouveau mot de passe :
+{{ protocol }}://{{ domain }}{% url 'password_reset_confirm' uidb64=uid token=token %}
\ No newline at end of file
diff --git a/webpizza/connexion/templates/password-reset.html b/webpizza/connexion/templates/password-reset.html
new file mode 100644
index 0000000..00195ce
--- /dev/null
+++ b/webpizza/connexion/templates/password-reset.html
@@ -0,0 +1,88 @@
+{% extends 'base.html' %}
+
+{% load static %}
+
+{% block title %}
+    Lasri Del Arte - Mot de passe oublié
+{% endblock %}
+
+{% block content %}
+    <div class="container">
+        {% if form.non_field_error %}
+            {% if status == 'success' %}
+                <div class="alert-message success" data-alert>
+                    <a class="close" href="#">×</a>
+                    <p><strong>Connecté !</p>
+                </div>
+            {% endif %}
+            {% if form.non_field_error %}
+                {% for error in form.non_field_errors %}
+                    <div class="alert-message error" data-alert>
+                        <a class="close" href="#">×</a>
+                        <p>{{ error }}</p>
+                    </div>
+                {% endfor %}
+            {% endif %}
+        {% endif %}
+        <h2>Mot de passe oublié</h2>
+        <p>Saisissez votre adresse email dans le formulaire, nous vous enverrons les instructions pour réintiliser votre
+            mot de passe.</p>
+
+        <form method="post" class="form-stacked" id="form_login">
+            {% csrf_token %}
+            {% for field in form %}
+                {% if field.errors %}
+                    <div class="clearfix error">
+                    <label for="{{ field.id_for_label }}">{{ field.label }}</label>
+                    <div class="input">
+                        {{ field }}
+                        <span class="help-inline">
+                                {% for error in field.errors %}
+                                    {{ error }}
+                                {% endfor %}
+                            </span>
+                    </div>
+                {% else %}
+                    <div class="clearfix">
+                        <label for="{{ field.id_for_label }}">{{ field.label }}</label>
+                        <div class="input">
+                            {{ field }}
+                        </div>
+                    </div>
+                {% endif %}
+            {% endfor %}
+            <div class="actions">
+                <input type="submit" value="Changer le mot de passe" class="btn success">
+                <a href="{% url 'login' %}">Retour à la page de connexion</a>
+            </div>
+        </form>
+    </div>
+{% endblock %}
+{% block javascripts %}
+    <script type="text/javascript" src="{% static 'applipizza/js/bootstrap-alerts.js' %}"></script>
+    <script type="text/javascript">
+        var form = $('form');
+        var formRequiredFields = form.find('input[required]');
+        var formSubmitButton = form.find('input[type=submit]');
+
+        // We disable form validation
+        form.attr('novalidate', 'novalidate');
+        form.submit(function (e) {
+            formRequiredFields.each(function () {
+                if ($(this).val() == '') {
+                    console.log('empty');
+                    e.preventDefault();
+                    $(this).parent().append('<span class="help-inline">Ce champ est obligatoire</span>');
+                    $(this).parent().parent().addClass('error');
+                } else {
+                    if ($(this).parent().find('span.help-inline').length > 0) {
+                        $(this).parent().find('span.help-inline').remove();
+                        $(this).parent().parent().removeClass('error');
+                    }
+                }
+            });
+        });
+        // We disable the submit button
+        //formSubmitButton.attr('disabled', 'disabled');
+    </script>
+{% endblock %}
\ No newline at end of file
diff --git a/webpizza/connexion/templates/register.html b/webpizza/connexion/templates/register.html
new file mode 100644
index 0000000..f2f315c
--- /dev/null
+++ b/webpizza/connexion/templates/register.html
@@ -0,0 +1,86 @@
+{% extends 'base.html' %}
+
+{% load static %}
+
+{% block title %}
+    Lasri Del Arte - Inscription
+{% endblock %}
+
+{% block content %}
+    <div class="container">
+        {% if form.non_field_error %}
+            {% if status == 'success' %}
+                <div class="alert-message success" data-alert>
+                    <a class="close" href="#">×</a>
+                    <p><strong>Inscription réussie !</p>
+                </div>
+            {% endif %}
+            {% if form.non_field_error %}
+                {% for error in form.non_field_errors %}
+                    <div class="alert-message error" data-alert>
+                        <a class="close" href="#">×</a>
+                        <p>{{ error }}</p>
+                    </div>
+                {% endfor %}
+            {% endif %}
+        {% endif %}
+        <h2>Se connecter</h2>
+
+        <form method="post" class="form-stacked" id="form_login">
+            {% csrf_token %}
+            {% for field in form %}
+                {% if field.errors %}
+                    <div class="clearfix error">
+                    <label for="{{ field.id_for_label }}">{{ field.label }}</label>
+                    <div class="input">
+                        {{ field }}
+                        <span class="help-inline">
+                                {% for error in field.errors %}
+                                    {{ error }}
+                                {% endfor %}
+                            </span>
+                    </div>
+                {% else %}
+                    <div class="clearfix">
+                        <label for="{{ field.id_for_label }}">{{ field.label }}</label>
+                        <div class="input">
+                            {{ field }}
+                        </div>
+                    </div>
+                {% endif %}
+            {% endfor %}
+            <div class="actions">
+                <input type="submit" value="S'inscrire" class="btn success">
+                <a href="{% url 'login' %}">Déjà inscrit ?</a>
+            </div>
+        </form>
+    </div>
+{% endblock %}
+{% block javascripts %}
+    <script type="text/javascript" src="{% static 'applipizza/js/bootstrap-alerts.js' %}"></script>
+    <script type="text/javascript">
+        var form = $('form');
+        var formRequiredFields = form.find('input[required]');
+        var formSubmitButton = form.find('input[type=submit]');
+
+        // We disable form validation
+        form.attr('novalidate', 'novalidate');
+        form.submit(function (e) {
+            formRequiredFields.each(function () {
+                if ($(this).val() == '') {
+                    console.log('empty');
+                    e.preventDefault();
+                    $(this).parent().append('<span class="help-inline">Ce champ est obligatoire</span>');
+                    $(this).parent().parent().addClass('error');
+                } else {
+                    if ($(this).parent().find('span.help-inline').length > 0) {
+                        $(this).parent().find('span.help-inline').remove();
+                        $(this).parent().parent().removeClass('error');
+                    }
+                }
+            });
+        });
+        // We disable the submit button
+        //formSubmitButton.attr('disabled', 'disabled');
+    </script>
+{% endblock %}
\ No newline at end of file
diff --git a/webpizza/connexion/urls.py b/webpizza/connexion/urls.py
index 463e065..30c69ca 100644
--- a/webpizza/connexion/urls.py
+++ b/webpizza/connexion/urls.py
@@ -1,7 +1,7 @@
 from django.contrib import admin
 from django.contrib.auth import views as auth_views
 from django.urls import path
-from applipizza import views
+from connexion import views
 from django.conf import settings  # new
 from django.urls import path, include  # new
 from django.conf.urls.static import static  # new
@@ -10,4 +10,18 @@ urlpatterns = [
     path('admin/', admin.site.urls),
     path('login/', auth_views.LoginView.as_view(template_name="login.html"), name="login"),
     path('logout/', auth_views.LogoutView.as_view(template_name="logout.html"), name="logout"),
+    path('register/', views.register, name='register'),
+    path('forgotten-password/', auth_views.PasswordResetView.as_view(
+        template_name="password-reset.html",
+        email_template_name="password-reset-email.html"
+    ), name="password_reset"),
+    path('reset/<uidb64>/<token>/', auth_views.PasswordResetConfirmView.as_view(
+        template_name="password-reset-confirm.html",
+    ), name="password_reset_confirm"),
+    path('reset/done', auth_views.PasswordResetDoneView.as_view(
+        template_name="password-reset-done.html"
+    ), name="password_reset_done"),
+path('reset/done', auth_views.PasswordResetCompleteView.as_view(
+        template_name="password-reset-complete.html"
+    ), name="password_reset_complete")
 ]
diff --git a/webpizza/connexion/views.py b/webpizza/connexion/views.py
index 91ea44a..59b9d14 100644
--- a/webpizza/connexion/views.py
+++ b/webpizza/connexion/views.py
@@ -1,3 +1,12 @@
-from django.shortcuts import render
+from django.shortcuts import render, redirect
+from .forms import UserRegistrationForm
 
-# Create your views here.
+def register(request):
+    if request.method == 'POST':
+        form = UserRegistrationForm(request.POST)
+        if form.is_valid():
+            form.save()
+            return redirect('home')
+    else:
+        form = UserRegistrationForm()
+    return render(request, 'register.html', {'form': form})
diff --git a/webpizza/webpizza/settings.py b/webpizza/webpizza/settings.py
index 757e1c1..99a2858 100644
--- a/webpizza/webpizza/settings.py
+++ b/webpizza/webpizza/settings.py
@@ -129,4 +129,5 @@ STATIC_URL = 'static/'
 DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
 MEDIA_ROOT = BASE_DIR/'media'
 MEDIA_URL = '/media/'
-LOGIN_REDIRECT_URL = "home"
\ No newline at end of file
+LOGIN_REDIRECT_URL = "home"
+EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
\ No newline at end of file
-- 
GitLab