# -*- coding: utf-8 -*- from django.utils import timezone from django.db.models import Sum KFET_WAKES_UP_AT = 7 # donne le nom des jours d'une liste de dates # dans un dico ordonné def daynames(dates): names = {} for i in dates: names[i] = dates[i].strftime("%A") return names # donne le nom des semaines une liste de dates # dans un dico ordonné def weeknames(dates): names = {} for i in dates: names[i] = dates[i].strftime("Semaine %W") return names # donne le nom des mois d'une liste de dates # dans un dico ordonné def monthnames(dates): names = {} for i in dates: names[i] = dates[i].strftime("%B") 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() first_days = {} this_year = first_month_day.year this_month = first_month_day.month for i in range(1, nb+1): month = ((this_month - 1 - (nb - i)) % 12) + 1 year = this_year + (nb - i) // 12 first_days[i] = timezone.datetime(year=year, month=month, day=1, hour=KFET_WAKES_UP_AT) return first_days def this_first_month_day(): now = timezone.now() first_day = timezone.datetime(year=now.year, month=now.month, day=1, hour=KFET_WAKES_UP_AT) 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, hour=KFET_WAKES_UP_AT) return monday_morning def this_morning(): now = timezone.now() morning = timezone.datetime(year=now.year, month=now.month, day=now.day, hour=KFET_WAKES_UP_AT) return morning # Étant donné un queryset d'operations # rend la somme des article_nb def tot_ventes(queryset): res = queryset.aggregate(Sum('article_nb'))['article_nb__sum'] return res and res or 0