def post(self, request, *args, **kwargs): date_from = request.POST['date_from'] date_to = request.POST['date_to'] empresa = request.POST['empresas'] date_from = datetime.strptime(str(date_from), '%d/%m/%Y') date_to = datetime.strptime(str(date_to), '%d/%m/%Y') empresa = str(empresa) data = Email.get_emails_by_dates(date_from, date_to, empresa) report_file = create_tablib(data, empresa) report_file_format = get_report_file_format(empresa) if report_file_format == 'xlsx': response_file = report_file.xlsx response_filename = 'consolidado' + get_date_to_string() + report_file_format response_filetype = 'application/vnd.ms-excel' elif report_file_format == 'tsv': response_file = report_file.tsv response_filename = 'consolidado' + get_date_to_string() + report_file_format response_filetype = 'text/tsv' else: response_file = report_file.csv response_filename = 'consolidado' + get_date_to_string() + report_file_format response_filetype = 'text/csv' general_conf = GeneralConfiguration.get_configuration(empresa) if general_conf is not None and general_conf.report_file_zipped: # ejecutar proceso de comprimir reporte in_memory = StringIO() with ZipFile(in_memory, 'w') as archive: archive.writestr(response_filename, str(response_file), ZIP_DEFLATED) response = HttpResponse(in_memory.getvalue(), content_type="application/x-zip-compressed") response['Content-Disposition'] = 'attachment; filename="reporte.zip"' return response else: # retornar el reporte response = HttpResponse(response_file, content_type=response_filetype) response['Content-Disposition'] = 'attachment; filename="' + response_filename + '"' return response
def post(self, request, *args, **kwargs): logging.info(request.body) export_type = request.POST.get('export_type') if export_type == 'export_general_email': tipo_receptor = request.POST.get('tipo_receptor') empresa = request.POST.get('empresa') user_email = request.POST.get('user_email') file_name = request.POST.get('file_name') date_from = request.POST.get('date_from') date_to = request.POST.get('date_to') date_from = int(date_from, base=10) date_to = int(date_to, base=10) date_from = timestamp_to_date(date_from) date_to = timestamp_to_date(date_to) params = dict() params['date_from'] = date_from params['date_to'] = date_to params['empresa'] = empresa params['tipo_receptor'] = tipo_receptor # Consulta data = Email.get_emails_by_dates_async(**params) elif export_type == 'export_sended_email': tipo_receptor = request.POST.get('tipo_receptor') empresa = request.POST.get('empresa') user_email = request.POST.get('user_email') file_name = request.POST.get('file_name') date_from = request.POST.get('date_from') date_to = request.POST.get('date_to') date_from = int(date_from, base=10) date_to = int(date_to, base=10) date_from = timestamp_to_date(date_from) date_to = timestamp_to_date(date_to) params = dict() params['date_from'] = date_from params['date_to'] = date_to params['empresa'] = empresa params['tipo_receptor'] = tipo_receptor # Consulta data = Email.get_sended_emails_by_dates_async(**params) elif export_type == 'export_failure_email': tipo_receptor = request.POST.get('tipo_receptor') empresa = request.POST.get('empresa') user_email = request.POST.get('user_email') file_name = request.POST.get('file_name') date_from = request.POST.get('date_from') date_to = request.POST.get('date_to') date_from = int(date_from, base=10) date_to = int(date_to, base=10) date_from = timestamp_to_date(date_from) date_to = timestamp_to_date(date_to) params = dict() params['date_from'] = date_from params['date_to'] = date_to params['empresa'] = empresa params['tipo_receptor'] = tipo_receptor # Consulta data = Email.get_failure_emails_by_dates_async(**params) elif export_type == 'export_dynamic_emails': user_email = request.POST.get('user_email') empresa = request.POST.get('empresa') file_name = request.POST.get('file_name') params = request.POST.get('params') params = json.loads(params) logging.info(params) data = Email.get_emails_by_dynamic_query_async(**params) if data is not None: # Creación del documento report_file = create_tablib(data, empresa) # evaluacion del formato del archivo reporte report_file_format = get_report_file_format(empresa) if report_file_format == 'xlsx': response_file = report_file.xlsx response_filename = file_name elif report_file_format == 'tsv': response_file = report_file.tsv response_filename = file_name else: response_file = report_file.csv response_filename = file_name # evaluar si el archivo es comprimido en zip general_conf = GeneralConfiguration.get_configuration(empresa) if general_conf is not None and general_conf.report_file_zipped: # ejecutar proceso de comprimir reporte in_memory = StringIO() with ZipFile(in_memory, 'w') as archive: archive.writestr(response_filename, str(response_file), ZIP_DEFLATED) response_file = in_memory.getvalue() response_filename = file_name + '.zip' # Crear objeto para enviarlo por correo data = dict() data['name'] = response_filename data['report'] = response_file # preparación de parametros mail = EmailClient(empresa) mail.send_report_to_user_with_attach(user_email, data) data = {"status": "ok"} return HttpResponse(json.dumps(data), content_type="application/json") else: logging.info("No se crear el archivo reporte por consulta vacía.") data = {"status": "consulta vacía"} return HttpResponse(json.dumps(data), content_type="application/json")
def export_task(**kwargs): logger.info(kwargs) export_type = kwargs['export_type'] if export_type == 'export_general_email': tipo_receptor = kwargs['tipo_receptor'] empresa = kwargs['empresa'] user_email = kwargs['user_email'] file_name = kwargs['file_name'] date_from = kwargs['date_from'] date_to = kwargs['date_to'] date_from = int(date_from, base=10) date_to = int(date_to, base=10) date_from = timestamp_to_date(date_from) date_to = timestamp_to_date(date_to) params = dict() params['date_from'] = date_from params['date_to'] = date_to params['empresa'] = empresa params['tipo_receptor'] = tipo_receptor # Consulta data = Email.get_emails_by_dates_async(**params) elif export_type == 'export_sended_email': tipo_receptor = kwargs['tipo_receptor'] empresa = kwargs['empresa'] user_email = kwargs['user_email'] file_name = kwargs['file_name'] date_from = kwargs['date_from'] date_to = kwargs['date_to'] date_from = int(date_from, base=10) date_to = int(date_to, base=10) date_from = timestamp_to_date(date_from) date_to = timestamp_to_date(date_to) params = dict() params['date_from'] = date_from params['date_to'] = date_to params['empresa'] = empresa params['tipo_receptor'] = tipo_receptor # Consulta data = Email.get_sended_emails_by_dates_async(**params) elif export_type == 'export_failure_email': tipo_receptor = kwargs['tipo_receptor'] empresa = kwargs['empresa'] user_email = kwargs['user_email'] file_name = kwargs['file_name'] date_from = kwargs['date_from'] date_to = kwargs['date_to'] date_from = int(date_from, base=10) date_to = int(date_to, base=10) date_from = timestamp_to_date(date_from) date_to = timestamp_to_date(date_to) params = dict() params['date_from'] = date_from params['date_to'] = date_to params['empresa'] = empresa params['tipo_receptor'] = tipo_receptor # Consulta data = Email.get_failure_emails_by_dates_async(**params) elif export_type == 'export_dynamic_emails': user_email = kwargs['user_email'] empresa = kwargs['empresa'] file_name = kwargs['file_name'] params = kwargs['params'] params = json.loads(params) logger.info(params) data = Email.get_emails_by_dynamic_query_async(**params) if data is not None: try: logger.info("Existen datos para reporte") # Creación del documento report_file = create_tablib(data, empresa) logger.info("Se ha creado el archivo tablib.") # evaluacion del formato del archivo reporte report_file_format = get_report_file_format(empresa) logger.info("Se va a exportar archivo en formato %s", report_file_format) if report_file_format == 'xlsx': response_file = report_file.xlsx response_filename = file_name elif report_file_format == 'tsv': response_file = report_file.tsv response_filename = file_name else: response_file = report_file.csv response_filename = file_name # evaluar si el archivo es comprimido en zip general_conf = GeneralConfiguration.get_configuration(empresa) logger.info("Se obtiene configuración General de la Empresa") logger.info(general_conf.__dict__) if general_conf is not None and general_conf.report_file_zipped: logger.info("Intentando comprimir archivo reporte") # ejecutar proceso de comprimir reporte in_memory = StringIO() with ZipFile(in_memory, 'w') as archive: archive.writestr(response_filename, str(response_file), ZIP_DEFLATED) response_file = in_memory.getvalue() response_filename = file_name + '.zip' logger.info("Archivo comprimido exitosamente.") # Crear objeto para enviarlo por correo data = dict() data['name'] = response_filename data['report'] = response_file # preparación de parametros mail = EmailClient(empresa) mail.send_report_to_user_with_attach(user_email, data) logger.info("Reportes generado correctamente") except Exception as e: logger.error("Error al enviar correo reporte") logger.error(e) else: logger.info("No se crear el archivo reporte por consulta vacía.")