Пример #1
0
def generate_all(request, pk):
    """View function to generate all report types for the specified report."""
    report_instance = Report.objects.get(pk=pk)
    docx_template_loc = os.path.join(settings.TEMPLATE_LOC, 'template.docx')
    pptx_template_loc = os.path.join(settings.TEMPLATE_LOC, 'template.pptx')
    # Ask Spenny to make us reports with these findings
    output_path = os.path.join(settings.MEDIA_ROOT, report_instance.title)
    evidence_path = os.path.join(settings.MEDIA_ROOT)
    template_loc = os.path.join(
        settings.MEDIA_ROOT,
        'templates',
        'template.docx')
    spenny = reportwriter.Reportwriter(
        report_instance,
        output_path,
        evidence_path,
        template_loc)
    json_doc, word_doc, excel_doc, ppt_doc = spenny.generate_all_reports(
        docx_template_loc,
        pptx_template_loc)
    # Create a zip file in memory and add the reports to it
    zip_buffer = io.BytesIO()
    zf = zipfile.ZipFile(zip_buffer, 'a')
    zf.writestr('report.json', json_doc)
    zf.writestr('report.docx', word_doc.getvalue())
    zf.writestr('report.xlsx', excel_doc.getvalue())
    zf.writestr('report.pptx', ppt_doc.getvalue())
    zf.close()
    zip_buffer.seek(0)
    # Return the buffer in the HTTP response
    response = HttpResponse(content_type='application/x-zip-compressed')
    response['Content-Disposition'] = 'attachment; filename=reports.zip'
    response.write(zip_buffer.read())
    return response
Пример #2
0
def archive(request, pk):
    """View function to generate all report types for the specified report and
    then zip all reports and evidence. The archive file is saved is saved in
    the archives directory.
    """
    report_instance = Report.objects.\
        select_related('project', 'project__client').get(pk=pk)
    archive_loc = os.path.join(settings.MEDIA_ROOT, 'archives')
    evidence_loc = os.path.join(settings.MEDIA_ROOT, 'evidence', str(pk))
    docx_template_loc = os.path.join(
        settings.MEDIA_ROOT,
        'templates',
        'template.docx')
    pptx_template_loc = os.path.join(
        settings.MEDIA_ROOT,
        'templates',
        'template.pptx')
    # Ask Spenny to make us reports with these findings
    output_path = os.path.join(settings.MEDIA_ROOT, report_instance.title)
    evidence_path = os.path.join(settings.MEDIA_ROOT)
    template_loc = os.path.join(
        settings.MEDIA_ROOT,
        'templates',
        'template.docx')
    spenny = reportwriter.Reportwriter(
        report_instance,
        output_path,
        evidence_path,
        template_loc)
    json_doc, word_doc, excel_doc, ppt_doc = spenny.generate_all_reports(
        docx_template_loc,
        pptx_template_loc)
    # Create a zip file in memory and add the reports to it
    zip_buffer = io.BytesIO()
    zf = zipfile.ZipFile(zip_buffer, 'a')
    zf.writestr('report.json', json_doc)
    zf.writestr('report.docx', word_doc.getvalue())
    zf.writestr('report.xlsx', excel_doc.getvalue())
    zf.writestr('report.pptx', ppt_doc.getvalue())
    zip_directory(evidence_loc, zf)
    zf.close()
    zip_buffer.seek(0)
    with open(os.path.join(
        archive_loc,
        report_instance.title + '.zip'),
      'wb') as archive_file:
        archive_file.write(zip_buffer.read())
        new_archive = Archive(
            client=report_instance.project.client,
            report_archive=File(open(os.path.join(
                archive_loc,
                report_instance.title + '.zip'), 'rb')))
    new_archive.save()
    messages.success(request, '%s has been archived successfully.' %
                     report_instance.title, extra_tags='alert-success')
    return HttpResponseRedirect(reverse('reporting:archived_reports'))
Пример #3
0
def generate_json(request, pk):
    """View function to generate a json report for the specified report."""
    report_instance = Report.objects.get(pk=pk)
    # Ask Spenny to make us a report with these findings
    output_path = os.path.join(settings.MEDIA_ROOT, report_instance.title)
    evidence_path = os.path.join(settings.MEDIA_ROOT)
    template_loc = None
    spenny = reportwriter.Reportwriter(report_instance, output_path,
                                       evidence_path, template_loc)
    json = spenny.generate_json()
    return HttpResponse(json, 'application/json')
Пример #4
0
def generate_pptx(request, pk):
    """View function to generate a pptx report for the specified report."""
    report_instance = Report.objects.get(pk=pk)
    # Ask Spenny to make us a report with these findings
    output_path = os.path.join(settings.MEDIA_ROOT, report_instance.title)
    evidence_path = os.path.join(settings.MEDIA_ROOT)
    template_loc = os.path.join(settings.TEMPLATE_LOC, 'template.pptx')
    spenny = reportwriter.Reportwriter(report_instance, output_path,
                                       evidence_path, template_loc)
    pptx = spenny.generate_powerpoint_pptx()
    response = HttpResponse(
        content_type='application/application/vnd.openxmlformats-'
        'officedocument.presentationml.presentation')
    response['Content-Disposition'] = 'attachment; filename=report.pptx'
    pptx.save(response)
    return response
Пример #5
0
def generate_xlsx(request, pk):
    """View function to generate a xlsx report for the specified report."""
    report_instance = Report.objects.get(pk=pk)
    # Ask Spenny to make us a report with these findings
    output_path = os.path.join(settings.MEDIA_ROOT, report_instance.title)
    evidence_path = os.path.join(settings.MEDIA_ROOT)
    template_loc = None
    spenny = reportwriter.Reportwriter(report_instance, output_path,
                                       evidence_path, template_loc)
    output = io.BytesIO()
    workbook = Workbook(output, {'in_memory': True})
    spenny.generate_excel_xlsx(workbook)
    output.seek(0)
    response = HttpResponse(
        output.read(),
        content_type='application/application/vnd.openxmlformats-'
        'officedocument.spreadsheetml.sheet')
    response['Content-Disposition'] = 'attachment; filename=report.xlsx'
    output.close()
    return response