diff --git a/apps/news/admin.py b/apps/news/admin.py index b866d867..dab70ec8 100644 --- a/apps/news/admin.py +++ b/apps/news/admin.py @@ -1,9 +1,7 @@ from django.contrib import admin from news import models -from notification.models import Subscriber from .tasks import send_email_with_news -from establishment.tasks import recalculate_price_levels_by_country @admin.register(models.NewsType) @@ -14,16 +12,12 @@ class NewsTypeAdmin(admin.ModelAdmin): def send_email_action(modeladmin, request, queryset): - print(queryset) - news_ids = [n.id for n in queryset] - print(news_ids) + send_email_with_news(news_ids) # send_email_with_news.delay(news_ids) - recalculate_price_levels_by_country.delay(news_ids) - print("TEST send_email_action IS CALLED!") return diff --git a/apps/news/tasks.py b/apps/news/tasks.py index a7e43c8c..c3c011d7 100644 --- a/apps/news/tasks.py +++ b/apps/news/tasks.py @@ -1,12 +1,28 @@ 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): - print(news) +# @shared_task +def send_email_with_news(news_ids): - print("EMAILS WAS SENT!") + subscribers = Subscriber.objects.filter(state=Subscriber.USABLE) - return news \ No newline at end of file + for s in subscribers: + try: + for n in news_ids: + sent_news = models.News.objects.get(id=n) + + send_mail("G&M News", render_to_string(settings.NEWS_EMAIL_TEMPLATE, + {"title": sent_news.title.get(s.country_code), + "subtitle": sent_news.subtitle.get(s.country_code), + "description": sent_news.description.get(s.country_code), + "code": s.update_code, + "domain_uri": settings.DOMAIN_URI}), + settings.EMAIL_HOST_USER, [s.send_to], fail_silently=False) + except SMTPException: + continue diff --git a/apps/notification/urls/common.py b/apps/notification/urls/common.py index df43c805..842aa642 100644 --- a/apps/notification/urls/common.py +++ b/apps/notification/urls/common.py @@ -2,6 +2,7 @@ from django.urls import path from notification.views import common +app_name = "notification" urlpatterns = [ path('subscribe/', common.SubscribeView.as_view(), name='subscribe'),