Skip to main content

Report classes using reportlab/pdf for clinicedc/edc projects

Project description

pypi actions codecov downloads

edc-pdf-reports

Reportlab/PDF classes for clinicedc/edc projects

Overview

This module offers functionality to link a PDF report to a model registered with ModelAdmin.

The CrfPdfReport class links a PDF report to a model registered with ModelAdmin. A link as is added to the changelist that opens an intermediate page to ask for a password. From the intermediate page a secure file is downloaded into the browser. See also PdfIntermediateView and PrintPdfReportView.

For this to work, you need to:

  • create a pdf report class;

  • declare the model with the PdfReportModelMixin and set the pdf_report_cls attr on the model;

  • declare the model’s ModelAdmin class with PdfButtonModelAdminMixin;

  • add print_to_pdf_action to Modeladmin.actions (required to print one or more pdfs using actions);

  • add “pdf_button” to the list_display (required for pdf button to appear on each row);

  • update your app’s urls;

  • add edc_pdf_reports to INSTALLED_APPS.

Your changelist will include options for printing one or many PDF reports into a password protected and secure PDF file.

DeathReport as an example

edc_adverse_event has this configured for its DeathReport model. Let’s use this as an example.

Create the DeathReport model:

# models.py

class DeathReport(PdfReportModelMixin, BaseUuidModel):

    pdf_report_cls = DeathPdfReport

Create the DeathPdfReport class. DeathPdfReport inherits from CrfPdfReport. Link the model and changelist_url` to this PDF report class.

# death_pdf_report.py

class DeathPdfReport(CrfPdfReport):
    model = f"{get_adverse_event_app_label()}.deathreport"
    changelist_url = (
        f"{get_adverse_event_app_label()}_admin:{get_adverse_event_app_label()}_"
        "deathreport_changelist"
    )

    def get_report_story(self, **kwargs):
        ...

Declare the ModelAdmin class with PdfButtonModelAdminMixin:

# admin.py

class DeathReportModelAdmin(PdfButtonModelAdminMixin, DeathReportModelAdminMixin):
    actions = [print_to_pdf_action]
    list_display = ["subject_identifier", "pdf_button", ...]

Update your url patterns:

# urls.py
url_patterns = [
    ...,
    *paths_for_urlpatterns("edc_pdf_reports"),
    ...]

Add to settings:

# settings.py
INSTALLED_APPS = [
    ...,
    "edc_pdf_reports.apps.AppConfig"
    ...]

Your changelist will have the new column “PDF” and the print as pdf action will be available.

ChangeList

The intermediate page, linked from the changelist, will look like this:

Intermediate page

Note the passphrase and click “Create File”. The file will be created in the view and downloaded by the browser.

Creating a PDF file outside of the view

The view PrintPdfReportView uses function write_queryset_to_secure_pdf to create a PDF. You can access this function directly.

For example:

import mempass
import tempfile
from pathlib import Path
from django.contrib.auth.models import User
from edc_pdf_reports.utils import write_queryset_to_secure_pdf, write_model_to_insecure_pdf
from effect_ae.models import DeathReport

dir = tempfile.mkdtemp()
p = Path(dir)
qs = DeathReport.objects.all()
user = User.objects.get(username="erikvw")

# create a secure PDF file for the queryset
q = p / "death_reports_secure.pdf"
password = mempass.mkpassword(2)
buffer = write_queryset_to_secure_pdf(queryset=qs, password=password, user=user)
q.write_bytes(buffer.getbuffer())
print(q)

# create an insecure PDF file for one model instance
q = p / "death_reports_insecure.pdf"
model_obj = qs[0]
buffer = write_model_to_insecure_pdf(model_obj, user=user)
q.write_bytes(buffer.getbuffer())
print(q)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

edc-pdf-reports-0.3.18.tar.gz (120.9 kB view details)

Uploaded Source

Built Distribution

edc_pdf_reports-0.3.18-py3-none-any.whl (42.2 kB view details)

Uploaded Python 3

File details

Details for the file edc-pdf-reports-0.3.18.tar.gz.

File metadata

  • Download URL: edc-pdf-reports-0.3.18.tar.gz
  • Upload date:
  • Size: 120.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for edc-pdf-reports-0.3.18.tar.gz
Algorithm Hash digest
SHA256 9c323dda6a0eb65a6b344ce0ffa2e7e36789f46e697a1a2b8e58d53c8a07fc13
MD5 fdaa1cdff0ba74ff78c82eef929d7c8b
BLAKE2b-256 aead353c70df7ec70c3d77727e9a8c5fc8bf5ede998966d7e3e9c85bdeb76153

See more details on using hashes here.

File details

Details for the file edc_pdf_reports-0.3.18-py3-none-any.whl.

File metadata

File hashes

Hashes for edc_pdf_reports-0.3.18-py3-none-any.whl
Algorithm Hash digest
SHA256 79af661d7082cdc07289e261608eba52af78479aacfdbb0753d42ac4ebacf80c
MD5 95e2ffced8a53b0616031d246d8c2b60
BLAKE2b-256 35e785c5a22c701fa4089e253f794ac253a8a153d5e72524bba2ca8703e48c54

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page