Skip to content

Commit

Permalink
Merge branch 'release/0.3.23' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
erikvw committed Nov 20, 2024
2 parents 10eb5b7 + 2056dfd commit 3b2bdee
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ repos:
- id: check-executables-have-shebangs
- id: check-merge-conflict
- id: check-toml
- id: check-yaml
- id: debug-statements
- id: detect-private-key
- id: end-of-file-fixer
- id: fix-byte-order-marker
- id: requirements-txt-fixer
files: requirements/.*\.txt$
Expand Down
5 changes: 4 additions & 1 deletion edc_pdf_reports/numbered_canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

class NumberedCanvas(canvas.Canvas):
static_footer_text = None
footer_row_height = 25

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand All @@ -30,5 +31,7 @@ def draw_page_number(self, page_count):
width, _ = A4
self.setFont("Helvetica", 6)
self.drawCentredString(
width / 2, 25, "Page %d of %d" % (self.getPageNumber(), page_count)
width / 2,
self.footer_row_height,
"Page %d of %d" % (self.getPageNumber(), page_count),
)
31 changes: 27 additions & 4 deletions edc_pdf_reports/report.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from __future__ import annotations

from abc import ABC
from typing import Type
from uuid import uuid4

from django.core.handlers.wsgi import WSGIRequest
from django.utils import timezone
from django_revision.revision import Revision
from edc_protocol.research_protocol_config import ResearchProtocolConfig
from edc_utils.date import to_local
from reportlab.lib.enums import TA_CENTER, TA_LEFT, TA_RIGHT
from reportlab.lib.pagesizes import A4
from reportlab.lib.styles import (
Expand All @@ -18,6 +20,8 @@
from reportlab.lib.units import cm
from reportlab.platypus import SimpleDocTemplate

from edc_pdf_reports.numbered_canvas import NumberedCanvas


class ReportError(Exception):
def __init__(self, message, code=None):
Expand All @@ -42,17 +46,30 @@ def __init__(
header_line: str | None = None,
filename: str | None = None,
request: WSGIRequest | None = None,
numbered_canvas: Type[NumberedCanvas] | None = None,
footer_row_height: int | None = None,
):
self._styles = None
self.request = request
self.page = page or self.default_page

self.filename = filename or f"{uuid4()}.pdf"
self.footer_row_height = footer_row_height or 25
self.numbered_canvas = numbered_canvas

if not header_line:
header_line = ResearchProtocolConfig().institution
self.header_line = header_line

def build(self, response):
doctemplate = self.document_template(response, **self.page)
story = self.get_report_story()
doctemplate.build(
story,
onFirstPage=self.on_first_page,
onLaterPages=self.on_later_pages,
canvasmaker=self.numbered_canvas,
)

@property
def report_filename(self) -> str:
return self.filename
Expand Down Expand Up @@ -81,16 +98,22 @@ def on_first_page(self, canvas, doc):

def on_later_pages(self, canvas, doc):
"""Callback for onLaterPages"""
self.draw_header(canvas, doc)
self.draw_footer(canvas, doc)

def draw_header(self, canvas, doc):
pass

def draw_footer(self, canvas, doc):
styles = getSampleStyleSheet()
styles.add(ParagraphStyle(name="header", fontSize=6, alignment=TA_CENTER))
width, _ = A4
canvas.setFontSize(6)
timestamp = timezone.now().strftime("%Y-%m-%d %H:%M")
canvas.drawRightString(width - len(timestamp) - 20, 25, f"printed on {timestamp}")
canvas.drawString(35, 25, f"clinicedc {Revision().tag}")
timestamp = to_local(timezone.now()).strftime("%Y-%m-%d %H:%M")
canvas.drawRightString(
width - len(timestamp) - 20, self.footer_row_height, f"printed on {timestamp}"
)
canvas.drawString(35, self.footer_row_height, f"clinicedc {Revision().tag}")

@property
def styles(self):
Expand Down
20 changes: 16 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,25 @@ legacy_tox_ini = """
envlist =
py{312,313}-dj{51,dev},
lint
pre-commit
isolated_build = true
[gh-actions]
python =
3.12: py312
3.13: py313, lint
3.12: py312, lint, pre-commit
3.13: py313
[gh-actions:env]
DJANGO =
5.1: dj51
dev: djdev, lint
dev: djdev, lint, pre-commit
[testenv]
deps =
-r https://raw.githubusercontent.com/clinicedc/edc/develop/requirements.tests/tox.txt
-r https://raw.githubusercontent.com/clinicedc/edc/develop/requirements.tests/test_utils.txt
-r https://raw.githubusercontent.com/clinicedc/edc/develop/requirements.tests/edc.txt
-r https://raw.githubusercontent.com/clinicedc/edc/develop/requirements.tests/third_party_dev.txt
dj51: Django>=5.1,<5.2
djdev: https://github.com/django/django/tarball/main
Expand All @@ -71,7 +71,19 @@ commands =
[testenv:lint]
deps = -r https://raw.githubusercontent.com/clinicedc/edc/develop/requirements.tests/lint.txt
commands =
python --version
pip --version
pip freeze
isort --profile=black --check --diff .
black --check --diff .
flake8 .
[testenv:pre-commit]
deps = pre-commit
commands =
python --version
pip --version
pip freeze
pre-commit autoupdate
pre-commit run --all-files
"""

0 comments on commit 3b2bdee

Please sign in to comment.