From 96597aa14693d7ca547769dfc33350d4813c84ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Delobelle?= Date: Sun, 2 Apr 2017 05:17:26 +0200 Subject: [PATCH 01/26] clean some kfet templates --- kfet/templates/kfet/account_read.html | 45 ++++++++++++--------- kfet/templates/kfet/account_read_title.html | 5 --- kfet/templates/kfet/article_read.html | 32 ++++++++------- 3 files changed, 43 insertions(+), 39 deletions(-) delete mode 100644 kfet/templates/kfet/account_read_title.html diff --git a/kfet/templates/kfet/account_read.html b/kfet/templates/kfet/account_read.html index 50ab7f20..25e48926 100644 --- a/kfet/templates/kfet/account_read.html +++ b/kfet/templates/kfet/account_read.html @@ -13,12 +13,17 @@ {% if account.user == request.user %} - {% endif %} @@ -66,22 +71,22 @@ {% if account.user == request.user %}

Statistiques

-
-
-
-

Ma balance

-
-
+
+
+
+

Ma balance

+
-
-
-
-
-

Ma consommation

-
-
+
+
+
+
+
+

Ma consommation

+
-
+
+
{% endif %}
diff --git a/kfet/templates/kfet/account_read_title.html b/kfet/templates/kfet/account_read_title.html deleted file mode 100644 index 6712ed77..00000000 --- a/kfet/templates/kfet/account_read_title.html +++ /dev/null @@ -1,5 +0,0 @@ -{% if account.user == request.user %} - Mon compte -{% else %} - Informations du compte {{ account.trigramme }} -{% endif %} diff --git a/kfet/templates/kfet/article_read.html b/kfet/templates/kfet/article_read.html index 35b484a5..a980cc75 100644 --- a/kfet/templates/kfet/article_read.html +++ b/kfet/templates/kfet/article_read.html @@ -1,6 +1,11 @@ {% extends 'kfet/base.html' %} {% load staticfiles %} +{% block extra_head %} + + +{% endblock %} + {% block title %}Informations sur l'article {{ article }}{% endblock %} {% block content-header-title %}Article - {{ article.name }}{% endblock %} @@ -82,27 +87,26 @@

Statistiques

-
-
-
-

Ventes de {{ article.name }}

-
-
+
+
+
+

Ventes de {{ article.name }}

+
-
+
+
{% endblock %} -{% block extra_head %} - - - {% endblock %} From e8fdd083aae2301240e6906355c2b14cbf295cdb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Delobelle?= Date: Sun, 2 Apr 2017 05:34:34 +0200 Subject: [PATCH 02/26] delete unused class-views --- kfet/views.py | 37 +++---------------------------------- 1 file changed, 3 insertions(+), 34 deletions(-) diff --git a/kfet/views.py b/kfet/views.py index 823a205d..14fc0b3b 100644 --- a/kfet/views.py +++ b/kfet/views.py @@ -2034,43 +2034,12 @@ class JSONResponseMixin(object): return context -class JSONDetailView(JSONResponseMixin, - BaseDetailView): - """ - Returns a DetailView that renders a JSON - """ +class JSONDetailView(JSONResponseMixin, BaseDetailView): + """Returns a DetailView that renders a JSON.""" + def render_to_response(self, context): return self.render_to_json_response(context) -class HybridDetailView(JSONResponseMixin, - SingleObjectTemplateResponseMixin, - BaseDetailView): - """ - Returns a DetailView as an html page except if a JSON file is requested - by the GET method in which case it returns a JSON response. - """ - def render_to_response(self, context): - # Look for a 'format=json' GET argument - if self.request.GET.get('format') == 'json': - return self.render_to_json_response(context) - else: - return super(HybridDetailView, self).render_to_response(context) - - -class HybridListView(JSONResponseMixin, - MultipleObjectTemplateResponseMixin, - BaseListView): - """ - Returns a ListView as an html page except if a JSON file is requested - by the GET method in which case it returns a JSON response. - """ - def render_to_response(self, context): - # Look for a 'format=json' GET argument - if self.request.GET.get('format') == 'json': - return self.render_to_json_response(context) - else: - return super(HybridListView, self).render_to_response(context) - class ObjectResumeStat(JSONDetailView): """ From 78aa5df350a5344dda67f1c329011b399eaed06b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Delobelle?= Date: Sun, 2 Apr 2017 12:55:44 +0200 Subject: [PATCH 03/26] fix template error --- kfet/templates/kfet/article_read.html | 1 - 1 file changed, 1 deletion(-) diff --git a/kfet/templates/kfet/article_read.html b/kfet/templates/kfet/article_read.html index a980cc75..9f2e7128 100644 --- a/kfet/templates/kfet/article_read.html +++ b/kfet/templates/kfet/article_read.html @@ -109,4 +109,3 @@ $(document).ready(function() { ); }); -{% endblock %} From f6022ecf7d4f8d48133123e90b619b7c9101bbda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Delobelle?= Date: Sun, 2 Apr 2017 16:49:41 +0200 Subject: [PATCH 04/26] Add str to Transfer model + PEP8 this model --- kfet/models.py | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/kfet/models.py b/kfet/models.py index b4af61c1..18929c4b 100644 --- a/kfet/models.py +++ b/kfet/models.py @@ -476,24 +476,29 @@ class TransferGroup(models.Model): related_name = "+", blank = True, null = True, default = None) + class Transfer(models.Model): group = models.ForeignKey( - TransferGroup, on_delete = models.PROTECT, - related_name = "transfers") + TransferGroup, on_delete=models.PROTECT, + related_name="transfers") from_acc = models.ForeignKey( - Account, on_delete = models.PROTECT, - related_name = "transfers_from") + Account, on_delete=models.PROTECT, + related_name="transfers_from") to_acc = models.ForeignKey( - Account, on_delete = models.PROTECT, - related_name = "transfers_to") - amount = models.DecimalField(max_digits = 6, decimal_places = 2) + Account, on_delete=models.PROTECT, + related_name="transfers_to") + amount = models.DecimalField(max_digits=6, decimal_places=2) # Optional canceled_by = models.ForeignKey( - Account, on_delete = models.PROTECT, - null = True, blank = True, default = None, - related_name = "+") + Account, on_delete=models.PROTECT, + null=True, blank=True, default=None, + related_name="+") canceled_at = models.DateTimeField( - null = True, blank = True, default = None) + null=True, blank=True, default=None) + + def __str__(self): + return '{} -> {}: {}€'.format(self.from_acc, self.to_acc, self.amount) + class OperationGroup(models.Model): on_acc = models.ForeignKey( From 87b9db520f5c73be52d6a3e8841217b376544d59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Delobelle?= Date: Sun, 2 Apr 2017 17:03:20 +0200 Subject: [PATCH 05/26] Refactor py base stats and account balance stats New mixin: PkUrlMixin - use with SingleObjectMixin standard django mixin (used by DetailView...) - `get_object` use field declared in `pk_url_kwarg` to get... the object SingleResumeStat - clean (part of) py code AccountStatBalanceList - renamed from `AccountStatBalanceAll` - url modified - add permission checking (only the connected user can get balance stats manifest) - clean py code AccountStatBalance - cleaner filtering management - merge urls using this class - clean py code --- kfet/urls.py | 13 +-- kfet/views.py | 270 +++++++++++++++++++++++++------------------------- 2 files changed, 141 insertions(+), 142 deletions(-) diff --git a/kfet/urls.py b/kfet/urls.py index 94a9eaec..ddd40ce6 100644 --- a/kfet/urls.py +++ b/kfet/urls.py @@ -82,15 +82,12 @@ urlpatterns = [ views.AccountStatLastDay.as_view(), name = 'kfet.account.stat.last.day'), - url('^accounts/(?P.{3})/stat/balance/$', - views.AccountStatBalanceAll.as_view(), - name = 'kfet.account.stat.balance'), - url('^accounts/(?P.{3})/stat/balance/d/(?P\d*)/$', + url(r'^accounts/(?P.{3})/stat/balance/list$', + views.AccountStatBalanceList.as_view(), + name='kfet.account.stat.balance.list'), + url(r'^accounts/(?P.{3})/stat/balance$', views.AccountStatBalance.as_view(), - name = 'kfet.account.stat.balance.days'), - url('^accounts/(?P.{3})/stat/balance/anytime/$', - views.AccountStatBalance.as_view(), - name = 'kfet.account.stat.balance.anytime'), + name='kfet.account.stat.balance'), # ----- # Checkout urls diff --git a/kfet/views.py b/kfet/views.py index 14fc0b3b..b0c90083 100644 --- a/kfet/views.py +++ b/kfet/views.py @@ -1,12 +1,13 @@ # -*- coding: utf-8 -*- +from urllib.parse import urlencode + from django.shortcuts import render, get_object_or_404, redirect from django.core.exceptions import PermissionDenied from django.core.cache import cache from django.views.generic import ListView, DetailView, TemplateView -from django.views.generic.list import BaseListView, MultipleObjectTemplateResponseMixin -from django.views.generic.detail import BaseDetailView, SingleObjectTemplateResponseMixin -from django.views.generic.edit import CreateView, UpdateView, DeleteView, FormView +from django.views.generic.detail import BaseDetailView +from django.views.generic.edit import CreateView, UpdateView from django.core.urlresolvers import reverse, reverse_lazy from django.contrib import messages from django.contrib.messages.views import SuccessMessageMixin @@ -2041,49 +2042,46 @@ class JSONDetailView(JSONResponseMixin, BaseDetailView): return self.render_to_json_response(context) -class ObjectResumeStat(JSONDetailView): +class PkUrlMixin(object): + + def get_object(self, *args, **kwargs): + get_by = self.kwargs.get(self.pk_url_kwarg) + return get_object_or_404(self.model, **{self.pk_url_kwarg: get_by}) + + +class SingleResumeStat(JSONDetailView): """ Summarize all the stats of an object Handles JSONResponse + """ - context_object_name = '' id_prefix = '' - # nombre de vues à résumer - nb_stat = 2 - # Le combienième est celui par defaut ? - # (entre 0 et nb_stat-1) nb_default = 0 - stat_labels = ['stat_1', 'stat_2'] - stat_urls = ['url_1', 'url_2'] - # sert à renverser les urls - # utile de le surcharger quand l'url prend d'autres arguments que l'id - def get_object_url_kwargs(self, **kwargs): - return {'pk': self.object.id} - - def url_kwargs(self, **kwargs): - return [{}] * self.nb_stat + stats = [] + url_stat = None def get_context_data(self, **kwargs): # On n'hérite pas object_id = self.object.id - url_kwargs = self.url_kwargs() context = {} - stats = {} - for i in range(self.nb_stat): - stats[i] = { - 'label': self.stat_labels[i], - 'btn': "btn_%s_%d_%d" % (self.id_prefix, - object_id, - i), - 'url': reverse(self.stat_urls[i], - kwargs=dict( - self.get_object_url_kwargs(), - **url_kwargs[i] - ), - ), - } - prefix = "%s_%d" % (self.id_prefix, object_id) + stats = [] + prefix = '{}_{}'.format(self.id_prefix, object_id) + for i, stat_def in enumerate(self.stats): + url_pk = getattr(self.object, self.pk_url_kwarg) + url_params_d = stat_def.get('url_params', {}) + if len(url_params_d) > 0: + url_params = '?{}'.format(urlencode(url_params_d)) + else: + url_params = '' + stats.append({ + 'label': stat_def['label'], + 'btn': 'btn_{}_{}'.format(prefix, i), + 'url': '{url}{params}'.format( + url=reverse(self.url_stat, args=[url_pk]), + params=url_params, + ), + }) context['id_prefix'] = prefix context['content_id'] = "content_%s" % prefix context['stats'] = stats @@ -2100,39 +2098,47 @@ ID_PREFIX_ACC_BALANCE = "balance_acc" # Un résumé de toutes les vues ArticleStatBalance # REND DU JSON -class AccountStatBalanceAll(ObjectResumeStat): +class AccountStatBalanceList(PkUrlMixin, SingleResumeStat): model = Account context_object_name = 'account' - trigramme_url_kwarg = 'trigramme' + pk_url_kwarg = 'trigramme' + url_stat = 'kfet.account.stat.balance' id_prefix = ID_PREFIX_ACC_BALANCE - nb_stat = 5 + stats = [ + { + 'label': 'Tout le temps', + }, + { + 'label': '1 an', + 'url_params': {'last_days': 365}, + }, + { + 'label': '6 mois', + 'url_params': {'last_days': 183}, + }, + { + 'label': '3 mois', + 'url_params': {'last_days': 90}, + }, + { + 'label': '30 jours', + 'url_params': {'last_days': 30}, + }, + ] nb_default = 0 - stat_labels = ["Tout le temps", "1 an", "6 mois", "3 mois", "30 jours"] - stat_urls = ['kfet.account.stat.balance.anytime'] \ - + ['kfet.account.stat.balance.days'] * 4 - 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} - - def url_kwargs(self, **kwargs): - context_list = (super(AccountStatBalanceAll, self) - .url_kwargs(**kwargs)) - context_list[1] = {'nb_date': 365} - context_list[2] = {'nb_date': 183} - context_list[3] = {'nb_date': 90} - context_list[4] = {'nb_date': 30} - return context_list + def get_object(self, *args, **kwargs): + obj = super().get_object(*args, **kwargs) + if self.request.user != obj.user: + raise PermissionDenied + return obj @method_decorator(login_required) def dispatch(self, *args, **kwargs): - return super(AccountStatBalanceAll, self).dispatch(*args, **kwargs) + return super().dispatch(*args, **kwargs) -class AccountStatBalance(JSONDetailView): +class AccountStatBalance(PkUrlMixin, JSONDetailView): """ Returns a JSON containing the evolution a the personnal balance of a trigramme between timezone.now() and `nb_days` @@ -2141,44 +2147,38 @@ class AccountStatBalance(JSONDetailView): does not takes into account the balance offset """ model = Account - trigramme_url_kwarg = 'trigramme' - nb_date_url_kwargs = 'nb_date' + pk_url_kwarg = 'trigramme' context_object_name = 'account' - id_prefix = "" - def get_object(self, **kwargs): - trigramme = self.kwargs.get(self.trigramme_url_kwarg) - return get_object_or_404(Account, trigramme=trigramme) - - def get_changes_list(self, **kwargs): + def get_changes_list(self, last_days=None, begin_date=None, end_date=None): account = self.object - nb_date = self.kwargs.get(self.nb_date_url_kwargs, None) - end_date = this_morning() - if nb_date is None: - begin_date = timezone.datetime(year=1980, month=1, day=1) - anytime = True - else: - begin_date = this_morning() \ - - timezone.timedelta(days=int(nb_date)) - anytime = False - # On récupère les opérations + + # prepare filters + if end_date is None: + end_date = this_morning() + + if last_days is not None: + begin_date = end_date - timezone.timedelta(days=last_days) + + # prepare querysets # TODO: retirer les opgroup dont tous les op sont annulées - opgroups = list(OperationGroup.objects - .filter(on_acc=account) - .filter(at__gte=begin_date) - .filter(at__lte=end_date)) - # On récupère les transferts reçus - received_transfers = list(Transfer.objects - .filter(to_acc=account) - .filter(canceled_at=None) - .filter(group__at__gte=begin_date) - .filter(group__at__lte=end_date)) - # On récupère les transferts émis - emitted_transfers = list(Transfer.objects - .filter(from_acc=account) - .filter(canceled_at=None) - .filter(group__at__gte=begin_date) - .filter(group__at__lte=end_date)) + opegroups = OperationGroup.objects.filter(on_acc=account) + recv_transfers = Transfer.objects.filter(to_acc=account, + canceled_at=None) + sent_transfers = Transfer.objects.filter(from_acc=account, + canceled_at=None) + + # apply filters + if begin_date is not None: + opegroups = opegroups.filter(at__gte=begin_date) + recv_transfers = recv_transfers.filter(group__at__gte=begin_date) + sent_transfers = sent_transfers.filter(group__at__gte=begin_date) + + if end_date is not None: + opegroups = opegroups.filter(at__lte=end_date) + recv_transfers = recv_transfers.filter(group__at__lte=end_date) + sent_transfers = sent_transfers.filter(group__at__lte=end_date) + # On transforme tout ça en une liste de dictionnaires sous la forme # {'at': date, # 'amount': changement de la balance (négatif si diminue la balance, @@ -2188,72 +2188,74 @@ class AccountStatBalance(JSONDetailView): # sera mis à jour lors d'une # autre passe) # } - actions = [ - # Maintenant (à changer si on gère autre chose que now) - { + actions = [] + if begin_date is not None: + actions.append({ + 'at': begin_date.isoformat(), + 'amount': 0, + 'label': "début", + 'balance': 0, + }) + if end_date is not None: + actions.append({ 'at': end_date.isoformat(), - 'amout': 0, + 'amount': 0, 'label': "actuel", 'balance': 0, - } - ] + [ + }) + + actions += [ { - 'at': op.at.isoformat(), - 'amount': op.amount, - 'label': str(op), + 'at': ope_grp.at.isoformat(), + 'amount': ope_grp.amount, + 'label': str(ope_grp), 'balance': 0, - } for op in opgroups + } for ope_grp in opegroups ] + [ { 'at': tr.group.at.isoformat(), 'amount': tr.amount, - 'label': "%d€: %s -> %s" % (tr.amount, - tr.from_acc.trigramme, - tr.to_acc.trigramme), + 'label': str(tr), 'balance': 0, - } for tr in received_transfers + } for tr in recv_transfers ] + [ { 'at': tr.group.at.isoformat(), 'amount': -tr.amount, - 'label': "%d€: %s -> %s" % (tr.amount, - tr.from_acc.trigramme, - tr.to_acc.trigramme), + 'label': str(tr), 'balance': 0, - } for tr in emitted_transfers + } for tr in sent_transfers ] - if not anytime: - actions += [ - # Date de début : - { - 'at': begin_date.isoformat(), - 'amount': 0, - 'label': "début", - 'balance': 0, - } - ] # Maintenant on trie la liste des actions par ordre du plus récent # an plus ancien et on met à jour la balance actions = sorted(actions, key=lambda k: k['at'], reverse=True) actions[0]['balance'] = account.balance for i in range(len(actions)-1): - actions[i+1]['balance'] = actions[i]['balance'] \ - - actions[i+1]['amount'] + actions[i+1]['balance'] = \ + actions[i]['balance'] - actions[i+1]['amount'] return actions - def get_context_data(self, **kwargs): + def get_context_data(self, *args, **kwargs): context = {} - changes = self.get_changes_list() - nb_days = self.kwargs.get(self.nb_date_url_kwargs, None) - if nb_days is None: - nb_days_string = 'anytime' - else: - nb_days_string = str(int(nb_days)) - context['charts'] = [ { "color": "rgb(255, 99, 132)", - "label": "Balance", - "values": changes } ] + + last_days = self.request.GET.get('last_days', None) + if last_days is not None: + last_days = int(last_days) + begin_date = self.request.GET.get('begin_date', None) + end_date = self.request.GET.get('end_date', None) + + changes = self.get_changes_list( + last_days=last_days, + begin_date=begin_date, end_date=end_date, + ) + + context['charts'] = [{ + "color": "rgb(255, 99, 132)", + "label": "Balance", + "values": changes, + }] context['is_time_chart'] = True - context['min_date'] = changes[len(changes)-1]['at'] + context['min_date'] = changes[-1]['at'] context['max_date'] = changes[0]['at'] # TODO: offset return context @@ -2274,7 +2276,7 @@ ID_PREFIX_ACC_LAST_MONTHS = "last_months_acc" # Un résumé de toutes les vues ArticleStatLast # NE REND PAS DE JSON -class AccountStatLastAll(ObjectResumeStat): +class AccountStatLastAll(SingleResumeStat): model = Account context_object_name = 'account' trigramme_url_kwarg = 'trigramme' @@ -2419,7 +2421,7 @@ ID_PREFIX_ART_LAST_MONTHS = "last_months_art" # Un résumé de toutes les vues ArticleStatLast # NE REND PAS DE JSON -class ArticleStatLastAll(ObjectResumeStat): +class ArticleStatLastAll(SingleResumeStat): model = Article context_object_name = 'article' id_prefix = ID_PREFIX_ART_LAST From 1ee993e1e11a0318de6ae1251eb57e30655c3c91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Delobelle?= Date: Sun, 2 Apr 2017 17:14:36 +0200 Subject: [PATCH 06/26] Add permission check to AccountStatBalance Only connected user can get its balance data --- kfet/views.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/kfet/views.py b/kfet/views.py index b0c90083..58413d80 100644 --- a/kfet/views.py +++ b/kfet/views.py @@ -2260,6 +2260,12 @@ class AccountStatBalance(PkUrlMixin, JSONDetailView): # TODO: offset return context + def get_object(self, *args, **kwargs): + obj = super().get_object(*args, **kwargs) + if self.request.user != obj.user: + raise PermissionDenied + return obj + @method_decorator(login_required) def dispatch(self, *args, **kwargs): return super(AccountStatBalance, self).dispatch(*args, **kwargs) From df7e935390aceea21e5afbc9be9ef80f4693f9b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Delobelle?= Date: Sun, 2 Apr 2017 17:16:05 +0200 Subject: [PATCH 07/26] Clean GET params in ajax calls - Use `data` arg of `$.getJSON` for `format` param - Delete `dictToArray` call on data returned by `SingleResumeStat` class view since this view now returns stats manifest as an array --- kfet/static/kfet/js/statistic.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/kfet/static/kfet/js/statistic.js b/kfet/static/kfet/js/statistic.js index 7ab56f1d..e6cc6132 100644 --- a/kfet/static/kfet/js/statistic.js +++ b/kfet/static/kfet/js/statistic.js @@ -42,8 +42,7 @@ $(this).addClass("focus"); // loads data and shows it - $.getJSON(this.stats_target_url + "?format=json", - displayStats); + $.getJSON(this.stats_target_url, {format: 'json'}, displayStats); } function displayStats (data) { @@ -158,7 +157,7 @@ "aria-label": "select-period"}); var to_click; - var context = dictToArray(data.stats); + var context = data.stats; for (var i = 0; i < context.length; i++) { // creates the button @@ -191,7 +190,7 @@ // constructor (function () { - $.getJSON(url + "?format=json", initialize); + $.getJSON(url, {format: 'json'}, initialize); })(); }; })(jQuery); From 48721b7dcafe376aa6a4b3d6d153922fabf96963 Mon Sep 17 00:00:00 2001 From: Ludovic Stephan Date: Sun, 2 Apr 2017 17:54:13 -0300 Subject: [PATCH 08/26] Reduce graph size --- kfet/static/kfet/css/index.css | 1 + kfet/templates/kfet/account_read.html | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/kfet/static/kfet/css/index.css b/kfet/static/kfet/css/index.css index 563d3839..a65372da 100644 --- a/kfet/static/kfet/css/index.css +++ b/kfet/static/kfet/css/index.css @@ -105,6 +105,7 @@ textarea { .panel-md-margin{ background-color: white; + overflow:hidden; padding-left: 15px; padding-right: 15px; padding-bottom: 15px; diff --git a/kfet/templates/kfet/account_read.html b/kfet/templates/kfet/account_read.html index 50ab7f20..be27192e 100644 --- a/kfet/templates/kfet/account_read.html +++ b/kfet/templates/kfet/account_read.html @@ -70,7 +70,7 @@

Ma balance

-
+
@@ -78,7 +78,7 @@

Ma consommation

-
+
From 31261fd3761aa25be035d08127c2244d6c59a64a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Delobelle?= Date: Sun, 2 Apr 2017 23:38:42 +0200 Subject: [PATCH 09/26] set height canvas graph & fix graph display --- kfet/static/kfet/js/statistic.js | 2 +- kfet/templates/kfet/account_read.html | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/kfet/static/kfet/js/statistic.js b/kfet/static/kfet/js/statistic.js index e6cc6132..f6d21237 100644 --- a/kfet/static/kfet/js/statistic.js +++ b/kfet/static/kfet/js/statistic.js @@ -138,7 +138,7 @@ var prev_chart = content.children(); // creates a blank canvas element and attach it to the DOM - var canvas = $(""); + var canvas = $(""); content.append(canvas); // create the chart diff --git a/kfet/templates/kfet/account_read.html b/kfet/templates/kfet/account_read.html index 8333d0f0..449914f9 100644 --- a/kfet/templates/kfet/account_read.html +++ b/kfet/templates/kfet/account_read.html @@ -75,7 +75,7 @@ $(document).ready(function() {

Ma balance

-
+
@@ -83,7 +83,7 @@ $(document).ready(function() {

Ma consommation

-
+
From f585247224180840fb12825bbaa468006e0cd56a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Delobelle?= Date: Mon, 3 Apr 2017 00:40:52 +0200 Subject: [PATCH 10/26] Refactor Account Operations stats MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit K-Fêt - Statistics New base class - StatScale - create scale, given chunk size (timedelta), start and end times - get labels of - get start and end datetimes of chunks DayStatScale: Scale whose chunks interval is 1 day WeekStatScale: same with 1 week MonthStatScale: same with 1 month AccountStatOperationList: manifest of operations stats of an account - renamed from AccountStatLastAll - updated according to SingleResumeStat AccountStatOperation: - renamed from AccountStatLast - remove scale logic with use of StatScale objects - used scale is given by `scale` and `scale_args` GET params - add filter on operations types with `types` GET param AccountStatLast(Day,Week,Month) are deleted ("merged" in AccountStatOperation) --- kfet/statistic.py | 149 +++++++++++++++++++++++------------- kfet/urls.py | 18 ++--- kfet/views.py | 191 ++++++++++++++++++++++------------------------ 3 files changed, 195 insertions(+), 163 deletions(-) diff --git a/kfet/statistic.py b/kfet/statistic.py index 09f9e935..5b5e297a 100644 --- a/kfet/statistic.py +++ b/kfet/statistic.py @@ -1,70 +1,117 @@ # -*- coding: utf-8 -*- + +from dateutil.relativedelta import relativedelta + from django.utils import timezone from django.db.models import Sum KFET_WAKES_UP_AT = 7 -# donne le nom des jours d'une liste de dates -# dans un dico ordonné -def daynames(dates): - names = {} - for i in dates: - names[i] = dates[i].strftime("%A") - return names + +def kfet_day(year, month, day, start_at=KFET_WAKES_UP_AT): + return timezone.datetime(year, month, day, hour=start_at) -# donne le nom des semaines une liste de dates -# dans un dico ordonné -def weeknames(dates): - names = {} - for i in dates: - names[i] = dates[i].strftime("Semaine %W") - return names +def to_kfet_day(dt, start_at=KFET_WAKES_UP_AT): + kfet_dt = kfet_day(year=dt.year, month=dt.month, day=dt.day) + if dt.hour < start_at: + kfet_dt -= timezone.timedelta(days=1) + return kfet_dt -# donne le nom des mois d'une liste de dates -# dans un dico ordonné -def monthnames(dates): - names = {} - for i in dates: - names[i] = dates[i].strftime("%B") - return names +class StatScale(object): + name = None + step = None + + def __init__(self, n_steps=0, begin=None, end=None, + last=False, std_chunk=True): + self.std_chunk = std_chunk + if last: + end = timezone.now() + + if begin is not None and n_steps != 0: + self.begin = self.get_from(begin) + self.end = self.do_step(self.begin, n_steps=n_steps) + elif end is not None and n_steps != 0: + self.end = self.get_from(end) + self.begin = self.do_step(self.end, n_steps=-n_steps) + elif begin is not None and end is not None: + self.begin = self.get_from(begin) + self.end = self.get_from(end) + else: + raise Exception('Two of these args must be specified: ' + 'n_steps, begin, end; ' + 'or use last and n_steps') + + self.datetimes = self.get_datetimes() + + @staticmethod + def by_name(name): + for cls in StatScale.__subclasses__(): + if cls.name == name: + return cls + raise Exception('scale %s not found' % name) + + def get_from(self, dt): + return self.std_chunk and self.get_chunk_start(dt) or dt + + def __getitem__(self, i): + return self.datetimes[i], self.datetimes[i+1] + + def __len__(self): + return len(self.datetimes) - 1 + + def do_step(self, dt, n_steps=1): + return dt + self.step * n_steps + + def get_datetimes(self): + datetimes = [self.begin] + tmp = self.begin + while tmp <= self.end: + tmp = self.do_step(tmp) + datetimes.append(tmp) + return datetimes + + def get_labels(self, label_fmt=None): + if label_fmt is None: + label_fmt = self.label_fmt + return [begin.strftime(label_fmt) for begin, end in self] + + @classmethod + def get_chunk_start(cls, dt): + dt_kfet = to_kfet_day(dt) + start = dt_kfet - cls.offset_to_chunk_start(dt_kfet) + return start -# rend les dates des nb derniers jours -# dans l'ordre chronologique -# aujourd'hui compris -# nb = 1 : rend hier -def lastdays(nb): - morning = this_morning() - days = {} - for i in range(1, nb+1): - days[i] = morning - timezone.timedelta(days=nb - i + 1) - return days +class DayStatScale(StatScale): + name = 'day' + step = timezone.timedelta(days=1) + label_fmt = '%A' + + @classmethod + def get_chunk_start(cls, dt): + return to_kfet_day(dt) -def lastweeks(nb): - monday_morning = this_monday_morning() - mondays = {} - for i in range(1, nb+1): - mondays[i] = monday_morning \ - - timezone.timedelta(days=7*(nb - i + 1)) - return mondays +class WeekStatScale(StatScale): + name = 'week' + step = timezone.timedelta(days=7) + label_fmt = 'Semaine %W' + + @classmethod + def offset_to_chunk_start(cls, dt): + return timezone.timedelta(days=dt.weekday()) -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 - 1 - (nb - i)) % 12) + 1 - year = this_year + (nb - i) // 12 - first_days[i] = timezone.datetime(year=year, - month=month, - day=1, - hour=KFET_WAKES_UP_AT) - return first_days +class MonthStatScale(StatScale): + name = 'month' + step = relativedelta(months=1) + label_fmt = '%B' + + @classmethod + def get_chunk_start(cls, dt): + return to_kfet_day(dt).replace(day=1) def this_first_month_day(): diff --git a/kfet/urls.py b/kfet/urls.py index ddd40ce6..2eeef513 100644 --- a/kfet/urls.py +++ b/kfet/urls.py @@ -69,18 +69,12 @@ urlpatterns = [ 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'), + url('^accounts/(?P.{3})/stat/operations/list$', + views.AccountStatOperationList.as_view(), + name='kfet.account.stat.operation.list'), + url('^accounts/(?P.{3})/stat/operations$', + views.AccountStatOperation.as_view(), + name='kfet.account.stat.operation'), url(r'^accounts/(?P.{3})/stat/balance/list$', views.AccountStatBalanceList.as_view(), diff --git a/kfet/views.py b/kfet/views.py index 58413d80..0b62cd2e 100644 --- a/kfet/views.py +++ b/kfet/views.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- +import ast from urllib.parse import urlencode from django.shortcuts import render, get_object_or_404, redirect @@ -47,10 +48,10 @@ from decimal import Decimal import django_cas_ng import heapq import statistics -from .statistic import daynames, monthnames, weeknames, \ - lastdays, lastweeks, lastmonths, \ - this_morning, this_monday_morning, this_first_month_day, \ - tot_ventes +from kfet.statistic import DayStatScale, MonthStatScale, StatScale, WeekStatScale +from .statistic import ( + this_morning, this_monday_morning, this_first_month_day, tot_ventes, +) class Home(TemplateView): template_name = "kfet/home.html" @@ -2282,138 +2283,128 @@ ID_PREFIX_ACC_LAST_MONTHS = "last_months_acc" # Un résumé de toutes les vues ArticleStatLast # NE REND PAS DE JSON -class AccountStatLastAll(SingleResumeStat): +class AccountStatOperationList(PkUrlMixin, SingleResumeStat): model = Account context_object_name = 'account' - trigramme_url_kwarg = 'trigramme' + pk_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'] + stats = [ + { + 'label': 'Derniers mois', + 'url_params': dict( + types=[Operation.PURCHASE], + scale=MonthStatScale.name, + scale_args=dict( + last=True, + n_steps=7, + ), + ), + }, + { + 'label': 'Dernières semaines', + 'url_params': dict( + types=[Operation.PURCHASE], + last_days=49, + scale=WeekStatScale.name, + scale_args=dict( + last=True, + n_steps=7, + ), + ), + }, + { + 'label': 'Derniers jours', + 'url_params': dict( + types=[Operation.PURCHASE], + last_days=7, + scale=DayStatScale.name, + scale_args=dict( + last=True, + n_steps=7, + ), + ), + }, + ] + url_stat = 'kfet.account.stat.operation' - 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} + def get_object(self, *args, **kwargs): + obj = super().get_object(*args, **kwargs) + if self.request.user != obj.user: + raise PermissionDenied + return obj @method_decorator(login_required) def dispatch(self, *args, **kwargs): - return super(AccountStatLastAll, self).dispatch(*args, **kwargs) + return super().dispatch(*args, **kwargs) -class AccountStatLast(JSONDetailView): +class AccountStatOperation(PkUrlMixin, JSONDetailView): """ Returns a JSON containing the evolution a the personnal consommation of a trigramme at the diffent dates specified """ model = Account - trigramme_url_kwarg = 'trigramme' + pk_url_kwarg = 'trigramme' context_object_name = 'account' - end_date = timezone.now() id_prefix = "" - # 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): - return {} - - # 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 + def get_operations(self, types, scale): # 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(type__in=types) .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]) - ) + operations = [] + for begin, end in scale: + operations.append(all_operations + .filter(group__at__gte=begin, + group__at__lte=end)) return operations def get_context_data(self, **kwargs): context = {} - nb_ventes = {} + + scale = self.request.GET.get('scale', None) + if scale is None: + scale = DayStatScale(n_steps=7, last=True) + else: + scale_cls = StatScale.by_name(scale) + scale_args = self.request.GET.get('scale_args', '{}') + scale = scale_cls(**ast.literal_eval(scale_args)) + print(scale.datetimes) + + types = self.request.GET.get('types', None) + if types is not None: + types = ast.literal_eval(types) + + operations = self.get_operations(types=types, scale=scale) # On récupère les labels des dates - context['labels'] = self.get_labels().copy() + context['labels'] = scale.get_labels() # On compte les opérations - operations = self.sort_operations() - for i in operations: - nb_ventes[i] = tot_ventes(operations[i]) - context['charts'] = [ { "color": "rgb(255, 99, 132)", - "label": "NB items achetés", - "values": nb_ventes } ] + nb_ventes = [] + for chunk in operations: + nb_ventes.append(tot_ventes(chunk)) + + context['labels'] = scale.get_labels() + context['charts'] = [{"color": "rgb(255, 99, 132)", + "label": "NB items achetés", + "values": nb_ventes}] return context + def get_object(self, *args, **kwargs): + obj = super().get_object(*args, **kwargs) + if self.request.user != obj.user: + raise PermissionDenied + return obj + @method_decorator(login_required) def dispatch(self, *args, **kwargs): - return super(AccountStatLast, self).dispatch(*args, **kwargs) - - -# 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) + return super().dispatch(*args, **kwargs) # ------------------------ From c01de558e1e1fd64006fe1559bcc968d9de42725 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Delobelle?= Date: Mon, 3 Apr 2017 03:12:52 +0200 Subject: [PATCH 11/26] Clean Article stats kfet.statistic - delete no longer used defs - new mixin - ScaleMixin - get scale args from GET params - chunkify querysets according to a scale Article stats - use SingleResumeStat for manifest - use ScaleMixin for sales - update urls - update permission required: teamkfet Account stats - update permission required: teamkfet - operations use ScaleMixin - fix manifests urls --- kfet/statistic.py | 93 ++++++++--- kfet/templates/kfet/account_read.html | 4 +- kfet/templates/kfet/article_read.html | 7 +- kfet/urls.py | 20 +-- kfet/views.py | 229 ++++++-------------------- 5 files changed, 132 insertions(+), 221 deletions(-) diff --git a/kfet/statistic.py b/kfet/statistic.py index 5b5e297a..8dd2e6d0 100644 --- a/kfet/statistic.py +++ b/kfet/statistic.py @@ -1,5 +1,7 @@ # -*- coding: utf-8 -*- +import ast + from dateutil.relativedelta import relativedelta from django.utils import timezone @@ -50,7 +52,7 @@ class StatScale(object): for cls in StatScale.__subclasses__(): if cls.name == name: return cls - raise Exception('scale %s not found' % name) + return None def get_from(self, dt): return self.std_chunk and self.get_chunk_start(dt) or dt @@ -114,32 +116,38 @@ class MonthStatScale(StatScale): return to_kfet_day(dt).replace(day=1) -def this_first_month_day(): - now = timezone.now() - first_day = timezone.datetime(year=now.year, - month=now.month, - day=1, - hour=KFET_WAKES_UP_AT) - return first_day +def stat_manifest(scales_def=None, scale_args=None, **url_params): + if scales_def is None: + scales_def = [] + if scale_args is None: + scale_args = {} + return [ + dict( + label=label, + url_params=dict( + scale=cls.name, + scale_args=scale_args, + **url_params, + ), + ) + for label, cls in scales_def + ] -def this_monday_morning(): - now = timezone.now() - monday = now - timezone.timedelta(days=now.isoweekday()-1) - monday_morning = timezone.datetime(year=monday.year, - month=monday.month, - day=monday.day, - hour=KFET_WAKES_UP_AT) - return monday_morning - - -def this_morning(): - now = timezone.now() - morning = timezone.datetime(year=now.year, - month=now.month, - day=now.day, - hour=KFET_WAKES_UP_AT) - return morning +def last_stats_manifest(scales_def=None, scale_args=None, **url_params): + scales_def = [ + ('Derniers mois', MonthStatScale, ), + ('Dernières semaines', WeekStatScale, ), + ('Derniers jours', DayStatScale, ), + ] + if scale_args is None: + scale_args = {} + scale_args.update(dict( + last=True, + n_steps=7, + )) + return stat_manifest(scales_def=scales_def, scale_args=scale_args, + **url_params) # Étant donné un queryset d'operations @@ -147,3 +155,38 @@ def this_morning(): def tot_ventes(queryset): res = queryset.aggregate(Sum('article_nb'))['article_nb__sum'] return res and res or 0 + + +class ScaleMixin(object): + + def get_context_data(self, *args, **kwargs): + context = super().get_context_data(*args, **kwargs) + + scale_name = self.request.GET.get('scale', None) + scale_args = self.request.GET.get('scale_args', None) + + cls = StatScale.by_name(scale_name) + if cls is None: + scale = self.get_default_scale() + else: + scale_args = self.request.GET.get('scale_args', {}) + if isinstance(scale_args, str): + scale_args = ast.literal_eval(scale_args) + scale = cls(**scale_args) + + self.scale = scale + context['labels'] = scale.get_labels() + return context + + def get_default_scale(self): + return DayStatScale(n_steps=7, last=True) + + def chunkify_qs(self, qs, scale, field=None): + if field is None: + field = 'at' + begin_f = '{}__gte'.format(field) + end_f = '{}__lte'.format(field) + return [ + qs.filter(**{begin_f: begin, end_f: end}) + for begin, end in scale + ] diff --git a/kfet/templates/kfet/account_read.html b/kfet/templates/kfet/account_read.html index 449914f9..3c2ccbcd 100644 --- a/kfet/templates/kfet/account_read.html +++ b/kfet/templates/kfet/account_read.html @@ -17,11 +17,11 @@ + +{% endblock %} diff --git a/kfet/urls.py b/kfet/urls.py index 2eeef513..0ffeb84f 100644 --- a/kfet/urls.py +++ b/kfet/urls.py @@ -140,20 +140,14 @@ urlpatterns = [ # Article - Update url('^articles/(?P\d+)/edit$', teamkfet_required(views.ArticleUpdate.as_view()), - name = 'kfet.article.update'), + name='kfet.article.update'), # Article - Statistics - 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'), - url('^articles/(?P\d+)/stat/last/day/$', - views.ArticleStatLastDay.as_view(), - name = 'kfet.article.stat.last.day'), + url(r'^articles/(?P\d+)/stat/sales/list$', + views.ArticleStatSalesList.as_view(), + name='kfet.article.stat.sales.list'), + url(r'^articles/(?P\d+)/stat/sales$', + views.ArticleStatSales.as_view(), + name='kfet.article.stat.sales'), # ----- # K-Psul urls diff --git a/kfet/views.py b/kfet/views.py index 0b62cd2e..3b50cc0a 100644 --- a/kfet/views.py +++ b/kfet/views.py @@ -48,10 +48,8 @@ from decimal import Decimal import django_cas_ng import heapq import statistics -from kfet.statistic import DayStatScale, MonthStatScale, StatScale, WeekStatScale -from .statistic import ( - this_morning, this_monday_morning, this_first_month_day, tot_ventes, -) +from kfet.statistic import ScaleMixin, last_stats_manifest, tot_ventes + class Home(TemplateView): template_name = "kfet/home.html" @@ -2155,10 +2153,8 @@ class AccountStatBalance(PkUrlMixin, JSONDetailView): account = self.object # prepare filters - if end_date is None: - end_date = this_morning() - if last_days is not None: + end_date = timezone.now() begin_date = end_date - timezone.timedelta(days=last_days) # prepare querysets @@ -2289,43 +2285,7 @@ class AccountStatOperationList(PkUrlMixin, SingleResumeStat): pk_url_kwarg = 'trigramme' id_prefix = ID_PREFIX_ACC_LAST nb_default = 2 - stats = [ - { - 'label': 'Derniers mois', - 'url_params': dict( - types=[Operation.PURCHASE], - scale=MonthStatScale.name, - scale_args=dict( - last=True, - n_steps=7, - ), - ), - }, - { - 'label': 'Dernières semaines', - 'url_params': dict( - types=[Operation.PURCHASE], - last_days=49, - scale=WeekStatScale.name, - scale_args=dict( - last=True, - n_steps=7, - ), - ), - }, - { - 'label': 'Derniers jours', - 'url_params': dict( - types=[Operation.PURCHASE], - last_days=7, - scale=DayStatScale.name, - scale_args=dict( - last=True, - n_steps=7, - ), - ), - }, - ] + stats = last_stats_manifest(types=[Operation.PURCHASE]) url_stat = 'kfet.account.stat.operation' def get_object(self, *args, **kwargs): @@ -2339,7 +2299,7 @@ class AccountStatOperationList(PkUrlMixin, SingleResumeStat): return super().dispatch(*args, **kwargs) -class AccountStatOperation(PkUrlMixin, JSONDetailView): +class AccountStatOperation(ScaleMixin, PkUrlMixin, JSONDetailView): """ Returns a JSON containing the evolution a the personnal consommation of a trigramme at the diffent dates specified @@ -2359,38 +2319,24 @@ class AccountStatOperation(PkUrlMixin, JSONDetailView): .filter(group__on_acc=self.object) .filter(canceled_at=None) ) - operations = [] - for begin, end in scale: - operations.append(all_operations - .filter(group__at__gte=begin, - group__at__lte=end)) - return operations + chunks = self.chunkify_qs(all_operations, scale, field='group__at') + return chunks - def get_context_data(self, **kwargs): - context = {} - - scale = self.request.GET.get('scale', None) - if scale is None: - scale = DayStatScale(n_steps=7, last=True) - else: - scale_cls = StatScale.by_name(scale) - scale_args = self.request.GET.get('scale_args', '{}') - scale = scale_cls(**ast.literal_eval(scale_args)) - print(scale.datetimes) + def get_context_data(self, *args, **kwargs): + old_ctx = super().get_context_data(*args, **kwargs) + context = {'labels': old_ctx['labels']} + scale = self.scale types = self.request.GET.get('types', None) if types is not None: types = ast.literal_eval(types) operations = self.get_operations(types=types, scale=scale) - # On récupère les labels des dates - context['labels'] = scale.get_labels() # On compte les opérations nb_ventes = [] for chunk in operations: nb_ventes.append(tot_ventes(chunk)) - context['labels'] = scale.get_labels() context['charts'] = [{"color": "rgb(255, 99, 132)", "label": "NB items achetés", "values": nb_ventes}] @@ -2418,141 +2364,66 @@ ID_PREFIX_ART_LAST_MONTHS = "last_months_art" # Un résumé de toutes les vues ArticleStatLast # NE REND PAS DE JSON -class ArticleStatLastAll(SingleResumeStat): +class ArticleStatSalesList(SingleResumeStat): model = Article context_object_name = 'article' id_prefix = ID_PREFIX_ART_LAST - 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'] + url_stat = 'kfet.article.stat.sales' + stats = last_stats_manifest() - @method_decorator(login_required) + @method_decorator(teamkfet_required) def dispatch(self, *args, **kwargs): - return super(ArticleStatLastAll, self).dispatch(*args, **kwargs) + return super().dispatch(*args, **kwargs) -class ArticleStatLast(JSONDetailView): +class ArticleStatSales(ScaleMixin, JSONDetailView): """ Returns a JSON containing the consommation of an article at the diffent dates precised """ model = Article context_object_name = 'article' - end_date = timezone.now() - id_prefix = "" - def render_to_response(self, context): - # Look for a 'format=json' GET argument - if self.request.GET.get('format') == 'json': - return self.render_to_json_response(context) - else: - return super(ArticleStatLast, self).render_to_response(context) + def get_context_data(self, *args, **kwargs): + old_ctx = super().get_context_data(*args, **kwargs) + context = {'labels': old_ctx['labels']} + scale = self.scale - # 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_context_data(self, **kwargs): - context = {} - # On récupère les labels des dates - context['labels'] = self.get_labels().copy() - # 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(article=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]) - ) + all_operations = ( + Operation.objects + .filter(type=Operation.PURCHASE, + article=self.object, + canceled_at=None, + ) + ) + chunks = self.chunkify_qs(all_operations, scale, field='group__at') # On compte les opérations - nb_ventes = {} - nb_accounts = {} - nb_liq = {} - for i in operations: - nb_ventes[i] = tot_ventes(operations[i]) - nb_liq[i] = tot_ventes( - operations[i] - .filter(group__on_acc__trigramme='LIQ') - ) - nb_accounts[i] = tot_ventes( - operations[i] - .exclude(group__on_acc__trigramme='LIQ') - ) - context['charts'] = [ { "color": "rgb(255, 99, 132)", - "label": "Toutes consommations", - "values": nb_ventes }, - { "color": "rgb(54, 162, 235)", - "label": "LIQ", - "values": nb_liq }, - { "color": "rgb(255, 205, 86)", - "label": "Comptes K-Fêt", - "values": nb_accounts } ] + nb_ventes = [] + nb_accounts = [] + nb_liq = [] + for qs in chunks: + nb_ventes.append( + tot_ventes(qs)) + nb_liq.append( + tot_ventes(qs.filter(group__on_acc__trigramme='LIQ'))) + nb_accounts.append( + tot_ventes(qs.exclude(group__on_acc__trigramme='LIQ'))) + context['charts'] = [{"color": "rgb(255, 99, 132)", + "label": "Toutes consommations", + "values": nb_ventes}, + {"color": "rgb(54, 162, 235)", + "label": "LIQ", + "values": nb_liq}, + {"color": "rgb(255, 205, 86)", + "label": "Comptes K-Fêt", + "values": nb_accounts}] return context - @method_decorator(login_required) + @method_decorator(teamkfet_required) def dispatch(self, *args, **kwargs): - return super(ArticleStatLast, self).dispatch(*args, **kwargs) - - -# Rend les ventes des 7 derniers jours -# Aujourd'hui non compris -class ArticleStatLastDay(ArticleStatLast): - end_date = this_morning() - id_prefix = ID_PREFIX_ART_LAST_DAYS - - def get_dates(self, **kwargs): - return lastdays(7) - - def get_labels(self, **kwargs): - days = lastdays(7) - return daynames(days) - - -# Rend les ventes de 7 dernières semaines -# La semaine en cours n'est pas comprise -class ArticleStatLastWeek(ArticleStatLast): - end_date = this_monday_morning() - id_prefix = ID_PREFIX_ART_LAST_WEEKS - - def get_dates(self, **kwargs): - return lastweeks(7) - - def get_labels(self, **kwargs): - weeks = lastweeks(7) - 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) + return super().dispatch(*args, **kwargs) From d97a7be8196800562b3c4a4bbd7e3e7ff8eb01b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Delobelle?= Date: Mon, 3 Apr 2017 03:15:07 +0200 Subject: [PATCH 12/26] stats: fix begin of balances graphs - graph begin at first operation or later --- kfet/views.py | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/kfet/views.py b/kfet/views.py index 3b50cc0a..fdcb3763 100644 --- a/kfet/views.py +++ b/kfet/views.py @@ -2185,23 +2185,8 @@ class AccountStatBalance(PkUrlMixin, JSONDetailView): # sera mis à jour lors d'une # autre passe) # } - actions = [] - if begin_date is not None: - actions.append({ - 'at': begin_date.isoformat(), - 'amount': 0, - 'label': "début", - 'balance': 0, - }) - if end_date is not None: - actions.append({ - 'at': end_date.isoformat(), - 'amount': 0, - 'label': "actuel", - 'balance': 0, - }) - actions += [ + actions = [ { 'at': ope_grp.at.isoformat(), 'amount': ope_grp.amount, From b3a9ad8a964dc3128b404316eb32e92698bc526a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Delobelle?= Date: Mon, 3 Apr 2017 13:24:04 +0200 Subject: [PATCH 13/26] clean mixin --- kfet/statistic.py | 1 - 1 file changed, 1 deletion(-) diff --git a/kfet/statistic.py b/kfet/statistic.py index 8dd2e6d0..2abb4fd8 100644 --- a/kfet/statistic.py +++ b/kfet/statistic.py @@ -163,7 +163,6 @@ class ScaleMixin(object): context = super().get_context_data(*args, **kwargs) scale_name = self.request.GET.get('scale', None) - scale_args = self.request.GET.get('scale_args', None) cls = StatScale.by_name(scale_name) if cls is None: From 1bb83ccdd70338455da0eecb269cfcda14275786 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Delobelle?= Date: Mon, 3 Apr 2017 15:10:53 +0200 Subject: [PATCH 14/26] simplify StatScale --- kfet/statistic.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/kfet/statistic.py b/kfet/statistic.py index 2abb4fd8..1db30d73 100644 --- a/kfet/statistic.py +++ b/kfet/statistic.py @@ -79,12 +79,6 @@ class StatScale(object): label_fmt = self.label_fmt return [begin.strftime(label_fmt) for begin, end in self] - @classmethod - def get_chunk_start(cls, dt): - dt_kfet = to_kfet_day(dt) - start = dt_kfet - cls.offset_to_chunk_start(dt_kfet) - return start - class DayStatScale(StatScale): name = 'day' @@ -102,8 +96,10 @@ class WeekStatScale(StatScale): label_fmt = 'Semaine %W' @classmethod - def offset_to_chunk_start(cls, dt): - return timezone.timedelta(days=dt.weekday()) + def get_chunk_start(cls, dt): + dt_kfet = to_kfet_day(dt) + offset = timezone.timedelta(days=dt_kfet.weekday()) + return dt_kfet - offset class MonthStatScale(StatScale): From 769c37634d1f9bfd320bd3e26cef4326be1d9d3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Delobelle?= Date: Mon, 3 Apr 2017 15:43:56 +0200 Subject: [PATCH 15/26] delete debug msg --- kfet/templates/kfet/article_read.html | 2 -- 1 file changed, 2 deletions(-) diff --git a/kfet/templates/kfet/article_read.html b/kfet/templates/kfet/article_read.html index 1e0fb650..6fe025f6 100644 --- a/kfet/templates/kfet/article_read.html +++ b/kfet/templates/kfet/article_read.html @@ -102,12 +102,10 @@ From 87bc90ec8bd25ce0ce4a4a0415b2dd44ebdf6181 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Delobelle?= Date: Mon, 3 Apr 2017 16:07:31 +0200 Subject: [PATCH 16/26] Begin/end balance stat graph - Anytime begin at account creation datetime - Others doesn't take care of account creation - Add check to avoid to bug on actions list length --- kfet/views.py | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/kfet/views.py b/kfet/views.py index fdcb3763..a82e5cea 100644 --- a/kfet/views.py +++ b/kfet/views.py @@ -2186,7 +2186,22 @@ class AccountStatBalance(PkUrlMixin, JSONDetailView): # autre passe) # } - actions = [ + actions = [] + + actions.append({ + 'at': (begin_date or account.created_at).isoformat(), + 'amount': 0, + 'label': 'début', + 'balance': 0, + }) + actions.append({ + 'at': (end_date or timezone.now()).isoformat(), + 'amount': 0, + 'label': 'fin', + 'balance': 0, + }) + + actions += [ { 'at': ope_grp.at.isoformat(), 'amount': ope_grp.amount, @@ -2210,11 +2225,12 @@ class AccountStatBalance(PkUrlMixin, JSONDetailView): ] # Maintenant on trie la liste des actions par ordre du plus récent # an plus ancien et on met à jour la balance - actions = sorted(actions, key=lambda k: k['at'], reverse=True) - actions[0]['balance'] = account.balance - for i in range(len(actions)-1): - actions[i+1]['balance'] = \ - actions[i]['balance'] - actions[i+1]['amount'] + if len(actions) > 1: + actions = sorted(actions, key=lambda k: k['at'], reverse=True) + actions[0]['balance'] = account.balance + for i in range(len(actions)-1): + actions[i+1]['balance'] = \ + actions[i]['balance'] - actions[i+1]['amount'] return actions def get_context_data(self, *args, **kwargs): From 903da795ab3f7a5399a33cf221ea70d8b19880b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Delobelle?= Date: Mon, 3 Apr 2017 16:53:28 +0200 Subject: [PATCH 17/26] clean kfet statistic.js - no longer dictToArray where it isn't necessary (because already an array) - fix chart height: - previous charts were causing bugs - height is fixed (even with window resizing) - clean whitespaces --- kfet/static/kfet/js/statistic.js | 37 ++++++++++++++++---------------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/kfet/static/kfet/js/statistic.js b/kfet/static/kfet/js/statistic.js index f6d21237..9593c9f7 100644 --- a/kfet/static/kfet/js/statistic.js +++ b/kfet/static/kfet/js/statistic.js @@ -1,10 +1,10 @@ (function($){ window.StatsGroup = function (url, target) { // a class to properly display statictics - + // url : points to an ObjectResumeStat that lists the options through JSON // target : element of the DOM where to put the stats - + var self = this; var element = $(target); var content = $("
"); @@ -22,21 +22,21 @@ return array; } - function handleTimeChart (dict) { + function handleTimeChart (data) { // reads the balance data and put it into chartjs formatting - var data = dictToArray(dict, 0); + chart_data = new Array(); for (var i = 0; i < data.length; i++) { var source = data[i]; - data[i] = { x: new Date(source.at), + chart_data[i] = { x: new Date(source.at), y: source.balance, label: source.label } } - return data; + return chart_data; } - + function showStats () { // CALLBACK : called when a button is selected - + // shows the focus on the correct button buttons.find(".focus").removeClass("focus"); $(this).addClass("focus"); @@ -50,14 +50,14 @@ var chart_datasets = []; var charts = dictToArray(data.charts); - + // are the points indexed by timestamps? var is_time_chart = data.is_time_chart || false; // reads the charts data for (var i = 0; i < charts.length; i++) { var chart = charts[i]; - + // format the data var chart_data = is_time_chart ? handleTimeChart(chart.values) : dictToArray(chart.values, 1); @@ -77,6 +77,7 @@ var chart_options = { responsive: true, + maintainAspectRatio: false, tooltips: { mode: 'index', intersect: false, @@ -129,25 +130,25 @@ type: 'line', options: chart_options, data: { - labels: dictToArray(data.labels, 1), + labels: (data.labels || []).slice(1), datasets: chart_datasets, } }; // saves the previous charts to be destroyed var prev_chart = content.children(); - + + // clean + prev_chart.remove(); + // creates a blank canvas element and attach it to the DOM - var canvas = $(""); + var canvas = $(""); content.append(canvas); // create the chart var chart = new Chart(canvas, chart_model); - - // clean - prev_chart.remove(); } - + // initialize the interface function initialize (data) { // creates the bar with the buttons @@ -158,7 +159,7 @@ var to_click; var context = data.stats; - + for (var i = 0; i < context.length; i++) { // creates the button var btn_wrapper = $("
", From 6d2e150aa0f54832ac61f56115158510051c0460 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Delobelle?= Date: Mon, 3 Apr 2017 16:56:44 +0200 Subject: [PATCH 18/26] clean align --- kfet/static/kfet/js/statistic.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/kfet/static/kfet/js/statistic.js b/kfet/static/kfet/js/statistic.js index 9593c9f7..f210c11d 100644 --- a/kfet/static/kfet/js/statistic.js +++ b/kfet/static/kfet/js/statistic.js @@ -27,9 +27,11 @@ chart_data = new Array(); for (var i = 0; i < data.length; i++) { var source = data[i]; - chart_data[i] = { x: new Date(source.at), - y: source.balance, - label: source.label } + chart_data[i] = { + x: new Date(source.at), + y: source.balance, + label: source.label, + } } return chart_data; } From 10d2b58fa7e01fa1ddbecc969b1aa1c460f8a3af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Delobelle?= Date: Mon, 3 Apr 2017 17:06:32 +0200 Subject: [PATCH 19/26] clean some comments - fix: error if actions are empty in balance stats --- kfet/views.py | 41 ++++++++++++++++------------------------- 1 file changed, 16 insertions(+), 25 deletions(-) diff --git a/kfet/views.py b/kfet/views.py index a82e5cea..17de388e 100644 --- a/kfet/views.py +++ b/kfet/views.py @@ -2049,9 +2049,10 @@ class PkUrlMixin(object): class SingleResumeStat(JSONDetailView): - """ - Summarize all the stats of an object - Handles JSONResponse + """Manifest for a kind of a stat about an object. + + Returns JSON whose payload is an array containing descriptions of a stat: + url to retrieve data, label, ... """ id_prefix = '' @@ -2095,9 +2096,8 @@ class SingleResumeStat(JSONDetailView): ID_PREFIX_ACC_BALANCE = "balance_acc" -# Un résumé de toutes les vues ArticleStatBalance -# REND DU JSON class AccountStatBalanceList(PkUrlMixin, SingleResumeStat): + """Manifest for balance stats of an account.""" model = Account context_object_name = 'account' pk_url_kwarg = 'trigramme' @@ -2138,12 +2138,10 @@ class AccountStatBalanceList(PkUrlMixin, SingleResumeStat): class AccountStatBalance(PkUrlMixin, JSONDetailView): - """ - Returns a JSON containing the evolution a the personnal - balance of a trigramme between timezone.now() and `nb_days` - ago (specified to the view as an argument) - takes into account the Operations and the Transfers - does not takes into account the balance offset + """Datasets of balance of an account. + + Operations and Transfers are taken into account. + """ model = Account pk_url_kwarg = 'trigramme' @@ -2253,8 +2251,9 @@ class AccountStatBalance(PkUrlMixin, JSONDetailView): "values": changes, }] context['is_time_chart'] = True - context['min_date'] = changes[-1]['at'] - context['max_date'] = changes[0]['at'] + if len(changes) > 0: + context['min_date'] = changes[-1]['at'] + context['max_date'] = changes[0]['at'] # TODO: offset return context @@ -2278,9 +2277,8 @@ 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 AccountStatOperationList(PkUrlMixin, SingleResumeStat): + """Manifest for operations stats of an account.""" model = Account context_object_name = 'account' pk_url_kwarg = 'trigramme' @@ -2301,10 +2299,7 @@ class AccountStatOperationList(PkUrlMixin, SingleResumeStat): class AccountStatOperation(ScaleMixin, PkUrlMixin, JSONDetailView): - """ - Returns a JSON containing the evolution a the personnal - consommation of a trigramme at the diffent dates specified - """ + """Datasets of operations of an account.""" model = Account pk_url_kwarg = 'trigramme' context_object_name = 'account' @@ -2363,9 +2358,8 @@ ID_PREFIX_ART_LAST_WEEKS = "last_weeks_art" ID_PREFIX_ART_LAST_MONTHS = "last_months_art" -# Un résumé de toutes les vues ArticleStatLast -# NE REND PAS DE JSON class ArticleStatSalesList(SingleResumeStat): + """Manifest for sales stats of an article.""" model = Article context_object_name = 'article' id_prefix = ID_PREFIX_ART_LAST @@ -2379,10 +2373,7 @@ class ArticleStatSalesList(SingleResumeStat): class ArticleStatSales(ScaleMixin, JSONDetailView): - """ - Returns a JSON containing the consommation - of an article at the diffent dates precised - """ + """Datasets of sales of an article.""" model = Article context_object_name = 'article' From f13d1072c70fc7ff9bbb9409911fad19445ab7af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Mon, 3 Apr 2017 22:55:54 +0100 Subject: [PATCH 20/26] Add simple tests for the stat views We check that we can get all the stats views with the appropriate permissions. --- cof/urls.py | 2 +- kfet/tests.py | 69 +++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 68 insertions(+), 3 deletions(-) diff --git a/cof/urls.py b/cof/urls.py index 7ec728da..06b1087a 100644 --- a/cof/urls.py +++ b/cof/urls.py @@ -84,7 +84,7 @@ urlpatterns = [ url(r'^k-fet/', include('kfet.urls')), ] -if settings.DEBUG: +if 'debug_toolbar' in settings.INSTALLED_APPS: import debug_toolbar urlpatterns += [ url(r'^__debug__/', include(debug_toolbar.urls)), diff --git a/kfet/tests.py b/kfet/tests.py index 5bea7afa..ffca7a44 100644 --- a/kfet/tests.py +++ b/kfet/tests.py @@ -1,5 +1,70 @@ # -*- coding: utf-8 -*- -from django.test import TestCase +from unittest.mock import patch -# Écrire les tests ici +from django.test import TestCase, Client +from django.contrib.auth.models import User, Permission + +from .models import Account, Article, ArticleCategory + + +class TestStats(TestCase): + @patch('kfet.signals.messages') + def test_user_stats(self, mock_messages): + """ + Checks that we can get the stat-related pages without any problem. + """ + # We setup two users and an article. Only the first user is part of the + # team. + user = User.objects.create(username="Foobar") + user.set_password("foobar") + user.save() + Account.objects.create(trigramme="FOO", cofprofile=user.profile) + perm = Permission.objects.get(codename="is_team") + user.user_permissions.add(perm) + + user2 = User.objects.create(username="Barfoo") + user2.set_password("barfoo") + user2.save() + Account.objects.create(trigramme="BAR", cofprofile=user2.profile) + + article = Article.objects.create( + name="article", + category=ArticleCategory.objects.create(name="C") + ) + + # Each user have its own client + client = Client() + client.login(username="Foobar", password="foobar") + client2 = Client() + client2.login(username="Barfoo", password="barfoo") + + # 1. FOO should be able to get these pages but BAR receives a Forbidden + # response + user_urls = [ + "/k-fet/accounts/FOO/stat/operations/list", + "/k-fet/accounts/FOO/stat/operations?{}".format( + '&'.join(["scale=day", + "types=['purchase']", + "scale_args={'n_steps':+7,+'last':+True}", + "format=json"])), + "/k-fet/accounts/FOO/stat/balance/list", + "/k-fet/accounts/FOO/stat/balance?format=json" + ] + for url in user_urls: + resp = client.get(url) + self.assertEqual(200, resp.status_code) + resp2 = client2.get(url) + self.assertEqual(403, resp2.status_code) + + # 2. FOO is a member of the team and can get these pages but BAR + # receives a Redirect response + articles_urls = [ + "/k-fet/articles/{}/stat/sales/list".format(article.pk), + "/k-fet/articles/{}/stat/sales".format(article.pk) + ] + for url in articles_urls: + resp = client.get(url) + self.assertEqual(200, resp.status_code) + resp2 = client2.get(url) + self.assertEqual(302, resp2.status_code) From df467767f441f55b193ac9a146b97660bf55db55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Delobelle?= Date: Tue, 4 Apr 2017 01:29:19 +0200 Subject: [PATCH 21/26] fix default GET param 'types' on operations stats --- kfet/views.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/kfet/views.py b/kfet/views.py index 17de388e..8f5cdb3a 100644 --- a/kfet/views.py +++ b/kfet/views.py @@ -2324,7 +2324,9 @@ class AccountStatOperation(ScaleMixin, PkUrlMixin, JSONDetailView): scale = self.scale types = self.request.GET.get('types', None) - if types is not None: + if types is None: + types = [] + else: types = ast.literal_eval(types) operations = self.get_operations(types=types, scale=scale) From 7989a07b5f61d7f4acb5165811b5c4765e43934f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Delobelle?= Date: Tue, 4 Apr 2017 01:36:19 +0200 Subject: [PATCH 22/26] cleaner fix --- kfet/views.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/kfet/views.py b/kfet/views.py index 8f5cdb3a..69126634 100644 --- a/kfet/views.py +++ b/kfet/views.py @@ -2305,16 +2305,17 @@ class AccountStatOperation(ScaleMixin, PkUrlMixin, JSONDetailView): context_object_name = 'account' id_prefix = "" - def get_operations(self, types, scale): + def get_operations(self, scale, types=None): # 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__in=types) .filter(group__on_acc=self.object) .filter(canceled_at=None) ) + if types is not None: + all_operations = all_operations.filter(type__in==types) chunks = self.chunkify_qs(all_operations, scale, field='group__at') return chunks @@ -2324,9 +2325,7 @@ class AccountStatOperation(ScaleMixin, PkUrlMixin, JSONDetailView): scale = self.scale types = self.request.GET.get('types', None) - if types is None: - types = [] - else: + if types is not None: types = ast.literal_eval(types) operations = self.get_operations(types=types, scale=scale) From 278459e80f2b478a958cdebe263936c466332649 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Delobelle?= Date: Tue, 4 Apr 2017 11:05:49 +0200 Subject: [PATCH 23/26] typo....... --- kfet/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kfet/views.py b/kfet/views.py index 69126634..fe174588 100644 --- a/kfet/views.py +++ b/kfet/views.py @@ -2315,7 +2315,7 @@ class AccountStatOperation(ScaleMixin, PkUrlMixin, JSONDetailView): .filter(canceled_at=None) ) if types is not None: - all_operations = all_operations.filter(type__in==types) + all_operations = all_operations.filter(type__in=types) chunks = self.chunkify_qs(all_operations, scale, field='group__at') return chunks From 885e40fd05da9e9d9cd44645a3e7c4c332196841 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Delobelle?= Date: Tue, 4 Apr 2017 18:11:15 +0200 Subject: [PATCH 24/26] cleaner scales - References to `Stat` in `Scale` objects are deleted (because scales are independent of stats) - KFET_WAKES_UP_AT is now a time object insted of an hour - Proper use of date, datetime, timedelta, etc (django.utils.timezone provides neither datetime nor timedelta) --- kfet/statistic.py | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/kfet/statistic.py b/kfet/statistic.py index 1db30d73..4ae17959 100644 --- a/kfet/statistic.py +++ b/kfet/statistic.py @@ -1,27 +1,29 @@ # -*- coding: utf-8 -*- import ast +from datetime import date, datetime, time, timedelta from dateutil.relativedelta import relativedelta from django.utils import timezone from django.db.models import Sum -KFET_WAKES_UP_AT = 7 +KFET_WAKES_UP_AT = time(7, 0) def kfet_day(year, month, day, start_at=KFET_WAKES_UP_AT): - return timezone.datetime(year, month, day, hour=start_at) + """datetime wrapper with time offset.""" + return datetime.combine(date(year, month, day), start_at) def to_kfet_day(dt, start_at=KFET_WAKES_UP_AT): kfet_dt = kfet_day(year=dt.year, month=dt.month, day=dt.day) - if dt.hour < start_at: - kfet_dt -= timezone.timedelta(days=1) + if dt.time() < start_at: + kfet_dt -= timedelta(days=1) return kfet_dt -class StatScale(object): +class Scale(object): name = None step = None @@ -49,7 +51,7 @@ class StatScale(object): @staticmethod def by_name(name): - for cls in StatScale.__subclasses__(): + for cls in Scale.__subclasses__(): if cls.name == name: return cls return None @@ -80,9 +82,9 @@ class StatScale(object): return [begin.strftime(label_fmt) for begin, end in self] -class DayStatScale(StatScale): +class DayScale(Scale): name = 'day' - step = timezone.timedelta(days=1) + step = timedelta(days=1) label_fmt = '%A' @classmethod @@ -90,19 +92,19 @@ class DayStatScale(StatScale): return to_kfet_day(dt) -class WeekStatScale(StatScale): +class WeekScale(Scale): name = 'week' - step = timezone.timedelta(days=7) + step = timedelta(days=7) label_fmt = 'Semaine %W' @classmethod def get_chunk_start(cls, dt): dt_kfet = to_kfet_day(dt) - offset = timezone.timedelta(days=dt_kfet.weekday()) + offset = timedelta(days=dt_kfet.weekday()) return dt_kfet - offset -class MonthStatScale(StatScale): +class MonthScale(Scale): name = 'month' step = relativedelta(months=1) label_fmt = '%B' @@ -132,9 +134,9 @@ def stat_manifest(scales_def=None, scale_args=None, **url_params): def last_stats_manifest(scales_def=None, scale_args=None, **url_params): scales_def = [ - ('Derniers mois', MonthStatScale, ), - ('Dernières semaines', WeekStatScale, ), - ('Derniers jours', DayStatScale, ), + ('Derniers mois', MonthScale, ), + ('Dernières semaines', WeekScale, ), + ('Derniers jours', DayScale, ), ] if scale_args is None: scale_args = {} @@ -160,7 +162,7 @@ class ScaleMixin(object): scale_name = self.request.GET.get('scale', None) - cls = StatScale.by_name(scale_name) + cls = Scale.by_name(scale_name) if cls is None: scale = self.get_default_scale() else: @@ -174,7 +176,7 @@ class ScaleMixin(object): return context def get_default_scale(self): - return DayStatScale(n_steps=7, last=True) + return DayScale(n_steps=7, last=True) def chunkify_qs(self, qs, scale, field=None): if field is None: From dc07b072aba5808f9da497558d7c8a4224afd4ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Delobelle?= Date: Tue, 4 Apr 2017 20:12:21 +0200 Subject: [PATCH 25/26] Flatten scale args in GET params of stats urls - get_scale_args method of ScaleMixin retrieves useful GET params for Scale object instanciation (by default from request.GET) - it takes into account the type of the scale arg - prefix used for GET param can be modified in stats_manifest funcs and ScaleMixin --- kfet/statistic.py | 87 +++++++++++++++++++++++++++++++++++------------ requirements.txt | 1 + 2 files changed, 67 insertions(+), 21 deletions(-) diff --git a/kfet/statistic.py b/kfet/statistic.py index 4ae17959..fe948f73 100644 --- a/kfet/statistic.py +++ b/kfet/statistic.py @@ -1,9 +1,9 @@ # -*- coding: utf-8 -*- -import ast from datetime import date, datetime, time, timedelta from dateutil.relativedelta import relativedelta +from dateutil.parser import parse as dateutil_parse from django.utils import timezone from django.db.models import Sum @@ -114,25 +114,29 @@ class MonthScale(Scale): return to_kfet_day(dt).replace(day=1) -def stat_manifest(scales_def=None, scale_args=None, **url_params): +def stat_manifest(scales_def=None, scale_args=None, scale_prefix=None, + **other_url_params): + if scale_prefix is None: + scale_prefix = 'scale_' if scales_def is None: scales_def = [] if scale_args is None: scale_args = {} - return [ - dict( + manifest = [] + for label, cls in scales_def: + url_params = {scale_prefix+'name': cls.name} + url_params.update({scale_prefix+key: value + for key, value in scale_args.items()}) + url_params.update(other_url_params) + manifest.append(dict( label=label, - url_params=dict( - scale=cls.name, - scale_args=scale_args, - **url_params, - ), - ) - for label, cls in scales_def - ] + url_params=url_params, + )) + return manifest -def last_stats_manifest(scales_def=None, scale_args=None, **url_params): +def last_stats_manifest(scales_def=None, scale_args=None, scale_prefix=None, + **url_params): scales_def = [ ('Derniers mois', MonthScale, ), ('Dernières semaines', WeekScale, ), @@ -145,7 +149,7 @@ def last_stats_manifest(scales_def=None, scale_args=None, **url_params): n_steps=7, )) return stat_manifest(scales_def=scales_def, scale_args=scale_args, - **url_params) + scale_prefix=scale_prefix, **url_params) # Étant donné un queryset d'operations @@ -156,20 +160,61 @@ def tot_ventes(queryset): class ScaleMixin(object): + scale_args_prefix = 'scale_' + + def get_scale_args(self, params=None, prefix=None): + """Retrieve scale args from params. + + Should search the same args of Scale constructor. + + Args: + params (dict, optional): Scale args are searched in this. + Default to GET params of request. + prefix (str, optional): Appended at the begin of scale args names. + Default to `self.scale_args_prefix`. + + """ + if params is None: + params = self.request.GET + if prefix is None: + prefix = self.scale_args_prefix + + scale_args = {} + + name = params.get(prefix+'name', None) + if name is not None: + scale_args['name'] = name + + n_steps = params.get(prefix+'n_steps', None) + if n_steps is not None: + scale_args['n_steps'] = int(n_steps) + + begin = params.get(prefix+'begin', None) + if begin is not None: + scale_args['begin'] = dateutil_parse(begin) + + end = params.get(prefix+'send', None) + if end is not None: + scale_args['end'] = dateutil_parse(end) + + last = params.get(prefix+'last', None) + if last is not None: + scale_args['last'] = ( + last in ['true', 'True', '1'] and True or False) + + return scale_args def get_context_data(self, *args, **kwargs): context = super().get_context_data(*args, **kwargs) - scale_name = self.request.GET.get('scale', None) + scale_args = self.get_scale_args() + scale_name = scale_args.pop('name', None) + scale_cls = Scale.by_name(scale_name) - cls = Scale.by_name(scale_name) - if cls is None: + if scale_cls is None: scale = self.get_default_scale() else: - scale_args = self.request.GET.get('scale_args', {}) - if isinstance(scale_args, str): - scale_args = ast.literal_eval(scale_args) - scale = cls(**scale_args) + scale = scale_cls(**scale_args) self.scale = scale context['labels'] = scale.get_labels() diff --git a/requirements.txt b/requirements.txt index 06f6c46e..ce081588 100644 --- a/requirements.txt +++ b/requirements.txt @@ -20,3 +20,4 @@ django-widget-tweaks==1.4.1 git+https://git.eleves.ens.fr/cof-geek/django_custommail.git#egg=django_custommail ldap3 git+https://github.com/Aureplop/channels.git#egg=channels +python-dateutil From 85ba44c23172e086de68787b2c3d00007c016705 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Tue, 4 Apr 2017 16:32:46 +0100 Subject: [PATCH 26/26] Tests the redirection using the appropriate method --- kfet/tests.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kfet/tests.py b/kfet/tests.py index ffca7a44..991b2545 100644 --- a/kfet/tests.py +++ b/kfet/tests.py @@ -66,5 +66,5 @@ class TestStats(TestCase): for url in articles_urls: resp = client.get(url) self.assertEqual(200, resp.status_code) - resp2 = client2.get(url) - self.assertEqual(302, resp2.status_code) + resp2 = client2.get(url, follow=True) + self.assertRedirects(resp2, "/")