Supprime le code mort ou redondant

This commit is contained in:
Ludovic Stephan 2020-03-09 16:09:12 +01:00
parent 78ad4402b0
commit 26bcd729bb
2 changed files with 33 additions and 86 deletions

View file

@ -65,7 +65,7 @@ class Scale(object):
"or use last and n_steps"
)
self.datetimes = self.get_datetimes()
self._gen_datetimes()
@staticmethod
def by_name(name):
@ -74,9 +74,6 @@ class Scale(object):
return cls
return None
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]
@ -86,13 +83,13 @@ class Scale(object):
def do_step(self, dt, n_steps=1):
return dt + self.step * n_steps
def get_datetimes(self):
def _gen_datetimes(self):
datetimes = [self.begin]
tmp = self.begin
while tmp < self.end:
tmp = self.do_step(tmp)
datetimes.append(tmp)
return datetimes
self.datetimes = datetimes
def get_labels(self, label_fmt=None):
if label_fmt is None:
@ -273,45 +270,32 @@ def last_stats_manifest(
"""
# Étant donné un queryset d'operations
# rend la somme des article_nb
def tot_ventes(queryset):
res = queryset.aggregate(Sum("article_nb"))["article_nb__sum"]
return res and res or 0
class ScaleMixin(object):
scale_args_prefix = "scale_"
def get_scale_args(self, params=None, prefix=None):
def parse_scale_args(self):
"""
Récupère les paramètres de subdivision encodés dans une requête GET.
"""
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)
name = self.request.GET.get("scale_name", None)
if name is not None:
scale_args["name"] = name
n_steps = params.get(prefix + "n_steps", None)
n_steps = self.request.GET.get("scale_n_steps", None)
if n_steps is not None:
scale_args["n_steps"] = int(n_steps)
begin = params.get(prefix + "begin", None)
begin = self.request.GET.get("scale_begin", None)
if begin is not None:
scale_args["begin"] = dateutil_parse(begin)
end = params.get(prefix + "send", None)
end = self.request.GET.get("scale_send", None)
if end is not None:
scale_args["end"] = dateutil_parse(end)
last = params.get(prefix + "last", None)
last = self.request.GET.get("scale_last", None)
if last is not None:
scale_args["last"] = last in ["true", "True", "1"] and True or False

View file

@ -2199,7 +2199,7 @@ class SupplierUpdate(SuccessMessageMixin, UpdateView):
# Vues génériques
# ---------------
# source : docs.djangoproject.com/fr/1.10/topics/class-based-views/mixins/
class JSONResponseMixin(object):
class JSONResponseMixin:
"""
A mixin that can be used to render a JSON response.
"""
@ -2228,12 +2228,6 @@ class JSONDetailView(JSONResponseMixin, BaseDetailView):
return self.render_to_json_response(context)
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):
"""
Génère l'interface de sélection pour les statistiques d'un compte/article.
@ -2286,13 +2280,28 @@ class SingleResumeStat(JSONDetailView):
return context
class UserAccountMixin:
"""
Mixin qui vérifie que le compte traité par la vue est celui de l'utilisateur·ice
actuel·le. Dans le cas contraire, renvoie un Http404.
"""
def get_object(self, *args, **kwargs):
obj = super().get_object(*args, **kwargs)
if self.request.user != obj.user:
raise Http404
return obj
# -----------------------
# Evolution Balance perso
# -----------------------
ID_PREFIX_ACC_BALANCE = "balance_acc"
class AccountStatBalanceList(PkUrlMixin, SingleResumeStat):
@method_decorator(login_required, name="dispatch")
class AccountStatBalanceList(UserAccountMixin, SingleResumeStat):
"""
Menu général pour l'historique de balance d'un compte
"""
@ -2310,20 +2319,11 @@ class AccountStatBalanceList(PkUrlMixin, SingleResumeStat):
]
nb_default = 0
def get_object(self, *args, **kwargs):
obj = super().get_object(*args, **kwargs)
if self.request.user != obj.user:
raise Http404
return obj
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super().dispatch(*args, **kwargs)
class AccountStatBalance(PkUrlMixin, JSONDetailView):
@method_decorator(login_required, name="dispatch")
class AccountStatBalance(UserAccountMixin, JSONDetailView):
"""
Statistiques (JSON) d'historique de balance d'un compte.
Prend en compte les opérations et transferts sur la période donnée.
@ -2430,28 +2430,15 @@ 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 Http404
return obj
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super().dispatch(*args, **kwargs)
# ------------------------
# 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"
class AccountStatOperationList(PkUrlMixin, SingleResumeStat):
@method_decorator(login_required, name="dispatch")
class AccountStatOperationList(UserAccountMixin, SingleResumeStat):
"""
Menu général pour l'historique de consommation d'un compte
"""
@ -2464,19 +2451,11 @@ class AccountStatOperationList(PkUrlMixin, SingleResumeStat):
stats = last_stats_manifest(types=[Operation.PURCHASE])
url_stat = "kfet.account.stat.operation"
def get_object(self, *args, **kwargs):
obj = super().get_object(*args, **kwargs)
if self.request.user != obj.user:
raise Http404
return obj
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super().dispatch(*args, **kwargs)
class AccountStatOperation(ScaleMixin, PkUrlMixin, JSONDetailView):
@method_decorator(login_required, name="dispatch")
class AccountStatOperation(UserAccountMixin, ScaleMixin, JSONDetailView):
"""
Statistiques (JSON) de consommation (nb d'items achetés) d'un compte.
"""
@ -2530,26 +2509,13 @@ class AccountStatOperation(ScaleMixin, PkUrlMixin, JSONDetailView):
]
return context
def get_object(self, *args, **kwargs):
obj = super().get_object(*args, **kwargs)
if self.request.user != obj.user:
raise Http404
return obj
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super().dispatch(*args, **kwargs)
# ------------------------
# 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"
@method_decorator(teamkfet_required, name="dispatch")
class ArticleStatSalesList(SingleResumeStat):
"""
Menu pour les statistiques de vente d'un article.
@ -2567,6 +2533,7 @@ class ArticleStatSalesList(SingleResumeStat):
return super().dispatch(*args, **kwargs)
@method_decorator(teamkfet_required, name="dispatch")
class ArticleStatSales(ScaleMixin, JSONDetailView):
"""
Statistiques (JSON) de vente d'un article.
@ -2623,7 +2590,3 @@ class ArticleStatSales(ScaleMixin, JSONDetailView):
},
]
return context
@method_decorator(teamkfet_required)
def dispatch(self, *args, **kwargs):
return super().dispatch(*args, **kwargs)