示例#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_projects():
    """
    Collect all completed :model:`rolodex.Project` that have not yet been archived and
    archive the associated reports. The archived reports are deleted and the new archive
    file is logged in the :model:`rolodex.Archive`.
    """
    # Get the non-archived reports for all projects marked as complete
    report_queryset = Report.objects.select_related("project").filter(
        Q(project__complete=False) & Q(archived=False)
    )
    for report in report_queryset:
        if date.today() >= report.project.end_date - datetime.timedelta(days=90):
            archive_loc = os.path.join(settings.MEDIA_ROOT, "archives")
            evidence_loc = os.path.join(
                settings.MEDIA_ROOT, "evidence", str(report.project.id)
            )
            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.title)
            evidence_path = os.path.join(settings.MEDIA_ROOT)
            template_loc = os.path.join(settings.MEDIA_ROOT, "templates", "template.docx")
            spenny = reportwriter.Reportwriter(
                report, 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()
            with zipfile.ZipFile(zip_buffer, "a") as zf:
                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)
            zip_buffer.seek(0)
            with open(
                os.path.join(archive_loc, report.title + ".zip"), "wb"
            ) as archive_file:
                archive_file.write(zip_buffer.read())
            new_archive = Archive(
                project=report.project,
                report_archive=File(
                    open(os.path.join(archive_loc, report.title + ".zip"), "rb")
                ),
            )
            new_archive.save()
            report.archived = True
            report.complete = True
            report.save()
        else:
            pass
示例#3
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'))
示例#4
0
def archive_projects():
    """Collect all completed projects that have not yet been archived and
    archive the associated reports. The archived reports are deleted and
    the new archive file is logged in the `Archive` model.
    """
    # Get the non-archived reports for all projects marked as complete
    report_queryset = Report.objects.select_related('project').\
        filter(Q(project__complete=False) & Q(archived=False))
    for report in report_queryset:
        if date.today() >= report.project.end_date - datetime.timedelta(
                days=90):
            archive_loc = os.path.join(settings.MEDIA_ROOT, 'archives')
            evidence_loc = os.path.join(settings.MEDIA_ROOT, 'evidence',
                                        str(report.project.id))
            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.title)
            evidence_path = os.path.join(settings.MEDIA_ROOT)
            template_loc = os.path.join(settings.MEDIA_ROOT, 'templates',
                                        'template.docx')
            spenny = reportwriter.Reportwriter(report, 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.title + '.zip'),
                      'wb') as archive_file:
                archive_file.write(zip_buffer.read())
            new_archive = Archive(project=report.project,
                                  report_archive=File(
                                      open(
                                          os.path.join(archive_loc,
                                                       report.title + '.zip'),
                                          'rb')))
            new_archive.save()
            report.archived = True
            report.complete = True
            report.save()
        else:
            pass
示例#5
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')
示例#6
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
示例#7
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