diff --git a/apps/account/models.py b/apps/account/models.py index 3eb07722..e06727f2 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 @@ -158,19 +161,21 @@ class User(AbstractUser): self.is_active = True self.save() - def get_body_email_message(self, subject: str, message: str): + def get_body_email_message(self, subject: str, message: str, emails=None): """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': [self.email, ] + 'recipient_list': emails if emails else [self.email, ], } - def send_email(self, subject: str, message: str): + def send_email(self, subject: str, message: str, emails=None): """Send an email to reset user password""" send_mail(**self.get_body_email_message(subject=subject, - message=message)) + message=message, + emails=emails)) @property def confirm_email_token(self): @@ -192,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): @@ -207,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, subject): """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, subject)) 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/serializers/common.py b/apps/account/serializers/common.py index d68cfe56..20016297 100644 --- a/apps/account/serializers/common.py +++ b/apps/account/serializers/common.py @@ -72,11 +72,13 @@ class UserSerializer(serializers.ModelSerializer): if settings.USE_CELERY: tasks.change_email_address.delay( user_id=instance.id, - country_code=self.context.get('request').country_code) + country_code=self.context.get('request').country_code, + emails=[validated_data['email'],]) else: tasks.change_email_address( user_id=instance.id, - country_code=self.context.get('request').country_code) + country_code=self.context.get('request').country_code, + emails=[validated_data['email'],]) return instance diff --git a/apps/account/tasks.py b/apps/account/tasks.py index d9fa7bb7..8afe1545 100644 --- a/apps/account/tasks.py +++ b/apps/account/tasks.py @@ -10,11 +10,12 @@ from account.models import User logging.basicConfig(format='[%(levelname)s] %(message)s', level=logging.INFO) logger = logging.getLogger(__name__) -def send_email(user_id: int, subject: str, message_prop: str, country_code: str): +def send_email(user_id: int, subject: str, message_prop: str, country_code: str, emails=None): 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() cal_frame = inspect.getouterframes(cur_frame, 2) @@ -35,9 +36,9 @@ def confirm_new_email_address(user_id, country_code): @shared_task -def change_email_address(user_id, country_code): +def change_email_address(user_id, country_code, emails=None): """Send email to user new email.""" - send_email(user_id, 'Validate new email address', 'change_email_template', country_code) + send_email(user_id, 'Validate new email address', 'change_email_template', country_code, emails) @shared_task diff --git a/apps/authorization/tasks.py b/apps/authorization/tasks.py index cb186142..d44c2b5c 100644 --- a/apps/authorization/tasks.py +++ b/apps/authorization/tasks.py @@ -16,7 +16,7 @@ def send_confirm_email(user_id: int, country_code: str): try: obj = account_models.User.objects.get(id=user_id) obj.send_email(subject=_('Email confirmation'), - message=obj.confirm_email_template(country_code)) + message=obj.confirm_email_template(country_code, obj.username, _('Email confirmation'))) except Exception as e: logger.error(f'METHOD_NAME: {send_confirm_email.__name__}\n' f'DETAIL: user {user_id}, - {e}') 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..4d61147d 100644 --- a/project/templates/account/password_reset_email.html +++ b/project/templates/account/password_reset_email.html @@ -1,11 +1,79 @@ {% 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 diff --git a/project/templates/authorization/confirm_email.html b/project/templates/authorization/confirm_email.html index 3e51add7..c05c85b0 100644 --- a/project/templates/authorization/confirm_email.html +++ b/project/templates/authorization/confirm_email.html @@ -1,10 +1,79 @@ {% load i18n %}{% autoescape off %} -{% blocktrans %}You're receiving this email because you trying to register new account at {{ site_name }}.{% endblocktrans %} + + + + + + + + + + +
-{% trans "Please confirm your email address to complete the registration:" %} -https://{{ country_code }}.{{ domain_uri }}/registered/{{ uidb64 }}/{{ token }}/ +
-{% trans "Thanks for using our site!" %} - -{% blocktrans %}The {{ site_name }} team{% endblocktrans %} +
+
+
+
+
+ + +
+
+
+ {{ title }} +
+
+
+ {% blocktrans %}You're receiving this email because you trying to register new account at {{ site_name }}.{% endblocktrans %} +
+ {% trans "Please confirm your email address to complete the registration:" %} + https://{{ country_code }}.{{ domain_uri }}/registered/{{ 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/news/news_email.html b/project/templates/news/news_email.html index 0227b9de..c2ae227c 100644 --- a/project/templates/news/news_email.html +++ b/project/templates/news/news_email.html @@ -19,7 +19,7 @@
@@ -35,7 +35,7 @@
{{ description | safe }}
- +
Go to news
@@ -44,20 +44,20 @@
@@ -69,9 +69,9 @@