diff --git a/apps/news/admin.py b/apps/news/admin.py index dab70ec8..ad4a87c7 100644 --- a/apps/news/admin.py +++ b/apps/news/admin.py @@ -3,7 +3,6 @@ from django.contrib import admin from news import models from .tasks import send_email_with_news - @admin.register(models.NewsType) class NewsTypeAdmin(admin.ModelAdmin): """News type admin.""" @@ -12,13 +11,9 @@ class NewsTypeAdmin(admin.ModelAdmin): def send_email_action(modeladmin, request, queryset): - news_ids = [n.id for n in queryset] - - send_email_with_news(news_ids) - - # send_email_with_news.delay(news_ids) - - return + news_ids =queryset.values('id') + list_id = list(queryset.values_list('id', flat=True)) + send_email_with_news.delay(list_id) send_email_action.short_description = "Send the selected news by email" diff --git a/apps/news/tasks.py b/apps/news/tasks.py index 7bc2ce23..41a4a8c7 100644 --- a/apps/news/tasks.py +++ b/apps/news/tasks.py @@ -1,4 +1,4 @@ -from celery import shared_task +from celery import shared_task, task from django.core.mail import send_mail from notification.models import Subscriber from news import models @@ -7,15 +7,16 @@ from django.conf import settings from smtplib import SMTPException -# @shared_task +@shared_task def send_email_with_news(news_ids): - subscribers = Subscriber.objects.filter(state=Subscriber.USABLE) for s in subscribers: try: for n in news_ids: + print(n) sent_news = models.News.objects.get(id=n) + # settings.NEWS_EMAIL_TEMPLATE send_mail("G&M News", render_to_string(settings.NEWS_EMAIL_TEMPLATE, {"title": sent_news.title.get(s.locale), @@ -26,4 +27,5 @@ def send_email_with_news(news_ids): "country_code": s.country_code}), settings.EMAIL_HOST_USER, [s.send_to], fail_silently=False) except SMTPException: + print('SMTPException') continue