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

mozilla / fx-private-relay / f05d270c-c132-4897-b7f5-8b7e12c9bd29

20 Sep 2023 03:27PM CUT coverage: 74.55% (+0.02%) from 74.526%
f05d270c-c132-4897-b7f5-8b7e12c9bd29

push

circleci

groovecoder
add return type annotation to test_invalid_email_address_skips_invalid

Co-authored-by: John Whitlock <jwhitlock@mozilla.com>

1897 of 2761 branches covered (0.0%)

Branch coverage included in aggregate %.

5971 of 7793 relevant lines covered (76.62%)

18.43 hits per line

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

94.0
/emails/management/commands/send_welcome_emails.py
1
import logging
1✔
2

3
from mypy_boto3_ses.type_defs import ContentTypeDef
1✔
4

5
from botocore.exceptions import ClientError
1✔
6

7
from django.apps import apps
1✔
8
from django.conf import settings
1✔
9
from django.core.management.base import BaseCommand
1✔
10

11
from allauth.socialaccount.models import SocialAccount
1✔
12
import django_ftl
1✔
13

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

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

21

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

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

36

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

40

41
def send_welcome_email(profile: Profile, **kwargs):
1✔
42
    user = profile.user
1✔
43
    app_config = apps.get_app_config("emails")
1✔
44
    assert isinstance(app_config, EmailsConfig)
1✔
45
    ses_client = app_config.ses_client
1✔
46
    assert ses_client
1✔
47
    assert settings.RELAY_FROM_ADDRESS
1✔
48
    with django_ftl.override(profile.language):
1✔
49
        translated_subject = ftl_bundle.format("first-time-user-email-welcome")
1✔
50
    try:
1✔
51
        ses_client.send_email(
1✔
52
            Destination={
53
                "ToAddresses": [user.email],
54
            },
55
            Source=settings.RELAY_FROM_ADDRESS,
56
            Message={
57
                "Subject": _ses_message_props(translated_subject),
58
                "Body": {
59
                    "Html": _ses_message_props(get_welcome_email(user, "html")),
60
                    "Text": _ses_message_props(get_welcome_email(user, "txt")),
61
                },
62
            },
63
        )
64
        logger.info(f"Sent welcome email to user ID: {profile.user.id}")
1✔
65
        profile.sent_welcome_email = True
1✔
66
        profile.save()
1✔
67
    # Don't send welcome emails to users with no social account.
68
    # E.g., users created thru admin tools.
69
    # TODO?: move this check deeper into get_welcome_email ?
70
    except SocialAccount.DoesNotExist:
1!
71
        profile.sent_welcome_email = True
×
72
        profile.save()
×
73
    except ClientError:
1✔
74
        logger.error(
1✔
75
            f"ClientError while sending welcome email to user ID: {profile.user.id}."
76
        )
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