Update app/renderer.py
Browse files- app/renderer.py +85 -12
app/renderer.py
CHANGED
|
@@ -6,24 +6,43 @@ from typing import Any, Dict
|
|
| 6 |
from jinja2 import Environment, FileSystemLoader, select_autoescape
|
| 7 |
from weasyprint import HTML
|
| 8 |
|
| 9 |
-
from .config import BASE_DIR
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
TEMPLATES_DIR = BASE_DIR / "templates"
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
TEMPLATE_MAP: Dict[str, str] = {
|
| 14 |
"RESTlet": "RESTlet.html.j2",
|
|
|
|
| 15 |
"ClientScript": "ClientScript.html.j2",
|
|
|
|
| 16 |
"Map/Reduce": "MapReduce.html.j2",
|
| 17 |
"MapReduce": "MapReduce.html.j2",
|
|
|
|
| 18 |
"Scheduled": "ScheduledScript.html.j2",
|
| 19 |
"ScheduledScript": "ScheduledScript.html.j2",
|
|
|
|
| 20 |
"Suitelet": "Suitelet.html.j2",
|
|
|
|
| 21 |
"UserEvent": "UserEventScript.html.j2",
|
| 22 |
"UserEventScript": "UserEventScript.html.j2",
|
|
|
|
| 23 |
"UserModule.js": "InternalModule.html.j2",
|
| 24 |
"InternalModule": "InternalModule.html.j2",
|
| 25 |
}
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
env = Environment(
|
| 28 |
loader=FileSystemLoader(str(TEMPLATES_DIR)),
|
| 29 |
autoescape=select_autoescape(["html", "xml"]),
|
|
@@ -32,31 +51,85 @@ env = Environment(
|
|
| 32 |
)
|
| 33 |
|
| 34 |
|
|
|
|
|
|
|
|
|
|
| 35 |
def _resolve_template(doc: Dict[str, Any]) -> str:
|
| 36 |
metadata = doc.get("metadata") or {}
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
)
|
| 40 |
-
return TEMPLATE_MAP.get(script_type, "InternalModule.html.j2")
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
def render_html(doc: Dict[str, Any]) -> str:
|
| 44 |
template_name = _resolve_template(doc)
|
| 45 |
template = env.get_template(template_name)
|
| 46 |
-
|
|
|
|
| 47 |
doc=doc,
|
| 48 |
generated_at=datetime.utcnow().strftime("%Y-%m-%d %H:%M UTC"),
|
| 49 |
)
|
| 50 |
-
return html
|
| 51 |
|
| 52 |
|
|
|
|
|
|
|
|
|
|
| 53 |
def render_pdf_bytes(doc: Dict[str, Any]) -> bytes:
|
| 54 |
-
"""
|
| 55 |
-
Genera un PDF vectorial a partir del HTML usando WeasyPrint.
|
| 56 |
-
El texto es seleccionable.
|
| 57 |
-
"""
|
| 58 |
html = render_html(doc)
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
| 62 |
return pdf_bytes
|
|
|
|
| 6 |
from jinja2 import Environment, FileSystemLoader, select_autoescape
|
| 7 |
from weasyprint import HTML
|
| 8 |
|
| 9 |
+
from .config import BASE_DIR
|
| 10 |
|
| 11 |
+
|
| 12 |
+
# =========================
|
| 13 |
+
# RUTAS
|
| 14 |
+
# =========================
|
| 15 |
TEMPLATES_DIR = BASE_DIR / "templates"
|
| 16 |
|
| 17 |
+
|
| 18 |
+
# =========================
|
| 19 |
+
# MAPA PRIMARIO DE TEMPLATES
|
| 20 |
+
# (coincidencias exactas)
|
| 21 |
+
# =========================
|
| 22 |
TEMPLATE_MAP: Dict[str, str] = {
|
| 23 |
"RESTlet": "RESTlet.html.j2",
|
| 24 |
+
|
| 25 |
"ClientScript": "ClientScript.html.j2",
|
| 26 |
+
|
| 27 |
"Map/Reduce": "MapReduce.html.j2",
|
| 28 |
"MapReduce": "MapReduce.html.j2",
|
| 29 |
+
|
| 30 |
"Scheduled": "ScheduledScript.html.j2",
|
| 31 |
"ScheduledScript": "ScheduledScript.html.j2",
|
| 32 |
+
|
| 33 |
"Suitelet": "Suitelet.html.j2",
|
| 34 |
+
|
| 35 |
"UserEvent": "UserEventScript.html.j2",
|
| 36 |
"UserEventScript": "UserEventScript.html.j2",
|
| 37 |
+
|
| 38 |
"UserModule.js": "InternalModule.html.j2",
|
| 39 |
"InternalModule": "InternalModule.html.j2",
|
| 40 |
}
|
| 41 |
|
| 42 |
+
|
| 43 |
+
# =========================
|
| 44 |
+
# ENTORNO DE TEMPLATES
|
| 45 |
+
# =========================
|
| 46 |
env = Environment(
|
| 47 |
loader=FileSystemLoader(str(TEMPLATES_DIR)),
|
| 48 |
autoescape=select_autoescape(["html", "xml"]),
|
|
|
|
| 51 |
)
|
| 52 |
|
| 53 |
|
| 54 |
+
# =========================
|
| 55 |
+
# RESOLVER DE TEMPLATES
|
| 56 |
+
# =========================
|
| 57 |
def _resolve_template(doc: Dict[str, Any]) -> str:
|
| 58 |
metadata = doc.get("metadata") or {}
|
| 59 |
+
raw_type = metadata.get("script_type") or metadata.get("type") or "InternalModule"
|
| 60 |
+
|
| 61 |
+
# ---- Normalizaci贸n del tipo ----
|
| 62 |
+
normalized = (
|
| 63 |
+
str(raw_type)
|
| 64 |
+
.strip()
|
| 65 |
+
.lower()
|
| 66 |
+
.replace(" ", "")
|
| 67 |
+
.replace("-", "")
|
| 68 |
+
.replace("_", "")
|
| 69 |
+
.replace("/", "")
|
| 70 |
)
|
|
|
|
| 71 |
|
| 72 |
+
# ---- Mapa normalizado ----
|
| 73 |
+
normalized_map = {
|
| 74 |
+
"restlet": "RESTlet.html.j2",
|
| 75 |
+
|
| 76 |
+
"clientscript": "ClientScript.html.j2",
|
| 77 |
+
"clients": "ClientScript.html.j2",
|
| 78 |
+
"cs": "ClientScript.html.j2",
|
| 79 |
|
| 80 |
+
"mapreduce": "MapReduce.html.j2",
|
| 81 |
+
"mr": "MapReduce.html.j2",
|
| 82 |
+
|
| 83 |
+
"scheduledscript": "ScheduledScript.html.j2",
|
| 84 |
+
"scheduled": "ScheduledScript.html.j2",
|
| 85 |
+
"ss": "ScheduledScript.html.j2",
|
| 86 |
+
|
| 87 |
+
"suitelet": "Suitelet.html.j2",
|
| 88 |
+
"sl": "Suitelet.html.j2",
|
| 89 |
+
|
| 90 |
+
"userevent": "UserEventScript.html.j2",
|
| 91 |
+
"usereventscript": "UserEventScript.html.j2",
|
| 92 |
+
"ue": "UserEventScript.html.j2",
|
| 93 |
+
|
| 94 |
+
"internalmodule": "InternalModule.html.j2",
|
| 95 |
+
"module": "InternalModule.html.j2",
|
| 96 |
+
"usermodulejs": "InternalModule.html.j2",
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
# ---- Primera prueba: coincidencia exacta ----
|
| 100 |
+
if raw_type in TEMPLATE_MAP:
|
| 101 |
+
return TEMPLATE_MAP[raw_type]
|
| 102 |
+
|
| 103 |
+
# ---- Segunda prueba: coincidencia normalizada ----
|
| 104 |
+
if normalized in normalized_map:
|
| 105 |
+
return normalized_map[normalized]
|
| 106 |
+
|
| 107 |
+
# ---- Fallback ----
|
| 108 |
+
return "InternalModule.html.j2"
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
# =========================
|
| 112 |
+
# RENDER HTML
|
| 113 |
+
# =========================
|
| 114 |
def render_html(doc: Dict[str, Any]) -> str:
|
| 115 |
template_name = _resolve_template(doc)
|
| 116 |
template = env.get_template(template_name)
|
| 117 |
+
|
| 118 |
+
return template.render(
|
| 119 |
doc=doc,
|
| 120 |
generated_at=datetime.utcnow().strftime("%Y-%m-%d %H:%M UTC"),
|
| 121 |
)
|
|
|
|
| 122 |
|
| 123 |
|
| 124 |
+
# =========================
|
| 125 |
+
# RENDER PDF
|
| 126 |
+
# =========================
|
| 127 |
def render_pdf_bytes(doc: Dict[str, Any]) -> bytes:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
html = render_html(doc)
|
| 129 |
|
| 130 |
+
pdf_bytes: bytes = HTML(
|
| 131 |
+
string=html,
|
| 132 |
+
base_url=str(TEMPLATES_DIR)
|
| 133 |
+
).write_pdf()
|
| 134 |
+
|
| 135 |
return pdf_bytes
|