Example #1
0
def custom_report(request):
    # saving the report
    form = CustomReportJsonForm(request.POST)

    if form.is_valid():
        selected_widgets = report_widget_factory(
            json_data=request.POST['json'],
            request=request,
            user=request.user,
            finding_notes=False)
        report_name = 'Custom PDF Report: ' + request.user.username
        report_format = 'AsciiDoc'
        finding_notes = True

        if 'report-options' in selected_widgets:
            options = selected_widgets['report-options']
            report_name = 'Custom PDF Report: ' + options.report_name
            report_format = options.report_type
            finding_notes = (options.include_finding_notes == '1')

        selected_widgets = report_widget_factory(
            json_data=request.POST['json'],
            request=request,
            user=request.user,
            finding_notes=finding_notes)

        if report_format == 'PDF':
            report = Report(name=report_name,
                            type="Custom",
                            format=report_format,
                            requester=request.user,
                            task_id='tbd',
                            options=request.POST['json'])
            report.save()
            async_custom_pdf_report.delay(
                report=report,
                template="dojo/custom_pdf_report.html",
                filename="custom_pdf_report.pdf",
                host=request.scheme + "://" + request.META['HTTP_HOST'],
                user=request.user,
                uri=request.build_absolute_uri(report.get_url()),
                finding_notes=finding_notes)
            messages.add_message(
                request,
                messages.SUCCESS,
                'Your report is building, you will receive an email when it is ready.',
                extra_tags='alert-success')

            return HttpResponseRedirect(reverse('reports'))
        elif report_format == 'AsciiDoc':
            widgets = selected_widgets.values()
            return render(request, 'dojo/custom_asciidoc_report.html', {
                "widgets": widgets,
                "finding_notes": finding_notes
            })
        else:
            return HttpResponseForbidden()
    else:
        return HttpResponseForbidden()
Example #2
0
def custom_report(request):
    # saving the report
    form = CustomReportJsonForm(request.POST)
    host = report_url_resolver(request)
    if form.is_valid():
        selected_widgets = report_widget_factory(json_data=request.POST['json'], request=request, user=request.user,
                                                 finding_notes=False, finding_images=False, host=host)
        report_name = 'Custom PDF Report: ' + request.user.username
        report_format = 'AsciiDoc'
        finding_notes = True
        finding_images = True

        if 'report-options' in selected_widgets:
            options = selected_widgets['report-options']
            report_name = 'Custom PDF Report: ' + options.report_name
            report_format = options.report_type
            finding_notes = (options.include_finding_notes == '1')
            finding_images = (options.include_finding_images == '1')

        selected_widgets = report_widget_factory(json_data=request.POST['json'], request=request, user=request.user,
                                                 finding_notes=finding_notes, finding_images=finding_images, host=host)

        if report_format == 'PDF':
            report = Report(name=report_name,
                            type="Custom",
                            format=report_format,
                            requester=request.user,
                            task_id='tbd',
                            options=request.POST['json'])
            report.save()
            async_custom_pdf_report.delay(report=report,
                                          template="dojo/custom_pdf_report.html",
                                          filename="custom_pdf_report.pdf",
                                          host=host,
                                          user=request.user,
                                          uri=request.build_absolute_uri(report.get_url()),
                                          finding_notes=finding_notes,
                                          finding_images=finding_images)
            messages.add_message(request, messages.SUCCESS,
                                 'Your report is building, you will receive an email when it is ready.',
                                 extra_tags='alert-success')

            return HttpResponseRedirect(reverse('reports'))
        elif report_format == 'AsciiDoc':
            widgets = selected_widgets.values()
            return render(request,
                          'dojo/custom_asciidoc_report.html',
                          {"widgets": widgets,
                           "host": host,
                           "finding_notes": finding_notes,
                           "finding_images": finding_images,
                           "user_id": request.user.id})
        else:
            return HttpResponseForbidden()
    else:
        return HttpResponseForbidden()
Example #3
0
def custom_report(request):
    # saving the report
    form = CustomReportJsonForm(request.POST)
    host = report_url_resolver(request)
    if form.is_valid():
        selected_widgets = report_widget_factory(
            json_data=request.POST['json'],
            request=request,
            user=request.user,
            finding_notes=False,
            finding_images=False,
            host=host)
        report_format = 'AsciiDoc'
        finding_notes = True
        finding_images = True

        if 'report-options' in selected_widgets:
            options = selected_widgets['report-options']
            report_format = options.report_type
            finding_notes = (options.include_finding_notes == '1')
            finding_images = (options.include_finding_images == '1')

        selected_widgets = report_widget_factory(
            json_data=request.POST['json'],
            request=request,
            user=request.user,
            finding_notes=finding_notes,
            finding_images=finding_images,
            host=host)

        if report_format == 'AsciiDoc':
            widgets = list(selected_widgets.values())
            return render(
                request, 'dojo/custom_asciidoc_report.html', {
                    "widgets": widgets,
                    "host": host,
                    "finding_notes": finding_notes,
                    "finding_images": finding_images,
                    "user_id": request.user.id
                })
        elif report_format == 'HTML':
            widgets = list(selected_widgets.values())
            return render(
                request, 'dojo/custom_html_report.html', {
                    "widgets": widgets,
                    "host": host,
                    "finding_notes": finding_notes,
                    "finding_images": finding_images,
                    "user_id": request.user.id
                })
        else:
            return HttpResponseForbidden()
    else:
        return HttpResponseForbidden()
Example #4
0
def async_custom_pdf_report(self,
                            report=None,
                            template="None",
                            filename='report.pdf',
                            host=None,
                            user=None,
                            uri=None,
                            finding_notes=False,
                            finding_images=False):
    config = pdfkit.configuration(wkhtmltopdf=settings.WKHTMLTOPDF_PATH)

    selected_widgets = report_widget_factory(json_data=report.options, request=None, user=user,
                                             finding_notes=finding_notes, finding_images=finding_images, host=host)

    widgets = list(selected_widgets.values())
    temp = None

    try:
        report.task_id = async_custom_pdf_report.request.id
        report.save()

        toc = None
        toc_depth = 4

        if 'table-of-contents' in selected_widgets:
            xsl_style_sheet_tempalte = "dojo/pdf_toc.xsl"
            temp = tempfile.NamedTemporaryFile()

            toc_settings = selected_widgets['table-of-contents']

            toc_depth = toc_settings.depth
            toc_bytes = render_to_string(xsl_style_sheet_tempalte, {'widgets': widgets,
                                                                    'depth': toc_depth,
                                                                    'title': toc_settings.title})
            temp.write(toc_bytes)
            temp.seek(0)

            toc = {'toc-header-text': toc_settings.title,
                   'xsl-style-sheet': temp.name}

        # default the cover to not come first by default
        cover_first_val = False

        cover = None
        if 'cover-page' in selected_widgets:
            cover_first_val = True
            cp = selected_widgets['cover-page']
            x = urlencode({'title': cp.title,
                           'subtitle': cp.sub_heading,
                           'info': cp.meta_info})
            cover = host + reverse(
                'report_cover_page') + "?" + x
        bytes = render_to_string(template, {'widgets': widgets,
                                            'toc_depth': toc_depth,
                                            'host': host,
                                            'report_name': report.name})
        pdf = pdfkit.from_string(bytes,
                                 False,
                                 configuration=config,
                                 toc=toc,
                                 cover=cover,
                                 cover_first=cover_first_val)

        if report.file.name:
            with open(report.file.path, 'w') as f:
                f.write(pdf)
            f.close()
        else:
            f = ContentFile(pdf)
            report.file.save(filename, f)
        report.status = 'success'
        report.done_datetime = timezone.now()
        report.save()

        create_notification(event='report_created', title='Report created', description='The report "%s" is ready.' % report.name, url=uri, report=report, objowner=report.requester)
    except Exception as e:
        report.status = 'error'
        report.save()
        # email_requester(report, uri, error=e)
        # raise e
        log_generic_alert("PDF Report", "Report Creation Failure", "Make sure WKHTMLTOPDF is installed. " + str(e))
    finally:
        if temp is not None:
            # deleting temp xsl file
            temp.close()

    return True
Example #5
0
def async_custom_pdf_report(self,
                            report=None,
                            template="None",
                            filename='report.pdf',
                            host=None,
                            user=None,
                            uri=None,
                            finding_notes=False,
                            finding_images=False):
    config = pdfkit.configuration(wkhtmltopdf=settings.WKHTMLTOPDF_PATH)

    selected_widgets = report_widget_factory(json_data=report.options,
                                             request=None,
                                             user=user,
                                             finding_notes=finding_notes,
                                             finding_images=finding_images,
                                             host=host)

    widgets = selected_widgets.values()
    temp = None

    try:
        report.task_id = async_custom_pdf_report.request.id
        report.save()

        toc = None
        toc_depth = 4

        if 'table-of-contents' in selected_widgets:
            xsl_style_sheet_tempalte = "dojo/pdf_toc.xsl"
            temp = tempfile.NamedTemporaryFile()

            toc_settings = selected_widgets['table-of-contents']

            toc_depth = toc_settings.depth

            toc_bytes = render_to_string(
                xsl_style_sheet_tempalte, {
                    'widgets': widgets,
                    'depth': toc_depth,
                    'title': toc_settings.title
                })

            temp.write(toc_bytes)
            temp.seek(0)

            toc = {
                'toc-header-text': toc_settings.title,
                'xsl-style-sheet': temp.name
            }

        cover = None
        if 'cover-page' in selected_widgets:
            cp = selected_widgets['cover-page']
            x = urlencode({
                'title': cp.title,
                'subtitle': cp.sub_heading,
                'info': cp.meta_info
            })
            cover = host + reverse('report_cover_page') + "?" + x

        bytes = render_to_string(
            template, {
                'widgets': widgets,
                'toc_depth': toc_depth,
                'host': host,
                'report_name': report.name
            })

        pdf = pdfkit.from_string(
            bytes,
            False,
            configuration=config,
            cover=cover,
            toc=toc,
        )

        if report.file.name:
            with open(report.file.path, 'w') as f:
                f.write(pdf)
            f.close()
        else:
            f = ContentFile(pdf)
            report.file.save(filename, f)
        report.status = 'success'
        report.done_datetime = datetime.now(tz=localtz)
        report.save()
        # email_requester(report, uri)
    except Exception as e:
        report.status = 'error'
        report.save()
        # email_requester(report, uri, error=e)
        raise e
    finally:
        if temp is not None:
            # deleting temp xsl file
            temp.close()

    return True
Example #6
0
def async_custom_pdf_report(self,
                            report=None,
                            template="None",
                            filename='report.pdf',
                            host=None,
                            user=None,
                            uri=None,
                            finding_notes=False,
                            finding_images=False):
    config = pdfkit.configuration(wkhtmltopdf=settings.WKHTMLTOPDF_PATH)

    selected_widgets = report_widget_factory(json_data=report.options, request=None, user=user,
                                             finding_notes=finding_notes, finding_images=finding_images, host=host)

    widgets = selected_widgets.values()
    temp = None

    try:
        report.task_id = async_custom_pdf_report.request.id
        report.save()

        toc = None
        toc_depth = 4

        if 'table-of-contents' in selected_widgets:
            xsl_style_sheet_tempalte = "dojo/pdf_toc.xsl"
            temp = tempfile.NamedTemporaryFile()

            toc_settings = selected_widgets['table-of-contents']

            toc_depth = toc_settings.depth

            toc_bytes = render_to_string(xsl_style_sheet_tempalte, {'widgets': widgets,
                                                                    'depth': toc_depth,
                                                                    'title': toc_settings.title})

            temp.write(toc_bytes)
            temp.seek(0)

            toc = {'toc-header-text': toc_settings.title,
                   'xsl-style-sheet': temp.name}

        cover = None
        if 'cover-page' in selected_widgets:
            cp = selected_widgets['cover-page']
            x = urlencode({'title': cp.title,
                           'subtitle': cp.sub_heading,
                           'info': cp.meta_info})
            cover = host + reverse(
                'report_cover_page') + "?" + x

        bytes = render_to_string(template, {'widgets': widgets,
                                            'toc_depth': toc_depth,
                                            'host': host,
                                            'report_name': report.name})

        pdf = pdfkit.from_string(bytes,
                                 False,
                                 configuration=config,
                                 cover=cover,
                                 toc=toc,
                                 )

        if report.file.name:
            with open(report.file.path, 'w') as f:
                f.write(pdf)
            f.close()
        else:
            f = ContentFile(pdf)
            report.file.save(filename, f)
        report.status = 'success'
        report.done_datetime = datetime.now(tz=localtz)
        report.save()
        # email_requester(report, uri)
    except Exception as e:
        report.status = 'error'
        report.save()
        # email_requester(report, uri, error=e)
        raise e
    finally:
        if temp is not None:
            # deleting temp xsl file
            temp.close()

    return True
Example #7
0
def async_custom_pdf_report(self,
                            report=None,
                            template="None",
                            filename='report.pdf',
                            host=None,
                            user=None,
                            uri=None,
                            finding_notes=False,
                            finding_images=False):
    config = pdfkit.configuration(wkhtmltopdf=settings.WKHTMLTOPDF_PATH)

    selected_widgets = report_widget_factory(json_data=report.options, request=None, user=user,
                                             finding_notes=finding_notes, finding_images=finding_images, host=host)

    widgets = selected_widgets.values()
    temp = None

    try:
        report.task_id = async_custom_pdf_report.request.id
        report.save()

        toc = None
        toc_depth = 4

        if 'table-of-contents' in selected_widgets:
            xsl_style_sheet_tempalte = "dojo/pdf_toc.xsl"
            temp = tempfile.NamedTemporaryFile()

            toc_settings = selected_widgets['table-of-contents']

            toc_depth = toc_settings.depth
            toc_bytes = render_to_string(xsl_style_sheet_tempalte, {'widgets': widgets,
                                                                    'depth': toc_depth,
                                                                    'title': toc_settings.title})
            temp.write(toc_bytes)
            temp.seek(0)

            toc = {'toc-header-text': toc_settings.title,
                   'xsl-style-sheet': temp.name}

        # default the cover to not come first by default
        cover_first_val = False

        cover = None
        if 'cover-page' in selected_widgets:
            cover_first_val = True
            cp = selected_widgets['cover-page']
            x = urlencode({'title': cp.title,
                           'subtitle': cp.sub_heading,
                           'info': cp.meta_info})
            cover = host + reverse(
                'report_cover_page') + "?" + x
        bytes = render_to_string(template, {'widgets': widgets,
                                            'toc_depth': toc_depth,
                                            'host': host,
                                            'report_name': report.name})
        pdf = pdfkit.from_string(bytes,
                                 False,
                                 configuration=config,
                                 toc=toc,
                                 cover=cover,
                                 cover_first=cover_first_val)

        if report.file.name:
            with open(report.file.path, 'w') as f:
                f.write(pdf)
            f.close()
        else:
            f = ContentFile(pdf)
            report.file.save(filename, f)
        report.status = 'success'
        report.done_datetime = timezone.now()
        report.save()

        create_notification(event='report_created', title='Report created', description='The report "%s" is ready.' % report.name, url=uri, report=report, objowner=report.requester)
    except Exception as e:
        report.status = 'error'
        report.save()
        # email_requester(report, uri, error=e)
        # raise e
        log_generic_alert("PDF Report", "Report Creation Failure", "Make sure WKHTMLTOPDF is installed. " + str(e))
    finally:
        if temp is not None:
            # deleting temp xsl file
            temp.close()

    return True