From 70f86c792b9c5c3ca39c4e2d9a9c7c8bedc79127 Mon Sep 17 00:00:00 2001 From: Kuroshini Date: Wed, 16 Oct 2019 14:11:07 +0300 Subject: [PATCH] New email templates --- apps/account/models.py | 52 +++++++----- apps/account/tasks.py | 2 +- apps/main/models.py | 3 + project/templates/account/change_email.html | 83 +++++++++++++++++-- .../account/password_change_email.html | 76 ++++++++++++++++- .../account/password_reset_email.html | 83 +++++++++++++++++-- 6 files changed, 260 insertions(+), 39 deletions(-) diff --git a/apps/account/models.py b/apps/account/models.py index 5d5005e8..e7fae828 100644 --- a/apps/account/models.py +++ b/apps/account/models.py @@ -1,10 +1,12 @@ """Account models""" +from datetime import datetime + from django.conf import settings from django.contrib.auth.models import AbstractUser, UserManager as BaseUserManager from django.contrib.auth.tokens import default_token_generator as password_token_generator from django.core.mail import send_mail from django.db import models -from django.template.loader import render_to_string +from django.template.loader import render_to_string, get_template from django.utils import timezone from django.utils.encoding import force_bytes from django.utils.html import mark_safe @@ -15,6 +17,7 @@ from rest_framework.authtoken.models import Token from authorization.models import Application from establishment.models import Establishment from location.models import Country +from main.models import SiteSettings from utils.models import GMTokenGenerator from utils.models import ImageMixin, ProjectBaseMixin, PlatformMixin from utils.tokens import GMRefreshToken @@ -162,9 +165,10 @@ class User(AbstractUser): """Prepare the body of the email message""" return { 'subject': subject, - 'message': str(message), + 'message': str(message[0]), + 'html_message': message[1], 'from_email': settings.EMAIL_HOST_USER, - 'recipient_list': emails if emails else [self.email, ] + 'recipient_list': emails if emails else [self.email, ], } def send_email(self, subject: str, message: str, emails=None): @@ -193,12 +197,20 @@ class User(AbstractUser): """Get base64 value for user by primary key identifier""" return urlsafe_base64_encode(force_bytes(self.pk)) - @property - def base_template(self): + def base_template(self, country_code='www', username='', subject=''): """Base email template""" - return {'domain_uri': settings.DOMAIN_URI, - 'uidb64': self.get_user_uidb64, - 'site_name': settings.SITE_NAME} + socials = SiteSettings.objects.by_country_code(country_code).first() + return { + 'title': subject, + 'domain_uri': settings.DOMAIN_URI, + 'uidb64': self.get_user_uidb64, + 'site_name': settings.SITE_NAME, + 'year': datetime.now().year, + 'twitter_page_url': socials.twitter_page_url if socials else '#', + 'instagram_page_url': socials.instagram_page_url if socials else '#', + 'facebook_page_url': socials.facebook_page_url if socials else '#', + 'send_to': username, + } @property def image_tag(self): @@ -208,41 +220,41 @@ class User(AbstractUser): def cropped_image_tag(self): return mark_safe(f'') - def reset_password_template(self, country_code): + def reset_password_template(self, country_code, username): """Get reset password template""" context = {'token': self.reset_password_token, 'country_code': country_code} - context.update(self.base_template) + context.update(self.base_template(country_code, username)) return render_to_string( template_name=settings.RESETTING_TOKEN_TEMPLATE, - context=context) + context=context), get_template(settings.RESETTING_TOKEN_TEMPLATE).render(context) - def notify_password_changed_template(self, country_code): + def notify_password_changed_template(self, country_code, username, subject): """Get notification email template""" context = {'contry_code': country_code} - context.update(self.base_template) + context.update(self.base_template(country_code, username, subject)) return render_to_string( template_name=settings.NOTIFICATION_PASSWORD_TEMPLATE, context=context, - ) + ), get_template(settings.NOTIFICATION_PASSWORD_TEMPLATE).render(context) - def confirm_email_template(self, country_code): + def confirm_email_template(self, country_code, username, subject): """Get confirm email template""" context = {'token': self.confirm_email_token, 'country_code': country_code} - context.update(self.base_template) + context.update(self.base_template(country_code, username, subject)) return render_to_string( template_name=settings.CONFIRM_EMAIL_TEMPLATE, - context=context) + context=context), get_template(settings.CONFIRM_EMAIL_TEMPLATE).render(context) - def change_email_template(self, country_code): + def change_email_template(self, country_code, username, subject): """Get change email template""" context = {'token': self.change_email_token, 'country_code': country_code} - context.update(self.base_template) + context.update(self.base_template(country_code, username, subject)) return render_to_string( template_name=settings.CHANGE_EMAIL_TEMPLATE, - context=context) + context=context), get_template(settings.CHANGE_EMAIL_TEMPLATE).render(context) @property def favorite_establishment_ids(self): diff --git a/apps/account/tasks.py b/apps/account/tasks.py index 21b22320..8afe1545 100644 --- a/apps/account/tasks.py +++ b/apps/account/tasks.py @@ -14,7 +14,7 @@ def send_email(user_id: int, subject: str, message_prop: str, country_code: str, try: user = User.objects.get(id=user_id) user.send_email(subject=_(subject), - message=getattr(user, message_prop, lambda _: '')(country_code), + message=getattr(user, message_prop, lambda _: '')(country_code, user.username, _(subject)), emails=emails) except: cur_frame = inspect.currentframe() diff --git a/apps/main/models.py b/apps/main/models.py index 97fe53c1..048aac3b 100644 --- a/apps/main/models.py +++ b/apps/main/models.py @@ -42,6 +42,9 @@ class SiteSettingsQuerySet(models.QuerySet): def with_country(self): return self.filter(country__isnull=False) + def by_country_code(self, code): + return self.filter(country__code=code) + class SiteSettings(ProjectBaseMixin): subdomain = models.CharField(max_length=255, db_index=True, unique=True, diff --git a/project/templates/account/change_email.html b/project/templates/account/change_email.html index 6b74d970..efe92766 100644 --- a/project/templates/account/change_email.html +++ b/project/templates/account/change_email.html @@ -1,11 +1,80 @@ {% load i18n %}{% autoescape off %} -{% blocktrans %}You're receiving this email because you want to change email address at {{ site_name }}.{% endblocktrans %} + + + + + + + + + + +
-{% trans "Please go to the following page for confirmation new email address:" %} +
-https://{{ country_code }}.{{ domain_uri }}/change-email-confirm/{{ uidb64 }}/{{ token }}/ - -{% trans "Thanks for using our site!" %} - -{% blocktrans %}The {{ site_name }} team{% endblocktrans %} +
+
+
+
+
+ + +
+
+
+ {{ title }} +
+
+ {% blocktrans %}You're receiving this email because you want to change email address at {{ site_name }}.{% endblocktrans %} +
+ {% trans "Please go to the following page for confirmation new email address:" %} +
+ https://{{ country_code }}.{{ domain_uri }}/change-email-confirm/{{ uidb64 }}/{{ token }}/ +
+ {% trans "Thanks for using our site!" %} +
+
+ {% blocktrans %}The {{ site_name }} team{% endblocktrans %} +
+ +
+ This email has been sent to {{ send_to }} , +
+ +
+
+
+ + {% endautoescape %} \ No newline at end of file diff --git a/project/templates/account/password_change_email.html b/project/templates/account/password_change_email.html index 30dd2aac..77cad83f 100644 --- a/project/templates/account/password_change_email.html +++ b/project/templates/account/password_change_email.html @@ -1,7 +1,77 @@ {% load i18n %}{% autoescape off %} -{% blocktrans %}You're receiving this email because your account's password address at {{ site_name }}.{% endblocktrans %} + + + + + + + + + + +
-{% trans "Thanks for using our site!" %} +
-{% blocktrans %}The {{ site_name }} team{% endblocktrans %} +
+
+
+
+
+ + +
+
+
+ {{ title }} +
+
+
+ {% blocktrans %}You're receiving this email because your account's password address at {{ site_name }}.{% endblocktrans %} +
+ {% trans "Thanks for using our site!" %} +
+
+ {% blocktrans %}The {{ site_name }} team{% endblocktrans %} +
+ +
+ This email has been sent to {{ send_to }} , +
+ +
+
+
+ + {% endautoescape %} \ No newline at end of file diff --git a/project/templates/account/password_reset_email.html b/project/templates/account/password_reset_email.html index 9fb3fbf3..52bbbd2b 100644 --- a/project/templates/account/password_reset_email.html +++ b/project/templates/account/password_reset_email.html @@ -1,11 +1,78 @@ -{% load i18n %}{% autoescape off %} -{% blocktrans %}You're receiving this email because you requested a password reset for your user account at {{ site_name }}.{% endblocktrans %} + + + + + + + + + + +
-{% trans "Please go to the following page and choose a new password:" %} +
-https://{{ country_code }}.{{ domain_uri }}/recovery/{{ uidb64 }}/{{ token }}/ - -{% trans "Thanks for using our site!" %} - -{% blocktrans %}The {{ site_name }} team{% endblocktrans %} +
+
+
+
+
+ + +
+
+
+ {{ title }} +
+
+ {% blocktrans %}You're receiving this email because you requested a password reset for your user account at {{ site_name }}.{% endblocktrans %} +
+ {% trans "Please go to the following page and choose a new password:" %} +
+ https://{{ country_code }}.{{ domain_uri }}/recovery/{{ uidb64 }}/{{ token }}/ +
+ {% trans "Thanks for using our site!" %} +

+ {% blocktrans %}The {{ site_name }} team{% endblocktrans %} +
+ +
+ This email has been sent to {{ send_to }} , +
+ +
+
+
+ + {% endautoescape %} \ No newline at end of file