Commit 1ee193b6 authored by Sébastiaan Versteeg's avatar Sébastiaan Versteeg
Browse files

Merge branch 'feature/save-newsletter-html' into 'master'

Write newsletter html to file on sending

Closes #321

See merge request !1174
parents b7673ca2 8632520d
......@@ -49,6 +49,14 @@ newsletters.models module
:undoc-members:
:show-inheritance:
newsletters.services module
---------------------------
.. automodule:: newsletters.services
:members:
:undoc-members:
:show-inheritance:
newsletters.urls module
-----------------------
......
......@@ -7,6 +7,8 @@ from django.utils import translation
from members.models import Member
from partners.models import Partner
from newsletters import services
def send_newsletter(request, newsletter):
"""
......@@ -55,4 +57,6 @@ def send_newsletter(request, newsletter):
msg.attach_alternative(html_message, "text/html")
msg.send()
services.write_to_file(newsletter.pk, language[0], html_message)
translation.deactivate()
from django.core.management.base import BaseCommand
from django.http import HttpRequest
from newsletters import emails, models
class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument(
'--include-unsent',
action='store_true',
dest='include-unsent',
default=False,
help="Include newsletters that haven't been sent yet"
)
parser.add_argument(
'server-name',
help='The server name for the request '
'to generate the html (typically thalia.nu)'
)
parser.add_argument(
'server-port',
type=int,
help='The server port for the request '
'to generate the html (typically 80)'
)
def handle(self, *args, **options):
request = HttpRequest()
request.META['SERVER_NAME'] = options['server-name']
request.META['SERVER_PORT'] = options['server-port']
for n in models.Newsletter.objects.all():
if n.sent or options['include-unsent']:
emails.save_to_disk(n, request)
import os
from django.conf import settings
from django.template.loader import get_template
from django.utils import translation
from partners.models import Partner
def write_to_file(pk, lang, html_message):
"""
Write newsletter to a file
"""
cache_dir = os.path.join(settings.MEDIA_ROOT, 'newsletters')
if not os.path.isdir(cache_dir):
os.makedirs(cache_dir)
with open(os.path.join(
cache_dir,
f'{pk}_{lang}.html'
), 'w+') as cache_file:
cache_file.write(html_message)
def save_to_disk(newsletter, request):
"""
Writes the newsletter as HTML to file (in all languages)
"""
partners = Partner.objects.filter(is_main_partner=True)
main_partner = partners[0] if len(partners) > 0 else None
html_template = get_template('newsletters/email.html')
for language in settings.LANGUAGES:
translation.activate(language[0])
context = {
'newsletter': newsletter,
'agenda_events': (
newsletter.newslettercontent_set
.filter(newsletteritem=None)
.order_by('newsletterevent__start_datetime')
),
'main_partner': main_partner,
'lang_code': language[0],
'request': request
}
html_message = html_template.render(context)
write_to_file(newsletter.pk, language[0], html_message)
"""Views provided by the newsletters package"""
from datetime import datetime, timedelta, date
import os
from django.conf import settings
from django.contrib.admin.views.decorators import staff_member_required
from django.contrib.auth.decorators import permission_required
from django.shortcuts import get_object_or_404, redirect, render
......@@ -10,6 +14,8 @@ from newsletters import emails
from newsletters.models import Newsletter
from partners.models import Partner
from sendfile import sendfile
def preview(request, pk, lang=None):
"""
......@@ -20,9 +26,6 @@ def preview(request, pk, lang=None):
:param lang: the language of the render
:return: HttpResponse 200 containing the newsletter HTML
"""
newsletter = get_object_or_404(Newsletter, pk=pk)
partners = Partner.objects.filter(is_main_partner=True)
main_partner = partners[0] if len(partners) > 0 else None
lang_code = request.LANGUAGE_CODE
if lang is not None:
......@@ -34,6 +37,19 @@ def preview(request, pk, lang=None):
# Language code not recognised by get_language_info
pass
# Send cached file, if it exists
file_path = os.path.join(
settings.MEDIA_ROOT,
'newsletters',
f'{pk}_{lang_code}.html'
)
if os.path.isfile(file_path):
return sendfile(request, file_path)
newsletter = get_object_or_404(Newsletter, pk=pk)
partners = Partner.objects.filter(is_main_partner=True)
main_partner = partners[0] if len(partners) > 0 else None
return render(request, 'newsletters/email.html', {
'newsletter': newsletter,
'agenda_events': newsletter.newslettercontent_set.filter(
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment