diff --git a/webpizza/applipizza/forms.py b/webpizza/applipizza/forms.py index 4dbf2062e0f3f65e5bf298b5c5acee1ac7334eec..42ebb811867f2c6987f98f2cdd887422f7e70dbb 100644 --- a/webpizza/applipizza/forms.py +++ b/webpizza/applipizza/forms.py @@ -1,9 +1,14 @@ from django.forms import ModelForm -from applipizza.models import Ingredient +from applipizza.models import Ingredient, Composition class IngredientForm(ModelForm): class Meta: model = Ingredient - fields = ['nom'] \ No newline at end of file + fields = ['nom'] + +class CompositionForm(ModelForm): + class Meta: + model = Composition + fields = ['ingredient', 'quantite'] \ No newline at end of file diff --git a/webpizza/applipizza/templates/applipizza/pizza.html b/webpizza/applipizza/templates/applipizza/pizza.html index 5489bc15f3c628c70b4ad8304308ef68f223e51f..3f1235a08bd6d535d37d27a0d2a8027bee4cc5a4 100644 --- a/webpizza/applipizza/templates/applipizza/pizza.html +++ b/webpizza/applipizza/templates/applipizza/pizza.html @@ -11,32 +11,101 @@ <h2>Pizza {{ pizza }}</h2> <p>Une délicieuse pizza traditionnelle.</p> - <h2>Composition</h2> + <h3>Composition</h3> <table class="zebra-striped" id="composition"> <thead> - <tr> - <th>Ingredient</th> - <th>Quantité</th> - </tr> + <tr> + <th>Ingredient</th> + <th>Quantité</th> + </tr> </thead> <tbody> - {% for composition in pizza.composition %} - <tr> - {% for field in composition %} - <td>{{ field }}</td> - {% endfor %} - </tr> - {% endfor %} + {% for composition in pizza.composition %} + <tr> + {% for field in composition %} + <td>{{ field }}</td> + {% endfor %} + </tr> + {% endfor %} </tbody> </table> + + {% 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 %} + <h3>Ajouter un ingrédient</h3> + <form action="/pizza/{{ pizza.id }}/addIngredient" 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" src="{% static 'applipizza/js/jquery.tablesorter.min.js' %}"></script> <script type="text/javascript"> - $(document).ready(function() { - $('#composition').tablesorter({ sortList: [[0,1]] }); + $(document).ready(function () { + $('#composition').tablesorter({sortList: [[0, 1]]}); + }); + + 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/views.py b/webpizza/applipizza/views.py index c5ff076f9126338a2bdc984a3559dba6fca82e8b..4b12dc572e8e2cb6ba16e917b574e7fc4eb90ee9 100644 --- a/webpizza/applipizza/views.py +++ b/webpizza/applipizza/views.py @@ -1,6 +1,6 @@ from django.shortcuts import render from applipizza.models import Pizza, Ingredient, Composition -from applipizza.forms import IngredientForm +from applipizza.forms import IngredientForm, CompositionForm # Create your views here. def pizzas(request): @@ -13,6 +13,8 @@ def pizzas(request): def pizza(request, id): + formulaire = CompositionForm() + pizza = Pizza.objects.get(id=id) composition = Composition.objects.filter(pizza_id=id) compositionArray = [] @@ -23,7 +25,7 @@ def pizza(request, id): return render( request, 'applipizza/pizza.html', - {'pizza': pizza} + {'pizza': pizza, 'form': formulaire} ) @@ -55,4 +57,25 @@ def creerIngredient(request): request, 'applipizza/formulaireCreationIngredient.html', {"form": form, "status": "error"} + ) + +def ajouterIngredientDansPizza(request, id): + pizza = Pizza.objects.get(id=id) + composition = Composition.objects.filter(pizza_id=id) + compositionArray = [] + for c in composition: + compositionArray.append([Ingredient.objects.get(id=c.ingredient_id).nom, c.quantite]) + pizza.composition = compositionArray + + form = CompositionForm(request.POST) + if form.is_valid(): + ingredient = form.cleaned_data['ingredient'] + quantite = form.cleaned_data['quantite'] + composition = Composition(ingredient=ingredient, quantite=quantite, pizza=pizza) + composition.save() + + return render( + request, + 'applipizza/pizza.html', + {"pizza": pizza, "status": "success"} ) \ No newline at end of file diff --git a/webpizza/webpizza/settings.py b/webpizza/webpizza/settings.py index ca79b745e53db6b13ee9e24892c5a6764387daf5..83da29b1286871ea4301837fedd60f13d3ede24f 100644 --- a/webpizza/webpizza/settings.py +++ b/webpizza/webpizza/settings.py @@ -78,7 +78,7 @@ DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'iut-dev', - 'USER': 'iut-dev-user', + 'USER': 'iut-dev', 'PASSWORD': 'p73i74KAV8lami2iyIpehE5ozic8GA', 'HOST': 'localhost', 'PORT': '3306', diff --git a/webpizza/webpizza/urls.py b/webpizza/webpizza/urls.py index 8f3c16efb5a89fcd00fb305d214bf4ef5a9e2e0f..5f12bdf751ed0a2e8fd3d985acb10d7903e2304f 100644 --- a/webpizza/webpizza/urls.py +++ b/webpizza/webpizza/urls.py @@ -22,6 +22,7 @@ urlpatterns = [ path('admin/', admin.site.urls), path('pizzas/', views.pizzas), path('pizza/<int:id>', views.pizza), + path('pizza/<int:id>/addIngredient', views.ajouterIngredientDansPizza), path('ingredients/add', views.formulaireCreationIngredient), path('ingredients/add/post', views.creerIngredient), ]