gestioCOF/kfet/statistic.py
2016-12-09 21:45:34 +01:00

130 lines
2.8 KiB
Python

# -*- 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]
# Pareil mais pour une liste de dates
# dans un dico ordonné
def daynames(dates):
names = {}
for i in dates:
names[i] = dayname(dates[i])
return names
# Pareil mais pour une liste de dates
# dans un dico ordonné
def weeksnames(dates):
names = {}
for i in dates:
names[i] = weekname(dates[i])
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])
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
# def lastmonths(nb):
# first_month_day = this_first_month_day()
# fisrt_days = {}
# for i in range(1, nb+1):
# days[i] =
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