From f38f013cee24a076fc1d844d57500735d8fd2500 Mon Sep 17 00:00:00 2001 From: karthik mv Date: Mon, 2 May 2022 12:43:26 +0530 Subject: [PATCH] Send jnf response as pdf attachment (#107) * Attach jnf response pdf after verifying email. * add pdfkit to requirments.txt --- CDC_Backend/APIs/companyViews.py | 29 +++------ CDC_Backend/APIs/constants.py | 3 + CDC_Backend/APIs/utils.py | 37 ++++++++++- .../templates/company_jnf_response.html | 61 +++++++++++++++++++ requirements.txt | 1 + 5 files changed, 106 insertions(+), 25 deletions(-) create mode 100644 CDC_Backend/templates/company_jnf_response.html diff --git a/CDC_Backend/APIs/companyViews.py b/CDC_Backend/APIs/companyViews.py index 9b96d16..503b6d3 100644 --- a/CDC_Backend/APIs/companyViews.py +++ b/CDC_Backend/APIs/companyViews.py @@ -260,36 +260,21 @@ def verifyEmail(request): if send_email_to_company: # Email sending part. - details = model_to_dict(opening, fields=[field.name for field in Placement._meta.fields], - exclude=['id', 'is_company_details_pdf', 'offer_accepted', 'is_description_pdf', - 'is_compensation_details_pdf', 'is_selection_procedure_details_pdf', - 'email_verified']) - keys = list(details.keys()) - newdetails = {} - for key in keys: - if isinstance(details[key], list): - details[key] = {"details": details[key], "type": ["list"]} - if key in ['website', 'company_details_pdf_names', 'description_pdf_names', - 'compensation_details_pdf_names', 'selection_procedure_pdf_names']: - if key == 'website': - details[key] = {"details": details[key], "type": ["link"]} - else: - details[key] = {"details": details[key]["details"], "type": ["list", "link"], - "link": LINK_TO_STORAGE_COMPANY_ATTACHMENT + opening.id + "/"} - new_key = key.replace('_', ' ') - if key == 'company_details_pdf_names': - new_key = 'company details pdf' - newdetails[new_key] = details[key] + pdfhtml = opening_description_table_html(opening) + name = opening.company_name + '_jnf_response.pdf' + attachment_jnf_respone = { + "name": name, + "html": pdfhtml, + } data = { "designation": opening.designation, "opening_type": PLACEMENT, "opening_link": PLACEMENT_OPENING_URL.format(id=opening.id), # Some Changes here too "company_name": opening.company_name, - "data": newdetails } json_data = json.dumps(data, default=str) sendEmail(opening.email, COMPANY_OPENING_SUBMITTED_TEMPLATE_SUBJECT.format(id=opening.id), json_data, - COMPANY_OPENING_SUBMITTED_TEMPLATE) + COMPANY_OPENING_SUBMITTED_TEMPLATE, attachment_jnf_respone) return Response({'action': "Verify Email", 'message': "Email Verified Successfully"}, status=status.HTTP_200_OK) diff --git a/CDC_Backend/APIs/constants.py b/CDC_Backend/APIs/constants.py index 7f3d70d..1dfcb69 100644 --- a/CDC_Backend/APIs/constants.py +++ b/CDC_Backend/APIs/constants.py @@ -134,6 +134,9 @@ COMPANY_OPENING_SUBMITTED_TEMPLATE = 'company_opening_submitted.html' STUDENT_APPLICATION_STATUS_SELECTED_TEMPLATE = 'student_application_status_selected.html' STUDENT_APPLICATION_STATUS_NOT_SELECTED_TEMPLATE = 'student_application_status_not_selected.html' COMPANY_EMAIL_VERIFICATION_TEMPLATE = 'company_email_verification.html' +COMPANY_JNF_RESPONSE_TEMPLATE = 'company_jnf_response.html' APPLICATION_CSV_COL_NAMES = ['Applied At', 'Roll No.', 'Name', 'Email', 'Phone Number', 'Branch', 'Batch', 'CPI', 'Resume', 'Selected', ] + +PDF_FILES_SERVING_ENDPOINT = 'http://localhost:5500/CDC_Backend/Storage/Company_Attachments/' # TODO: Change this to actual URL diff --git a/CDC_Backend/APIs/utils.py b/CDC_Backend/APIs/utils.py index 959fdc6..6ffbc9f 100644 --- a/CDC_Backend/APIs/utils.py +++ b/CDC_Backend/APIs/utils.py @@ -12,6 +12,7 @@ import background_task import jwt from django.conf import settings from django.core.mail import EmailMultiAlternatives +from django.forms.models import model_to_dict from django.http import Http404 from django.shortcuts import get_object_or_404 from django.template.loader import render_to_string @@ -21,9 +22,10 @@ from google.oauth2 import id_token from rest_framework import status from rest_framework.response import Response import requests as rq +import pdfkit from .constants import * -from .models import User, PrePlacementOffer, PlacementApplication +from .models import User, PrePlacementOffer, PlacementApplication, Placement logger = logging.getLogger('db') @@ -134,7 +136,7 @@ def saveFile(file, location): @background_task.background(schedule=10) -def sendEmail(email_to, subject, data, template): +def sendEmail(email_to, subject, data, template, attachment_jnf_respone=None): try: if not isinstance(data, dict): data = json.loads(data) @@ -146,6 +148,10 @@ def sendEmail(email_to, subject, data, template): msg = EmailMultiAlternatives(subject, text_content, email_from, recipient_list) msg.attach_alternative(html_content, "text/html") + if attachment_jnf_respone: + logger.info(attachment_jnf_respone) + pdf = pdfkit.from_string(attachment_jnf_respone['html'], False,options={"--enable-local-file-access": "",'--dpi':'96'}) + msg.attach(attachment_jnf_respone['name'], pdf, 'application/pdf') msg.send() return True except: @@ -261,4 +267,29 @@ def verify_recaptcha(request): print(sys.exc_info()) print(traceback.format_exc()) logger.warning("Utils - verify_recaptcha: " + str(sys.exc_info())) - return False, "_" \ No newline at end of file + return False, "_" + +def opening_description_table_html(opening): + details = model_to_dict(opening, fields = [field.name for field in Placement._meta.fields], exclude = ['id','is_company_details_pdf','offer_accepted','is_description_pdf','is_compensation_details_pdf','is_selection_procedure_details_pdf','email_verified']) + keys = list(details.keys()) + newdetails = {} + for key in keys: + if isinstance(details[key], list): + details[key] = {"details": details[key], "type": ["list"]} + if key in ['website','company_details_pdf_names','description_pdf_names','compensation_details_pdf_names','selection_procedure_pdf_names']: + if key == 'website': + details[key] = {"details": details[key], "type": ["link"]} + else: + details[key] = {"details": details[key]["details"], "type": ["list","link"], "link": PDF_FILES_SERVING_ENDPOINT+opening.id+"/"} + new_key = key.replace('_',' ') + if key.endswith(' names'): + new_key = key[:-6] + new_key = new_key.capitalize() + newdetails[new_key] = details[key] + imagepath = os.path.abspath('./templates/image.png') + print(imagepath) + data = { + "data": newdetails, + "imgpath": imagepath + } + return render_to_string(COMPANY_JNF_RESPONSE_TEMPLATE, data) \ No newline at end of file diff --git a/CDC_Backend/templates/company_jnf_response.html b/CDC_Backend/templates/company_jnf_response.html new file mode 100644 index 0000000..e8b23c5 --- /dev/null +++ b/CDC_Backend/templates/company_jnf_response.html @@ -0,0 +1,61 @@ + + + + + + + + Document + + + +
cdc logo
+

Job Notification Form Response

+ + {% for key, value in data.items %} + + + + + {% endfor %} +
+ {{ key }} + + {% if 'list' in value.type %} + + {% for item in value.details %} +
  • + {% if 'link' in value.type and value.link %} + {{ item }} + {% elif 'link' in value.type %} + {{ item }} + {% else %} + {{ item }} + {% endif %} +
  • + {% endfor %} + {% else %} + {% if 'link' in value.type %} + {{ value.details }} + {% else %} + {{ value }} + {% endif %} + {% endif %} + +
    +

    In case of any descripency regarding above details, please contact cdc@iitdh.ac.in + +

    + + + diff --git a/requirements.txt b/requirements.txt index 5a3194d..6f11953 100644 --- a/requirements.txt +++ b/requirements.txt @@ -21,6 +21,7 @@ jsonfield==3.1.0 lazy-object-proxy==1.6.0 Markdown==3.3.6 mccabe==0.7.0 +pdfkit==1.0.0 platformdirs==2.5.1 psycopg2-binary==2.9.3 pyasn1==0.4.8