Merge branch 'feature/sending-news-email' into 'develop'

Feature/sending news email

See merge request gm/gm-backend!45
This commit is contained in:
d.kuzmenko 2019-10-02 14:17:29 +00:00
commit 750f141017
6 changed files with 64 additions and 2 deletions

View File

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

28
apps/news/tasks.py Normal file
View File

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

View File

@ -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'),

View File

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

View File

@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{ title }}</title>
</head>
<body>
<h1>{{ title }}</h1>
{% if subtitle %}
<h3>{{ subtitle }}</h3>
{% endif %}
<p>{{ description }} </p>
https://{{ country_code }}.{{ domain_uri }}{% url 'web:notification:unsubscribe' code %}
</body>
</html>

View File

@ -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')),