From ee221d093817e5aaa6fb2f2119938d78584536e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B8=D0=BA=D1=82=D0=BE=D1=80=20=D0=93=D0=BB=D0=B0?= =?UTF-8?q?=D0=B4=D0=BA=D0=B8=D1=85?= Date: Tue, 1 Oct 2019 17:33:21 +0300 Subject: [PATCH] Fix celery --- apps/news/admin.py | 11 +++-------- apps/news/tasks.py | 8 +++++--- 2 files changed, 8 insertions(+), 11 deletions(-) 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