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

Commit

Permalink
Add some basic docstrings to email class
Browse files Browse the repository at this point in the history
  • Loading branch information
chadwhitacre committed Feb 20, 2017
1 parent bb92d3e commit fd21b9a
Showing 1 changed file with 43 additions and 5 deletions.
48 changes: 43 additions & 5 deletions gratipay/models/participant/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,29 @@


class Email(object):
"""Participants may associate email addresses with their account.
Email addresses are stored in an ``emails`` table in the database, which
holds the addresses themselves as well as info related to address
verification. While a participant may have multiple email addresses on
file, verified or not, only one will be the *primary* email address: the
one also recorded in ``participants.email_address``. It's a bug for the
primary address not to be verified, or for an address to be in
``participants.email_address`` but not also in ``emails``.
Having a verified email is a prerequisite for certain other features on
Gratipay, such as linking a PayPal account, or filing a national identity.
"""

def add_email(self, email, resend_threshold='3 minutes'):
"""
This is called when
1) Adding a new email address
2) Resending the verification email for an unverified email address
"""Add an email address for a participant.
This is called when adding a new email address, and when resending the
verification email for an unverified email address.
:returns: the number of emails sent.
Returns the number of emails sent.
"""

# Check that this address isn't already verified
Expand Down Expand Up @@ -96,6 +111,8 @@ def add_email(self, email, resend_threshold='3 minutes'):
return 1

def update_email(self, email):
"""Set the email address for the participant.
"""
if not getattr(self.get_email(email), 'verified', False):
raise EmailNotVerified(email)
username = self.username
Expand Down Expand Up @@ -137,6 +154,8 @@ def verify_email(self, email, nonce):
return emails.VERIFICATION_SUCCEEDED

def get_email(self, email):
"""Return a record for a single email address on file for this participant.
"""
return self.db.one("""
SELECT *
FROM emails
Expand All @@ -145,6 +164,8 @@ def get_email(self, email):
""", (self.id, email))

def get_emails(self):
"""Return a list of all email addresses on file for this participant.
"""
return self.db.all("""
SELECT *
FROM emails
Expand All @@ -153,9 +174,15 @@ def get_emails(self):
""", (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
noop if the email address is not on file.
"""
if address == self.email_address:
raise CannotRemovePrimaryEmail()
with self.db.get_cursor() as c:
Expand All @@ -164,6 +191,9 @@ def remove_email(self, address):
(self.id, address))

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.
"""
context['participant'] = self
context['username'] = self.username
context['button_style'] = (
Expand Down Expand Up @@ -208,6 +238,9 @@ def render(t, context):
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)
Expand All @@ -216,6 +249,8 @@ def queue_email(self, spt_name, **context):

@classmethod
def dequeue_emails(cls):
"""Load messages queued for sending, and send them.
"""
fetch_messages = lambda: cls.db.all("""
SELECT *
FROM email_queue
Expand All @@ -237,6 +272,9 @@ def dequeue_emails(cls):
return nsent

def set_email_lang(self, accept_lang):
"""Given a language identifier, set it for the participant as their
preferred language in which to receive email.
"""
if not accept_lang:
return
self.db.run("UPDATE participants SET email_lang=%s WHERE id=%s",
Expand Down

0 comments on commit fd21b9a

Please sign in to comment.