Example #1
0
 def post(self, request, *args, **kwargs):
     logging.info("Entrando a la cola de envío de emails.")
     logging.info(request.body)
     try:
         # recibir parametros de la cola
         email_id = request.POST.get('email_id')
         email_id = int(email_id, base=10)
         empresa_id = request.POST.get('empresa_id')
         # enviar correo
         email_client = EmailClient(empresa_id)
         email_client.enviar_correo_dte(email_id)
         return HttpResponse()
     except Exception, e:
         logging.error(e)
Example #2
0
    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")