Spaces:
Sleeping
Sleeping
Download core/reporting/base_report.py from clabs/learningview: direct link, hf CLI and curl.
- Browser
- Download file 5.35 kB
-
https://huggingface.co/spaces/clabs/learningview/resolve/main/core/reporting/base_report.py
- Command line
-
hf download hf://spaces/clabs/learningview/core/reporting/base_report.py
-
curl -L -o base_report.py https://huggingface.co/spaces/clabs/learningview/resolve/main/core/reporting/base_report.py
5.35 kB
| 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() |