From 8910cd36fdafd984c20ba370d20a71d90cf57f1b Mon Sep 17 00:00:00 2001
From: Sofiane Lasri <alasri250@gmail.com>
Date: Mon, 15 May 2023 11:54:03 +0200
Subject: [PATCH] =?UTF-8?q?Envoie=20du=20formulaire=20termin=C3=A9,=20TP6?=
 =?UTF-8?q?=20termin=C3=A9.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../formulaireCreationIngredient.html         | 17 ++++++++++++++-
 webpizza/applipizza/views.py                  | 21 +++++++++++++++++++
 webpizza/webpizza/urls.py                     |  3 ++-
 3 files changed, 39 insertions(+), 2 deletions(-)

diff --git a/webpizza/applipizza/templates/applipizza/formulaireCreationIngredient.html b/webpizza/applipizza/templates/applipizza/formulaireCreationIngredient.html
index bafed8c..a9d877b 100644
--- a/webpizza/applipizza/templates/applipizza/formulaireCreationIngredient.html
+++ b/webpizza/applipizza/templates/applipizza/formulaireCreationIngredient.html
@@ -8,10 +8,24 @@
 
 {% 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 action="" method="post" class="form-stacked">
+        <form action="/ingredients/add/post" method="post" class="form-stacked">
             {% csrf_token %}
             {% for field in form %}
                 {% if field.errors %}
@@ -42,6 +56,7 @@
     </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]');
diff --git a/webpizza/applipizza/views.py b/webpizza/applipizza/views.py
index 5aacd2e..c5ff076 100644
--- a/webpizza/applipizza/views.py
+++ b/webpizza/applipizza/views.py
@@ -35,3 +35,24 @@ def formulaireCreationIngredient(request):
         'applipizza/formulaireCreationIngredient.html',
         {"form": formulaire}
     )
+
+def creerIngredient(request):
+    form = IngredientForm(request.POST)
+    if form.is_valid():
+        nomIngredient = form.cleaned_data['nom']
+        ingredient = Ingredient(nom=nomIngredient)
+        ingredient.save()
+
+        formulaire = IngredientForm()
+
+        return render(
+            request,
+            'applipizza/formulaireCreationIngredient.html',
+            {"form": IngredientForm(), "status": "success", "nomIngredient": nomIngredient}
+        )
+    else:
+        return render(
+            request,
+            'applipizza/formulaireCreationIngredient.html',
+            {"form": form, "status": "error"}
+        )
\ No newline at end of file
diff --git a/webpizza/webpizza/urls.py b/webpizza/webpizza/urls.py
index 2464b72..8f3c16e 100644
--- a/webpizza/webpizza/urls.py
+++ b/webpizza/webpizza/urls.py
@@ -22,5 +22,6 @@ urlpatterns = [
     path('admin/', admin.site.urls),
     path('pizzas/', views.pizzas),
     path('pizza/<int:id>', views.pizza),
-    path('ingredients/add', views.formulaireCreationIngredient)
+    path('ingredients/add', views.formulaireCreationIngredient),
+    path('ingredients/add/post', views.creerIngredient),
 ]
-- 
GitLab