Dynamic asset url depending on request host
This commit is contained in:
@@ -2,7 +2,7 @@ import datetime
|
||||
import os
|
||||
import base64
|
||||
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from fastapi import APIRouter, HTTPException, Request
|
||||
from fastapi.responses import HTMLResponse, FileResponse
|
||||
from fastapi.templating import Jinja2Templates
|
||||
|
||||
@@ -61,47 +61,47 @@ print_router = APIRouter()
|
||||
templates = Jinja2Templates(directory=str(BASE_PATH / "templates"))
|
||||
|
||||
|
||||
async def render_print(host, contract):
|
||||
async def render_print(root_url, contract):
|
||||
template = templates.get_template("print.html")
|
||||
return template.render({
|
||||
"contract": contract,
|
||||
"static_host": host
|
||||
"root_url": root_url
|
||||
})
|
||||
|
||||
|
||||
async def render_css(host, contract):
|
||||
async def render_css(root_url, contract):
|
||||
template = templates.get_template("styles.css")
|
||||
return template.render({
|
||||
"contract": contract,
|
||||
"static_host": host
|
||||
"root_url": root_url
|
||||
})
|
||||
|
||||
|
||||
@print_router.get("/preview/draft/{draft_id}", response_class=HTMLResponse)
|
||||
async def preview_draft(draft_id: str) -> str:
|
||||
async def preview_draft(draft_id: str, request: Request) -> str:
|
||||
draft = await build_model(await ContractDraft.get(draft_id))
|
||||
|
||||
return await render_print('localhost', draft)
|
||||
return await render_print(f'{request.url.scheme}://{request.url.hostname}', draft)
|
||||
|
||||
|
||||
@print_router.get("/preview/signature/{signature_id}", response_class=HTMLResponse)
|
||||
async def preview_contract_by_signature(signature_id: str) -> str:
|
||||
async def preview_contract_by_signature(signature_id: str, request: Request) -> str:
|
||||
contract = await Contract.find_by_signature_id(signature_id)
|
||||
for p in contract.parties:
|
||||
if p.signature_affixed:
|
||||
p.signature_png = retrieve_signature_png(f'media/signatures/{p.signature_uuid}.png')
|
||||
|
||||
return await render_print('localhost', contract)
|
||||
return await render_print(f'{request.url.scheme}://{request.url.hostname}', contract)
|
||||
|
||||
|
||||
@print_router.get("/preview/{contract_id}", response_class=HTMLResponse)
|
||||
async def preview_contract(contract_id: str) -> str:
|
||||
async def preview_contract(contract_id: str, request: Request) -> str:
|
||||
contract = await Contract.get(contract_id)
|
||||
for p in contract.parties:
|
||||
if p.signature_affixed:
|
||||
p.signature_png = retrieve_signature_png(f'media/signatures/{p.signature_uuid}.png')
|
||||
|
||||
return await render_print('localhost', contract)
|
||||
return await render_print(f'{request.url.scheme}://{request.url.hostname}', contract)
|
||||
|
||||
|
||||
@print_router.get("/pdf/{contract_id}", response_class=FileResponse)
|
||||
@@ -118,8 +118,8 @@ async def create_pdf(contract_id: str) -> str:
|
||||
# os.remove(signature_path)
|
||||
|
||||
font_config = FontConfiguration()
|
||||
html = HTML(string=await render_print('nginx', contract))
|
||||
css = CSS(string=await render_css('nginx', contract), font_config=font_config)
|
||||
html = HTML(string=await render_print('http://nginx', contract))
|
||||
css = CSS(string=await render_css('http://nginx', contract), font_config=font_config)
|
||||
|
||||
html.write_pdf(contract_path, stylesheets=[css], font_config=font_config)
|
||||
update_query = {"$set": {
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
<div class="frontpage">
|
||||
<div id="front-page-header">
|
||||
<table><tr>
|
||||
<td><img id="top-logo" src="http://{{ static_host }}/assets/logotransparent.png" alt="Cooper, Hillman & Toshi logo"></td>
|
||||
<td><img id="top-logo" src="{{ root_url }}/assets/logotransparent.png" alt="Cooper, Hillman & Toshi logo"></td>
|
||||
<td id="office-info">Cooper, Hillman & Toshi LLP<br />6834 Innocence Boulevard<br />LOS SANTOS - SA<br /><a href="#">consulting@cht.law.com</a></td>
|
||||
</tr></table>
|
||||
<h1>{{ contract.title|upper }}</h1>
|
||||
|
||||
@@ -1,24 +1,24 @@
|
||||
|
||||
@font-face {
|
||||
font-family: 'Century Schoolbook';
|
||||
src: url('http://{{ static_host }}/assets/century-schoolbook/CenturySchoolbookRegular.ttf');
|
||||
src: url('{{ root_url }}/assets/century-schoolbook/CenturySchoolbookRegular.ttf');
|
||||
}
|
||||
|
||||
@font-face {
|
||||
font-family: "Century Schoolbook";
|
||||
src: url("http://{{ static_host }}/assets/century-schoolbook/CenturySchoolbookBold.ttf");
|
||||
src: url("{{ root_url }}/assets/century-schoolbook/CenturySchoolbookBold.ttf");
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
@font-face {
|
||||
font-family: "Century Schoolbook";
|
||||
src: url("http://{{ static_host }}/assets/century-schoolbook/CenturySchoolbookItalic.ttf");
|
||||
src: url("{{ root_url }}/assets/century-schoolbook/CenturySchoolbookItalic.ttf");
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
@font-face {
|
||||
font-family: "Century Schoolbook";
|
||||
src: url("http://{{ static_host }}/assets/century-schoolbook/CenturySchoolbookBoldItalic.ttf");
|
||||
src: url("{{ root_url }}/assets/century-schoolbook/CenturySchoolbookBoldItalic.ttf");
|
||||
font-weight: bold;
|
||||
font-style: italic;
|
||||
}
|
||||
@@ -31,7 +31,7 @@
|
||||
content: "© Cooper, Hillman & Toshi LLC - {{ contract.name }} - Page " counter(page) "/" counter(pages);
|
||||
font-size: 0.8em;
|
||||
}
|
||||
background: url('http://{{ static_host }}/assets/watermark.png') no-repeat;
|
||||
background: url('{{ root_url }}/assets/watermark.png') no-repeat;
|
||||
background-size:contain;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user