Misc cleanup

On utilise SingleObjectMixin partout, et on simplifie 2-3 trucs
This commit is contained in:
Ludovic Stephan 2020-03-09 16:15:15 +01:00
parent ef35f45ad2
commit 48ad5cd1c7
2 changed files with 21 additions and 35 deletions

View file

@ -125,7 +125,7 @@ class DayScale(Scale):
class WeekScale(Scale): class WeekScale(Scale):
name = "week" name = "week"
step = timedelta(days=7) step = timedelta(days=7)
label_fmt = "Semaine %W" label_fmt = "%d %b."
@classmethod @classmethod
def get_chunk_start(cls, dt): def get_chunk_start(cls, dt):
@ -222,20 +222,18 @@ class ScaleMixin(object):
return scale_args return scale_args
def get_context_data(self, *args, **kwargs): def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs) # On n'hérite pas
scale_args = self.get_scale_args() scale_args = self.parse_scale_args()
scale_name = scale_args.pop("name", None) scale_name = scale_args.pop("name", None)
scale_cls = Scale.by_name(scale_name) scale_cls = Scale.by_name(scale_name)
if scale_cls is None: if scale_cls is None:
scale = self.get_default_scale() self.scale = self.get_default_scale()
else: else:
scale = scale_cls(**scale_args) self.scale = scale_cls(**scale_args)
self.scale = scale return {"labels": self.scale.get_labels()}
context["labels"] = scale.get_labels()
return context
def get_default_scale(self): def get_default_scale(self):
return DayScale(n_steps=7, last=True) return DayScale(n_steps=7, last=True)

View file

@ -2244,7 +2244,6 @@ class SingleResumeStat(JSONDetailView):
On peut aussi définir `stats` dynamiquement, via la fonction `get_stats`. On peut aussi définir `stats` dynamiquement, via la fonction `get_stats`.
""" """
id_prefix = ""
nb_default = 0 nb_default = 0
stats = [] stats = []
@ -2252,12 +2251,15 @@ class SingleResumeStat(JSONDetailView):
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
# On n'hérite pas # On n'hérite pas
object_id = self.object.id
context = {} context = {}
stats = [] stats = []
prefix = "{}_{}".format(self.id_prefix, object_id) # On peut avoir récupéré self.object via pk ou slug
for i, stat_def in enumerate(self.stats): if self.pk_url_kwarg in self.kwargs:
url_pk = getattr(self.object, self.pk_url_kwarg) url_pk = getattr(self.object, self.pk_url_kwarg)
else:
url_pk = getattr(self.object, self.slug_url_kwarg)
for stat_def in self.get_stats():
url_params_d = stat_def.get("url_params", {}) url_params_d = stat_def.get("url_params", {})
if len(url_params_d) > 0: if len(url_params_d) > 0:
url_params = "?{}".format(urlencode(url_params_d)) url_params = "?{}".format(urlencode(url_params_d))
@ -2266,17 +2268,13 @@ class SingleResumeStat(JSONDetailView):
stats.append( stats.append(
{ {
"label": stat_def["label"], "label": stat_def["label"],
"btn": "btn_{}_{}".format(prefix, i),
"url": "{url}{params}".format( "url": "{url}{params}".format(
url=reverse(self.url_stat, args=[url_pk]), params=url_params url=reverse(self.url_stat, args=[url_pk]), params=url_params
), ),
} }
) )
context["id_prefix"] = prefix
context["content_id"] = "content_%s" % prefix
context["stats"] = stats context["stats"] = stats
context["default_stat"] = self.nb_default context["default_stat"] = self.nb_default
context["object_id"] = object_id
return context return context
@ -2296,7 +2294,6 @@ class UserAccountMixin:
# ----------------------- # -----------------------
# Evolution Balance perso # Evolution Balance perso
# ----------------------- # -----------------------
ID_PREFIX_ACC_BALANCE = "balance_acc"
@method_decorator(login_required, name="dispatch") @method_decorator(login_required, name="dispatch")
@ -2306,10 +2303,9 @@ class AccountStatBalanceList(UserAccountMixin, SingleResumeStat):
""" """
model = Account model = Account
context_object_name = "account" slug_url_kwarg = "trigramme"
pk_url_kwarg = "trigramme" slug_field = "trigramme"
url_stat = "kfet.account.stat.balance" url_stat = "kfet.account.stat.balance"
id_prefix = ID_PREFIX_ACC_BALANCE
stats = [ stats = [
{"label": "Tout le temps"}, {"label": "Tout le temps"},
{"label": "1 an", "url_params": {"last_days": 365}}, {"label": "1 an", "url_params": {"last_days": 365}},
@ -2330,8 +2326,8 @@ class AccountStatBalance(UserAccountMixin, JSONDetailView):
""" """
model = Account model = Account
pk_url_kwarg = "trigramme" slug_url_kwarg = "trigramme"
context_object_name = "account" slug_field = "trigramme"
def get_changes_list(self, last_days=None, begin_date=None, end_date=None): def get_changes_list(self, last_days=None, begin_date=None, end_date=None):
account = self.object account = self.object
@ -2444,9 +2440,8 @@ class AccountStatOperationList(UserAccountMixin, SingleResumeStat):
""" """
model = Account model = Account
context_object_name = "account" slug_url_kwarg = "trigramme"
pk_url_kwarg = "trigramme" slug_field = "trigramme"
id_prefix = ID_PREFIX_ACC_LAST
nb_default = 2 nb_default = 2
stats = last_stats_manifest(types=[Operation.PURCHASE]) stats = last_stats_manifest(types=[Operation.PURCHASE])
url_stat = "kfet.account.stat.operation" url_stat = "kfet.account.stat.operation"
@ -2461,9 +2456,8 @@ class AccountStatOperation(UserAccountMixin, ScaleMixin, JSONDetailView):
""" """
model = Account model = Account
pk_url_kwarg = "trigramme" slug_url_kwarg = "trigramme"
context_object_name = "account" slug_field = "trigramme"
id_prefix = ""
def get_operations(self, types=None): def get_operations(self, types=None):
# On selectionne les opérations qui correspondent # On selectionne les opérations qui correspondent
@ -2514,15 +2508,10 @@ class ArticleStatSalesList(SingleResumeStat):
""" """
model = Article model = Article
context_object_name = "article"
id_prefix = ID_PREFIX_ART_LAST
nb_default = 2 nb_default = 2
url_stat = "kfet.article.stat.sales" url_stat = "kfet.article.stat.sales"
stats = last_stats_manifest() stats = last_stats_manifest()
@method_decorator(teamkfet_required)
def dispatch(self, *args, **kwargs):
return super().dispatch(*args, **kwargs)
@method_decorator(teamkfet_required, name="dispatch") @method_decorator(teamkfet_required, name="dispatch")
@ -2536,8 +2525,7 @@ class ArticleStatSales(ScaleMixin, JSONDetailView):
context_object_name = "article" context_object_name = "article"
def get_context_data(self, *args, **kwargs): def get_context_data(self, *args, **kwargs):
old_ctx = super().get_context_data(*args, **kwargs) context = super().get_context_data(*args, **kwargs)
context = {"labels": old_ctx["labels"]}
scale = self.scale scale = self.scale
all_purchases = ( all_purchases = (