Fix celery

This commit is contained in:
Виктор Гладких 2019-10-01 17:33:21 +03:00
parent 88df92a7a5
commit ee221d0938
2 changed files with 8 additions and 11 deletions

View File

@ -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"

View File

@ -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