Skip to content

Commit 402c0e3

Browse files
authored
Fix signup issues and brand allauth emails (#2448)
* Fix signup issues and brand allauth emails This fixes a bug with the notification system on logged out users and overrides the allauth email templates. The base emails are very generic, aren't themed like the rest of our emails, and had very generic copy. * Lint fix
1 parent 4c595b4 commit 402c0e3

12 files changed

+124
-8
lines changed

readthedocs/core/adapters.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""Allauth overrides"""
2+
3+
from allauth.account.adapter import DefaultAccountAdapter
4+
from django.template.loader import render_to_string
5+
6+
from readthedocs.core.utils import send_email
7+
8+
try:
9+
from django.utils.encoding import force_text
10+
except ImportError:
11+
from django.utils.encoding import force_unicode as force_text
12+
13+
14+
class AccountAdapter(DefaultAccountAdapter):
15+
16+
"""Customize Allauth emails to match our current patterns"""
17+
18+
def format_email_subject(self, subject):
19+
return force_text(subject)
20+
21+
def send_mail(self, template_prefix, email, context):
22+
subject = render_to_string(
23+
'{0}_subject.txt'.format(template_prefix), context
24+
)
25+
subject = " ".join(subject.splitlines()).strip()
26+
subject = self.format_email_subject(subject)
27+
28+
send_email(
29+
recipient=email,
30+
subject=subject,
31+
template='{0}_message.txt'.format(template_prefix),
32+
template_html='{0}_message.html'.format(template_prefix),
33+
context=context
34+
)

readthedocs/core/tasks.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from django.conf import settings
77
from django.core.mail import EmailMultiAlternatives
88
from django.template.loader import get_template
9+
from django.template import TemplateDoesNotExist
910

1011

1112
log = logging.getLogger(__name__)
@@ -38,7 +39,10 @@ def send_email_task(recipient, subject, template, template_html, context=None):
3839
settings.DEFAULT_FROM_EMAIL,
3940
[recipient]
4041
)
41-
msg.attach_alternative(get_template(template_html).render(context),
42-
'text/html')
42+
try:
43+
msg.attach_alternative(get_template(template_html).render(context),
44+
'text/html')
45+
except TemplateDoesNotExist:
46+
pass
4347
msg.send()
4448
log.info('Sent email to recipient: %s', recipient)

readthedocs/notifications/storages.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,12 @@ def _get(self, *args, **kwargs):
4545
return safe_messages, all_ret
4646

4747
def add(self, level, message, extra_tags='', *args, **kwargs):
48-
persist_messages = (PersistentMessage.objects
49-
.filter(message=message,
50-
user=self.request.user,
51-
read=False))
52-
if persist_messages.exists():
53-
return
48+
if self.request.user.is_authenticated():
49+
persist_messages = (PersistentMessage.objects
50+
.filter(message=message,
51+
user=self.request.user,
52+
read=False))
53+
if persist_messages.exists():
54+
return
5455
super(FallbackUniqueStorage, self).add(level, message, extra_tags,
5556
*args, **kwargs)

readthedocs/settings/base.py

+1
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ def INSTALLED_APPS(self): # noqa
240240
DOCKER_IMAGE = 'readthedocs/build:14.04'
241241

242242
# All auth
243+
ACCOUNT_ADAPTER = 'readthedocs.core.adapters.AccountAdapter'
243244
ACCOUNT_EMAIL_REQUIRED = True
244245
ACCOUNT_EMAIL_VERIFICATION = "mandatory"
245246
ACCOUNT_AUTHENTICATION_METHOD = "username_email"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{% extends "core/email/common.html" %}
2+
3+
{% load i18n %}
4+
5+
{% block content %}
6+
{% blocktrans %}
7+
<p>
8+
To complete setting up your account, please verify this email address by
9+
going to:
10+
</p>
11+
12+
<p>
13+
<a href="{{ activate_url }}">{{ activate_url }}</a>
14+
</p>
15+
16+
<p>
17+
If you did not sign up for an account with Read the Docs, you can
18+
disregard this email.
19+
</p>
20+
{% endblocktrans %}
21+
{% endblock %}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{% extends "core/email/common.txt" %}
2+
3+
{% load i18n %}
4+
5+
{% block content %}{% blocktrans %}
6+
To verify your email address and finish setting up your account, please
7+
go to:
8+
9+
{{ activate_url }}
10+
11+
If you did not sign up for an account with Read the Docs, you can
12+
disregard this email.
13+
{% endblocktrans %}{% endblock %}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{% extends "account/email/email_confirmation_message.html" %}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{% extends "account/email/email_confirmation_message.txt" %}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{% load i18n %}
2+
{% autoescape off %}
3+
{% blocktrans %}Verify your e-mail address{% endblocktrans %}
4+
{% endautoescape %}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{% extends "core/email/common.html" %}
2+
3+
{% load i18n %}
4+
5+
{% block content %}
6+
{% blocktrans %}
7+
<p>
8+
A request has been made to reset your Read the Docs password. To confirm
9+
this reset request, please go to:
10+
</p>
11+
12+
<p>
13+
<a href="{{ password_reset_url }}">{{ password_reset_url }}</a>
14+
</p>
15+
16+
<p>
17+
If you did not request to reset you password, you can disregard this email.
18+
</p>
19+
{% endblocktrans %}
20+
{% endblock %}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{% extends "core/email/common.txt" %}
2+
3+
{% load i18n %}
4+
5+
{% block content %}{% blocktrans %}
6+
A request has been made to reset your Read the Docs password. To confirm
7+
this reset request, please go to:
8+
9+
{{ password_reset_url }}
10+
11+
If you did not request to reset you password, you can disregard this email.
12+
{% endblocktrans %}{% endblock %}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{% load i18n %}
2+
{% autoescape off %}
3+
{% blocktrans %}Password reset{% endblocktrans %}
4+
{% endautoescape %}

0 commit comments

Comments
 (0)