b0ce868178cc7e2e7a745bcba949529381c6a909
[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_active_monthly_count():
22     return Schedule.objects.filter(
23         expires_at__gt=now(),
24         monthly=True,
25         is_cancelled=False
26     ).count()
27
28 @register.simple_tag
29 def club_active_monthly_sum():
30     return Schedule.objects.filter(
31         expires_at__gt=now(),
32         monthly=True,
33         is_cancelled=False
34     ).aggregate(s=Sum('amount'))['s'] or 0
35
36 @register.simple_tag
37 def club_active_yearly_count():
38     return Schedule.objects.filter(
39         expires_at__gt=now(),
40         yearly=True,
41         is_cancelled=False
42     ).count()
43
44 @register.simple_tag
45 def club_active_yearly_sum():
46     return Schedule.objects.filter(
47         expires_at__gt=now(),
48         yearly=True,
49         is_cancelled=False
50     ).aggregate(s=Sum('amount'))['s'] or 0
51
52 @register.simple_tag
53 def club_active_30day_count():
54     return Schedule.objects.filter(
55         yearly=False, monthly=False,
56         payed_at__gte=now() - timedelta(days=30)
57     ).count()
58
59 @register.simple_tag
60 def club_active_30day_sum():
61     return Schedule.objects.filter(
62         yearly=False, monthly=False,
63         payed_at__gte=now() - timedelta(days=30)
64     ).aggregate(s=Sum('amount'))['s'] or 0