From 9598fe2299414476f0ef0e07518a0273b6cbc314 Mon Sep 17 00:00:00 2001 From: Hugo LEVY-FALK Date: Fri, 14 Dec 2018 20:25:20 +0100 Subject: [PATCH] messages --- comet/account/templates/account/balance.html | 1 + comet/account/templates/account/base.html | 131 ++++++++++++------- comet/account/views.py | 46 ++++++- 3 files changed, 123 insertions(+), 55 deletions(-) diff --git a/comet/account/templates/account/balance.html b/comet/account/templates/account/balance.html index cf1dc99..f8601d3 100644 --- a/comet/account/templates/account/balance.html +++ b/comet/account/templates/account/balance.html @@ -4,6 +4,7 @@

{{ account.firstname}} {{ account.lastname }}

Remettre le solde à 0 Recharger le compte +Imprimer

Ventes

diff --git a/comet/account/templates/account/base.html b/comet/account/templates/account/base.html index c10c721..567a7c6 100644 --- a/comet/account/templates/account/base.html +++ b/comet/account/templates/account/base.html @@ -12,82 +12,113 @@ - + +.messages li { + list-style-type: none; + padding: 10px; + border-radius: 10px; +} + +.messages .success { + background-color: #b1ddab; + color: white; +} + +.messages .warning { + background-color: #f7db7e; +} + +.messages .error { + background-color: #f7947e; + color: white; +} +
- - + -
-
-
-
-
- {% block content %}{% endblock %} -
-
-
+
+
+
+ {% if messages %} +
    + {% for message in messages %} + + {{ message }} + + {% endfor %} +
+ {% endif %}
-
+
+
+
+ {% block content %}{% endblock %} +
+
+
+
+
-
-
- - - +
+
+ + -
+ +
+
- - - - - - - + + + + + + + diff --git a/comet/account/views.py b/comet/account/views.py index e6de136..0f2d1f7 100644 --- a/comet/account/views.py +++ b/comet/account/views.py @@ -5,6 +5,7 @@ from django.shortcuts import ( ) from django.urls import reverse from django.contrib.auth.decorators import login_required +from django.contrib import messages from .models import Account, Sale, Article from .forms import SaleForm, ChooseArticleForm, AccountForm, SearchForm, ArticleForm @@ -22,6 +23,14 @@ def refill_balance(request, pk): account.calc_balance() bal = account.balance account.save() + messages.success( + request, + "Le solde de {} {} a été rechargé de {}€".format( + account.firstname, + account.lastname, + sale.price + ) + ) return redirect(reverse('account', args=[account.pk,])) return render( @@ -42,10 +51,17 @@ def pay(request, pk): if form.is_valid(): sale = form.to_sale() - sale.account = account - sale.save() - account.calc_balance() - account.save() + if float(sale.price + account.balance) >= 0: + sale.account = account + sale.save() + account.calc_balance() + account.save() + messages.success( + request, + "Paiement de {}€ réalisé avec succès.".format(sale.price) + ) + else: + messages.error(request, "Solde trop bas pour l'opération") return redirect(reverse('account', args=[account.pk,])) return render( @@ -72,8 +88,16 @@ def reset_balance(request, pk): sale.save() account.calc_balance() account.save() + messages.success( + request, + "Remise à zéro du solde réussie." + ) return redirect(reverse('account', args=[account.pk,])) - elif account.balance is 0: + elif float(account.balance) == 0: + messages.warning( + request, + "Le solde est déjà nul." + ) return redirect(reverse('account', args=[account.pk,])) return render( @@ -92,6 +116,10 @@ def create_account(request): if form.is_valid(): account = form.save() + messages.success( + request, + "Le compte a été créé." + ) return redirect(reverse('account', args=[account.pk,])) return render( @@ -139,6 +167,10 @@ def article(request, pk): form = ArticleForm(request.POST or None, instance=article) if form.is_valid(): form.save() + messages.success( + request, + "L'article a été modifié." + ) return redirect(reverse('list-articles')) return render( request, @@ -155,6 +187,10 @@ def new_article(request): form = ArticleForm(request.POST or None) if form.is_valid(): form.save() + messages.success( + request, + "L'article a été créé." + ) return redirect(reverse('list-articles')) return render( request,