Skip to content
Snippets Groups Projects
Commit 6667af33 authored by Sofiane Lasri's avatar Sofiane Lasri
Browse files

TP7 terminé (c'est honteux)

parent 8910cd36
Branches
No related tags found
No related merge requests found
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']
class CompositionForm(ModelForm):
class Meta:
model = Composition
fields = ['ingredient', 'quantite']
\ No newline at end of file
......@@ -11,7 +11,7 @@
<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>
......@@ -29,14 +29,83 @@
{% 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]]});
});
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
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}
)
......@@ -56,3 +58,24 @@ def creerIngredient(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
......@@ -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',
......
......@@ -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),
]
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment