diff --git a/kfet/statistic.py b/kfet/statistic.py index 25993901..94aa529b 100644 --- a/kfet/statistic.py +++ b/kfet/statistic.py @@ -52,7 +52,7 @@ def daynames(dates): # Pareil mais pour une liste de dates # dans un dico ordonné -def weeksnames(dates): +def weeknames(dates): names = {} for i in dates: names[i] = weekname(dates[i]) @@ -89,11 +89,18 @@ def lastweeks(nb): return mondays -# def lastmonths(nb): -# first_month_day = this_first_month_day() -# fisrt_days = {} -# for i in range(1, nb+1): -# days[i] = +def lastmonths(nb): + first_month_day = this_first_month_day() + first_days = {} + this_year = first_month_day.year + this_month = first_month_day.month + for i in range(1, nb+1): + month = this_month - (nb - i) % 12 + year = this_year + (nb - i) // 12 + first_days[i] = timezone.datetime(year=year, + month=month, + day=1) + return first_days def this_first_month_day(): diff --git a/kfet/templates/kfet/account_read.html b/kfet/templates/kfet/account_read.html index 2d91197b..cfd2fbb2 100644 --- a/kfet/templates/kfet/account_read.html +++ b/kfet/templates/kfet/account_read.html @@ -10,6 +10,30 @@ +{% if account.user == request.user %} + + +{% endif %} {% endblock %} {% block title %} @@ -51,6 +75,19 @@ {% endif %} + {% if account.user == request.user %} +
+

Statistiques

+
+
+
+

Ma consommation

+
+
+
+
+
+ {% endif %}

Historique

diff --git a/kfet/templates/kfet/account_stat_last.html b/kfet/templates/kfet/account_stat_last.html new file mode 100644 index 00000000..54d18505 --- /dev/null +++ b/kfet/templates/kfet/account_stat_last.html @@ -0,0 +1,52 @@ + + + + + + + diff --git a/kfet/urls.py b/kfet/urls.py index 162811dc..73437fb1 100644 --- a/kfet/urls.py +++ b/kfet/urls.py @@ -64,6 +64,20 @@ urlpatterns = [ permission_required('kfet.view_negs')(views.AccountNegativeList.as_view()), name = 'kfet.account.negative'), + # Account - Statistics + url('^accounts/(?P.{3})/stat/last/$', + views.AccountStatLastAll.as_view(), + name = 'kfet.account.stat.last'), + url('^accounts/(?P.{3})/stat/last/month/$', + views.AccountStatLastMonth.as_view(), + name = 'kfet.account.stat.last.month'), + url('^accounts/(?P.{3})/stat/last/week/$', + views.AccountStatLastWeek.as_view(), + name = 'kfet.account.stat.last.week'), + url('^accounts/(?P.{3})/stat/last/day/$', + views.AccountStatLastDay.as_view(), + name = 'kfet.account.stat.last.day'), + # ----- # Checkout urls # ----- @@ -124,6 +138,9 @@ urlpatterns = [ url('^articles/(?P\d+)/stat/last/$', views.ArticleStatLastAll.as_view(), name = 'kfet.article.stat.last'), + url('^articles/(?P\d+)/stat/last/month/$', + views.ArticleStatLastMonth.as_view(), + name = 'kfet.article.stat.last.month'), url('^articles/(?P\d+)/stat/last/week/$', views.ArticleStatLastWeek.as_view(), name = 'kfet.article.stat.last.week'), diff --git a/kfet/views.py b/kfet/views.py index ad9d9e61..33afe7bd 100644 --- a/kfet/views.py +++ b/kfet/views.py @@ -37,8 +37,10 @@ import django_cas_ng import hashlib import heapq import statistics -from .statistic import lastdays, daynames, this_morning,\ - tot_ventes, weeksnames, this_monday_morning, lastweeks +from .statistic import daynames, monthnames, weeknames, \ + lastdays, lastweeks, lastmonths, \ + this_morning, this_monday_morning, this_first_month_day, \ + tot_ventes @login_required def home(request): @@ -2023,6 +2025,8 @@ class ObjectResumeStat(DetailView): stat_labels = ['stat_1', 'stat_2'] stat_urls = ['url_1', 'url_2'] + def get_object_url_kwargs(self, **kwargs): + return {'pk': self.object.id} def get_context_data(self, **kwargs): # On hérite @@ -2038,7 +2042,7 @@ class ObjectResumeStat(DetailView): object_id, i), 'url': reverse_lazy(self.stat_urls[i], - args=[object_id]), + kwargs=self.get_object_url_kwargs()), } prefix = "%s_%d" % (self.id_prefix, object_id) context['id_prefix'] = prefix @@ -2049,12 +2053,151 @@ class ObjectResumeStat(DetailView): return context +# ------------------------ +# Consommation personnelle +# ------------------------ +ID_PREFIX_ACC_LAST = "last_acc" +ID_PREFIX_ACC_LAST_DAYS = "last_days_acc" +ID_PREFIX_ACC_LAST_WEEKS = "last_weeks_acc" +ID_PREFIX_ACC_LAST_MONTHS = "last_months_acc" + + +# Un résumé de toutes les vues ArticleStatLast +# NE REND PAS DE JSON +class AccountStatLastAll(ObjectResumeStat): + model = Account + context_object_name = 'account' + trigramme_url_kwarg = 'trigramme' + id_prefix = ID_PREFIX_ACC_LAST + nb_stat = 3 + nb_default = 2 + stat_labels = ["Derniers mois", "Dernières semaines", "Derniers jours"] + stat_urls = ['kfet.account.stat.last.month', + 'kfet.account.stat.last.week', + 'kfet.account.stat.last.day'] + + def get_object(self, **kwargs): + trigramme = self.kwargs.get(self.trigramme_url_kwarg) + return get_object_or_404(Account, trigramme=trigramme) + + def get_object_url_kwargs(self, **kwargs): + return {'trigramme': self.object.trigramme} + + +class AccountStatLast(HybridDetailView): + model = Account + trigramme_url_kwarg = 'trigramme' + template_name = 'kfet/account_stat_last.html' + context_object_name = 'account' + end_date = timezone.now() + id_prefix = "lol" + + # doit rendre un dictionnaire des dates + # la première date correspond au début + # la dernière date est la fin de la dernière plage + def get_dates(self, **kwargs): + pass + + # doit rendre un dictionnaire des labels + # le dernier label ne sera pas utilisé + def get_labels(self, **kwargs): + pass + + def get_object(self, **kwargs): + trigramme = self.kwargs.get(self.trigramme_url_kwarg) + return get_object_or_404(Account, trigramme=trigramme) + + def sort_operations(self, **kwargs): + # On récupère les dates + dates = self.get_dates() + # On ajoute la date de fin + extended_dates = dates.copy() + extended_dates[len(dates)+1] = self.end_date + # On selectionne les opérations qui correspondent + # à l'article en question et qui ne sont pas annulées + # puis on choisi pour chaques intervalle les opérations + # effectuées dans ces intervalles de temps + all_operations = (Operation.objects + .filter(type='purchase') + .filter(group__on_acc=self.object) + .filter(canceled_at=None) + ) + operations = {} + for i in dates: + operations[i] = (all_operations + .filter(group__at__gte=extended_dates[i]) + .filter(group__at__lte=extended_dates[i+1]) + ) + return operations + + def get_context_data(self, **kwargs): + # On hérite + # en fait non, pas besoin et c'est chiant à dumper + # context = super(AccountStat, self).get_context_data(**kwargs) + context = {} + nb_ventes = {} + # On récupère les labels des dates + context['labels'] = self.get_labels().copy() + # On compte les opérations + operations = self.sort_operations() + for i in operations: + nb_ventes[i] = tot_ventes(operations[i]) + context['nb_ventes'] = nb_ventes + # ID unique + context['chart_id'] = "%s_%d" % (self.id_prefix, + self.object.id) + return context + + +# Rend les achats pour ce compte des 7 derniers jours +# Aujourd'hui non compris +class AccountStatLastDay(AccountStatLast): + end_date = this_morning() + id_prefix = ID_PREFIX_ACC_LAST_DAYS + + def get_dates(self, **kwargs): + return lastdays(7) + + def get_labels(self, **kwargs): + days = lastdays(7) + return daynames(days) + + +# Rend les achats de ce compte des 7 dernières semaines +# La semaine en cours n'est pas comprise +class AccountStatLastWeek(AccountStatLast): + end_date = this_monday_morning() + id_prefix = ID_PREFIX_ACC_LAST_WEEKS + + def get_dates(self, **kwargs): + return lastweeks(7) + + def get_labels(self, **kwargs): + weeks = lastweeks(7) + return weeknames(weeks) + + +# Rend les achats de ce compte des 7 derniers mois +# Le mois en cours n'est pas compris +class AccountStatLastMonth(AccountStatLast): + end_date = this_monday_morning() + id_prefix = ID_PREFIX_ACC_LAST_MONTHS + + def get_dates(self, **kwargs): + return lastmonths(7) + + def get_labels(self, **kwargs): + months = lastmonths(7) + return monthnames(months) + + # ------------------------ # Article Satistiques Last # ------------------------ ID_PREFIX_ART_LAST = "last_art" ID_PREFIX_ART_LAST_DAYS = "last_days_art" ID_PREFIX_ART_LAST_WEEKS = "last_weeks_art" +ID_PREFIX_ART_LAST_MONTHS = "last_months_art" # Un résumé de toutes les vues ArticleStatLast @@ -2063,10 +2206,11 @@ class ArticleStatLastAll(ObjectResumeStat): model = Article context_object_name = 'article' id_prefix = ID_PREFIX_ART_LAST - nb_stat = 2 - nb_default = 1 - stat_labels = ["Dernières semaines", "Derniers jours"] - stat_urls = ['kfet.article.stat.last.week', + nb_stat = 3 + nb_default = 2 + stat_labels = ["Derniers mois", "Dernières semaines", "Derniers jours"] + stat_urls = ['kfet.article.stat.last.month', + 'kfet.article.stat.last.week', 'kfet.article.stat.last.day'] @@ -2172,7 +2316,20 @@ class ArticleStatLastWeek(ArticleStatLast): def get_labels(self, **kwargs): weeks = lastweeks(7) - return weeksnames(weeks) + return weeknames(weeks) + +# Rend les ventes des 7 derniers mois +# Le mois en cours n'est pas compris +class ArticleStatLastMonth(ArticleStatLast): + end_date = this_monday_morning() + id_prefix = ID_PREFIX_ART_LAST_MONTHS + + def get_dates(self, **kwargs): + return lastmonths(7) + + def get_labels(self, **kwargs): + months = lastmonths(7) + return monthnames(months) # ------------------------------ # Article Statistique Catégories