Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

B check #554

Merged
merged 2 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion backend/newsletter/serializers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from rest_framework import serializers
from .models import Newsletter
from django.db import migrations, models


class NewsletterSerializer(serializers.ModelSerializer):
category = serializers.ListField()
content = serializers.ListField(child=serializers.DictField())
class Meta:
model = Newsletter
fields = ("newsletter_uuid", "submitter_id", "content", "category")
31 changes: 19 additions & 12 deletions backend/newsletter/tasks.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
from celery import shared_task
from backend.celery import celery_app
from .models import SubscribedUsers, Newsletter
from django.utils import timezone


@shared_task(name="send_newsletter")
def send_newsletter():
logging.info("Sending Newsletter")
today = date.today()
newsletters = Newsletter.objects.filter(created_at__contains=today).all()
if newsletter is not None:
subscribed_users = SubscribedUsers.get.all()
now = timezone.now()
past_24_hours = now - timezone.timedelta(hours=24)

# Get all objects created in the past 24 hours
newsletters = Newsletter.objects.filter(created_at__gte=past_24_hours).all()
if newsletters is not None:
subscribed_users = SubscribedUsers.objects.all()
for newsletter in newsletters:
for subscribed_user in subscribed_users:
logging.info("Sending Mail to %s", suscribed_user.user.email)
send_mail(
"Chitralekha - Newsletter",
message,
settings.DEFAULT_FROM_EMAIL,
[manager.email],
html_message=newsletter.content,
)
logging.info("Sending Mail to %s", subscribed_user.user.email)
try:
send_mail(
"Chitralekha - Newsletter",
"",
settings.DEFAULT_FROM_EMAIL,
[subscribed_user.user.email],
html_message=newsletter.content,
)
except:
logging.info("Mail can't be sent.")
115 changes: 113 additions & 2 deletions backend/newsletter/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,111 @@ 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")
def preview(self, request, pk=None, *args, **kwargs):
category = request.data.get("category")
content = request.data.get("content")
submitter_id = request.data.get("submitter_id")
template_id = request.data.get("template_id")
BASE_DIR = Path(__file__).resolve().parent.parent

if content is None or content == "":
return Response(
{"message": "missing param : content can't be empty"},
status=status.HTTP_400_BAD_REQUEST,
)

if template_id == 1:
if len(content) != 0:
final_html_content = ""
for c in content:
header_variable = c["header"]
paragraph_variable = c["paragraph"]
html_content = """<tr><td align="left" style="font-size:0px;padding:10px 25px;word-break:break-word;"><div style="font-family:Muli, Arial, sans-serif;font-size:20px;font-weight:400;line-height:30px;text-align:left;color:#333333;"><h1 style="margin: 0; font-size: 24px; line-height: normal; font-weight: bold;">{header}</h1></div></td></tr><tr><td style="font-size:0px;padding:10px 25px;word-break:break-word;"><p style="border-top: solid 1px #F4F5FB; font-size: 1px; margin: 0px auto; width: 100%;"></p></td></tr><tr><td align="left" style="font-size:0px;padding:10px 25px;word-break:break-word;"><div style="font-family:Muli, Arial, sans-serif;font-size:16px;font-weight:400;line-height:20px;text-align:left;color:#333333;"><p style="margin: 0;">{paragraph}</p></div></td></tr>""".format(
header=header_variable, paragraph=paragraph_variable
)
final_html_content = final_html_content + html_content
requested_html = os.path.join(
BASE_DIR, "newsletter", "templates", "cl_newsletter_1.html"
)
file_html = open(
os.path.join(BASE_DIR, "newsletter", "templates", "variable.html"),
"w",
)
soup = BeautifulSoup(final_html_content, "html.parser")
file_html.write(soup.prettify())
context = {"variable": ""}
file_html.close()
html_file = loader.get_template(requested_html)
html_content = html_file.render(context, request)
elif template_id == 2:
if len(content) != 0:
final_html_content = ""
for c in content:
header_variable = c["header"]
paragraph_variable = c["paragraph"]
video_poster = c["image"]
youtube_url = c["youtube_url"]
html_content = """<tr><td style="width:40%;text-align:center;"><div style="font-family:Muli, Arial, sans-serif;font-size:16px;text-align:center;color:#333333;"><a href={youtube_url}><img src={video_poster} style="border:0;display:block;outline:none;text-decoration:none;height:auto;width:220px;font-size:13px;margin-left:20px;" width="45" alt="image instead of video" /></a></div></td><td style="font-size:0px;padding:10px 25px;word-break:break-word;"><div style="font-family:Muli, Arial, sans-serif;font-size:16px;font-weight:400;line-height:20px;color:#333333;"><h3>{header}</h3><p style="margin-left: 0px;">{paragraph}</p></div></td></tr>""".format(
header=header_variable,
video_poster=video_poster,
youtube_url=youtube_url,
paragraph=paragraph_variable,
)
final_html_content = final_html_content + html_content
requested_html = os.path.join(
BASE_DIR, "newsletter", "templates", "cl_newsletter_1.html"
)
file_html = open(
os.path.join(BASE_DIR, "newsletter", "templates", "variable.html"),
"w",
)
soup = BeautifulSoup(final_html_content, "html.parser")
file_html.write(soup.prettify())
context = {"variable": ""}
file_html.close()
html_file = loader.get_template(requested_html)
html_content = html_file.render(context, request)
elif template_id == 3:
if len(content) != 0:
message = base64.b64decode(content).decode("utf-8")
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'))

# Check for common HTML errors.
if parser.error_list:
return Response(
{"message": "Error in HTML."},
status=status.HTTP_400_BAD_REQUEST,
)
html_content = message
else:
return Response(
{"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 Expand Up @@ -220,8 +325,14 @@ def subscribe(self, request):
{"message": "User with this Email Id doesn't exist."},
status=status.HTTP_400_BAD_REQUEST,
)
sub_user = SubscribedUsers(user=user)
sub_user.save()

sub_user, created = SubscribedUsers.objects.get_or_create(user=user)
if not created:
return Response(
{"message": "User is already subscribed."},
status=status.HTTP_400_BAD_REQUEST,
)

return Response(
{"message": "Newsletter is successfully subscribed."},
status=status.HTTP_200_OK,
Expand Down