Skip to content
This repository has been archived by the owner on Feb 8, 2018. It is now read-only.

Commit

Permalink
Reorder methods + whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
chadwhitacre committed Feb 28, 2017
1 parent 26b7e42 commit 7174836
Showing 1 changed file with 43 additions and 33 deletions.
76 changes: 43 additions & 33 deletions gratipay/models/participant/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ def add_email(self, email, resend_threshold='3 minutes'):
return 2
return 1


def update_email(self, email):
"""Set the email address for the participant.
"""
Expand All @@ -125,6 +126,7 @@ def update_email(self, email):
""", locals())
self.set_attributes(email_address=email)


def verify_email(self, email, nonce):
if '' in (email, nonce):
return emails.VERIFICATION_MISSING
Expand Down Expand Up @@ -153,6 +155,7 @@ def verify_email(self, email, nonce):
self.update_email(email)
return emails.VERIFICATION_SUCCEEDED


def get_email(self, email):
"""Return a record for a single email address on file for this participant.
"""
Expand All @@ -163,6 +166,7 @@ def get_email(self, email):
AND address=%s
""", (self.id, email))


def get_emails(self):
"""Return a list of all email addresses on file for this participant.
"""
Expand All @@ -173,11 +177,13 @@ def get_emails(self):
ORDER BY id
""", (self.id,))


def get_verified_email_addresses(self):
"""Return a list of verified email addresses on file for this participant.
"""
return [email.address for email in self.get_emails() if email.verified]


def remove_email(self, address):
"""Remove the given email address from the participant's account.
Raises ``CannotRemovePrimaryEmail`` if the address is primary. It's a
Expand All @@ -190,6 +196,43 @@ def remove_email(self, address):
c.run("DELETE FROM emails WHERE participant_id=%s AND address=%s",
(self.id, address))


def queue_email(self, spt_name, **context):
"""Given the name of a template under ``emails/`` and context in
kwargs, queue a message to be sent.
"""
self.db.run("""
INSERT INTO email_queue
(participant, spt_name, context)
VALUES (%s, %s, %s)
""", (self.id, spt_name, pickle.dumps(context)))


@classmethod
def dequeue_emails(cls):
"""Load messages queued for sending, and send them.
"""
fetch_messages = lambda: cls.db.all("""
SELECT *
FROM email_queue
ORDER BY id ASC
LIMIT 60
""")
nsent = 0
while True:
messages = fetch_messages()
if not messages:
break
for msg in messages:
p = cls.from_id(msg.participant)
r = p.send_email(msg.spt_name, **pickle.loads(msg.context))
cls.db.run("DELETE FROM email_queue WHERE id = %s", (msg.id,))
if r == 1:
sleep(1)
nsent += r
return nsent


def send_email(self, spt_name, **context):
"""Given the name of an email template and context in kwargs, send an
email using the configured mailer.
Expand Down Expand Up @@ -237,39 +280,6 @@ def render(t, context):
self._mailer.send_email(**message)
return 1 # Sent

def queue_email(self, spt_name, **context):
"""Given the name of a template under ``emails/`` and context in
kwargs, queue a message to be sent.
"""
self.db.run("""
INSERT INTO email_queue
(participant, spt_name, context)
VALUES (%s, %s, %s)
""", (self.id, spt_name, pickle.dumps(context)))

@classmethod
def dequeue_emails(cls):
"""Load messages queued for sending, and send them.
"""
fetch_messages = lambda: cls.db.all("""
SELECT *
FROM email_queue
ORDER BY id ASC
LIMIT 60
""")
nsent = 0
while True:
messages = fetch_messages()
if not messages:
break
for msg in messages:
p = cls.from_id(msg.participant)
r = p.send_email(msg.spt_name, **pickle.loads(msg.context))
cls.db.run("DELETE FROM email_queue WHERE id = %s", (msg.id,))
if r == 1:
sleep(1)
nsent += r
return nsent

def set_email_lang(self, accept_lang):
"""Given a language identifier, set it for the participant as their
Expand Down

0 comments on commit 7174836

Please sign in to comment.