d6912972014785510e4068ec74dbaf6211a4e7a0
[wolnelektury.git] / src / club / templatetags / club.py
1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
3 #
4 from datetime import timedelta
5 from django.db.models import Sum
6 from django import template
7 from django.utils.timezone import now
8 from ..helpers import get_active_schedule
9 from ..models import Schedule
10
11
12 register = template.Library()
13
14
15 @register.filter
16 def active_schedule(user):
17     return get_active_schedule(user)
18
19
20 @register.simple_tag
21 def club_count_recurring():
22     return Schedule.objects.exclude(monthly=False, yearly=False).filter(expires_at__gt=now()).count()
23
24 @register.simple_tag
25 def club_active_monthly_count():
26     return Schedule.objects.filter(
27         expires_at__gt=now(),
28         monthly=True,
29         is_cancelled=False
30     ).count()
31
32 @register.simple_tag
33 def club_active_monthly_sum():
34     return Schedule.objects.filter(
35         expires_at__gt=now(),
36         monthly=True,
37         is_cancelled=False
38     ).aggregate(s=Sum('amount'))['s'] or 0
39
40 @register.simple_tag
41 def club_active_yearly_count():
42     return Schedule.objects.filter(
43         expires_at__gt=now(),
44         yearly=True,
45         is_cancelled=False
46     ).count()
47
48 @register.simple_tag
49 def club_active_yearly_sum():
50     return Schedule.objects.filter(
51         expires_at__gt=now(),
52         yearly=True,
53         is_cancelled=False
54     ).aggregate(s=Sum('amount'))['s'] or 0
55
56 @register.simple_tag
57 def club_active_30day_count():
58     return Schedule.objects.filter(
59         yearly=False, monthly=False,
60         payed_at__gte=now() - timedelta(days=30)
61     ).count()
62
63 @register.simple_tag
64 def club_active_30day_sum():
65     return Schedule.objects.filter(
66         yearly=False, monthly=False,
67         payed_at__gte=now() - timedelta(days=30)
68     ).aggregate(s=Sum('amount'))['s'] or 0
69
70
71 @register.simple_tag
72 def club_monthly_since(start):
73     return Schedule.objects.filter(
74         monthly=True, payed_at__gte=start).count()
75
76
77 @register.simple_tag
78 def club_monthly_missing_since(start, target):
79     return target - Schedule.objects.filter(
80         monthly=True, payed_at__gte=start).count()