示例#1
0
 def test_parse_recipients_standard(self):
     """Test parsing a standard recipients file."""
     exp = {'foo1': ('abcABC123', ['*****@*****.**']),
            'foo2': ('1skdiwoadk',
                     ['*****@*****.**', '*****@*****.**', '*****@*****.**'])}
     obs = parse_recipients(self.recipients1)
     self.assertEqual(obs, exp)
示例#2
0
def notify_participants(recipients_f, email_settings_f, dry_run=True):
    """Sends an email to each participant in the study.

    Arguments:
        recipients_f - file containing email recipients (see
            parse.parse_recipients for more details)
        email_settings_f - file containing settings for sending emails (see
            parse.parse_email_settings for more details)
        dry_run - if True, no emails are sent and information of what would
            have been done is printed to stdout. If False, no output is printed
            and emails are sent
    """
    recipients = parse_recipients(recipients_f)
    email_settings = parse_email_settings(email_settings_f)

    sender = email_settings['sender']
    email_password = email_settings['password']
    server = email_settings['smtp_server']
    port = email_settings['smtp_port']

    if dry_run:
        num_recipients = len(recipients)

        print("Running script in dry-run mode. No emails will be sent. Here's "
              "what I would have done:\n")
        print("Sender information:\n\nFrom address: %s\nPassword: %s\nSMTP "
              "server: %s\nPort: %s\n" % (sender, email_password, server,
                                          port))
        print "Sending emails to %d recipient(s)." % num_recipients

        if num_recipients > 0:
            # Sort so that we will grab the same recipient each time this is
            # run over the same input files.
            sample_recipient = sorted(recipients.items())[0]
            personal_id = sample_recipient[0]
            password, addresses = sample_recipient[1]

            print "\nSample email:\n"
            print "To: %s" % ', '.join(addresses)
            print "From: %s" % sender
            print "Subject: %s" % notification_email_subject
            print "Body:\n%s\n" % get_personalized_notification_email_text(
                    personal_id, password)
    else:
        for personal_id, (password, addresses) in recipients.items():
            personalized_text = \
                    get_personalized_notification_email_text(personal_id,
                                                             password)
            print "Sending email to %s (%s)... " % (personal_id,
                                                    ', '.join(addresses)),
            send_email(server, port, sender, email_password, addresses,
                       notification_email_subject, personalized_text)
            print "success!"
示例#3
0
def format_participant_table(participants_f, url_prefix):
    """Formats an HTML table of personal IDs with links to personal results.

    Returns the HTMl table as a string suitable for writing to a file. Personal
    IDs will be sorted.

    Arguments:
        participants_f - file in same format as that accepted by
            my_microbes.parse.parse_recipients. Email addresses are
            ignored
        url_prefix - URL to prefix each personal ID with to provide links to
            personalized results (string)
    """
    personal_ids = sorted(parse_recipients(participants_f).keys())
    url_prefix = url_prefix if url_prefix.endswith('/') else url_prefix + '/'

    result = '<table class="data-table">\n<tr><th>Personal ID</th></tr>\n'
    for personal_id in personal_ids:
        url = url_prefix + personal_id + '/index.html'
        result += '<tr><td><a href="%s">%s</a></td></tr>\n' % (url,
                                                               personal_id)
    result += '</table>\n'

    return result
示例#4
0
 def test_parse_recipients_empty(self):
     """Test parsing an empty recipients file."""
     obs = parse_recipients(self.recipients2)
     self.assertEqual(obs, {})
示例#5
0
 def test_parse_recipients_standard(self):
     """Test parsing a standard recipients file."""
     exp = {'foo1': ['*****@*****.**'],
            'foo2': ['*****@*****.**', '*****@*****.**', '*****@*****.**']}
     obs = parse_recipients(self.recipients1)
     self.assertEqual(obs, exp)