Repository: svthalia/concrexit

Issue:
Newsletter HTML is not saved when sending
### Describe the bug

Newsletter HTML is not saved when sending

### How to reproduce
Steps to reproduce the behaviour:
1. Create a newsletter
2. Send it
3. An HTML file should be saved to the media folder
4. But your newsletter is not there

### Expected behaviour

There should be an HTML file saved upon sending the newsletter. This HTML is the state of the newsletter when sending, which retains the original layout used. So if we ever change the newsletter generator we do not lose the original newsletters the way they were sent.



Discussion:
Note: There is code that creates the HTML files and is used in a management command. However, this code expect a request object to be passed to the template. This was previously required since the baseurl tag depended upon this object. However, the tag has been changed since and I believe it should be possible to generate the HTML without passing the request object. That would make your life easier since you only should add the write to disk helper to the helper that currently sends the email.

Resolving patch:
diff --git a/website/newsletters/emails.py b/website/newsletters/emails.py
--- a/website/newsletters/emails.py
+++ b/website/newsletters/emails.py
@@ -62,8 +62,6 @@ def send_newsletter(newsletter):
         html_message = html_template.render(context)
         text_message = text_template.render(context)
 
-        services.write_to_file(newsletter.pk, language[0], html_message)
-
         msg = EmailMultiAlternatives(
             subject=subject,
             body=text_message,
diff --git a/website/newsletters/management/commands/createnewsletterhtml.py b/website/newsletters/management/commands/createnewsletterhtml.py
--- a/website/newsletters/management/commands/createnewsletterhtml.py
+++ b/website/newsletters/management/commands/createnewsletterhtml.py
@@ -1,5 +1,4 @@
 from django.core.management.base import BaseCommand
-from django.http import HttpRequest
 
 from newsletters import models, services
 
@@ -13,22 +12,8 @@ def add_arguments(self, parser):
             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"]:
-                services.save_to_disk(n, request)
+                services.save_to_disk(n)
diff --git a/website/newsletters/services.py b/website/newsletters/services.py
--- a/website/newsletters/services.py
+++ b/website/newsletters/services.py
@@ -23,7 +23,7 @@ def write_to_file(pk, lang, html_message):
         cache_file.write(html_message)
 
 
-def save_to_disk(newsletter, request):
+def save_to_disk(newsletter):
     """
     Writes the newsletter as HTML to file (in all languages)
     """
@@ -45,7 +45,6 @@ def save_to_disk(newsletter, request):
             "main_partner": main_partner,
             "local_partner": local_partner,
             "lang_code": language[0],
-            "request": request,
         }
 
         html_message = html_template.render(context)
@@ -77,3 +76,5 @@ def send_newsletter(newsletter):
     )
     message.users.set(Member.current_members.all())
     message.send()
+
+    save_to_disk(newsletter)


Test patch: