From 469a246f49da7bb4815834d8cae6642bea94e32e Mon Sep 17 00:00:00 2001 From: dormantman Date: Mon, 13 Jan 2020 13:36:49 +0300 Subject: [PATCH] Added subscribes notification --- apps/notification/models.py | 8 ++ apps/notification/tasks.py | 88 +++++++++++++++++++ project/settings/base.py | 1 + .../templates/notification/update_email.html | 75 ++++++++++++++++ 4 files changed, 172 insertions(+) create mode 100644 apps/notification/tasks.py create mode 100644 project/templates/notification/update_email.html diff --git a/apps/notification/models.py b/apps/notification/models.py index ef3ca70b..6dbf0230 100644 --- a/apps/notification/models.py +++ b/apps/notification/models.py @@ -9,6 +9,7 @@ from account.models import User from location.models import Country from utils.methods import generate_string_code from utils.models import ProjectBaseMixin, TJSONField, TranslatedFieldsMixin +from notification.tasks import send_subscribes_update_email, send_unsubscribe_email class SubscriptionType(ProjectBaseMixin, TranslatedFieldsMixin): @@ -100,6 +101,8 @@ class Subscriber(ProjectBaseMixin): objects = SubscriberManager() + # todo: Add sending subscribes with update email + class Meta: """Meta class.""" @@ -116,6 +119,11 @@ class Subscriber(ProjectBaseMixin): """Unsubscribe user.""" self.subscribe_set.update(unsubscribe_date=now()) + if settings.USE_CELERY: + send_unsubscribe_email.delay(self.pk) + else: + send_unsubscribe_email(self.pk) + @property def send_to(self): """Actual email.""" diff --git a/apps/notification/tasks.py b/apps/notification/tasks.py new file mode 100644 index 00000000..4a0ff5f3 --- /dev/null +++ b/apps/notification/tasks.py @@ -0,0 +1,88 @@ +from datetime import datetime + +from celery import shared_task +from django.conf import settings +from django.core.mail import send_mail +from django.template.loader import get_template, render_to_string + +from main.models import SiteSettings +from notification.models import Subscriber + + +@shared_task +def send_subscribes_update_email(subscriber_id): + subscriber = Subscriber.objects.get(subscriber_id) + + country_code = subscriber.country_code + + html_template = get_template(settings.NEWS_EMAIL_TEMPLATE) + year = datetime.now().year + + socials = list(SiteSettings.objects.with_country().select_related('country')) + socials = dict(zip(map(lambda social: social.country.code, socials), socials)) + + socials_for_subscriber = socials.get(country_code) + + context = { + "title": "G&M Subscriptions", + "subtitle": "You have subscribed on news G&M", + "description": "\n".join([ + name.get(subscriber.locale) + for name in subscriber.subscription_types.values_list('name', flat=True) + ]), + "code": subscriber.update_code, + "domain_uri": settings.DOMAIN_URI, + "country_code": subscriber.country_code, + "twitter_page_url": socials_for_subscriber.twitter_page_url if socials_for_subscriber else '#', + "instagram_page_url": socials_for_subscriber.instagram_page_url if socials_for_subscriber else '#', + "facebook_page_url": socials_for_subscriber.facebook_page_url if socials_for_subscriber else '#', + "send_to": subscriber.send_to, + "year": year + } + + send_mail( + subject="G&M Subscriptions", + message=render_to_string(settings.NOTIFICATION_SUBSCRIBE_TEMPLATE, context), + from_email=settings.EMAIL_HOST_USER, + recipient_list=[subscriber.send_to], + fail_silently=False, + html_message=html_template.render(context) + ) + + +@shared_task +def send_unsubscribe_email(subscriber_id): + subscriber = Subscriber.objects.get(subscriber_id) + + country_code = subscriber.country_code + + html_template = get_template(settings.NEWS_EMAIL_TEMPLATE) + year = datetime.now().year + + socials = list(SiteSettings.objects.with_country().select_related('country')) + socials = dict(zip(map(lambda social: social.country.code, socials), socials)) + + socials_for_subscriber = socials.get(country_code) + + context = { + "title": "G&M Subscriptions", + "subtitle": "You have successfully unsubscribed from G&M news", + "description": "", + "code": subscriber.update_code, + "domain_uri": settings.DOMAIN_URI, + "country_code": subscriber.country_code, + "twitter_page_url": socials_for_subscriber.twitter_page_url if socials_for_subscriber else '#', + "instagram_page_url": socials_for_subscriber.instagram_page_url if socials_for_subscriber else '#', + "facebook_page_url": socials_for_subscriber.facebook_page_url if socials_for_subscriber else '#', + "send_to": subscriber.send_to, + "year": year + } + + send_mail( + subject="G&M Subscriptions", + message=render_to_string(settings.NOTIFICATION_SUBSCRIBE_TEMPLATE, context), + from_email=settings.EMAIL_HOST_USER, + recipient_list=[subscriber.send_to], + fail_silently=False, + html_message=html_template.render(context) + ) diff --git a/project/settings/base.py b/project/settings/base.py index f34638aa..08a36609 100644 --- a/project/settings/base.py +++ b/project/settings/base.py @@ -454,6 +454,7 @@ CHANGE_EMAIL_TEMPLATE = 'account/change_email.html' CONFIRM_EMAIL_TEMPLATE = 'authorization/confirm_email.html' NEWS_EMAIL_TEMPLATE = 'news/news_email.html' NOTIFICATION_PASSWORD_TEMPLATE = 'account/password_change_email.html' +NOTIFICATION_SUBSCRIBE_TEMPLATE = 'notification/update_email.html' # COOKIES diff --git a/project/templates/notification/update_email.html b/project/templates/notification/update_email.html new file mode 100644 index 00000000..dad6e9b8 --- /dev/null +++ b/project/templates/notification/update_email.html @@ -0,0 +1,75 @@ + + + + + + + + {{ title }} + + +
+ +
+ +
+
+
+
+
+ + +
+
+ {{ title }} +
+ {% if not image_url is None %} +
+ +
+ {% endif %} +
+ {{ description | safe }} +
+
+ +
+ This email has been sent to {{ send_to }} , + click here to unsubscribe +
+ +
+
+
+ + \ No newline at end of file