-
Notifications
You must be signed in to change notification settings - Fork 1
/
render.py
37 lines (28 loc) · 1.04 KB
/
render.py
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
import logging
from pathlib import Path
from typing import Any, Dict
from uuid import uuid4
from docxtpl import DocxTemplate
from config import RENDERED_DIR
logging.basicConfig()
logger = logging.getLogger("processing")
logger.setLevel(level=logging.INFO)
class Render:
@classmethod
def render_file(cls, file_path: str, context: Dict[str, Any]) -> Path:
logger.info(f"Received context: {context}")
doc = DocxTemplate(file_path)
doc.render(context)
file_path = cls.get_path(
base_name=context.get("FullName") or context.get("CashDocumentNumber")
)
logger.info("Saving rendered file to: {path}".format(path=file_path))
doc.save(file_path)
return file_path
@classmethod
def get_path(cls, base_name: str = None) -> Path:
if base_name is None:
base_name = str(uuid4())
path = Path(RENDERED_DIR / base_name.lower().replace(" ", "-"))
path.mkdir(parents=True, exist_ok=True)
return path.joinpath("rendered-agreement.docx")