Exemplo n.º 1
0
    def run_worker(self, *args, **kwargs):
        current_time = datetime.utcnow()
        email_medium = get_medium()
        to_send = (
            Email.objects.filter(scheduled__lte=current_time, sent__isnull=True)
            .select_related("event")
            .prefetch_related("recipients")
        )

        # Fetch the contexts of every event so that they may be rendered
        context_loader.load_contexts_and_renderers([e.event for e in to_send], [email_medium])

        default_from_email = get_from_email_address()
        emails = []
        for email in to_send:
            to_email_addresses = get_subscribed_email_addresses(email)
            if to_email_addresses:
                text_message, html_message = email.render(email_medium)
                message = create_email_message(
                    to_emails=to_email_addresses,
                    from_email=email.from_address or default_from_email,
                    subject=email.subject or extract_email_subject_from_html_content(html_message),
                    text=text_message,
                    html=html_message,
                )
                emails.append(message)

        connection = mail.get_connection()
        connection.send_messages(emails)
        to_send.update(sent=current_time)
Exemplo n.º 2
0
def convert_events_to_emails():
    """
    Converts unseen events to emails and marks them as seen.
    """
    email_medium = get_medium()

    for event, targets in email_medium.events_targets(seen=False, mark_seen=True):
        Email.objects.create_email(event=event, recipients=targets)
Exemplo n.º 3
0
def get_subscribed_email_addresses(email):
    """From an email object determine the appropriate email addresses.

    Excludes the addresses of those who unsubscribed from the email's
    type.

    Returns:
      A list of strings: email addresses.
    """
    email_medium = get_medium()
    if email.subentity_type is not None:
        all_entities = list(email.send_to.get_sub_entities().is_any_type(
            email.subentity_type))
    else:
        all_entities = [email.send_to]
    send_to = Subscription.objects.filter_not_subscribed(source=email.source,
                                                         medium=email_medium,
                                                         entities=all_entities)
    emails = [e.entity_meta['email'] for e in send_to]
    return emails
 def test_custom(self):
     custom_medium_name = 'test-email'
     with self.settings(ENTITY_EMAILER_MEDIUM_NAME=custom_medium_name):
         call_command('add_email_medium')
         medium = get_medium()
     self.assertEqual(medium.name, custom_medium_name)
 def test_default(self):
     call_command('add_email_medium')
     medium = get_medium()
     self.assertEqual(medium.name, 'email')
 def test_custom(self):
     custom_medium_name = 'test-email'
     with self.settings(ENTITY_EMAILER_MEDIUM_NAME=custom_medium_name):
         call_command('add_email_medium')
         medium = get_medium()
     self.assertEqual(medium.name, custom_medium_name)
 def test_default(self):
     call_command('add_email_medium')
     medium = get_medium()
     self.assertEqual(medium.name, 'email')