Spaces:
Sleeping
Sleeping
File size: 5,354 Bytes
408b7e6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | import os
import pikepdf
from fpdf import FPDF
from datetime import datetime
from core.reporting.report_config import REPORT_CONFIG
class BaseClinicalReport(FPDF):
"""
Standardized PDF Factory for LearnView Pro.
Handles Multi-Audience Insights, Security (AES-256), and Jurisdictional Branding.
"""
def __init__(self, session_id, region, context):
super().__init__()
self.session_id = session_id
self.region = region
self.context = context
# Dynamically load jurisdiction config
self.cfg = REPORT_CONFIG.get(region, REPORT_CONFIG["AU_VIC"])
self.report_date = datetime.now().strftime("%d %b %Y")
self.alias_nb_pages()
self.add_page()
def header(self):
"""Custom header with jurisdictional branding and student metadata."""
# Regional Branding: Solid header bar
self.set_fill_color(*self.cfg["primary_color"])
self.rect(0, 0, 210, 40, 'F')
self.set_text_color(255, 255, 255)
self.set_font('Helvetica', 'B', 16)
self.set_xy(12, 10)
# India Edition branding check
header_title = "LEARNVIEW PRO: INDIA EDITION" if "IN" in self.region else self.cfg["header_title"]
self.cell(0, 10, header_title, 0, 1)
# VCL Jurisdictional Compliance Badge
self.set_font('Helvetica', 'B', 7)
compliance = "Victoria Child Link (VCL) Alignment Protocol" if "IN" in self.region else self.cfg['footer_compliance']
badge_text = f"JURISDICTION COMPLIANCE: {compliance.upper()}"
self.text(120, 10, badge_text)
self.set_font('Helvetica', '', 9)
self.set_xy(12, 18)
self.cell(0, 5, f"Clinical Session Artifact | Ref: {self.session_id}", 0, 1)
# Metadata: Student Details
self.set_font('Helvetica', 'B', 11)
self.set_x(12)
name = getattr(self.context, 'child_name', 'Anonymous Student')
grade = getattr(self.context, 'child_class', 'N/A')
student_meta = f"Student: {name} | Grade: {grade}"
self.cell(0, 5, student_meta, 0, 1)
self.set_font('Helvetica', '', 9)
self.set_x(12)
# Display Audit Key (PWD) to match Dashboard
pwd = getattr(self.context, 'report_password', 'N/A')
meta_sub = f"Report Generated: {self.report_date} | Audit Key (PWD): {pwd}"
self.cell(0, 5, meta_sub, 0, 1)
self.set_text_color(0, 0, 0)
self.set_y(45)
def draw_parent_insight(self, title, description):
"""Plain-language summary box for parents."""
self.set_fill_color(248, 250, 252)
self.set_draw_color(203, 213, 225)
curr_y = self.get_y()
self.rect(12, curr_y, 186, 20, 'FD')
self.set_font("Helvetica", 'B', 9)
self.set_text_color(51, 65, 85)
self.set_xy(15, curr_y + 2)
self.cell(0, 5, f"FOR PARENTS: {title.upper()}", ln=True)
self.set_font("Helvetica", '', 8)
self.set_x(15)
self.multi_cell(180, 4, description)
self.set_y(curr_y + 25)
def footer(self):
"""Compliance footer with VCL strings."""
self.set_y(-20)
self.set_font('Helvetica', 'I', 8)
self.set_text_color(120, 120, 120)
compliance = "VCL Alignment Protocol" if "IN" in self.region else self.cfg['footer_compliance']
compliance_text = f"{compliance} | Session ID: {self.session_id}"
self.cell(0, 10, compliance_text, 0, 0, 'C')
self.set_x(-30)
self.cell(20, 10, f'Page {self.page_no()}/{{nb}}', 0, 0, 'R')
def draw_h2(self, text):
"""Standardized Blue Section Header."""
self.set_font("Helvetica", 'B', 14)
self.set_text_color(*self.cfg["primary_color"])
self.cell(0, 10, text, ln=True)
self.set_draw_color(*self.cfg["primary_color"])
self.line(12, self.get_y(), 62, self.get_y())
self.ln(5)
self.set_text_color(0, 0, 0)
def finalize(self, raw_filename, final_filename):
"""Secures artifact with AES-256 using the Audit Hash (PWD)."""
self.output(raw_filename)
try:
with pikepdf.open(raw_filename) as pdf:
# Use Audit Key (PWD) from Dashboard
pw = str(getattr(self.context, 'report_password', self.session_id))
pdf.save(final_filename, encryption=pikepdf.Encryption(
user=pw, owner=pw, allow=pikepdf.Permissions(accessibility=True)
))
if os.path.exists(raw_filename): os.remove(raw_filename)
return final_filename
except Exception:
if os.path.exists(raw_filename): os.rename(raw_filename, final_filename)
return final_filename
def draw_table(self, data, highlight_row=None):
"""Renders Metrics/Hardware Audit Table."""
self.set_font("Helvetica", size=9)
col_width = self.epw / len(data[0])
for i, row in enumerate(data):
self.set_fill_color(240, 244, 248) if (i == 0 or i == highlight_row) else None
self.set_font("Helvetica", 'B' if (i == 0 or i == highlight_row) else '', 9)
for item in row:
self.cell(col_width, 10, str(item), border=1, fill=(i == 0 or i == highlight_row), align='C')
self.ln() |