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

mozilla / fx-private-relay / b2e067fe-ce4e-4099-9bef-07b368e99782

15 Apr 2024 04:18PM CUT coverage: 75.544% (+0.002%) from 75.542%
b2e067fe-ce4e-4099-9bef-07b368e99782

push

circleci

jwhitlock
Enable pyupgrade, fix issues

2443 of 3405 branches covered (71.75%)

Branch coverage included in aggregate %.

56 of 59 new or added lines in 14 files covered. (94.92%)

234 existing lines in 24 files now uncovered.

6793 of 8821 relevant lines covered (77.01%)

20.04 hits per line

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

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

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

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

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

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

19

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

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

34

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

38

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