Fetch and display if drunk

This commit is contained in:
Ludovic Stephan 2020-05-20 17:44:24 +02:00
parent e533966f55
commit 39a2d309b5
5 changed files with 51 additions and 1 deletions

View file

@ -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; }

View file

@ -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 `<i class="fa fa-beer" title="${n_units} unités"></i>`
},
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';
}

View file

@ -17,6 +17,7 @@
<link rel="stylesheet" type="text/css" href="{% static 'vendor/jquery/jquery-ui.min.css' %}">
<link rel="stylesheet" type="text/css" href="{% static 'kfet/vendor/jquery/jquery-confirm.min.css' %}">
<link rel="stylesheet" type="text/css" href="{% static 'kfet/css/index.css' %}">
<link type="text/css" rel="stylesheet" href="{% static 'gestioncof/vendor/font-awesome/css/font-awesome.min.css' %}">
{# JS #}
<script type="text/javascript" src="{% static 'kfet/vendor/js.cookie.js' %}"></script>

View file

@ -86,6 +86,7 @@
<span id="account-promo"></span>
</div>
<div id="account-email" class="data_line"></div>
<div id="alcohol-warning"></div>
<div class="buttons">
</div>
</div>

View file

@ -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)