From 805c0e015402010eb9a7fb7b5adf096778d112a2 Mon Sep 17 00:00:00 2001 From: karthikmurakonda Date: Wed, 31 Aug 2022 12:48:20 +0530 Subject: [PATCH] Send alert email to cdc when error while submitting jnf by company --- CDC_Backend/APIs/companyViews.py | 6 +++-- CDC_Backend/APIs/constants.py | 1 + CDC_Backend/APIs/utils.py | 39 ++++++++++++++++++++++++++++---- 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/CDC_Backend/APIs/companyViews.py b/CDC_Backend/APIs/companyViews.py index 0b708f9..77d228e 100644 --- a/CDC_Backend/APIs/companyViews.py +++ b/CDC_Backend/APIs/companyViews.py @@ -230,12 +230,14 @@ def addPlacement(request): status=status.HTTP_200_OK) except ValueError as e: - exception_email(opening) + store_all_files(request) + exception_email(data) logger.info("ValueError in addPlacement: " + str(e)) return Response({'action': "Add Placement", 'message': str(e)}, status=status.HTTP_400_BAD_REQUEST) except: - exception_email(opening) + store_all_files(request) + exception_email(data) logger.warning("Add New Placement: " + str(sys.exc_info())) return Response({'action': "Add Placement", 'message': "Something went wrong"}, status=status.HTTP_400_BAD_REQUEST) diff --git a/CDC_Backend/APIs/constants.py b/CDC_Backend/APIs/constants.py index 35afd94..60b59c5 100644 --- a/CDC_Backend/APIs/constants.py +++ b/CDC_Backend/APIs/constants.py @@ -150,6 +150,7 @@ SPECIAL_FORMAT_IN_PDF = ['website', 'company_details_pdf_names', 'description_pd 'compensation_details_pdf_names', 'selection_procedure_details_pdf_names'] +COMPANY_OPENING_ERROR_TEMPLATE = "Alert! Error submitting opening for {company_name}." COMPANY_OPENING_SUBMITTED_TEMPLATE_SUBJECT = "Notification Submitted - {id} - Career Development Cell, IIT Dharwad" STUDENT_APPLICATION_STATUS_TEMPLATE_SUBJECT = 'Application Status - {company_name} - {id}' STUDENT_APPLICATION_SUBMITTED_TEMPLATE_SUBJECT = 'CDC - Application Submitted - {company_name}' diff --git a/CDC_Backend/APIs/utils.py b/CDC_Backend/APIs/utils.py index 41c09b0..8c4cca5 100644 --- a/CDC_Backend/APIs/utils.py +++ b/CDC_Backend/APIs/utils.py @@ -57,6 +57,7 @@ def precheck(required_data=None): return view_func(request, *args, **kwargs) except: + logger.warning("Pre check: " + str(sys.exc_info())) return Response({'action': "Pre check", 'message': "Something went wrong"}, status=status.HTTP_400_BAD_REQUEST) @@ -264,8 +265,13 @@ def verify_recaptcha(request): def opening_description_table_html(opening): - details = model_to_dict(opening, fields=[field.name for field in Placement._meta.fields], + # check typing of opening + if isinstance(opening, Placement): + details = model_to_dict(opening, fields=[field.name for field in Placement._meta.fields], exclude=EXCLUDE_IN_PDF) + # check typing of opening is query dict + else: #if isinstance(opening, QueryDict): + details = opening keys = list(details.keys()) newdetails = {} for key in keys: @@ -333,17 +339,40 @@ def send_opening_notifications(placement_id): return False def exception_email(opening): + opening = opening.dict() data = { - "designation": opening.designation, + "designation": opening["designation"], "opening_type": PLACEMENT, - "company_name": opening.company_name, + "company_name": opening["company_name"], } pdfhtml = opening_description_table_html(opening) - name = opening.company_name + '_jnf_response.pdf' + name = opening["company_name"]+'_jnf_response.pdf' attachment_jnf_respone = { "name": name, "html": pdfhtml, } - sendEmail(CDC_MAIl_ADDRESS, COMPANY_OPENING_SUBMITTED_TEMPLATE_SUBJECT.format(id=opening.id), data, + sendEmail(CDC_MAIl_ADDRESS, COMPANY_OPENING_ERROR_TEMPLATE.format(company_name=opening["company_name"]), data, COMPANY_OPENING_SUBMITTED_TEMPLATE, attachment_jnf_respone) + +def store_all_files(request): + files = request.FILES + data = request.data + # save all the files + if files: + # company details pdf + for file in files.getlist(COMPANY_DETAILS_PDF): + file_location = STORAGE_DESTINATION_COMPANY_ATTACHMENTS + "temp" + '/' + saveFile(file, file_location) + # compensation details pdf + for file in files.getlist(COMPENSATION_DETAILS_PDF): + file_location = STORAGE_DESTINATION_COMPANY_ATTACHMENTS + "temp" + '/' + saveFile(file, file_location) + # selection procedure details pdf + for file in files.getlist(SELECTION_PROCEDURE_DETAILS_PDF): + file_location = STORAGE_DESTINATION_COMPANY_ATTACHMENTS + "temp" + '/' + saveFile(file, file_location) + # description pdf + for file in files.getlist(DESCRIPTION_PDF): + file_location = STORAGE_DESTINATION_COMPANY_ATTACHMENTS + "temp" + '/' + saveFile(file, file_location) \ No newline at end of file