def post(self, request): input_excel = request.FILES['paySlipsFile'] extracted_data = ExcelOperations(input_excel).extract_data_from_excel() if not extracted_data: return Response( { "result": "error", "errorData": { "errorCode": BULK_IMPORT_ERROR_SCHEMA.get("SHEET EMPTY"), "errorMsg": BULK_IMPORT_ERROR_CODES.get( BULK_IMPORT_ERROR_SCHEMA.get("SHEET EMPTY")) } }, 500) else: validation_report = ExcelOperations( input_excel).validate_excel_data(extracted_data) if validation_report["result"]: return Response( { "result": "error", "errorData": validation_report["result"] }, 500) else: return Response( { "result": "success", "jsonData": extracted_data }, 200)
def check_for_empty_data(self, excel_data_as_json): for each_record in excel_data_as_json: for key, value in each_record.items(): if key in MANDATORY_FIELDS_FOR_PAYSLIP and not value: print key self.validation_report["result"] = { "errorCode": BULK_IMPORT_ERROR_SCHEMA.get("DATA MISSING"), "errorMsg": BULK_IMPORT_ERROR_CODES.get( BULK_IMPORT_ERROR_SCHEMA.get("DATA MISSING")) }
def check_for_mandatory_fields(self, excel_data_as_json): keys_list = [ each_key.lower() for each_key in excel_data_as_json[0].keys() ] for mandatory_fields in MANDATORY_FIELDS_FOR_PAYSLIP: if mandatory_fields.lower() not in keys_list: self.validation_report["result"] = { "errorCode": BULK_IMPORT_ERROR_SCHEMA.get("HEADER MISMATCH"), "errorMsg": BULK_IMPORT_ERROR_CODES.get( BULK_IMPORT_ERROR_SCHEMA.get("HEADER MISMATCH")) }
def send_emails_to_the_users(self, user_content): try: if is_network_available(): pdf = pdfkit.from_string(str(user_content.get("pdfString")), "") email_result = send_mail("Payslip for the month " + MONTHS.get(str(self.model_data.month)), user_content.get("htmlString"), self.model_data.emailId, pdf) self.mail_status_list["mailResultList"].append(email_result) else: self.mail_status_list["errorResult"] = {"errorCode":BULK_IMPORT_ERROR_SCHEMA.get("INTERNET CONNECTION"), "errorMsg":BULK_IMPORT_ERROR_CODES.get(BULK_IMPORT_ERROR_SCHEMA.get("INTERNET CONNECTION"))} return self.mail_status_list except Exception as e: self.mail_status_list["errorResult"] = {"errorCode": "500", "errorMsg": str(e.message)} return self.mail_status_list
def check_for_data_format(self, excel_data_as_json): for each_record in excel_data_as_json: for key, value in each_record.items(): if str(type(value)) not in PAYSLIPS_UPLOAD_FORMAT.get( PAYSLIPS_UPLOAD_FORMAT_VERSION[-1]).get(key.lower()): self.validation_report["result"] = { "errorCode": BULK_IMPORT_ERROR_SCHEMA.get("DATA FORMAT ERROR"), "errorMsg": BULK_IMPORT_ERROR_CODES.get( BULK_IMPORT_ERROR_SCHEMA.get("DATA FORMAT ERROR")), "other": key }
def check_email_validation(self, excel_data_as_json): invalid_emails = list() for each_record in excel_data_as_json: each_email_id = each_record.get("email id") is_valid = validate_email(each_email_id) if not is_valid: invalid_emails.append(each_email_id) if invalid_emails: self.validation_report["result"] = { "errorCode": BULK_IMPORT_ERROR_SCHEMA.get("INVALID EMAILS"), "errorMsg": BULK_IMPORT_ERROR_CODES.get( BULK_IMPORT_ERROR_SCHEMA.get("INVALID EMAILS")), "other": invalid_emails }
def check_email_duplication(self, excel_data_as_json): all_email_ids = [] duplicate_emails = [] for each_record in excel_data_as_json: all_email_ids.append(each_record.get("email id")) duplicate_emails = [ k for k, v in Counter(all_email_ids).items() if v > 1 ] if duplicate_emails: self.validation_report["result"] = { "errorCode": BULK_IMPORT_ERROR_SCHEMA.get("DUPLICATES"), "errorMsg": BULK_IMPORT_ERROR_CODES.get( BULK_IMPORT_ERROR_SCHEMA.get("DUPLICATES")), "other": duplicate_emails }
def check_for_mail_limit(self, mail_records): try: mail_status_register = smtpStatus.objects.get( date=datetime.now().date()) no_of_mails_remaining = SMTP_MAIL_LIMIT - mail_status_register.noOfMails if len(mail_records) > no_of_mails_remaining or len( mail_records) > SMTP_MAIL_LIMIT: self.mail_count_report["result"] = { "errorCode": BULK_IMPORT_ERROR_SCHEMA.get("MAILS LIMIT EXCEEDED"), "errorMsg": BULK_IMPORT_ERROR_CODES.get( BULK_IMPORT_ERROR_SCHEMA.get("MAILS LIMIT EXCEEDED")) + "you can only send " + str(no_of_mails_remaining) + " mails for today" } except: print "Data doesn't exist in db" return self.mail_count_report