]>
Commit | Line | Data |
---|---|---|
d73bba54 MT |
1 | #!/usr/bin/python3 |
2 | ||
3 | import logging | |
4 | ||
5 | from .decorators import * | |
6 | from .misc import Object | |
7 | ||
8 | class Campaigns(Object): | |
18c38597 | 9 | async def launch_manually(self, name, uid): |
d73bba54 MT |
10 | account = self.backend.accounts.get_by_uid(uid) |
11 | if account: | |
18c38597 | 12 | self.launch(name, account) |
d73bba54 | 13 | |
18c38597 | 14 | def launch(self, name, account): |
d73bba54 MT |
15 | logging.debug("Launching all campaigns for %s" % account) |
16 | ||
17 | # Update old timestamps first | |
18 | self.db.execute("UPDATE campaign_templates \ | |
19 | SET launch_at = launch_at + repeat_after \ | |
20 | WHERE (launch_at IS NOT NULL AND launch_at <= CURRENT_TIMESTAMP) \ | |
21 | AND repeat_after IS NOT NULL") | |
22 | ||
23 | # Launch all campaigns | |
24 | self.db.execute("INSERT INTO campaign_emails(account_uid, template, \ | |
a0f2633f | 25 | launch_at, repeat_after, promotional) \ |
d73bba54 | 26 | SELECT %s, template, COALESCE(launch_at, CURRENT_TIMESTAMP + launch_after), \ |
a0f2633f | 27 | repeat_after, promotional FROM campaign_templates WHERE name = %s", |
18c38597 | 28 | account.uid, name) |
d73bba54 MT |
29 | |
30 | def _get_campaign_emails(self, query, *args): | |
31 | res = self.db.query(query, *args) | |
32 | ||
33 | for row in res: | |
34 | yield CampaignEmail(self.backend, row.id, data=row) | |
35 | ||
36 | async def run(self): | |
37 | with self.db.transaction(): | |
38 | emails = self._get_campaign_emails("SELECT * FROM campaign_emails \ | |
39 | WHERE launch_at <= CURRENT_TIMESTAMP ORDER BY launch_at") | |
40 | ||
41 | # Send them all | |
42 | for email in emails: | |
43 | email.send() | |
44 | ||
45 | ||
46 | class CampaignEmail(Object): | |
47 | def init(self, id, data=None): | |
48 | self.id = id | |
49 | self.data = data | |
50 | ||
51 | @lazy_property | |
52 | def account(self): | |
53 | return self.backend.accounts.get_by_uid(self.data.account_uid) | |
54 | ||
55 | @property | |
56 | def template(self): | |
57 | return self.data.template | |
58 | ||
59 | @property | |
60 | def repeat_after(self): | |
61 | return self.data.repeat_after | |
62 | ||
a0f2633f MT |
63 | @property |
64 | def promotional(self): | |
65 | return self.data.promotional | |
66 | ||
67 | @property | |
68 | def should_be_sent(self): | |
69 | # Send all non-promotional emails | |
70 | if not self.promotional: | |
71 | return True | |
72 | ||
73 | # For promotional emails, send them if we have consent | |
74 | return self.account.consents_to_promotional_emails | |
75 | ||
d73bba54 MT |
76 | def send(self): |
77 | # Delete if the account does not exist any more | |
78 | if not self.account: | |
79 | return self._delete() | |
80 | ||
54764eeb | 81 | logging.debug("Sending %s to %s" % (self.template, self.account)) |
d73bba54 MT |
82 | |
83 | # Generate the email | |
a0f2633f MT |
84 | if self.should_be_sent: |
85 | self.backend.messages.send_template(self.template, account=self.account) | |
d73bba54 MT |
86 | |
87 | # Update this email for the next launch | |
88 | self._update() | |
89 | ||
90 | def _update(self): | |
91 | # If this email repeats, we will update the timestamp | |
92 | if self.repeat_after: | |
93 | self.db.execute("UPDATE campaign_emails \ | |
94 | SET launch_at = launch_at + repeat_after \ | |
95 | WHERE id = %s", self.id) | |
96 | ||
97 | # Otherwise we will delete it | |
98 | else: | |
99 | self._delete() | |
100 | ||
101 | def _delete(self): | |
102 | """ | |
103 | Deletes this email | |
104 | """ | |
105 | self.db.execute("DELETE FROM campaign_emails WHERE id = %s", self.id) |