Bugfix
[wolnelektury.git] / src / messaging / states.py
1 from datetime import timedelta
2 from django.utils.timezone import now
3 from django.utils.translation import ugettext_lazy as _
4 from .recipient import Recipient
5
6
7 class State:
8     allow_negative_offset = False
9     context_fields = []
10
11
12     def __init__(self, offset=0, time=None):
13         self.time = time or now()
14         if isinstance(offset, int):
15             offset = timedelta(offset)
16         self.offset = offset
17
18     def get_recipients(self):
19         return [
20             Recipient(
21                 hash_value=self.get_hash_value(obj),
22                 email=self.get_email(obj),
23                 context=self.get_context(obj),
24             )
25             for obj in self.get_objects()
26         ]
27
28     def get_objects(self):
29         raise NotImplemented
30
31     def get_hash_value(self, obj):
32         return str(obj.pk)
33     
34     def get_email(self, obj):
35         return obj.email
36
37     def get_context(self, obj):
38         ctx = {
39             obj._meta.model_name: obj,
40         }
41         return ctx
42
43
44 class ClubSingle(State):
45     slug = 'club-single'
46     name = _('club one-time donors')
47
48
49 class ClubSingleExpired(State):
50     slug = 'club-membership-expiring'
51     allow_negative_offset = True
52     name = _('club one-time donors with donation expiring')
53
54     def get_objects(self):
55         from club.models import Schedule
56         return Schedule.objects.filter(
57                 is_active=True,
58                 expires_at__lt=self.time - self.offset
59             )
60
61     def get_hashed_value(self, obj):
62         return '%s:%s' % (obj.pk, obj.expires_at.isoformat())
63
64
65 class ClubTried(State):
66     slug = 'club-payment-unfinished'
67     name = _('club would-be donors')
68
69     def get_objects(self):
70         from club.models import Schedule
71         return Schedule.objects.filter(
72                 payuorder=None,
73                 started_at__lt=self.time - self.offset,
74             )
75
76
77 class ClubRecurring(State):
78     slug = 'club-recurring'
79     name = _('club recurring donors')
80
81
82 class ClubRecurringExpired(State):
83     slug = 'club-recurring-payment-problem'
84     name = _('club recurring donors with donation expired')
85
86     def get_objects(self):
87         from club.models import Schedule
88         return Schedule.objects.none()
89
90
91 class Cold(State):
92     slug = 'cold'
93     name = _('cold group')
94
95
96 states = [
97     Cold,
98     ClubTried,
99     ClubSingle,
100     ClubSingleExpired,
101     ClubRecurring,
102     ClubRecurringExpired,
103 ]
104