From 47815486c454c4a27444937c81504b3f06617811 Mon Sep 17 00:00:00 2001
From: Sofiane Lasri <alasri250@gmail.com>
Date: Wed, 7 Jun 2023 10:33:27 +0200
Subject: [PATCH] =?UTF-8?q?TP10=20termin=C3=A9=20+=20Page=20ingr=C3=A9dien?=
 =?UTF-8?q?ts=20&=20cr=C3=A9ation=20pizza.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../applipizza/templates/applipizza/base.html | 27 ++++--
 .../applipizza/formulaireCreationPizza.html   | 85 ++++++++++++++++++
 .../templates/applipizza/ingredients.html     | 38 ++++++++
 webpizza/applipizza/urls.py                   | 21 +++++
 webpizza/applipizza/views.py                  | 26 +++++-
 webpizza/connexion/__init__.py                |  0
 webpizza/connexion/admin.py                   |  3 +
 webpizza/connexion/apps.py                    |  6 ++
 webpizza/connexion/migrations/__init__.py     |  0
 webpizza/connexion/models.py                  |  3 +
 webpizza/connexion/templates/base.html        | 71 +++++++++++++++
 webpizza/connexion/templates/login.html       | 87 +++++++++++++++++++
 webpizza/connexion/templates/logout.html      | 14 +++
 webpizza/connexion/tests.py                   |  3 +
 webpizza/connexion/urls.py                    | 13 +++
 webpizza/connexion/views.py                   |  3 +
 webpizza/webpizza/settings.py                 |  6 +-
 webpizza/webpizza/urls.py                     | 23 ++---
 18 files changed, 408 insertions(+), 21 deletions(-)
 create mode 100644 webpizza/applipizza/templates/applipizza/formulaireCreationPizza.html
 create mode 100644 webpizza/applipizza/templates/applipizza/ingredients.html
 create mode 100644 webpizza/applipizza/urls.py
 create mode 100644 webpizza/connexion/__init__.py
 create mode 100644 webpizza/connexion/admin.py
 create mode 100644 webpizza/connexion/apps.py
 create mode 100644 webpizza/connexion/migrations/__init__.py
 create mode 100644 webpizza/connexion/models.py
 create mode 100644 webpizza/connexion/templates/base.html
 create mode 100644 webpizza/connexion/templates/login.html
 create mode 100644 webpizza/connexion/templates/logout.html
 create mode 100644 webpizza/connexion/tests.py
 create mode 100644 webpizza/connexion/urls.py
 create mode 100644 webpizza/connexion/views.py

diff --git a/webpizza/applipizza/templates/applipizza/base.html b/webpizza/applipizza/templates/applipizza/base.html
index e3f2b1b..663fbfa 100644
--- a/webpizza/applipizza/templates/applipizza/base.html
+++ b/webpizza/applipizza/templates/applipizza/base.html
@@ -27,12 +27,27 @@
         </div>
         <nav class="navbar">
             <ul>
-                <li class="nav-link active">
-                    <a href="/pizzas/">Les pizzas</a>
-                </li>
-                <li class="nav-link">
-                    <a href="/ingredients/">Les ingrédients</a>
-                </li>
+                {% if user.is_authenticated %}
+                    <li class="nav-link active">
+                        <a href="/pizzas/">Les pizzas</a>
+                    </li>
+                    <li class="nav-link">
+                        <a href="/ingredients/">Les ingrédients</a>
+                    </li>
+                    <li class="nav-link">
+                        <a href="/pizzas/add">Ajouter une pizza</a>
+                    </li>
+                    <li class="nav-link">
+                        <a href="/ingredients/add">Ajouter un ingrédient</a>
+                    </li>
+                    <li class="nav-link">
+                        <a href="{% url 'logout' %}">Déconnexion</a>
+                    </li>
+                {% else %}
+                    <li class="nav-link">
+                        <a href="{% url 'login' %}">Connexion</a>
+                    </li>
+                {% endif %}
             </ul>
         </nav>
     </div>
diff --git a/webpizza/applipizza/templates/applipizza/formulaireCreationPizza.html b/webpizza/applipizza/templates/applipizza/formulaireCreationPizza.html
new file mode 100644
index 0000000..fc52264
--- /dev/null
+++ b/webpizza/applipizza/templates/applipizza/formulaireCreationPizza.html
@@ -0,0 +1,85 @@
+{% extends 'applipizza/base.html' %}
+
+{% load static %}
+
+{% block title %}
+    Lasri Del Arte - Ajouter un ingrédient
+{% endblock %}
+
+{% block content %}
+    <div class="container">
+        {% if status %}
+            {% if status == 'success' %}
+                <div class="alert-message success" data-alert>
+                    <a class="close" href="#">×</a>
+                    <p><strong>Enregistré !</strong> L'ingrédient {{ nomIngredient }} a bien été ajouté.</p>
+                </div>
+            {% endif %}
+            {% if status == 'error' %}
+                <div class="alert-message error" data-alert>
+                    <a class="close" href="#">×</a>
+                    <p><strong>Erreur !</strong> L'ingrédient n'a pas pu être ajouté.</p>
+                </div>
+            {% endif %}
+        {% endif %}
+        <h2>Ajouter un ingrédient</h2>
+        <p>Renseigner le nom de l'ingrédient à ajouter.</p>
+
+        <form method="post" class="form-stacked">
+            {% 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="Ajouter" class="btn primary">
+                <button type="reset" class="btn">Annuler</button>
+            </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/applipizza/templates/applipizza/ingredients.html b/webpizza/applipizza/templates/applipizza/ingredients.html
new file mode 100644
index 0000000..1270f4a
--- /dev/null
+++ b/webpizza/applipizza/templates/applipizza/ingredients.html
@@ -0,0 +1,38 @@
+{% extends 'applipizza/base.html' %}
+
+{% load static %}
+
+{% block title %}
+    Lasri Del Arte - Nos pizzas
+{% endblock %}
+
+{% block content %}
+    <div class="container">
+        <h2>Nos ingrédients</h2>
+        <p>Découvrez notre large choix d'ingrédients pas bios.</p>
+
+        <table id="ingredients">
+            <thead>
+            <tr>
+                <th>Nom de l'ingrédient</th>
+            </tr>
+            </thead>
+            <tbody>
+            {% for ingredient in ingredients %}
+                <tr>
+                    <td>{{ ingredient.nom }}</td>
+                </tr>
+            {% endfor %}
+            </tbody>
+        </table>
+    </div>
+{% endblock %}
+{% block javascripts %}
+    <script type="text/javascript" src="{% static 'applipizza/js/bootstrap-alerts.js' %}"></script>
+    <script type="text/javascript" src="{% static 'applipizza/js/jquery.tablesorter.min.js' %}"></script>
+    <script type="text/javascript">
+        $(document).ready(function () {
+            $('#ingredients').tablesorter({sortList: [[0]]});
+        });
+    </script>
+{% endblock %}
\ No newline at end of file
diff --git a/webpizza/applipizza/urls.py b/webpizza/applipizza/urls.py
new file mode 100644
index 0000000..f39dac5
--- /dev/null
+++ b/webpizza/applipizza/urls.py
@@ -0,0 +1,21 @@
+from applipizza import views
+from django.conf import settings  # new
+from django.urls import path, include  # new
+from django.conf.urls.static import static  # new
+
+urlpatterns = [
+    path('', views.pizzas, name="home"),
+    path('pizzas/', views.pizzas),
+    path('pizzas/add', views.create_pizza),
+    path('pizza/<int:id>', views.pizza),
+    path('pizza/<int:id>/addIngredient', views.ajouterIngredientDansPizza),
+    path('pizza/<int:pizzaId>/deleteIngredient/<int:compositionId>', views.supprimerIngredientDansPizza),
+    path('pizza/<int:id>/update', views.modifierPizza),
+    path('pizza/<int:id>/update/post', views.traitementFormulaireModificationPizza),
+    path('pizza/<int:id>/delete', views.supprimerPizza),
+    path('ingredients/', views.listeIngredients),
+    path('ingredients/add', views.formulaireCreationIngredient),
+    path('ingredients/add/post', views.creerIngredient),
+]
+
+urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)  # new
diff --git a/webpizza/applipizza/views.py b/webpizza/applipizza/views.py
index 05a35a4..ae9f44d 100644
--- a/webpizza/applipizza/views.py
+++ b/webpizza/applipizza/views.py
@@ -1,4 +1,4 @@
-from django.shortcuts import render
+from django.shortcuts import render, redirect
 from applipizza.models import Pizza, Ingredient, Composition
 from applipizza.forms import IngredientForm, CompositionForm, PizzaForm
 
@@ -124,4 +124,26 @@ def viewPizza(request, id, status=None):
 def supprimerIngredientDansPizza(request, pizzaId, compositionId):
     composition = Composition.objects.get(id=compositionId)
     composition.delete()
-    return viewPizza(request, pizzaId, 'success')
\ No newline at end of file
+    return viewPizza(request, pizzaId, 'success')
+
+def create_pizza(request):
+    if request.method == 'POST':
+        form = PizzaForm(request.POST, request.FILES)
+        if form.is_valid():
+            form.save()
+            return redirect('pizzas')
+    else:
+        form = PizzaForm()
+    return render(
+        request,
+        'applipizza/formulaireCreationPizza.html',
+        {'form': form}
+    )
+
+def listeIngredients(request):
+    ingredients = Ingredient.objects.all()
+    return render(
+        request,
+        'applipizza/ingredients.html',
+        {'ingredients': ingredients}
+    )
\ No newline at end of file
diff --git a/webpizza/connexion/__init__.py b/webpizza/connexion/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/webpizza/connexion/admin.py b/webpizza/connexion/admin.py
new file mode 100644
index 0000000..8c38f3f
--- /dev/null
+++ b/webpizza/connexion/admin.py
@@ -0,0 +1,3 @@
+from django.contrib import admin
+
+# Register your models here.
diff --git a/webpizza/connexion/apps.py b/webpizza/connexion/apps.py
new file mode 100644
index 0000000..a263761
--- /dev/null
+++ b/webpizza/connexion/apps.py
@@ -0,0 +1,6 @@
+from django.apps import AppConfig
+
+
+class ConnexionConfig(AppConfig):
+    default_auto_field = 'django.db.models.BigAutoField'
+    name = 'connexion'
diff --git a/webpizza/connexion/migrations/__init__.py b/webpizza/connexion/migrations/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/webpizza/connexion/models.py b/webpizza/connexion/models.py
new file mode 100644
index 0000000..71a8362
--- /dev/null
+++ b/webpizza/connexion/models.py
@@ -0,0 +1,3 @@
+from django.db import models
+
+# Create your models here.
diff --git a/webpizza/connexion/templates/base.html b/webpizza/connexion/templates/base.html
new file mode 100644
index 0000000..245f45f
--- /dev/null
+++ b/webpizza/connexion/templates/base.html
@@ -0,0 +1,71 @@
+{% load static %}
+<!DOCTYPE html>
+<html lang="fr">
+<head>
+    <meta charset="utf-8">
+    <title>{% block title %}{% endblock %}</title>
+    <link rel="stylesheet" href="{% static 'applipizza/css/bootstrap.css' %}">
+    <link rel="stylesheet" href="{% static 'applipizza/css/font-awesome.min.css' %}">
+    <link rel="stylesheet" href="{% static 'applipizza/css/styles.css' %}">
+</head>
+<body>
+<header>
+    <div class="container">
+        <div class="top-meta">
+            <div class="logo-link">
+                <a href="/"><img src="{% static 'applipizza/img/lasridelarte.png' %}" alt="logo"></a>
+            </div>
+            <div class="right-part">
+                <div class="certificate">
+                    <img src="{% static 'applipizza/img/certificate.png' %}" alt="certificat">
+                </div>
+                <div class="meta">
+                    <p><strong>Lasri Del Arte
+                        <br>5th Cave Jonhson Street, Rosewood</strong></p>
+                </div>
+            </div>
+        </div>
+        <nav class="navbar">
+            <ul>
+                <li class="nav-link">
+                    <a href="/pizzas">Revenir sur le site</a>
+                </li>
+            </ul>
+        </nav>
+    </div>
+</header>
+<div class="page-content">
+    {% block content %}{% endblock %}
+</div>
+<footer>
+    <div class="container">
+        <p>© 2012 Lasri Del Arte - Tous droits réservés</p>
+    </div>
+</footer>
+
+<script type="text/javascript">
+    // Si le footer n'est pas tout en bas de la page car il y a peu de contenu
+    // On le place tout en bas de la page
+    function footerPosition() {
+        var footer = document.querySelector("footer")
+        var footerHeight = footer.offsetHeight;
+        var windowHeight = window.innerHeight;
+        var bodyHeight = document.body.offsetHeight;
+
+        if (bodyHeight < windowHeight) {
+            footer.style.position = 'absolute';
+            footer.style.bottom = '0';
+        } else {
+            footer.style.position = 'relative';
+            footer.style.bottom = '0';
+        }
+    }
+
+    footerPosition();
+
+    window.addEventListener('resize', footerPosition);
+</script>
+<script src="{% static 'applipizza/js/jquery-1.5.2.min.js' %}"></script>
+{% block javascripts %}{% endblock %}
+</body>
+</html>
\ No newline at end of file
diff --git a/webpizza/connexion/templates/login.html b/webpizza/connexion/templates/login.html
new file mode 100644
index 0000000..5a0d4e7
--- /dev/null
+++ b/webpizza/connexion/templates/login.html
@@ -0,0 +1,87 @@
+{% extends 'base.html' %}
+
+{% load static %}
+
+{% block title %}
+    Lasri Del Arte - Ajouter un ingrédient
+{% 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>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="Se connecter" class="btn success">
+                <a href="/forgotten-password" class="btn danger">Mot de passe oublié ?</a>
+                <a href="/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/logout.html b/webpizza/connexion/templates/logout.html
new file mode 100644
index 0000000..38a4775
--- /dev/null
+++ b/webpizza/connexion/templates/logout.html
@@ -0,0 +1,14 @@
+{% extends 'base.html' %}
+
+{% load static %}
+
+{% block title %}
+    Lasri Del Arte - Ajouter un ingrédient
+{% 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>
+    </div>
+{% endblock %}
\ No newline at end of file
diff --git a/webpizza/connexion/tests.py b/webpizza/connexion/tests.py
new file mode 100644
index 0000000..7ce503c
--- /dev/null
+++ b/webpizza/connexion/tests.py
@@ -0,0 +1,3 @@
+from django.test import TestCase
+
+# Create your tests here.
diff --git a/webpizza/connexion/urls.py b/webpizza/connexion/urls.py
new file mode 100644
index 0000000..463e065
--- /dev/null
+++ b/webpizza/connexion/urls.py
@@ -0,0 +1,13 @@
+from django.contrib import admin
+from django.contrib.auth import views as auth_views
+from django.urls import path
+from applipizza import views
+from django.conf import settings  # new
+from django.urls import path, include  # new
+from django.conf.urls.static import static  # new
+
+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"),
+]
diff --git a/webpizza/connexion/views.py b/webpizza/connexion/views.py
new file mode 100644
index 0000000..91ea44a
--- /dev/null
+++ b/webpizza/connexion/views.py
@@ -0,0 +1,3 @@
+from django.shortcuts import render
+
+# Create your views here.
diff --git a/webpizza/webpizza/settings.py b/webpizza/webpizza/settings.py
index 51b5d88..757e1c1 100644
--- a/webpizza/webpizza/settings.py
+++ b/webpizza/webpizza/settings.py
@@ -38,6 +38,7 @@ INSTALLED_APPS = [
     'django.contrib.messages',
     'django.contrib.staticfiles',
     'applipizza',
+    'connexion'
 ]
 
 MIDDLEWARE = [
@@ -108,7 +109,7 @@ AUTH_PASSWORD_VALIDATORS = [
 # Internationalization
 # https://docs.djangoproject.com/en/4.2/topics/i18n/
 
-LANGUAGE_CODE = 'en-us'
+LANGUAGE_CODE = 'fr-fr'
 
 TIME_ZONE = 'UTC'
 
@@ -127,4 +128,5 @@ STATIC_URL = 'static/'
 
 DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
 MEDIA_ROOT = BASE_DIR/'media'
-MEDIA_URL = '/media/'
\ No newline at end of file
+MEDIA_URL = '/media/'
+LOGIN_REDIRECT_URL = "home"
\ No newline at end of file
diff --git a/webpizza/webpizza/urls.py b/webpizza/webpizza/urls.py
index 7c7f5e4..dc21fc6 100644
--- a/webpizza/webpizza/urls.py
+++ b/webpizza/webpizza/urls.py
@@ -21,17 +21,18 @@ from django.conf import settings  # new
 from django.urls import path, include  # new
 from django.conf.urls.static import static  # new
 
+
+from django.contrib import admin
+from django.contrib.auth import views as auth_views
+from django.urls import path
+from applipizza import views
+from django.conf import settings  # new
+from django.urls import path, include  # new
+from django.conf.urls.static import static  # new
+
 urlpatterns = [
-    path('admin/', admin.site.urls),
-    path('pizzas/', views.pizzas),
-    path('pizza/<int:id>', views.pizza),
-    path('pizza/<int:id>/addIngredient', views.ajouterIngredientDansPizza),
-    path('pizza/<int:pizzaId>/deleteIngredient/<int:compositionId>', views.supprimerIngredientDansPizza),
-    path('pizza/<int:id>/update', views.modifierPizza),
-    path('pizza/<int:id>/update/post', views.traitementFormulaireModificationPizza),
-    path('pizza/<int:id>/delete', views.supprimerPizza),
-    path('ingredients/add', views.formulaireCreationIngredient),
-    path('ingredients/add/post', views.creerIngredient),
+    path('', include('connexion.urls')),
+    path('', include('applipizza.urls'))
 ]
 
-urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)  # new
+urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)  # new
\ No newline at end of file
-- 
GitLab