gestioCOF/kfet/statistic.py

138 lines
3.1 KiB
Python
Raw Normal View History

2016-12-09 21:45:34 +01:00
# -*- coding: utf-8 -*-
from django.utils import timezone
french_days = {
1: "lundi",
2: "mardi",
3: "mercredi",
4: "jeudi",
5: "vendredi",
6: "samedi",
7: "dimanche",
}
french_months = {
1: "janvier",
2: "février",
3: "mars",
4: "avril",
5: "mai",
6: "juin",
7: "juillet",
8: "août",
9: "septembre",
10: "octobre",
11: "novembre",
12: "décembre",
}
def dayname(date):
return french_days[date.isoweekday()]
def weekname(date):
(_, a, _) = date.isocalendar()
week_num = a
return "semaine %d" % week_num
def monthname(date):
return french_months[date.month]
2016-12-09 21:45:34 +01:00
# Pareil mais pour une liste de dates
# dans un dico ordonné
def daynames(dates):
names = {}
for i in dates:
names[i] = dayname(dates[i])
2016-12-09 21:45:34 +01:00
return names
# Pareil mais pour une liste de dates
# dans un dico ordonné
2016-12-20 22:46:38 +01:00
def weeknames(dates):
2016-12-09 21:45:34 +01:00
names = {}
for i in dates:
names[i] = weekname(dates[i])
2016-12-09 21:45:34 +01:00
return names
# Pareil mais pour une liste de dates
# dans un dico ordonné
def monthnames(dates):
names = {}
for i in dates:
names[i] = monthname(dates[i])
2016-12-09 21:45:34 +01:00
return names
# rend les dates des nb derniers jours
# dans l'ordre chronologique
# aujourd'hui compris
# nb = 1 : rend hier
def lastdays(nb):
morning = this_morning()
days = {}
for i in range(1, nb+1):
days[i] = morning - timezone.timedelta(days=nb - i + 1)
return days
def lastweeks(nb):
monday_morning = this_monday_morning()
mondays = {}
for i in range(1, nb+1):
mondays[i] = monday_morning \
- timezone.timedelta(days=7*(nb - i + 1))
return mondays
2016-12-20 22:46:38 +01:00
def lastmonths(nb):
first_month_day = this_first_month_day()
first_days = {}
this_year = first_month_day.year
this_month = first_month_day.month
for i in range(1, nb+1):
month = this_month - (nb - i) % 12
year = this_year + (nb - i) // 12
first_days[i] = timezone.datetime(year=year,
month=month,
day=1)
return first_days
2016-12-09 21:45:34 +01:00
def this_first_month_day():
now = timezone.now()
first_day = timezone.datetime(year=now.year,
month=now.month,
day=1)
return first_day
def this_monday_morning():
now = timezone.now()
monday = now - timezone.timedelta(days=now.isoweekday()-1)
monday_morning = timezone.datetime(year=monday.year,
month=monday.month,
day=monday.day)
return monday_morning
def this_morning():
now = timezone.now()
morning = timezone.datetime(year=now.year,
month=now.month,
day=now.day)
return morning
# Étant donné un queryset d'operations
# rend la somme des article_nb
def tot_ventes(queryset):
res = 0
for op in queryset:
res += op.article_nb
return res