News email #1

This commit is contained in:
Kuroshini 2019-10-04 20:24:48 +03:00
parent cc51e36457
commit 90b8dafba9
2 changed files with 86 additions and 19 deletions

View File

@ -1,3 +1,5 @@
from datetime import datetime
from celery import shared_task from celery import shared_task
from django.core.mail import send_mail from django.core.mail import send_mail
from notification.models import Subscriber from notification.models import Subscriber
@ -5,24 +7,30 @@ from news import models
from django.template.loader import render_to_string from django.template.loader import render_to_string
from django.conf import settings from django.conf import settings
from smtplib import SMTPException from smtplib import SMTPException
from django.template.loader import get_template
@shared_task @shared_task
def send_email_with_news(news_ids): def send_email_with_news(news_ids):
subscribers = Subscriber.objects.filter(state=Subscriber.USABLE) subscribers = Subscriber.objects.filter(state=Subscriber.USABLE)
sent_news = models.News.objects.filter(id__in=news_ids) 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: for s in subscribers:
try: try:
for n in sent_news: for n in sent_news:
send_mail("G&M News", render_to_string(settings.NEWS_EMAIL_TEMPLATE, context = {"title": n.title.get(s.locale),
{"title": n.title.get(s.locale), "subtitle": n.subtitle.get(s.locale),
"subtitle": n.subtitle.get(s.locale), "description": n.description.get(s.locale),
"description": n.description.get(s.locale), "code": s.update_code,
"code": s.update_code, "image_url": n.image_url,
"domain_uri": settings.DOMAIN_URI, "domain_uri": settings.DOMAIN_URI,
"country_code": s.country_code}), "slug": n.slug,
settings.EMAIL_HOST_USER, [s.send_to], fail_silently=False) "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: except SMTPException:
continue continue

File diff suppressed because one or more lines are too long