2016-12-09 21:45:34 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from django.utils import timezone
|
2017-02-15 14:21:00 +01:00
|
|
|
from django.db.models import Sum
|
2017-01-17 17:16:53 +01:00
|
|
|
|
2017-02-15 14:44:58 +01:00
|
|
|
KFET_WAKES_UP_AT = 7
|
2017-01-17 17:16:53 +01:00
|
|
|
|
2017-01-20 20:07:03 +01:00
|
|
|
# donne le nom des jours d'une liste de dates
|
2016-12-09 21:45:34 +01:00
|
|
|
# dans un dico ordonné
|
|
|
|
def daynames(dates):
|
|
|
|
names = {}
|
|
|
|
for i in dates:
|
2017-01-20 20:07:03 +01:00
|
|
|
names[i] = dates[i].strftime("%A")
|
2016-12-09 21:45:34 +01:00
|
|
|
return names
|
|
|
|
|
|
|
|
|
2017-01-20 20:07:03 +01:00
|
|
|
# donne le nom des semaines une liste de dates
|
2016-12-09 21:45:34 +01:00
|
|
|
# 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:
|
2017-01-20 20:07:03 +01:00
|
|
|
names[i] = dates[i].strftime("Semaine %W")
|
2016-12-09 21:45:34 +01:00
|
|
|
return names
|
|
|
|
|
|
|
|
|
2017-01-20 20:07:03 +01:00
|
|
|
# donne le nom des mois d'une liste de dates
|
2016-12-09 21:45:34 +01:00
|
|
|
# dans un dico ordonné
|
|
|
|
def monthnames(dates):
|
|
|
|
names = {}
|
|
|
|
for i in dates:
|
2017-01-20 20:07:03 +01:00
|
|
|
names[i] = dates[i].strftime("%B")
|
2016-12-09 21:45:34 +01:00
|
|
|
return names
|
|
|
|
|
2017-02-15 14:21:00 +01:00
|
|
|
|
2016-12-09 21:45:34 +01:00
|
|
|
# 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):
|
2017-02-15 14:21:00 +01:00
|
|
|
month = ((this_month - 1 - (nb - i)) % 12) + 1
|
2016-12-20 22:46:38 +01:00
|
|
|
year = this_year + (nb - i) // 12
|
|
|
|
first_days[i] = timezone.datetime(year=year,
|
|
|
|
month=month,
|
2017-02-15 14:44:58 +01:00
|
|
|
day=1,
|
|
|
|
hour=KFET_WAKES_UP_AT)
|
2016-12-20 22:46:38 +01:00
|
|
|
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,
|
2017-02-15 14:44:58 +01:00
|
|
|
day=1,
|
|
|
|
hour=KFET_WAKES_UP_AT)
|
2016-12-09 21:45:34 +01:00
|
|
|
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,
|
2017-02-15 14:44:58 +01:00
|
|
|
day=monday.day,
|
|
|
|
hour=KFET_WAKES_UP_AT)
|
2016-12-09 21:45:34 +01:00
|
|
|
return monday_morning
|
|
|
|
|
|
|
|
|
|
|
|
def this_morning():
|
|
|
|
now = timezone.now()
|
|
|
|
morning = timezone.datetime(year=now.year,
|
|
|
|
month=now.month,
|
2017-02-15 14:44:58 +01:00
|
|
|
day=now.day,
|
|
|
|
hour=KFET_WAKES_UP_AT)
|
2016-12-09 21:45:34 +01:00
|
|
|
return morning
|
|
|
|
|
|
|
|
|
|
|
|
# Étant donné un queryset d'operations
|
|
|
|
# rend la somme des article_nb
|
|
|
|
def tot_ventes(queryset):
|
2017-02-15 14:21:00 +01:00
|
|
|
res = queryset.aggregate(Sum('article_nb'))['article_nb__sum']
|
|
|
|
return res and res or 0
|