Skip to main content

karni-reports

Reports for Karni: a row printed to PDF from an HTML template in the background, cached by template and data, served only to users who may see the row.

PyPI License: Apache-2.0 Docs

Experimental. A Celery task renders the template with WeasyPrint and stores the file through Django's storage. The file is cached by the template and the data, so a changed row or template gives a new file and an unchanged one is served again without rendering. The API serves it only to users who may retrieve the row through its own viewset.

Install

pip install karni-reports
karni add reports
karni migrate

No URL include: the endpoints ride the API router. WeasyPrint draws with Pango, a system library. Install it in the image or on the host:

apt-get install -y libpango-1.0-0 libpangoft2-1.0-0 libharfbuzz0b fonts-dejavu-core   # Debian, Ubuntu
apk add pango font-dejavu                                                              # Alpine
brew install pango                                                                     # macOS

Declare a report

# catalog/reports.py
from karni_reports import Report, register

from catalog.models import Product


@register
class ProductSheet(Report):
    model = Product
    template = "catalog/product_sheet.html"

    def get_context(self, obj: Product) -> dict[str, object]:
        return {"product": obj, "category": obj.category}
{% extends "karni_reports/base.html" %}
{% block content %}<h1>{{ product.name }}</h1>{% endblock %}

get_context gets only the row: one file serves everyone who may see the row, so it must not depend on who asks.

Main features

  • A report declared in <app>/reports.py: the model, the template and get_context(obj)
  • Rendered in the background by a Celery task, with WeasyPrint
  • Cached by the template and a hash of the data
  • POST /api/reports/<report>/<id>/, its status and its download, under the row's own access
  • A base print template, manage.py karni_pregenerate and a Generated reports admin
  • A React button in @karni/reports-ui

Endpoints

Method and path Answer
POST /api/reports/<report>/<id>/ 200 with url when a file is ready at the current key, else 202 with status pending
GET /api/reports/<report>/<id>/status/ the last request's status: none, pending, rendering, ready or failed
GET /api/reports/<report>/<id>/download/ the PDF, once ready (404 REPORT008 before)

Each checks the row through its model's viewset retrieve: 401 or 403 from its gate, 404 for a row out of the caller's scope.

Settings

Setting Env var Default Meaning
KARNI_REPORTS_RENDERER same "weasyprint" weasyprint, fake (tests) or a Renderer subclass's dotted path
KARNI_REPORTS_STORAGE same "default" the STORAGES alias the files go to
KARNI_REPORTS_DIRECTORY same "karni/reports" the folder inside that storage
KARNI_REPORTS_PAGE_SIZE same "A4" the CSS page size of the base template
KARNI_REPORTS_KEEP same 1 ready files kept per report and row (0: all)
KARNI_REPORTS_RENDER_TIMEOUT same 600 seconds before a stuck render is queued again

Admin

Generated reports lists every file with its status, size, data version and times. Filter by status failed to read why a render failed, and use Regenerate selected reports to queue renders again. The admin is an explicit registration (karni_reports.admin.GeneratedReportAdmin).

Escape hatches

  • Report.get_context, get_template_name, get_filename, get_renderer, get_queryset, get_template_version, get_data_version and get_render_options: one change for one report.
  • Report.viewset, or Report.get_object, decides access for one report.
  • KARNI_REPORTS_RENDERER: your own karni_reports.rendering.Renderer. Subclass WeasyPrintRenderer with allow_network = True to let templates load images from other hosts (only static, media and data: URLs by default).
  • Throttles: reports.request_report, reports.report_status and reports.download_report in REST_FRAMEWORK["DEFAULT_THROTTLE_RATES"].
  • The task: karni_reports.tasks.render_report. Route it with Celery's task_routes.

Metadata

Release files for karni-reports 0.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for karni-reports 0.6.0
File Size Uploaded Provenance Actions
karni_reports-0.6.0.tar.gz 31.9 kB Without provenance Details for karni_reports-0.6.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for karni-reports 0.6.0
File Interpreter ABI Platform Actions
karni_reports-0.6.0-py3-none-any.whl Python 3 none any Details for karni_reports-0.6.0-py3-none-any.whl

Total release size: 74.3 kB

Release files / karni_reports-0.6.0.tar.gz

Download URL karni_reports-0.6.0.tar.gz
Size 31.9 kB
Tags Source
SHA-256 checksum
How to use checksums
97476e0c3b923576a61a9045c451fea991a44939fbf4f363277dc3f63672a8df
BLAKE2b-256 checksum
How to use checksums
ad560ca8b444d43369579ed40e89858a321d7a2d7aaa5848c973d30488f949a2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / karni_reports-0.6.0-py3-none-any.whl

Download URL karni_reports-0.6.0-py3-none-any.whl
Size 42.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
959c60f5bcb44783563a42d349a33d79b88c7af675a36e196b59b97bc221ef67
BLAKE2b-256 checksum
How to use checksums
975aa09e6c034182aa31a28eab67cd2abe5f190ae6406185da3cca41ea079a8f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Security for karni-reports 0.6.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page