0da9c13192
Иерархия рамочных/дочерних договоров и платежей, интернет-биллинг, месячные IT-отчёты (импорт, DOCX/PDF, шаблоны выводов). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
63 lines
2.5 KiB
Python
63 lines
2.5 KiB
Python
"""Единая точка: ReportTemplate + report dict → html | md | docx | pdf."""
|
|
from __future__ import annotations
|
|
|
|
from apps.reports.engine.blocks_v1 import render_blocks_html, render_blocks_md
|
|
from apps.reports.engine.context import DEFAULT_LABELS, build_template_context, load_default_company
|
|
from apps.reports.engine.django_tpl import TemplateRenderError, render_django_string
|
|
from apps.reports.it_monthly import report_to_markdown
|
|
from apps.reports.it_monthly.docx import report_to_docx, report_to_preview_html
|
|
from apps.reports.it_monthly.pdf import report_to_pdf
|
|
from apps.reports.models import ReportTemplate
|
|
|
|
|
|
def _labels_from_template(tpl: ReportTemplate | None) -> dict:
|
|
if not tpl:
|
|
return dict(DEFAULT_LABELS)
|
|
blocks = tpl.blocks or {}
|
|
labels = blocks.get('labels') if isinstance(blocks, dict) else None
|
|
if isinstance(labels, dict):
|
|
return {**DEFAULT_LABELS, **labels}
|
|
return dict(DEFAULT_LABELS)
|
|
|
|
|
|
def render_report(
|
|
report: dict,
|
|
tpl: ReportTemplate | None,
|
|
fmt: str,
|
|
) -> str | bytes:
|
|
"""
|
|
fmt: 'html' | 'md' | 'docx' | 'pdf'
|
|
blocks_v1 — сборка из секций; django_html — body_html/body_md.
|
|
DOCX/PDF пока builtin-сборщики.
|
|
"""
|
|
company = load_default_company()
|
|
labels = _labels_from_template(tpl)
|
|
ctx = build_template_context(report, company=company, labels=labels)
|
|
|
|
if fmt == 'html':
|
|
if tpl and tpl.engine == ReportTemplate.ENGINE_BLOCKS_V1:
|
|
return render_blocks_html(ctx, tpl.blocks)
|
|
if tpl and tpl.engine == ReportTemplate.ENGINE_DJANGO_HTML and (tpl.body_html or '').strip():
|
|
return render_django_string(tpl.body_html, ctx)
|
|
html = report_to_preview_html(report, company)
|
|
if labels.get('main_title') and labels['main_title'] != DEFAULT_LABELS['main_title']:
|
|
html = html.replace(DEFAULT_LABELS['main_title'], labels['main_title'], 1)
|
|
return html
|
|
|
|
if fmt == 'md':
|
|
if tpl and tpl.engine == ReportTemplate.ENGINE_BLOCKS_V1:
|
|
return render_blocks_md(ctx, tpl.blocks)
|
|
if tpl and tpl.engine == ReportTemplate.ENGINE_DJANGO_HTML and (tpl.body_md or '').strip():
|
|
return render_django_string(tpl.body_md, ctx)
|
|
return report_to_markdown(report)
|
|
|
|
if fmt == 'docx':
|
|
return report_to_docx(report, company)
|
|
if fmt == 'pdf':
|
|
return report_to_pdf(report, company)
|
|
|
|
raise ValueError(f'Unknown format: {fmt}')
|
|
|
|
|
|
__all__ = ['TemplateRenderError', 'render_report']
|