From 90b8dafba9bedce2ec21d20338e58bed07d9db72 Mon Sep 17 00:00:00 2001 From: Kuroshini Date: Fri, 4 Oct 2019 20:24:48 +0300 Subject: [PATCH 1/4] News email #1 --- apps/news/tasks.py | 28 ++++++---- project/templates/news/news_email.html | 77 +++++++++++++++++++++++--- 2 files changed, 86 insertions(+), 19 deletions(-) diff --git a/apps/news/tasks.py b/apps/news/tasks.py index 99065fc8..beed2060 100644 --- a/apps/news/tasks.py +++ b/apps/news/tasks.py @@ -1,3 +1,5 @@ +from datetime import datetime + from celery import shared_task from django.core.mail import send_mail from notification.models import Subscriber @@ -5,24 +7,30 @@ from news import models from django.template.loader import render_to_string from django.conf import settings from smtplib import SMTPException +from django.template.loader import get_template @shared_task def send_email_with_news(news_ids): - subscribers = Subscriber.objects.filter(state=Subscriber.USABLE) sent_news = models.News.objects.filter(id__in=news_ids) - + htmly = get_template(settings.NEWS_EMAIL_TEMPLATE) + year = datetime.now().year for s in subscribers: try: for n in sent_news: - send_mail("G&M News", render_to_string(settings.NEWS_EMAIL_TEMPLATE, - {"title": n.title.get(s.locale), - "subtitle": n.subtitle.get(s.locale), - "description": n.description.get(s.locale), - "code": s.update_code, - "domain_uri": settings.DOMAIN_URI, - "country_code": s.country_code}), - settings.EMAIL_HOST_USER, [s.send_to], fail_silently=False) + context = {"title": n.title.get(s.locale), + "subtitle": n.subtitle.get(s.locale), + "description": n.description.get(s.locale), + "code": s.update_code, + "image_url": n.image_url, + "domain_uri": settings.DOMAIN_URI, + "slug": n.slug, + "country_code": s.country_code, + "send_to": s.send_to, + "year": year} + send_mail("G&M News", render_to_string(settings.NEWS_EMAIL_TEMPLATE, context), + settings.EMAIL_HOST_USER, [s.send_to], fail_silently=False, + html_message=htmly.render(context)) except SMTPException: continue diff --git a/project/templates/news/news_email.html b/project/templates/news/news_email.html index a47af685..dd4dea5b 100644 --- a/project/templates/news/news_email.html +++ b/project/templates/news/news_email.html @@ -1,20 +1,79 @@ - + + + + {{ title }} - -

{{ title }}

+ +
- {% if subtitle %} -

{{ subtitle }}

- {% endif %} +
-

{{ description }}

-https://{{ country_code }}.{{ domain_uri }}{% url 'web:notification:unsubscribe' code %} +
+
+
+
+
+ + +
+
+ {{ title }} +
+ {% if not image_url is None %} +
+ +
+ {% endif %} +
+ {{ description | safe }} +
+ + + +
+ +
+ This email has been sent to {{ send_to }} , + click here to unsubscribe +
+ +
+
+
- From 73646eb05f5ef1754ccb5f2d73e1048238234edc Mon Sep 17 00:00:00 2001 From: Kuroshini Date: Sat, 5 Oct 2019 18:08:58 +0300 Subject: [PATCH 2/4] News email #2 --- apps/news/tasks.py | 10 +++++++++- project/templates/news/news_email.html | 18 +++++++++--------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/apps/news/tasks.py b/apps/news/tasks.py index beed2060..c2b63b48 100644 --- a/apps/news/tasks.py +++ b/apps/news/tasks.py @@ -8,6 +8,8 @@ from django.template.loader import render_to_string from django.conf import settings from smtplib import SMTPException from django.template.loader import get_template +from django.core.validators import EMPTY_VALUES +from main.models import SiteSettings @shared_task @@ -16,17 +18,23 @@ def send_email_with_news(news_ids): sent_news = models.News.objects.filter(id__in=news_ids) htmly = get_template(settings.NEWS_EMAIL_TEMPLATE) year = datetime.now().year + socials = list(SiteSettings.objects.with_country()) + socials = dict(zip(map(lambda s: s.country.code, socials), socials)) for s in subscribers: + socials_for_subscriber = socials.get(s.country_code) try: for n in sent_news: context = {"title": n.title.get(s.locale), "subtitle": n.subtitle.get(s.locale), "description": n.description.get(s.locale), "code": s.update_code, - "image_url": n.image_url, + "image_url": n.image_url if n.image_url not in EMPTY_VALUES else None, "domain_uri": settings.DOMAIN_URI, "slug": n.slug, "country_code": s.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": s.send_to, "year": year} send_mail("G&M News", render_to_string(settings.NEWS_EMAIL_TEMPLATE, context), diff --git a/project/templates/news/news_email.html b/project/templates/news/news_email.html index dd4dea5b..33ebe29c 100644 --- a/project/templates/news/news_email.html +++ b/project/templates/news/news_email.html @@ -33,30 +33,30 @@ {% endif %} -
+
{{ description | safe }}
- +
{% if not image_url is None %} -
+
{% endif %} From b57303898082028e32346b20669dc5d958db0930 Mon Sep 17 00:00:00 2001 From: "e.stoyushko" Date: Wed, 9 Oct 2019 09:46:23 +0000 Subject: [PATCH 4/4] Update tasks.py --- apps/news/tasks.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/apps/news/tasks.py b/apps/news/tasks.py index c2b63b48..7ff4d504 100644 --- a/apps/news/tasks.py +++ b/apps/news/tasks.py @@ -4,10 +4,9 @@ from celery import shared_task from django.core.mail import send_mail from notification.models import Subscriber from news import models -from django.template.loader import render_to_string +from django.template.loader import render_to_string, get_template from django.conf import settings from smtplib import SMTPException -from django.template.loader import get_template from django.core.validators import EMPTY_VALUES from main.models import SiteSettings