Rework stats_manifest

On change la façon dont les vues gèrent l'interface avec `Scale`.
Side effect : on peut avoir l'historique sur tout le temps
This commit is contained in:
Ludovic Stephan 2020-03-09 16:20:49 +01:00
parent c66fb7eb6f
commit 97cb9d1f3b
3 changed files with 44 additions and 51 deletions

View file

@ -145,7 +145,7 @@
.on("click", showStats);
// saves the default option to select
if (i == data.default_stat || i == 0)
if (stat.default)
to_click = btn;
// append the elements to the parent

View file

@ -144,43 +144,7 @@ class MonthScale(Scale):
return to_kfet_day(dt).replace(day=1)
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 = {}
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=url_params))
return manifest
def last_stats_manifest(
scales_def=None, scale_args=None, scale_prefix=None, **url_params
):
scales_def = [
("Derniers mois", MonthScale),
("Dernières semaines", WeekScale),
("Derniers jours", DayScale),
]
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,
scale_prefix=scale_prefix,
**url_params
)
def scale_url_params(scales_def, **other_url_params):
"""
Convertit une spécification de scales en arguments GET utilisables par ScaleMixin.
La spécification est de la forme suivante :
@ -189,7 +153,14 @@ def last_stats_manifest(
- other_url_params : paramètres GET supplémentaires
"""
params_list = []
for label, cls, params, default in scales_def:
url_params = {"scale_name": cls.name}
url_params.update({"scale_" + key: value for key, value in params.items()})
url_params.update(other_url_params)
params_list.append(dict(label=label, url_params=url_params, default=default))
return params_list
class ScaleMixin(object):

View file

@ -76,7 +76,7 @@ from kfet.models import (
Transfer,
TransferGroup,
)
from kfet.statistic import ScaleMixin, WeekScale, last_stats_manifest
from kfet.statistic import DayScale, MonthScale, ScaleMixin, WeekScale, scale_url_params
from .auth import KFET_GENERIC_TRIGRAMME
from .auth.views import ( # noqa
@ -2244,10 +2244,11 @@ class SingleResumeStat(JSONDetailView):
On peut aussi définir `stats` dynamiquement, via la fonction `get_stats`.
"""
nb_default = 0
stats = []
url_stat = None
stats = []
def get_stats(self):
return self.stats
def get_context_data(self, **kwargs):
# On n'hérite pas
@ -2271,10 +2272,10 @@ class SingleResumeStat(JSONDetailView):
"url": "{url}{params}".format(
url=reverse(self.url_stat, args=[url_pk]), params=url_params
),
"default": stat_def.get("default", False),
}
)
context["stats"] = stats
context["default_stat"] = self.nb_default
return context
@ -2310,12 +2311,9 @@ class AccountStatBalanceList(UserAccountMixin, SingleResumeStat):
{"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": "3 mois", "url_params": {"last_days": 90}, "default": True},
{"label": "30 jours", "url_params": {"last_days": 30}},
]
nb_default = 0
@method_decorator(login_required, name="dispatch")
@ -2427,7 +2425,6 @@ class AccountStatBalance(UserAccountMixin, JSONDetailView):
return context
# ------------------------
# Consommation personnelle
# ------------------------
@ -2442,11 +2439,22 @@ class AccountStatOperationList(UserAccountMixin, SingleResumeStat):
model = Account
slug_url_kwarg = "trigramme"
slug_field = "trigramme"
nb_default = 2
stats = last_stats_manifest(types=[Operation.PURCHASE])
url_stat = "kfet.account.stat.operation"
def get_stats(self):
scales_def = [
(
"Tout le temps",
MonthScale,
{"last": True, "begin": self.object.created_at},
False,
),
("1 an", MonthScale, {"last": True, "n_steps": 12}, False),
("3 mois", WeekScale, {"last": True, "n_steps": 13}, True),
("2 semaines", DayScale, {"last": True, "n_steps": 14}, False),
]
return scale_url_params(scales_def, types=[Operation.PURCHASE])
@method_decorator(login_required, name="dispatch")
@ -2510,8 +2518,22 @@ class ArticleStatSalesList(SingleResumeStat):
model = Article
nb_default = 2
url_stat = "kfet.article.stat.sales"
stats = last_stats_manifest()
def get_stats(self):
first_conso = (
Operation.objects.filter(article=self.object)
.order_by("group__at")
.first()
.group.at
)
scales_def = [
("Tout le temps", MonthScale, {"last": True, "begin": first_conso}, False),
("1 an", MonthScale, {"last": True, "n_steps": 12}, False),
("3 mois", WeekScale, {"last": True, "n_steps": 13}, True),
("2 semaines", DayScale, {"last": True, "n_steps": 14}, False),
]
return scale_url_params(scales_def)
@method_decorator(teamkfet_required, name="dispatch")