1 from datetime import timedelta
2 from django.apps import apps
3 from django.utils.timezone import now
4 from django.utils.translation import ugettext_lazy as _
16 allow_negative_offset = False
20 def __init__(self, time=None, min_days_since=None, max_days_since=None, test=False):
21 self.time = time or now()
22 self.min_days_since = min_days_since
23 self.max_days_since = max_days_since
26 def get_contacts(self):
27 Contact = apps.get_model('messaging', 'Contact')
28 contacts = Contact.objects.filter(level=self.level)
30 if self.min_days_since is not None or self.expired:
31 cutoff = self.time - timedelta(self.min_days_since or 0)
33 contacts = contacts.filter(expires_at__lt=cutoff)
35 contacts = contacts.filter(since__lt=cutoff)
37 if self.max_days_since is not None:
38 cutoff = self.time - timedelta(self.max_days_since)
40 contacts = contacts.filter(expires_at__gt=cutoff)
42 contacts = contacts.filter(since__gt=cutoff)
44 if self.expired is False:
45 contacts = contacts.exclude(expires_at__lt=self.time)
49 def get_context(self, contact):
51 return self.get_example_context(contact)
53 Schedule = apps.get_model('club', 'Schedule')
54 schedules = Schedule.objects.filter(email=contact.email)
56 "schedule": self.get_schedule(schedules)
59 def get_example_context(self, contact):
60 Schedule = apps.get_model('club', 'Schedule')
66 payed_at=self.time - timedelta(2),
67 started_at=self.time - timedelta(1),
68 expires_at=self.time + timedelta(1),
73 class ClubSingle(State):
75 name = _('club one-time donors')
79 def get_schedule(self, schedules):
80 # Find first single non-expired schedule.
81 return schedules.filter(
82 monthly=False, yearly=False,
83 expires_at__gt=self.time
84 ).order_by('started_at').first()
87 class ClubSingleExpired(State):
88 slug = 'club-membership-expiring'
89 allow_negative_offset = True
90 name = _('club one-time donors with donation expiring')
94 def get_schedule(self, schedules):
95 # Find last single expired schedule.
96 return schedules.filter(
97 monthly=False, yearly=False,
98 expires_at__lt=self.time
99 ).order_by('-expires_at').first()
102 class ClubTried(State):
103 slug = 'club-payment-unfinished'
104 name = _('club would-be donors')
107 def get_schedule(self, schedules):
108 # Find last unpaid schedule.
109 return schedules.filter(
111 ).order_by('-started_at').first()
114 class ClubRecurring(State):
115 slug = 'club-recurring'
116 name = _('club recurring donors')
117 level = Level.RECURRING
120 def get_schedule(self, schedules):
121 # Find first recurring non-expired schedule.
122 return schedules.exclude(
123 monthly=False, yearly=False
125 expires_at__gt=self.time
126 ).order_by('started_at').first()
129 class ClubRecurringExpired(State):
130 slug = 'club-recurring-payment-problem'
131 name = _('club recurring donors with donation expired')
132 level = Level.RECURRING
135 def get_schedule(self, schedules):
136 # Find last recurring expired schedule.
137 return schedules.exclude(
138 monthly=False, yearly=False
140 expires_at__lt=self.time
141 ).order_by('-expires_at').first()
146 name = _('cold group')
149 def get_context(self, contact):
159 ClubRecurringExpired,