示例#1
0
def download_spreadsheet(request, report_id, file_type) -> HttpResponse:
    account = get_account(request)

    filename, spreadsheet = create_spreadsheet(account=account, report_id=report_id)

    if not spreadsheet:
        return JsonResponse({}, encoder=JSEncoder)

    if file_type == "xlsx":
        # todo: requesting infinite files will flood the system as temp files are saved. Probably load file into
        #   memory and then remove the original file. With the current group of users the risk is minimal, so no bother
        tmp_file_handle = upgrade_excel_spreadsheet(spreadsheet)
        with open(tmp_file_handle.name, 'rb') as fh:
            response = HttpResponse(fh.read(),
                                    content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
            response["Content-Disposition"] = "attachment; filename=%s.xlsx" % slugify(filename)
        return response

    if file_type == "ods":
        output = excel.make_response(spreadsheet, file_type)
        output["Content-Disposition"] = "attachment; filename=%s.ods" % slugify(filename)
        output["Content-type"] = "application/vnd.oasis.opendocument.spreadsheet"
        return output

    if file_type == "csv":
        output = excel.make_response(spreadsheet, file_type)
        output["Content-Disposition"] = "attachment; filename=%s.csv" % slugify(filename)
        output["Content-type"] = "text/csv"
        return output

    # anything that is not valid at all.
    return JsonResponse({}, encoder=JSEncoder)
def test_report_to_spreadsheet(db) -> None:
    account, url, endpoint, scan = make_url_with_endpoint_and_scan()

    urllist = get_or_create_list_by_name(account, "test list 1")
    _add_to_urls_to_urllist(account, urllist, [url])
    create_url_reports(url)
    create_scan_report(account, urllist)

    # make sure there is a urllistreport to get a spreadsheet from
    assert UrlListReport.objects.all().count() == 1

    filename, spreadsheet = create_spreadsheet(account=account,
                                               report_id=urllist.pk)

    # there should be a spreadsheet
    assert spreadsheet

    tmp_file_handle = upgrade_excel_spreadsheet(spreadsheet)

    # and there should be a file handle
    assert tmp_file_handle
示例#3
0
 def handle(self, *args, **options):
     from dashboard.internet_nl_dashboard.logic.report_to_spreadsheet import create_spreadsheet
     create_spreadsheet(41)