diff --git a/gestion/models.py b/gestion/models.py
index a9fc147..9921418 100644
--- a/gestion/models.py
+++ b/gestion/models.py
@@ -45,6 +45,22 @@ class Product(models.Model):
def __str__(self):
return self.name
+ def user_ranking(self, pk):
+ user = User.objects.get(pk=pk)
+ consumptions = ConsumptionHistory.objects.filter(customer=user).filter(product=self)
+ # add menu
+ nb = 0
+ for consumption in consumptions:
+ nb += consumption.quantity
+ return (user, nb)
+
+ @property
+ def ranking(self):
+ users = User.objects.all()
+ ranking = [self.user_ranking(user.pk) for user in users]
+ ranking.sort(key=lambda x:x[1], reverse=True)
+ return ranking[0:25]
+
def isPinte(id):
product = Product.objects.get(id=id)
diff --git a/gestion/templates/gestion/ranking.html b/gestion/templates/gestion/ranking.html
index 204fc50..fb9d4eb 100644
--- a/gestion/templates/gestion/ranking.html
+++ b/gestion/templates/gestion/ranking.html
@@ -1,10 +1,11 @@
{% extends "base.html" %}
{%load static %}
{%block entete%}Classement{%endblock%}
-{% block nav %}
+{% block navbar %}
{% endblock %}
{% block content %}
@@ -70,4 +71,44 @@
+
+
+
+
+ Classement par produit
+
+
+
+
+ {% if product_ranking %}
+
+
+
+ Place |
+ Pseudo |
+ Quantités consommées |
+
+
+
+ {% for customer in product_ranking %}
+
+ {{ forloop.counter }} |
+ {{ customer.0.username }} |
+ {{ customer.1 }} |
+
+ {%endfor%}
+
+
+ {% endif %}
+
+
+
+
+
+{{form.media}}
{%endblock%}
\ No newline at end of file
diff --git a/gestion/views.py b/gestion/views.py
index af72595..696da59 100644
--- a/gestion/views.py
+++ b/gestion/views.py
@@ -939,7 +939,12 @@ def ranking(request):
alcohol = customer.profile.alcohol
list.append([customer, alcohol])
bestDrinkers = sorted(list, key=lambda x: x[1], reverse=True)[:25]
- return render(request, "gestion/ranking.html", {"bestBuyers": bestBuyers, "bestDrinkers": bestDrinkers})
+ form = SearchProductForm(request.POST or None)
+ if(form.is_valid()):
+ product_ranking = form.cleaned_data['product'].ranking
+ else:
+ product_ranking = None
+ return render(request, "gestion/ranking.html", {"bestBuyers": bestBuyers, "bestDrinkers": bestDrinkers, "product_ranking": product_ranking, "form": form})
########## Pinte monitoring ##########