diff --git a/apps/news/admin.py b/apps/news/admin.py index 7cbfb049..77ea8388 100644 --- a/apps/news/admin.py +++ b/apps/news/admin.py @@ -1,6 +1,7 @@ from django.contrib import admin -from news import models +from news import models +from .tasks import send_email_with_news @admin.register(models.NewsType) class NewsTypeAdmin(admin.ModelAdmin): @@ -9,6 +10,17 @@ class NewsTypeAdmin(admin.ModelAdmin): list_display_links = ['id', 'name'] +def send_email_action(modeladmin, request, queryset): + news_ids = list(queryset.values_list("id", flat=True)) + + send_email_with_news.delay(news_ids) + + + +send_email_action.short_description = "Send the selected news by email" + + @admin.register(models.News) class NewsAdmin(admin.ModelAdmin): """News admin.""" + actions = [send_email_action] diff --git a/apps/news/tasks.py b/apps/news/tasks.py new file mode 100644 index 00000000..99065fc8 --- /dev/null +++ b/apps/news/tasks.py @@ -0,0 +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_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": n.title.get(s.locale), + "subtitle": n.subtitle.get(s.locale), + "description": n.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 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'), diff --git a/project/settings/base.py b/project/settings/base.py index d61f0829..d145d1ba 100644 --- a/project/settings/base.py +++ b/project/settings/base.py @@ -397,6 +397,7 @@ PASSWORD_RESET_TIMEOUT_DAYS = 1 RESETTING_TOKEN_TEMPLATE = 'account/password_reset_email.html' CHANGE_EMAIL_TEMPLATE = 'account/change_email.html' CONFIRM_EMAIL_TEMPLATE = 'authorization/confirm_email.html' +NEWS_EMAIL_TEMPLATE = "news/news_email.html" # COOKIES diff --git a/project/templates/news/news_email.html b/project/templates/news/news_email.html new file mode 100644 index 00000000..a47af685 --- /dev/null +++ b/project/templates/news/news_email.html @@ -0,0 +1,20 @@ + + + + + {{ title }} + + +

{{ title }}

+ + {% if subtitle %} +

{{ subtitle }}

+ {% endif %} + +

{{ description }}

+ +https://{{ country_code }}.{{ domain_uri }}{% url 'web:notification:unsubscribe' code %} + + + + diff --git a/project/urls/web.py b/project/urls/web.py index 0a81672d..5bf538f3 100644 --- a/project/urls/web.py +++ b/project/urls/web.py @@ -23,7 +23,7 @@ urlpatterns = [ path('collections/', include('collection.urls.web')), path('establishments/', include('establishment.urls.web')), path('news/', include('news.urls.web')), - path('notifications/', include('notification.urls.web')), + path('notifications/', include(('notification.urls.web', "notification"), namespace='notification')), path('partner/', include('partner.urls.web')), path('location/', include('location.urls.web')), path('main/', include('main.urls')),