Exemplo n.º 1
0
def export_response(file, format, filename, checkpoint=None):
    """
    Get an http response for an export
    file can be either a io.BytesIO or io.StringIO
    or an open file object (which this function is responsible for closing)

    """
    from couchexport.export import Format
    if not filename:
        filename = "NAMELESS EXPORT"

    format = Format.from_format(format)
    if isinstance(file, TempBase):
        file = file.file

    if isinstance(file, (io.BytesIO, io.StringIO)):
        response = HttpResponse(file.getvalue(), content_type=format.mimetype)
        # I don't know why we need to close the file. Keeping around.
        file.close()
    else:
        response = StreamingHttpResponse(FileWrapper(file), content_type=format.mimetype)

    if format.download:
        from corehq.util.files import safe_filename_header
        response['Content-Disposition'] = safe_filename_header(filename, format.extension)

    if checkpoint:
        response['X-CommCareHQ-Export-Token'] = checkpoint.get_id

    return response
Exemplo n.º 2
0
def export_response(file, format, filename, checkpoint=None):
    """
    Get an http response for an export
    file can be either a io.BytesIO or io.StringIO
    or an open file object (which this function is responsible for closing)

    """
    from couchexport.export import Format
    if not filename:
        filename = "NAMELESS EXPORT"

    format = Format.from_format(format)
    if isinstance(file, TempBase):
        file = file.file

    if isinstance(file, (io.BytesIO, io.StringIO)):
        response = HttpResponse(file.getvalue(), content_type=format.mimetype)
        # I don't know why we need to close the file. Keeping around.
        file.close()
    else:
        response = StreamingHttpResponse(FileWrapper(file),
                                         content_type=format.mimetype)

    if format.download:
        from corehq.util.files import safe_filename_header
        response['Content-Disposition'] = safe_filename_header(
            filename, format.extension)

    if checkpoint:
        response['X-CommCareHQ-Export-Token'] = checkpoint.get_id

    return response
Exemplo n.º 3
0
def _render_report_configs(request, configs, domain, owner_id, couch_user, email, notes=None, attach_excel=False):
    from dimagi.utils.web import get_url_base

    report_outputs = []
    excel_attachments = []
    format = Format.from_format(request.GET.get('format') or Format.XLS_2007)
    for config in configs:
        content, excel_file = config.get_report_content(attach_excel=attach_excel)
        if excel_file:
            excel_attachments.append({
                'title': config.full_name + "." + format.extension,
                'file_obj': excel_file,
                'mimetype': format.mimetype
            })
        report_outputs.append({
            'title': config.full_name,
            'url': config.url,
            'content': content,
            'description': config.description,
        })

    date_range = config.get_date_range()
    return render(request, "reports/report_email.html", {
        "reports": report_outputs,
        "domain": domain,
        "couch_user": owner_id,
        "DNS_name": get_url_base(),
        "owner_name": couch_user.full_name or couch_user.get_email(),
        "email": email,
        "notes": notes,
        "startdate": date_range["startdate"] if date_range else "",
        "enddate": date_range["enddate"] if date_range else "",
    }), excel_attachments
Exemplo n.º 4
0
def _render_report_configs(request, configs, domain, owner_id, couch_user, email, notes=None, attach_excel=False):
    from dimagi.utils.web import get_url_base

    report_outputs = []
    excel_attachments = []
    format = Format.from_format(request.GET.get('format') or Format.XLS_2007)
    for config in configs:
        content, excel_file = config.get_report_content(attach_excel=attach_excel)
        if excel_file:
            excel_attachments.append({
                'title': config.full_name + "." + format.extension,
                'file_obj': excel_file,
                'mimetype': format.mimetype
            })
        report_outputs.append({
            'title': config.full_name,
            'url': config.url,
            'content': content,
            'description': config.description,
        })

    date_range = config.get_date_range()
    return render(request, "reports/report_email.html", {
        "reports": report_outputs,
        "domain": domain,
        "couch_user": owner_id,
        "DNS_name": get_url_base(),
        "owner_name": couch_user.full_name or couch_user.get_email(),
        "email": email,
        "notes": notes,
        "startdate": date_range["startdate"] if date_range else "",
        "enddate": date_range["enddate"] if date_range else "",
    }), excel_attachments
Exemplo n.º 5
0
def download_saved_export(request, export_id):
    export = SavedBasicExport.get(export_id)
    content_type = Format.from_format(export.configuration.format).mimetype
    payload = export.get_payload(stream=True)
    response = StreamingHttpResponse(FileWrapper(payload), content_type=content_type)
    if export.configuration.format != 'html':
        # ht: http://stackoverflow.com/questions/1207457/convert-unicode-to-string-in-python-containing-extra-symbols
        normalized_filename = unicodedata.normalize(
            'NFKD', unicode(export.configuration.filename),
        ).encode('ascii', 'ignore')
        response['Content-Disposition'] = 'attachment; filename="%s"' % normalized_filename
    return response
Exemplo n.º 6
0
def download_cases(request, domain):
    include_closed = json.loads(request.GET.get('include_closed', 'false'))
    try:
        format = Format.from_format(
            request.GET.get('format') or Format.XLS_2007)
    except URLError as e:
        return HttpResponseBadRequest(e.reason)
    group = request.GET.get('group', None)
    user_filter, _ = UserTypeFilter.get_user_filter(request)

    async = request.GET.get('async') == 'true'

    kwargs = {
        'domain': domain,
        'include_closed': include_closed,
        'format': format,
        'group': group,
        'user_filter': user_filter,
    }
    payload_func = SerializableFunction(generate_case_export_payload, **kwargs)
    content_disposition = 'attachment; filename="{domain}_data.{ext}"'.format(
        domain=domain, ext=format.extension)
    mimetype = "%s" % format.mimetype

    def generate_payload(payload_func):
        if async:
            download = DownloadBase()
            a_task = prepare_download.delay(download.download_id, payload_func,
                                            content_disposition, mimetype)
            download.set_task(a_task)
            return download.get_start_response()
        else:
            payload = payload_func()
            response = HttpResponse(payload)
            response['Content-Type'] = mimetype
            response['Content-Disposition'] = content_disposition
            return response

    return generate_payload(payload_func)
Exemplo n.º 7
0
def export_response(file, format, filename, checkpoint=None):
    """
    Get an http response for an export
    file can be either a StringIO
    or an open file object (which this function is responsible for closing)

    """
    from couchexport.export import Format
    if not filename:
        filename = "NAMELESS EXPORT"

    format = Format.from_format(format)
    if isinstance(file, TempBase):
        file = file.file

    if isinstance(file, StringIO):
        response = HttpResponse(file.getvalue(), content_type=format.mimetype)
        # I don't know why we need to close the file. Keeping around.
        file.close()
    else:
        response = StreamingHttpResponse(FileWrapper(file),
                                         content_type=format.mimetype)

    if format.download:
        try:
            filename = unidecode(filename)
        except Exception:
            logging.exception("Error with filename: %r" % filename)
            filename = "data"
        finally:
            response[
                'Content-Disposition'] = 'attachment; filename="{filename}.{format.extension}"'.format(
                    filename=filename, format=format)

    if checkpoint:
        response['X-CommCareHQ-Export-Token'] = checkpoint.get_id

    return response
Exemplo n.º 8
0
def export_response(file, format, filename, checkpoint=None):
    """
    Get an http response for an export
    file can be either a StringIO
    or an open file object (which this function is responsible for closing)

    """
    from couchexport.export import Format
    if not filename:
        filename = "NAMELESS EXPORT"

    format = Format.from_format(format)
    if isinstance(file, TempBase):
        file = file.file

    if isinstance(file, StringIO):
        response = HttpResponse(file.getvalue(), content_type=format.mimetype)
        # I don't know why we need to close the file. Keeping around.
        file.close()
    else:
        response = StreamingHttpResponse(FileWrapper(file), content_type=format.mimetype)

    if format.download:
        try:
            filename = unidecode(filename)
        except Exception:
            logging.exception("Error with filename: %r" % filename)
            filename = "data"
        finally:
            response['Content-Disposition'] = 'attachment; filename="{filename}.{format.extension}"'.format(
                filename=filename,
                format=format
            )

    if checkpoint:
        response['X-CommCareHQ-Export-Token'] = checkpoint.get_id

    return response
Exemplo n.º 9
0
def download_cases(request, domain):
    include_closed = json.loads(request.GET.get('include_closed', 'false'))
    try:
        format = Format.from_format(request.GET.get('format') or Format.XLS_2007)
    except URLError as e:
        return HttpResponseBadRequest(e.reason)
    group = request.GET.get('group', None)
    user_filter, _ = UserTypeFilter.get_user_filter(request)

    async = request.GET.get('async') == 'true'

    kwargs = {
        'domain': domain,
        'include_closed': include_closed,
        'format': format,
        'group': group,
        'user_filter': user_filter,
    }
    payload_func = SerializableFunction(generate_case_export_payload, **kwargs)
    content_disposition = 'attachment; filename="{domain}_data.{ext}"'.format(domain=domain, ext=format.extension)
    mimetype = "%s" % format.mimetype

    def generate_payload(payload_func):
        if async:
            download = DownloadBase()
            a_task = prepare_download.delay(download.download_id, payload_func,
                                            content_disposition, mimetype)
            download.set_task(a_task)
            return download.get_start_response()
        else:
            payload = payload_func()
            response = HttpResponse(payload)
            response['Content-Type'] = mimetype
            response['Content-Disposition'] = content_disposition
            return response

    return generate_payload(payload_func)
Exemplo n.º 10
0
def export_response(file, format, filename, checkpoint=None):
    """
    Get an http response for an export
    """
    from couchexport.export import Format
    if not filename:
        filename = "NAMELESS EXPORT"
        
    format = Format.from_format(format)

    if not format.download:
        # For direct-display formats like HTML and JSON, don't attach the file, just send it directly.
        response = HttpResponse(file.getvalue(), mimetype=format.mimetype)
        if checkpoint:
            response['X-CommCareHQ-Export-Token'] = checkpoint.get_id
        return response


    response = HttpResponse(mimetype=format.mimetype)

    try:
        filename = unidecode(filename)
    except Exception:
        logging.exception("Error with filename: %r" % filename)
        filename = "data"
    finally:
        response['Content-Disposition'] = 'attachment; filename="{filename}.{format.extension}"'.format(
            filename=filename,
            format=format
        )

    if checkpoint:
        response['X-CommCareHQ-Export-Token'] = checkpoint.get_id
    response.write(file.getvalue())
    file.close()
    return response
Exemplo n.º 11
0
def download_cases(request, domain):
    include_closed = json.loads(request.GET.get("include_closed", "false"))
    try:
        format = Format.from_format(request.GET.get("format") or Format.XLS_2007)
    except URLError as e:
        return HttpResponseBadRequest(e.reason)
    group = request.GET.get("group", None)
    user_filter, _ = UserTypeFilter.get_user_filter(request)

    async = request.GET.get("async") == "true"

    kwargs = {
        "domain": domain,
        "include_closed": include_closed,
        "format": format,
        "group": group,
        "user_filter": user_filter,
    }
    payload_func = SerializableFunction(generate_case_export_payload, **kwargs)
    content_disposition = 'attachment; filename="{domain}_data.{ext}"'.format(domain=domain, ext=format.extension)
    mimetype = "%s" % format.mimetype

    def generate_payload(payload_func):
        if async:
            download = DownloadBase()
            a_task = prepare_download.delay(download.download_id, payload_func, content_disposition, mimetype)
            download.set_task(a_task)
            return download.get_start_response()
        else:
            payload = payload_func()
            response = HttpResponse(payload)
            response["Content-Type"] = mimetype
            response["Content-Disposition"] = content_disposition
            return response

    return generate_payload(payload_func)