Beispiel #1
0
    def post(self, request):
        """
        Renders a test email
        """
        form = self.form_cls(request.POST)

        if not form.is_valid():
            return JsonResponse({"error": "invalid input"})

        email_type = form.cleaned_data["email_type"]
        context = {
            "base_url": settings.SITE_BASE_URL,
            "site_name": settings.SITE_NAME
        }

        email_extra_context = EMAIL_DEBUG_EXTRA_CONTEXT.get(email_type, {})
        context.update(email_extra_context)

        subject, text_body, html_body = api.render_email_templates(
            email_type, context)

        return JsonResponse({
            "subject": subject,
            "html_body": html_body,
            "text_body": text_body
        })
def test_render_email_templates(user):
    """Test render_email_templates"""
    user.profile.name = "Jane Smith"
    context = context_for_user(user=user,
                               extra_context={"url": "http://example.com"})
    subject, text_body, html_body = render_email_templates("sample", context)
    assert subject == "Welcome Jane Smith"
    assert text_body == "html link (http://example.com)"
    assert html_body == ('<style type="text/css">\n'
                         "a {\n"
                         "  color: red;\n"
                         "}\n"
                         "</style>\n"
                         '<a href="http://example.com">html link</a>\n')
Beispiel #3
0
 def test_render_email_templates(self, _):
     """Test render_email_templates"""
     video = VideoFactory.create()
     context = context_for_video(video)
     subject, text_body, html_body = render_email_templates("sample", context)
     assert subject == f'Your video "{video.title}" is ready'
     assert text_body == f"html link ({context['video_url']})"
     assert html_body == (
         '<style type="text/css">\n'
         "a {\n"
         "  color: red;\n"
         "}\n"
         "</style>\n"
         f'<a href="{context["video_url"]}">html link</a>\n'
     )
Beispiel #4
0
def send_notification_email(video):
    """
    Sends notification emails to the admin of the channels where a video belongs
    according to the video status.

    Args:
        video (ui.models.Video): a video object
    """
    if video.status not in STATUS_TO_NOTIFICATION.keys():
        log.error(
            "Unexpected video status",
            video_hexkey=video.hexkey,
            video_status=video.status,
        )
        return
    # get the list of emails
    recipients = _get_recipients_for_video(video)
    if not recipients:
        log.error(
            "No email sent, no valid recipient emails",
            video_hexkey=video.hexkey,
            video_status=video.status,
        )
        return
    try:
        email_template = STATUS_TO_NOTIFICATION[video.status]
        subject, text_body, html_body = render_email_templates(
            email_template, context_for_video(video))
        email_kwargs = {
            "subject": subject,
            "html_body": html_body,
            "text_body": text_body,
            "recipients": [(recipient, {}) for recipient in recipients],
            "raise_for_status": True,
            "sender_address": settings.EMAIL_SUPPORT,
        }
        api.MailgunClient.send_batch(**email_kwargs)
        if video.status in STATUSES_THAT_TRIGGER_DEBUG_EMAIL:
            _send_debug_email(video=video, email_kwargs=email_kwargs)
    except:  # pylint: disable=bare-except
        log.exception(
            "Impossible to send notification",
            video_hexkey=video.hexkey,
            video_status=video.status,
        )
Beispiel #5
0
def test_send_notification_email_happy_path(mocker):
    """
    Tests send_notification_email with happy path
    """
    mocked_mailgun = mocker.patch("mail.api.MailgunClient", autospec=True)
    assert VideoStatus.COMPLETE in tasks.STATUS_TO_NOTIFICATION
    video = VideoFactory(status=VideoStatus.COMPLETE)
    subject, text, html = render_email_templates(
        STATUS_TO_NOTIFICATION[VideoStatus.COMPLETE], context_for_video(video))
    tasks.send_notification_email(video)
    mocked_mailgun.send_batch.assert_called_once_with(
        **{
            "subject": subject,
            "html_body": html,
            "text_body": text,
            "recipients": [(video.collection.owner.email, {})],
            "sender_address": settings.EMAIL_SUPPORT,
            "raise_for_status": True,
        })
Beispiel #6
0
 def send(self, to, *args, **kwargs):
     """
     Overrides djoser.email.PasswordResetEmail#send to use our mail API.
     """
     context = self.get_context_data()
     context.update(self.context)
     with django_mail.get_connection(
             settings.NOTIFICATION_EMAIL_BACKEND) as connection:
         subject, text_body, html_body = render_email_templates(
             "password_reset", context)
         msg = AnymailMessage(
             subject=subject,
             body=text_body,
             to=to,
             from_email=settings.MAILGUN_FROM_EMAIL,
             connection=connection,
         )
         msg.attach_alternative(html_body, "text/html")
         send_messages([msg])
Beispiel #7
0
    def post(self, request):
        """
        Renders a test email
        """
        form = self.form_cls(request.POST)

        if not form.is_valid():
            return JsonResponse({"error": "invalid input"})

        email_type = form.cleaned_data["email_type"]
        context = {
            "base_url": settings.SITE_BASE_URL,
            "anon_token": "abc123",
            "site_name": get_default_site().title,
        }

        # static, dummy data
        if email_type == "password_reset":
            context.update({"uid": "abc-def", "token": "abc-def"})
        elif email_type == "verification":
            context.update(
                {"confirmation_url": "http://www.example.com/comfirm/url"})
        elif email_type == "comments":
            context.update({
                "post":
                SimpleNamespace(
                    id="abc",
                    title="Batman Rules!",
                    slug="batman_rules",
                    channel_name="channel_name",
                    channel_title="Favorite Superheros",
                ),
                "comment":
                SimpleNamespace(id="def", text="Your post is really awesome!"),
            })
        elif email_type == "frontpage":
            context.update({
                "posts": [
                    SimpleNamespace(
                        id="abc",
                        author_name="Steve Brown",
                        author_headline="Physics Professor",
                        author_id="njksdfg",
                        title="Batman Rules!",
                        url="http://example.com/batman.jpg",
                        url_domain="example.com",
                        slug="batman_rules",
                        created="2018-09-19T18:50:32+00:00",
                        channel_name="channel_name",
                        channel_title="Favorite Superheros",
                    ),
                    SimpleNamespace(
                        id="def",
                        author_name="Casey Adams",
                        author_headline="Graduate Student",
                        author_id="ghjkl",
                        title="I, however, do not concur",
                        slug="i_however_do_not_concur",
                        created="2018-09-19T18:50:32+00:00",
                        channel_name="channel_name",
                        channel_title="Favorite Superheros",
                    ),
                ]
            })

        subject, text_body, html_body = api.render_email_templates(
            email_type, context)

        return JsonResponse({
            "subject": subject,
            "html_body": html_body,
            "text_body": text_body
        })