Conditions générales & particulières
+ + {% for provision in draft.provisions %} +Article {{loop.index}} - {{ provision.title|safe }}
+{{ provision.body|safe }}
+diff --git a/back/Dockerfile b/back/Dockerfile index f43f98d6..b1d70e26 100644 --- a/back/Dockerfile +++ b/back/Dockerfile @@ -1,6 +1,8 @@ FROM python:3.10 -# make the 'app' folder the current working directory +RUN apt update && apt install -y xfonts-base xfonts-75dpi python3-pip python3-cffi python3-brotli libpango-1.0-0 libpangoft2-1.0-0 \ + && rm -rf /var/lib/apt/lists/* + WORKDIR /code # copy both 'package.json' and 'package-lock.json' (if available) diff --git a/back/app/contract/__init__.py b/back/app/contract/__init__.py index 944807a6..a386e75f 100644 --- a/back/app/contract/__init__.py +++ b/back/app/contract/__init__.py @@ -1,7 +1,7 @@ from fastapi import APIRouter from .routes_draft import draft_router -from .routes_print import print_router +from .print import print_router contract_router = APIRouter() diff --git a/back/app/contract/models.py b/back/app/contract/models.py index cfc78bc8..495b7c31 100644 --- a/back/app/contract/models.py +++ b/back/app/contract/models.py @@ -1,3 +1,4 @@ +import datetime from typing import List, Literal from enum import Enum @@ -28,6 +29,15 @@ class Party(BaseModel): } ) part: str + representative_id: str = Field( + foreignKey={ + "reference": { + "resource": "entity", + "schema": "Entity", + } + }, + default="" + ) class ProvisionGenuine(BaseModel): @@ -64,3 +74,5 @@ class ContractDraft(CrudDocument): format="dictionary", ) status: ContractDraftStatus = Field(default=ContractDraftStatus.draft) + location: str = "" + date: datetime.date = datetime.date(1970, 1, 1) diff --git a/back/app/contract/print/__init__.py b/back/app/contract/print/__init__.py new file mode 100644 index 00000000..c16f4969 --- /dev/null +++ b/back/app/contract/print/__init__.py @@ -0,0 +1,96 @@ +from fastapi import APIRouter +from fastapi.responses import HTMLResponse, FileResponse +from fastapi.templating import Jinja2Templates + +from weasyprint import HTML, CSS +from weasyprint.text.fonts import FontConfiguration + +from pathlib import Path + +from app.entity.models import Entity +from app.template.models import ProvisionTemplate +from ..schemas import ContractDraft + + +async def build_model(model): + parties = [] + for p in model.parties: + party = { + "entity": await Entity.get(p.entity_id), + "part": p.part + } + if p.representative_id: + party['representative'] = await Entity.get(p.representative_id) + + parties.append(party) + + + model.parties = parties + + provisions = [] + for p in model.provisions: + if p.provision.type == "template": + provisions.append(await ProvisionTemplate.get(p.provision.provision_template_id)) + else: + provisions.append(p.provision) + model.provisions = provisions + + model.location = "Toulouse" + model.date = "01/01/1970" + return model + + +BASE_PATH = Path(__file__).resolve().parent + +print_router = APIRouter() + + +templates = Jinja2Templates(directory=str(BASE_PATH / "templates")) + + +async def render_print(host, draft, lawyer): + template = templates.get_template("print.html") + return template.render({ + "draft": draft, + "lawyer": lawyer, + "static_host": host + }) + + +async def render_css(host, draft): + template = templates.get_template("styles.css") + return template.render({ + "draft": draft, + "static_host": host + }) + + +@print_router.get("/", response_class=HTMLResponse) +async def create() -> str: + draft = await build_model(await ContractDraft.get("63e92534aafed8b509f229c4")) + lawyer = { + "firstname": "Nathaniel", + "lastname": "Toshi", + } + + return await render_print('localhost', draft, lawyer) + + +@print_router.get("/pdf", response_class=FileResponse) +async def create_pdf() -> str: + draft = await build_model(await ContractDraft.get("63e92534aafed8b509f229c4")) + lawyer = { + "firstname": "Nathaniel", + "lastname": "Toshi", + } + + font_config = FontConfiguration() + html = HTML(string=await render_print('nginx', draft, lawyer)) + css = CSS(string=await render_css('nginx', draft), font_config=font_config) + + html.write_pdf('out.pdf', stylesheets=[css], font_config=font_config) + + return FileResponse( + "out.pdf", + media_type="application/pdf", + filename=draft.name) diff --git a/back/app/contract/print/templates/content.html b/back/app/contract/print/templates/content.html new file mode 100644 index 00000000..c7fdff47 --- /dev/null +++ b/back/app/contract/print/templates/content.html @@ -0,0 +1,30 @@ + +
+ + + +{{ provision.body|safe }}
+![]() |
+ Cooper, Hillman & Toshi LLP 6834 Innocence Boulevard LOS SANTOS - SA consulting@cht.law.com |
+
Le {{ draft.date }} à {{ draft.location}}
+Entre les soussignés :
+ {% for party in draft.parties %} +ET
+ {% endif %} ++ {% if party.entity.entity_data.type == "corporation" %} + {{ party.entity.entity_data.title }} société de {{ party.entity.entity_data.activity }} enregistrée auprès du gouvernement de San Andreas et domiciliée au {{ party.entity.address }}{% if party.representative %}, représentée par {{ party.representative.entity_data.firstname }} {{ party.representative.entity_data.middlenames }} {{ party.representative.entity_data.lastname }}{% endif %} + {% elif party.entity.entity_data.type == "individual" %} + {{ party.entity.entity_data.firstname }} {{ party.entity.entity_data.middlenames }} {{ party.entity.entity_data.lastname }} + {% if party.entity.entity_data.day_of_birth %} né le {{ party.entity.entity_data.day_of_birth.strftime('%d/%m/%Y') }} {% if true %} à {{ party.entity.entity_data.place_of_birth }}{% endif %},{% endif %} + {% if party.entity.address %} résidant à {{ party.entity.address }}, {% endif %} + {% elif party.entity.entity_data.type == "institution" %} + + {% endif %} +
+Ci-après dénommé {{ party.part|safe }}
+ {% if loop.first %} +d'une part
+ {% endif %} +d'autre part
+Sous la supervision légale de Maître {{ lawyer.firstname }} {{ lawyer.lastname }}
+Il a été convenu l'exécution des prestations ci-dessous, conformément aux conditions générales et particulières ci-après:
+![]() |
+ Cooper, Hillman & Toshi LLP 6834 Innocence Boulevard LOS SANTOS - SA consulting@cht.law.com |
+
Le {{ draft.date }} à {{ draft.location}}
+Entre les soussignés :
+ {% for party in draft.parties %} +ET
+ {% endif %} ++ {% if party.entity.entity_data.type == "corporation" %} + {{ party.entity.entity_data.title }} société de {{ party.entity.entity_data.activity }} enregistrée auprès du gouvernement de San Andreas et domiciliée au {{ party.entity.address }}{% if party.representative %}, représentée par {{ party.representative.entity_data.firstname }} {{ party.representative.entity_data.middlenames }} {{ party.representative.entity_data.lastname }}{% endif %} + {% elif party.entity.entity_data.type == "individual" %} + {{ party.entity.entity_data.firstname }} {{ party.entity.entity_data.middlenames }} {{ party.entity.entity_data.lastname }} + {% if party.entity.entity_data.day_of_birth %} né le {{ party.entity.entity_data.day_of_birth.strftime('%d/%m/%Y') }} {% if true %} à {{ party.entity.entity_data.place_of_birth }}{% endif %},{% endif %} + {% if party.entity.address %} résidant à {{ party.entity.address }}, {% endif %} + {% elif party.entity.entity_data.type == "institution" %} + + {% endif %} +
+Ci-après dénommé {{ party.part|safe }}
+ {% if loop.first %} +d'une part
+ {% endif %} +d'autre part
+Sous la supervision légale de Maître {{ lawyer.firstname }} {{ lawyer.lastname }}
+Il a été convenu l'exécution des prestations ci-dessous, conformément aux conditions générales et particulières ci-après:
+{{ provision.body|safe }}
+