mirror of
https://github.com/inventree/InvenTree.git
synced 2025-05-15 11:33:08 +00:00
[PUI] Template helpers (#5993)
* added helper to render an instance for an url * made manifest loading more error tolerant (to the user) * refactored return string composition * made manifest path configurable * made django app relative * fix typing for py 3.9 * move css up
This commit is contained in:
parent
1994b0f3b2
commit
2509c8f48b
@ -3,6 +3,7 @@
|
|||||||
import json
|
import json
|
||||||
from logging import getLogger
|
from logging import getLogger
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
from django import template
|
from django import template
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
@ -15,30 +16,44 @@ FRONTEND_SETTINGS = json.dumps(settings.FRONTEND_SETTINGS)
|
|||||||
|
|
||||||
|
|
||||||
@register.simple_tag
|
@register.simple_tag
|
||||||
def spa_bundle():
|
def spa_bundle(manifest_path: Union[str, Path] = '', app: str = 'web'):
|
||||||
"""Render SPA bundle."""
|
"""Render SPA bundle."""
|
||||||
manifest = Path(__file__).parent.parent.joinpath("static/web/manifest.json")
|
def get_url(file: str) -> str:
|
||||||
|
"""Get static url for file."""
|
||||||
|
return f"{settings.STATIC_URL}{app}/{file}"
|
||||||
|
|
||||||
|
if manifest_path == '':
|
||||||
|
manifest_path = Path(__file__).parent.parent.joinpath("static/web/manifest.json")
|
||||||
|
manifest = Path(manifest_path)
|
||||||
|
|
||||||
if not manifest.exists():
|
if not manifest.exists():
|
||||||
logger.error("Manifest file not found")
|
logger.error("Manifest file not found")
|
||||||
return
|
return
|
||||||
|
|
||||||
|
try:
|
||||||
manifest_data = json.load(manifest.open())
|
manifest_data = json.load(manifest.open())
|
||||||
index = manifest_data.get("index.html")
|
except (TypeError, json.decoder.JSONDecodeError):
|
||||||
css_index = manifest_data.get("index.css")
|
logger.exception("Failed to parse manifest file")
|
||||||
|
return
|
||||||
|
|
||||||
|
return_string = ""
|
||||||
|
# CSS (based on index.css file as entrypoint)
|
||||||
|
css_index = manifest_data.get("index.css")
|
||||||
|
if css_index:
|
||||||
|
return_string += f'<link rel="stylesheet" href="{get_url(css_index["file"])}" />'
|
||||||
|
|
||||||
|
# JS (based on index.html file as entrypoint)
|
||||||
|
index = manifest_data.get("index.html")
|
||||||
dynamic_files = index.get("dynamicImports", [])
|
dynamic_files = index.get("dynamicImports", [])
|
||||||
imports_files = "".join(
|
imports_files = "".join(
|
||||||
[
|
[
|
||||||
f'<script type="module" src="{settings.STATIC_URL}web/{manifest_data[file]["file"]}"></script>'
|
f'<script type="module" src="{get_url(manifest_data[file]["file"])}"></script>'
|
||||||
for file in dynamic_files
|
for file in dynamic_files
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
return_string += f'<script type="module" src="{get_url(index["file"])}"></script>{imports_files}'
|
||||||
|
|
||||||
return mark_safe(
|
return mark_safe(return_string)
|
||||||
f"""<link rel="stylesheet" href="{settings.STATIC_URL}web/{css_index['file']}" />
|
|
||||||
<script type="module" src="{settings.STATIC_URL}web/{index['file']}"></script>{imports_files}"""
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@register.simple_tag
|
@register.simple_tag
|
||||||
|
Loading…
x
Reference in New Issue
Block a user