From 39a2d309b5aa52f09866fe393b539d598f68ad4f Mon Sep 17 00:00:00 2001 From: Ludovic Stephan Date: Wed, 20 May 2020 17:44:24 +0200 Subject: [PATCH] Fetch and display if drunk --- kfet/static/kfet/css/kpsul.css | 8 ++++++++ kfet/static/kfet/js/account.js | 16 ++++++++++++++++ kfet/templates/kfet/base.html | 1 + kfet/templates/kfet/kpsul.html | 1 + kfet/views.py | 26 +++++++++++++++++++++++++- 5 files changed, 51 insertions(+), 1 deletion(-) diff --git a/kfet/static/kfet/css/kpsul.css b/kfet/static/kfet/css/kpsul.css index 1616ce3e..166ef5f5 100644 --- a/kfet/static/kfet/css/kpsul.css +++ b/kfet/static/kfet/css/kpsul.css @@ -112,6 +112,14 @@ input[type=number]::-webkit-outer-spin-button { right:0; } +#account #alcohol-warning { + padding: 5px; + position: absolute; + top:0; + right:0; + font-size:35px; +} + @media (min-width: 600px) { #account_form input { font-size:60px; } diff --git a/kfet/static/kfet/js/account.js b/kfet/static/kfet/js/account.js index 5ce3c8cd..cf1e06dc 100644 --- a/kfet/static/kfet/js/account.js +++ b/kfet/static/kfet/js/account.js @@ -11,6 +11,7 @@ var Account = Backbone.Model.extend({ 'departement': '', 'nickname': '', 'trigramme': '', + 'n_units': 0, }, url: function () { @@ -45,6 +46,7 @@ var AccountView = Backbone.View.extend({ el: '#account', input: '#id_trigramme', buttons: '.buttons', + alcohol_warning: '#alcohol-warning', props: _.keys(Account.prototype.defaults), @@ -98,6 +100,15 @@ var AccountView = Backbone.View.extend({ return buttons }, + get_alcohol_warning: function () { + n_units = parseFloat(this.model.get("n_units")).toFixed(2); + + if (n_units < 4) + return ""; + + return `` + }, + render: function () { for (let prop of this.props) { var selector = "#account-" + prop; @@ -106,6 +117,7 @@ var AccountView = Backbone.View.extend({ this.$el.attr("data-balance", this.attr_data_balance()); this.$(this.buttons).html(this.get_buttons()); + this.$(this.alcohol_warning).html(this.get_alcohol_warning()); }, reset: function () { @@ -124,6 +136,10 @@ var LIQView = AccountView.extend({ return ""; }, + get_alcohol_warning: function () { + return "" + }, + attr_data_balance: function () { return 'ok'; } diff --git a/kfet/templates/kfet/base.html b/kfet/templates/kfet/base.html index 9b75af03..40b3b51b 100644 --- a/kfet/templates/kfet/base.html +++ b/kfet/templates/kfet/base.html @@ -17,6 +17,7 @@ + {# JS #} diff --git a/kfet/templates/kfet/kpsul.html b/kfet/templates/kfet/kpsul.html index 7b292087..5660e70b 100644 --- a/kfet/templates/kfet/kpsul.html +++ b/kfet/templates/kfet/kpsul.html @@ -86,6 +86,7 @@
+
diff --git a/kfet/views.py b/kfet/views.py index b6c49f72..602f800e 100644 --- a/kfet/views.py +++ b/kfet/views.py @@ -2,7 +2,7 @@ import ast import heapq import statistics from collections import defaultdict -from datetime import timedelta +from datetime import datetime, time, timedelta from decimal import Decimal from typing import List from urllib.parse import urlencode @@ -916,6 +916,29 @@ def account_read_json(request, trigramme): account = get_object_or_404(Account, trigramme=trigramme) if not account.readable: raise Http404 + + # Calcul des unités d'alcool consommées + # 1UA = 10g d'alcool (éthanol) pur + alcohol_density = 0.8 + now = timezone.localtime(timezone.now()) + # une soirée va de XXh à 06h + if time(18) <= now.time() or now.time() <= time(6): + begin_time = datetime.combine(now.date(), time(18)) + # si on est après minuit, il faut retrancher un jour + if now.time() <= time(6): + begin_time -= timedelta(days=1) + + qs = Operation.objects.filter( + group__on_acc=account, type=Operation.PURCHASE, group__at__gte=begin_time, + ).annotate( + units=F("article__volume") * F("article__abv") * alcohol_density / 100 + ) + # Sum retourne None sur un queryset vide : + # https://docs.djangoproject.com/en/3.0/ref/models/querysets/#aggregation-functions + n_units = qs.aggregate(agg=Sum("units"))["agg"] or 0 + else: + n_units = 0 + data = { "id": account.pk, "name": account.name, @@ -927,6 +950,7 @@ def account_read_json(request, trigramme): "departement": account.departement, "nickname": account.nickname, "trigramme": account.trigramme, + "n_units": n_units, } return JsonResponse(data)