forked from DGNum/gestioCOF
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)
This commit is contained in:
parent
278459e80f
commit
885e40fd05
1 changed files with 19 additions and 17 deletions
|
@ -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:
|
||||
|
|
Loading…
Reference in a new issue