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):
name = "week"
step = timedelta(days=7)
label_fmt = "Semaine %W"
label_fmt = "%d %b."
@classmethod
def get_chunk_start(cls, dt):
@ -222,20 +222,18 @@ class ScaleMixin(object):
return scale_args
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_cls = Scale.by_name(scale_name)
if scale_cls is None:
scale = self.get_default_scale()
self.scale = self.get_default_scale()
else:
scale = scale_cls(**scale_args)
self.scale = scale_cls(**scale_args)
self.scale = scale
context["labels"] = scale.get_labels()
return context
return {"labels": self.scale.get_labels()}
def get_default_scale(self):
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`.
"""
id_prefix = ""
nb_default = 0
stats = []
@ -2252,12 +2251,15 @@ class SingleResumeStat(JSONDetailView):
def get_context_data(self, **kwargs):
# On n'hérite pas
object_id = self.object.id
context = {}
stats = []
prefix = "{}_{}".format(self.id_prefix, object_id)
for i, stat_def in enumerate(self.stats):
# On peut avoir récupéré self.object via pk ou slug
if self.pk_url_kwarg in self.kwargs:
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", {})
if len(url_params_d) > 0:
url_params = "?{}".format(urlencode(url_params_d))
@ -2266,17 +2268,13 @@ class SingleResumeStat(JSONDetailView):
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
context["default_stat"] = self.nb_default
context["object_id"] = object_id
return context
@ -2296,7 +2294,6 @@ class UserAccountMixin:
# -----------------------
# Evolution Balance perso
# -----------------------
ID_PREFIX_ACC_BALANCE = "balance_acc"
@method_decorator(login_required, name="dispatch")
@ -2306,10 +2303,9 @@ class AccountStatBalanceList(UserAccountMixin, SingleResumeStat):
"""
model = Account
context_object_name = "account"
pk_url_kwarg = "trigramme"
slug_url_kwarg = "trigramme"
slug_field = "trigramme"
url_stat = "kfet.account.stat.balance"
id_prefix = ID_PREFIX_ACC_BALANCE
stats = [
{"label": "Tout le temps"},
{"label": "1 an", "url_params": {"last_days": 365}},
@ -2330,8 +2326,8 @@ class AccountStatBalance(UserAccountMixin, JSONDetailView):
"""
model = Account
pk_url_kwarg = "trigramme"
context_object_name = "account"
slug_url_kwarg = "trigramme"
slug_field = "trigramme"
def get_changes_list(self, last_days=None, begin_date=None, end_date=None):
account = self.object
@ -2444,9 +2440,8 @@ class AccountStatOperationList(UserAccountMixin, SingleResumeStat):
"""
model = Account
context_object_name = "account"
pk_url_kwarg = "trigramme"
id_prefix = ID_PREFIX_ACC_LAST
slug_url_kwarg = "trigramme"
slug_field = "trigramme"
nb_default = 2
stats = last_stats_manifest(types=[Operation.PURCHASE])
url_stat = "kfet.account.stat.operation"
@ -2461,9 +2456,8 @@ class AccountStatOperation(UserAccountMixin, ScaleMixin, JSONDetailView):
"""
model = Account
pk_url_kwarg = "trigramme"
context_object_name = "account"
id_prefix = ""
slug_url_kwarg = "trigramme"
slug_field = "trigramme"
def get_operations(self, types=None):
# On selectionne les opérations qui correspondent
@ -2514,15 +2508,10 @@ class ArticleStatSalesList(SingleResumeStat):
"""
model = Article
context_object_name = "article"
id_prefix = ID_PREFIX_ART_LAST
nb_default = 2
url_stat = "kfet.article.stat.sales"
stats = last_stats_manifest()
@method_decorator(teamkfet_required)
def dispatch(self, *args, **kwargs):
return super().dispatch(*args, **kwargs)
@method_decorator(teamkfet_required, name="dispatch")
@ -2536,8 +2525,7 @@ class ArticleStatSales(ScaleMixin, JSONDetailView):
context_object_name = "article"
def get_context_data(self, *args, **kwargs):
old_ctx = super().get_context_data(*args, **kwargs)
context = {"labels": old_ctx["labels"]}
context = super().get_context_data(*args, **kwargs)
scale = self.scale
all_purchases = (