Skip to content

Commit

Permalink
Fix Variable html creation file
Browse files Browse the repository at this point in the history
  • Loading branch information
Shruti1229 committed Dec 5, 2023
1 parent 0d56d29 commit d424a65
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 28 deletions.
2 changes: 1 addition & 1 deletion backend/newsletter/templates/cl_newsletter_1.html
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
<td style="direction:ltr;font-size:0px;padding:0px;text-align:center;">
<div class="mj-column-per-100 mj-outlook-group-fix" style="font-size:0px;text-align:left;direction:ltr;display:inline-block;vertical-align:top;width:100%;">
<table border="0" cellpadding="0" cellspacing="0" role="presentation" style="vertical-align:top;" width="100%">
{% include 'variable.html' %}
{% include dynamic_template_name %}

</table>
</div>
Expand Down
58 changes: 31 additions & 27 deletions backend/newsletter/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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 = ""
Expand All @@ -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:
Expand All @@ -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")
Expand Down Expand Up @@ -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 = ""
Expand All @@ -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:
Expand All @@ -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(
Expand Down

0 comments on commit d424a65

Please sign in to comment.