]>
git.ipfire.org Git - ipfire.org.git/blob - src/backend/campaigns.py
5 from .decorators
import *
6 from .misc
import Object
8 class Campaigns(Object
):
9 async def launch_manually(self
, uid
):
10 account
= self
.backend
.accounts
.get_by_uid(uid
)
14 def launch(self
, account
):
15 logging
.debug("Launching all campaigns for %s" % account
)
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")
23 # Launch all campaigns
24 self
.db
.execute("INSERT INTO campaign_emails(account_uid, template, \
25 launch_at, repeat_after, groups ) \
26 SELECT %s, template, COALESCE(launch_at, CURRENT_TIMESTAMP + launch_after), \
27 repeat_after, groups FROM campaign_templates", account
.uid
)
29 def _get_campaign_emails(self
, query
, *args
):
30 res
= self
.db
.query(query
, *args
)
33 yield CampaignEmail(self
.backend
, row
.id, data
=row
)
36 with self
.db
.transaction():
37 emails
= self
._get
_campaign
_emails
("SELECT * FROM campaign_emails \
38 WHERE launch_at <= CURRENT_TIMESTAMP ORDER BY launch_at")
45 class CampaignEmail(Object
):
46 def init(self
, id, data
=None):
52 return self
.backend
.accounts
.get_by_uid(self
.data
.account_uid
)
56 return self
.data
.template
59 def repeat_after(self
):
60 return self
.data
.repeat_after
63 # Delete if the account does not exist any more
67 logging
.info("Sending %s to %s" % (self
.template
, self
.account
))
70 self
.backend
.messages
.send_template(self
.template
, account
=self
.account
)
72 # Update this email for the next launch
76 # If this email repeats, we will update the timestamp
78 self
.db
.execute("UPDATE campaign_emails \
79 SET launch_at = launch_at + repeat_after \
80 WHERE id = %s", self
.id)
82 # Otherwise we will delete it
90 self
.db
.execute("DELETE FROM campaign_emails WHERE id = %s", self
.id)