gault-millau/apps/news/tasks.py
2019-10-02 13:27:33 +05:00

29 lines
1.3 KiB
Python

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.conf import settings
from smtplib import SMTPException
@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)
for s in subscribers:
try:
for n in sent_news:
send_mail("G&M News", render_to_string(settings.NEWS_EMAIL_TEMPLATE,
{"title": sent_news.title.get(s.locale),
"subtitle": sent_news.subtitle.get(s.locale),
"description": sent_news.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)
except SMTPException:
continue