-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathemails.py
More file actions
47 lines (36 loc) · 1.61 KB
/
emails.py
File metadata and controls
47 lines (36 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import logging
import random
from apps.constants import HHS_SERVER_LOGNAME_FMT
from django.conf import settings
from django.urls import reverse
from libs.decorators import waffle_function_switch
from libs.mail import Mailer
logger = logging.getLogger(HHS_SERVER_LOGNAME_FMT.format(__name__))
def random_secret(y=40):
return ''.join(random.choice('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789') for x in range(y))
@waffle_function_switch('outreach_email')
def send_activation_key_via_email(user, signup_key):
"""Send an email with activation key and welcome message."""
activation_link = '%s%s' % (get_hostname(), reverse('activation_verify', args=(signup_key,)))
mailer = Mailer(
subject='Verify Your Blue Button 2.0 Developer Sandbox Account',
template_text='email/email-activate.txt',
template_html='email/email-activate.html',
to=[
user.email,
],
context={'ACTIVATION_LINK': activation_link, 'ACTIVATION_KEY': signup_key, 'EXPIRATION': settings.SIGNUP_TIMEOUT_DAYS},
)
mailer.send()
logger.info('Activation link sent to {} ({})'.format(user.username, user.email))
def get_hostname():
hostname = getattr(settings, 'HOSTNAME_URL', 'http://localhost:8000')
if 'http://' in hostname.lower():
pass
elif 'https://' in hostname.lower():
pass
else:
logger.debug('HOSTNAME_URL [%s] does not contain http or https prefix. Issuer:%s' % (settings.HOSTNAME_URL, hostname))
# no http/https prefix in HOST_NAME_URL so we add it
hostname = 'https://%s' % (hostname)
return hostname