• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mozilla / fx-private-relay / d3128616-238d-446e-82c5-ab66cd38ceaf

09 May 2024 06:22PM CUT coverage: 84.07% (-0.6%) from 84.64%
d3128616-238d-446e-82c5-ab66cd38ceaf

push

circleci

web-flow
Merge pull request #4684 from mozilla/enable-flak8-bandit-checks-mpp-3802

fix MPP-3802: stop ignoring bandit security checks

3601 of 4734 branches covered (76.07%)

Branch coverage included in aggregate %.

74 of 158 new or added lines in 24 files covered. (46.84%)

5 existing lines in 5 files now uncovered.

14686 of 17018 relevant lines covered (86.3%)

10.86 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.33
/emails/management/commands/send_welcome_emails.py
1
import logging
1✔
2
from typing import Any
1✔
3

4
from django.apps import apps
1✔
5
from django.conf import settings
1✔
6
from django.core.management.base import BaseCommand
1✔
7

8
import django_ftl
1✔
9
from allauth.socialaccount.models import SocialAccount
1✔
10
from botocore.exceptions import ClientError
1✔
11
from mypy_boto3_ses.type_defs import ContentTypeDef
1✔
12

13
from emails.apps import EmailsConfig
1✔
14
from emails.models import Profile
1✔
15
from emails.utils import get_welcome_email, ses_message_props
1✔
16
from privaterelay.ftl_bundles import main as ftl_bundle
1✔
17

18
logger = logging.getLogger("eventsinfo.send_welcome_emails")
1✔
19

20

21
class Command(BaseCommand):
1✔
22
    help = "Send the welcome email to all users who haven't received it yet."
1✔
23

24
    def handle(self, *args: Any, **kwargs: Any) -> None:
1✔
25
        logger.info("Starting send_welcome_emails")
1✔
26
        profiles_without_welcome_email = Profile.objects.filter(
1✔
27
            sent_welcome_email=False
28
        ).order_by("user_id")
29
        emails_to_send = len(profiles_without_welcome_email)
1✔
30
        logger.info(f"Emails to send: {emails_to_send}")
1✔
31
        for profile in profiles_without_welcome_email:
1✔
32
            send_welcome_email(profile)
1✔
33
        logger.info("Exiting send_welcome_emails")
1✔
34

35

36
def _ses_message_props(data: str) -> ContentTypeDef:
1✔
37
    return {"Charset": "UTF-8", "Data": data}
×
38

39

40
def send_welcome_email(profile: Profile) -> None:
1✔
41
    user = profile.user
1✔
42
    app_config = apps.get_app_config("emails")
1✔
43
    if not isinstance(app_config, EmailsConfig):
1!
NEW
44
        raise TypeError("app_config must be type EmailsConfig")
×
45
    ses_client = app_config.ses_client
1✔
46
    if not ses_client:
1!
NEW
47
        raise ValueError("ses_client must be truthy value")
×
48
    if not settings.RELAY_FROM_ADDRESS:
1!
NEW
49
        raise ValueError("settings.RELAY_FROM_ADDRESS must be truthy value.")
×
50
    with django_ftl.override(profile.language):
1✔
51
        translated_subject = ftl_bundle.format("first-time-user-email-welcome")
1✔
52
    try:
1✔
53
        ses_client.send_email(
1✔
54
            Destination={
55
                "ToAddresses": [user.email],
56
            },
57
            Source=settings.RELAY_FROM_ADDRESS,
58
            Message={
59
                "Subject": ses_message_props(translated_subject),
60
                "Body": {
61
                    "Html": ses_message_props(get_welcome_email(user, "html")),
62
                    "Text": ses_message_props(get_welcome_email(user, "txt")),
63
                },
64
            },
65
        )
66
        logger.info(f"Sent welcome email to user ID: {profile.user.id}")
1✔
67
        profile.sent_welcome_email = True
1✔
68
        profile.save()
1✔
69
    # Don't send welcome emails to users with no social account.
70
    # E.g., users created thru admin tools.
71
    # TODO?: move this check deeper into get_welcome_email ?
72
    except SocialAccount.DoesNotExist:
1!
73
        profile.sent_welcome_email = True
×
74
        profile.save()
×
75
    except ClientError:
1✔
76
        logger.error(
1✔
77
            f"ClientError while sending welcome email to user ID: {profile.user.id}."
78
        )
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc