diff --git a/readthedocs/core/adapters.py b/readthedocs/core/adapters.py new file mode 100644 index 00000000000..500dbb10eb6 --- /dev/null +++ b/readthedocs/core/adapters.py @@ -0,0 +1,34 @@ +"""Allauth overrides""" + +from allauth.account.adapter import DefaultAccountAdapter +from django.template.loader import render_to_string + +from readthedocs.core.utils import send_email + +try: + from django.utils.encoding import force_text +except ImportError: + from django.utils.encoding import force_unicode as force_text + + +class AccountAdapter(DefaultAccountAdapter): + + """Customize Allauth emails to match our current patterns""" + + def format_email_subject(self, subject): + return force_text(subject) + + def send_mail(self, template_prefix, email, context): + subject = render_to_string( + '{0}_subject.txt'.format(template_prefix), context + ) + subject = " ".join(subject.splitlines()).strip() + subject = self.format_email_subject(subject) + + send_email( + recipient=email, + subject=subject, + template='{0}_message.txt'.format(template_prefix), + template_html='{0}_message.html'.format(template_prefix), + context=context + ) diff --git a/readthedocs/core/tasks.py b/readthedocs/core/tasks.py index b52cf870a0e..2c2f28b6a2c 100644 --- a/readthedocs/core/tasks.py +++ b/readthedocs/core/tasks.py @@ -6,6 +6,7 @@ from django.conf import settings from django.core.mail import EmailMultiAlternatives from django.template.loader import get_template +from django.template import TemplateDoesNotExist log = logging.getLogger(__name__) @@ -38,7 +39,10 @@ def send_email_task(recipient, subject, template, template_html, context=None): settings.DEFAULT_FROM_EMAIL, [recipient] ) - msg.attach_alternative(get_template(template_html).render(context), - 'text/html') + try: + msg.attach_alternative(get_template(template_html).render(context), + 'text/html') + except TemplateDoesNotExist: + pass msg.send() log.info('Sent email to recipient: %s', recipient) diff --git a/readthedocs/notifications/storages.py b/readthedocs/notifications/storages.py index 1038273cbc2..9b11da94c4b 100644 --- a/readthedocs/notifications/storages.py +++ b/readthedocs/notifications/storages.py @@ -45,11 +45,12 @@ def _get(self, *args, **kwargs): return safe_messages, all_ret def add(self, level, message, extra_tags='', *args, **kwargs): - persist_messages = (PersistentMessage.objects - .filter(message=message, - user=self.request.user, - read=False)) - if persist_messages.exists(): - return + if self.request.user.is_authenticated(): + persist_messages = (PersistentMessage.objects + .filter(message=message, + user=self.request.user, + read=False)) + if persist_messages.exists(): + return super(FallbackUniqueStorage, self).add(level, message, extra_tags, *args, **kwargs) diff --git a/readthedocs/settings/base.py b/readthedocs/settings/base.py index 1b1cc4b793f..2848fe8fbcd 100644 --- a/readthedocs/settings/base.py +++ b/readthedocs/settings/base.py @@ -240,6 +240,7 @@ def INSTALLED_APPS(self): # noqa DOCKER_IMAGE = 'readthedocs/build:14.04' # All auth + ACCOUNT_ADAPTER = 'readthedocs.core.adapters.AccountAdapter' ACCOUNT_EMAIL_REQUIRED = True ACCOUNT_EMAIL_VERIFICATION = "mandatory" ACCOUNT_AUTHENTICATION_METHOD = "username_email" diff --git a/readthedocs/templates/account/email/email_confirmation_message.html b/readthedocs/templates/account/email/email_confirmation_message.html new file mode 100644 index 00000000000..2b539356957 --- /dev/null +++ b/readthedocs/templates/account/email/email_confirmation_message.html @@ -0,0 +1,21 @@ +{% extends "core/email/common.html" %} + +{% load i18n %} + +{% block content %} + {% blocktrans %} +
+ To complete setting up your account, please verify this email address by + going to: +
+ + + ++ If you did not sign up for an account with Read the Docs, you can + disregard this email. +
+ {% endblocktrans %} +{% endblock %} diff --git a/readthedocs/templates/account/email/email_confirmation_message.txt b/readthedocs/templates/account/email/email_confirmation_message.txt new file mode 100644 index 00000000000..d88d010f577 --- /dev/null +++ b/readthedocs/templates/account/email/email_confirmation_message.txt @@ -0,0 +1,13 @@ +{% extends "core/email/common.txt" %} + +{% load i18n %} + +{% block content %}{% blocktrans %} +To verify your email address and finish setting up your account, please +go to: + +{{ activate_url }} + +If you did not sign up for an account with Read the Docs, you can +disregard this email. +{% endblocktrans %}{% endblock %} diff --git a/readthedocs/templates/account/email/email_confirmation_signup_message.html b/readthedocs/templates/account/email/email_confirmation_signup_message.html new file mode 100644 index 00000000000..c6f10e37dc9 --- /dev/null +++ b/readthedocs/templates/account/email/email_confirmation_signup_message.html @@ -0,0 +1 @@ +{% extends "account/email/email_confirmation_message.html" %} diff --git a/readthedocs/templates/account/email/email_confirmation_signup_message.txt b/readthedocs/templates/account/email/email_confirmation_signup_message.txt new file mode 100644 index 00000000000..ea5141a62f6 --- /dev/null +++ b/readthedocs/templates/account/email/email_confirmation_signup_message.txt @@ -0,0 +1 @@ +{% extends "account/email/email_confirmation_message.txt" %} diff --git a/readthedocs/templates/account/email/email_confirmation_subject.txt b/readthedocs/templates/account/email/email_confirmation_subject.txt new file mode 100644 index 00000000000..374afb7c271 --- /dev/null +++ b/readthedocs/templates/account/email/email_confirmation_subject.txt @@ -0,0 +1,4 @@ +{% load i18n %} +{% autoescape off %} +{% blocktrans %}Verify your e-mail address{% endblocktrans %} +{% endautoescape %} diff --git a/readthedocs/templates/account/email/password_key_reset_message.html b/readthedocs/templates/account/email/password_key_reset_message.html new file mode 100644 index 00000000000..31bc1313061 --- /dev/null +++ b/readthedocs/templates/account/email/password_key_reset_message.html @@ -0,0 +1,20 @@ +{% extends "core/email/common.html" %} + +{% load i18n %} + +{% block content %} + {% blocktrans %} ++ A request has been made to reset your Read the Docs password. To confirm + this reset request, please go to: +
+ + + ++ If you did not request to reset you password, you can disregard this email. +
+ {% endblocktrans %} +{% endblock %} diff --git a/readthedocs/templates/account/email/password_key_reset_message.txt b/readthedocs/templates/account/email/password_key_reset_message.txt new file mode 100644 index 00000000000..b63c69bb6e4 --- /dev/null +++ b/readthedocs/templates/account/email/password_key_reset_message.txt @@ -0,0 +1,12 @@ +{% extends "core/email/common.txt" %} + +{% load i18n %} + +{% block content %}{% blocktrans %} +A request has been made to reset your Read the Docs password. To confirm +this reset request, please go to: + +{{ password_reset_url }} + +If you did not request to reset you password, you can disregard this email. +{% endblocktrans %}{% endblock %} diff --git a/readthedocs/templates/account/email/password_key_reset_subject.txt b/readthedocs/templates/account/email/password_key_reset_subject.txt new file mode 100644 index 00000000000..deeb0e86727 --- /dev/null +++ b/readthedocs/templates/account/email/password_key_reset_subject.txt @@ -0,0 +1,4 @@ +{% load i18n %} +{% autoescape off %} +{% blocktrans %}Password reset{% endblocktrans %} +{% endautoescape %}