Sending test emails.
[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, time=None, min_days_since=None, max_days_since=None):
13         self.time = time or now()
14         self.min_days_since = min_days_since
15         self.max_days_since = max_days_since
16
17     def get_recipients(self):
18         return [
19             self.get_recipient(obj)
20             for obj in self.get_objects()
21         ]
22
23     def get_recipient(self, obj):
24         return Recipient(
25                 hash_value=self.get_hash_value(obj),
26                 email=self.get_email(obj),
27                 context=self.get_context(obj),
28             )
29
30     def get_example_recipient(self, email):
31         return self.get_recipient(
32                 self.get_example_object(email)
33             )
34
35     def get_example_object(self, email):
36         from club.models import Schedule
37         n = now()
38         return Schedule(
39                 email=email,
40                 key='xxxxxxxxx',
41                 amount=100,
42                 payed_at=n - timedelta(2),
43                 started_at=n - timedelta(1),
44                 expires_at=n + timedelta(1),
45             )
46
47     def get_objects(self):
48         raise NotImplemented
49
50     def get_hash_value(self, obj):
51         return str(obj.pk)
52     
53     def get_email(self, obj):
54         return obj.email
55
56     def get_context(self, obj):
57         ctx = {
58             obj._meta.model_name: obj,
59         }
60         return ctx
61
62
63 class ClubSingle(State):
64     slug = 'club-single'
65     name = _('club one-time donors')
66
67
68 class ClubSingleExpired(State):
69     slug = 'club-membership-expiring'
70     allow_negative_offset = True
71     name = _('club one-time donors with donation expiring')
72
73     def get_objects(self):
74         from club.models import Schedule
75         return Schedule.objects.filter(
76                 is_active=True,
77                 expires_at__lt=self.time - self.offset
78             )
79
80     def get_hashed_value(self, obj):
81         return '%s:%s' % (obj.pk, obj.expires_at.isoformat())
82
83
84 class ClubTried(State):
85     slug = 'club-payment-unfinished'
86     name = _('club would-be donors')
87
88     def get_objects(self):
89         from club.models import Schedule
90         return Schedule.objects.filter(
91                 payuorder=None,
92                 started_at__lt=self.time - self.offset,
93             )
94
95
96 class ClubRecurring(State):
97     slug = 'club-recurring'
98     name = _('club recurring donors')
99
100
101 class ClubRecurringExpired(State):
102     slug = 'club-recurring-payment-problem'
103     name = _('club recurring donors with donation expired')
104
105     def get_objects(self):
106         from club.models import Schedule
107         return Schedule.objects.none()
108
109
110 class Cold(State):
111     slug = 'cold'
112     name = _('cold group')
113
114
115 states = [
116     Cold,
117     ClubTried,
118     ClubSingle,
119     ClubSingleExpired,
120     ClubRecurring,
121     ClubRecurringExpired,
122 ]
123