예제 #1
0
 def pending_examples(self, session, ident):
     email = session.query(AutomatedEmail).filter_by(ident=ident).first()
     examples = []
     model = email.model_class
     query = AutomatedEmailFixture.queries.get(model)(session).order_by(
         model.id)
     limit = 100
     offset = 0
     for model_instance in query.limit(limit).offset(offset):
         if email.would_send_if_approved(model_instance):
             # These examples are never added to the session or saved to the database.
             # They are only used to render an example of the automated email.
             example = Email(
                 subject=email.render_subject(model_instance),
                 body=email.render_body(model_instance),
                 sender=email.sender,
                 to=model_instance.email,
                 cc=email.cc,
                 bcc=email.bcc,
                 ident=email.ident,
                 fk_id=model_instance.id,
                 automated_email_id=email.id,
                 automated_email=email,
             )
             examples.append((model_instance, example))
             example_count = len(examples)
             if example_count > 1 or (example_count == 1 and offset > 0):
                 break
         offset += limit
     return {
         'email': email,
         'examples': examples,
     }
예제 #2
0
def test_get_fk_from_id():
    with Session() as session:
        a = Attendee(first_name='Regular', last_name='Attendee')
        e = Email(fk_id=a.id, model='Attendee')
        session.add(a)
        session.add(e)
        session.commit()
        assert a == e.fk
예제 #3
0
def test_no_name():
    assert None is Email().rcpt_name
예제 #4
0
def test_attendee_name(monkeypatch):
    monkeypatch.setattr(Email, 'fk', Attendee(first_name="Test", last_name="Attendee"))
    assert "Test Attendee" == Email(model='Attendee').rcpt_name
 def test_fk_no_id(self):
     assert None is Email().fk
예제 #6
0
def test_html_from_html():
    assert "HTML Content" == Email(body="<body>HTML Content</body>").html
예제 #7
0
def test_is_html():
    assert Email(body="<body").is_html
예제 #8
0
def test_no_email():
    assert None is Email().rcpt_email
예제 #9
0
def send_email(sender,
               to,
               subject,
               body,
               format='text',
               cc=(),
               bcc=(),
               model=None,
               ident=None,
               automated_email=None,
               session=None):

    to, cc, bcc = map(lambda x: listify(x if x else []), [to, cc, bcc])
    original_to, original_cc, original_bcc = to, cc, bcc
    ident = ident or subject
    if c.DEV_BOX:
        to, cc, bcc = map(lambda xs: list(filter(_is_dev_email, xs)),
                          [to, cc, bcc])

    if c.SEND_EMAILS and to:
        msg_kwargs = {'bodyText' if format == 'text' else 'bodyHtml': body}
        message = EmailMessage(subject=subject, **msg_kwargs)
        AmazonSES(c.AWS_ACCESS_KEY,
                  c.AWS_SECRET_KEY).sendEmail(source=sender,
                                              toAddresses=to,
                                              ccAddresses=cc,
                                              bccAddresses=bcc,
                                              message=message)
        sleep(0.1)  # Avoid hitting rate limit
    else:
        log.error('Email sending turned off, so unable to send {}', locals())

    if original_to:
        body = body.decode('utf-8') if isinstance(body, bytes) else body
        if isinstance(model, MagModel):
            fk_kwargs = {'fk_id': model.id, 'model': model.__class__.__name__}
        elif isinstance(model, Mapping):
            fk_kwargs = {
                'fk_id': model.get('id', None),
                'model': model.get('_model', model.get('__type__', 'n/a'))
            }
        else:
            fk_kwargs = {'model': 'n/a'}

        if automated_email:
            if isinstance(automated_email, MagModel):
                fk_kwargs['automated_email_id'] = automated_email.id
            elif isinstance(model, Mapping):
                fk_kwargs['automated_email_id'] = automated_email.get(
                    'id', None)

        email = Email(subject=subject,
                      body=body,
                      sender=sender,
                      to=','.join(original_to),
                      cc=','.join(original_cc),
                      bcc=','.join(original_bcc),
                      ident=ident,
                      **fk_kwargs)

        session = session or getattr(model, 'session',
                                     getattr(automated_email, 'session', None))
        if session:
            session.add(email)
        else:
            with Session() as session:
                session.add(email)
 def test_is_html(self, body, expected):
     assert Email(body=body).is_html == expected
 def test_fk_email(self, kwargs, expected):
     assert expected == Email(**kwargs).fk_email
 def test_fk_email_with_group(self, set_email_fk_group):
     assert '*****@*****.**' == Email(model='Group').fk_email
 def test_fk_email_with_attendee(self, set_email_fk_attendee):
     assert '*****@*****.**' == Email(model='Attendee').fk_email
 def test_fk_invalid_id(self):
     assert None is Email(fk_id='invalid').fk
예제 #15
0
def test_group_email(monkeypatch):
    monkeypatch.setattr(Email, 'fk', Group(leader=Attendee(email="*****@*****.**")))
    assert "*****@*****.**" == Email(model='Group').rcpt_email
예제 #16
0
def test_attendee_email(monkeypatch):
    monkeypatch.setattr(Email, 'fk', Attendee(email="*****@*****.**"))
    assert "*****@*****.**" == Email(model='Attendee').rcpt_email
예제 #17
0
def test_get_fk_no_id():
    assert None is Email().fk
예제 #18
0
def test_email_from_dest():
    assert "*****@*****.**" == Email(dest="*****@*****.**").rcpt_email
예제 #19
0
def test_get_fk_fake_id():
    assert None is Email(fk_id="blah").fk
예제 #20
0
def test_is_not_html():
    assert not Email().is_html
예제 #21
0
def test_group_name(monkeypatch):
    monkeypatch.setattr(Email, 'fk', Group(leader=Attendee(first_name="Test", last_name="Leader")))
    assert "Test Leader" == Email(model='Group').rcpt_name
예제 #22
0
def test_html_from_text():
    assert "Test<br/>Content" == Email(body="Test\nContent").html
 def test_model_class(self, model, expected):
     assert Email(model=model).model_class is expected