diff --git a/backend/newsletter/templates/cl_newsletter_1.html b/backend/newsletter/templates/cl_newsletter_1.html index e3e5b042..2dfcc01e 100644 --- a/backend/newsletter/templates/cl_newsletter_1.html +++ b/backend/newsletter/templates/cl_newsletter_1.html @@ -130,7 +130,7 @@
- {% include 'variable.html' %} + {% include dynamic_template_name %}
diff --git a/backend/newsletter/views.py b/backend/newsletter/views.py index 00053410..6eb61798 100644 --- a/backend/newsletter/views.py +++ b/backend/newsletter/views.py @@ -30,6 +30,7 @@ from bs4 import BeautifulSoup from django.core.mail import send_mail from django.conf import settings +import uuid class NewsletterViewSet(ModelViewSet): @@ -71,16 +72,19 @@ def create(self, request, pk=None, *args, **kwargs): requested_html = os.path.join( BASE_DIR, "newsletter", "templates", "cl_newsletter_1.html" ) + uuid_num = str(uuid.uuid4()) + temp_file = "variable_" + uuid_num + ".html" file_html = open( - os.path.join(BASE_DIR, "newsletter", "templates", "variable.html"), - "w", + os.path.join(BASE_DIR, "newsletter", "templates", temp_file), "w" ) soup = BeautifulSoup(final_html_content, "html.parser") file_html.write(soup.prettify()) - context = {"variable": ""} + dynamic_template_name = temp_file + context = {"dynamic_template_name": dynamic_template_name} file_html.close() html_file = loader.get_template(requested_html) html_content = html_file.render(context, request) + html_file = "" elif template_id == 2: if len(content) != 0: final_html_content = "" @@ -99,27 +103,30 @@ def create(self, request, pk=None, *args, **kwargs): requested_html = os.path.join( BASE_DIR, "newsletter", "templates", "cl_newsletter_1.html" ) + uuid_num = str(uuid.uuid4()) + temp_file = "variable_" + uuid_num + ".html" file_html = open( - os.path.join(BASE_DIR, "newsletter", "templates", "variable.html"), - "w", + os.path.join(BASE_DIR, "newsletter", "templates", temp_file), "w" ) soup = BeautifulSoup(final_html_content, "html.parser") file_html.write(soup.prettify()) - context = {"variable": ""} + dynamic_template_name = temp_file + context = {"dynamic_template_name": dynamic_template_name} file_html.close() html_file = loader.get_template(requested_html) html_content = html_file.render(context, request) + html_file = "" elif template_id == 3: if type(content) == dict: message = base64.b64decode(content["html"]).decode("utf-8") - f = open('content.html','w') + f = open("content.html", "w") f.write(message) f.close() # Parse the file using an HTML parser. parser = html.parser.HTMLParser() - with open('content.html', 'rb') as f: - parser.feed(f.read().decode('utf-8')) + with open("content.html", "rb") as f: + parser.feed(f.read().decode("utf-8")) # Check for common HTML errors. if parser.error_list: @@ -145,7 +152,6 @@ def create(self, request, pk=None, *args, **kwargs): status=status.HTTP_200_OK, ) - @is_admin @swagger_auto_schema(request_body=NewsletterSerializer) @action(detail=False, methods=["post"], url_path="preview") @@ -175,16 +181,19 @@ def preview(self, request, pk=None, *args, **kwargs): requested_html = os.path.join( BASE_DIR, "newsletter", "templates", "cl_newsletter_1.html" ) + uuid_num = str(uuid.uuid4()) + temp_file = "variable_" + uuid_num + ".html" file_html = open( - os.path.join(BASE_DIR, "newsletter", "templates", "variable.html"), - "w", + os.path.join(BASE_DIR, "newsletter", "templates", temp_file), "w" ) soup = BeautifulSoup(final_html_content, "html.parser") file_html.write(soup.prettify()) - context = {"variable": ""} + dynamic_template_name = temp_file + context = {"dynamic_template_name": dynamic_template_name} file_html.close() html_file = loader.get_template(requested_html) html_content = html_file.render(context, request) + html_file = "" elif template_id == 2: if len(content) != 0: final_html_content = "" @@ -203,27 +212,30 @@ def preview(self, request, pk=None, *args, **kwargs): requested_html = os.path.join( BASE_DIR, "newsletter", "templates", "cl_newsletter_1.html" ) + uuid_num = str(uuid.uuid4()) + temp_file = "variable_" + uuid_num + ".html" file_html = open( - os.path.join(BASE_DIR, "newsletter", "templates", "variable.html"), - "w", + os.path.join(BASE_DIR, "newsletter", "templates", temp_file), "w" ) soup = BeautifulSoup(final_html_content, "html.parser") file_html.write(soup.prettify()) - context = {"variable": ""} + dynamic_template_name = temp_file + context = {"dynamic_template_name": dynamic_template_name} file_html.close() html_file = loader.get_template(requested_html) html_content = html_file.render(context, request) + html_file = "" elif template_id == 3: if len(content) != 0: message = base64.b64decode(content).decode("utf-8") - f = open('content.html','w') + f = open("content.html", "w") f.write(message) f.close() # Parse the file using an HTML parser. parser = html.parser.HTMLParser() - with open('content.html', 'rb') as f: - parser.feed(f.read().decode('utf-8')) + with open("content.html", "rb") as f: + parser.feed(f.read().decode("utf-8")) # Check for common HTML errors. if parser.error_list: @@ -237,19 +249,11 @@ def preview(self, request, pk=None, *args, **kwargs): {"message": "Template not supported."}, status=status.HTTP_400_BAD_REQUEST, ) - - new_newsletter = Newsletter( - content=html_content, - submitter_id=User.objects.get(pk=submitter_id), - category="NEW_FEATURE", - ) - new_newsletter.save() return Response( {"html": html_content}, status=status.HTTP_200_OK, ) - @swagger_auto_schema( method="post", request_body=openapi.Schema(