93 lines
3.7 KiB
Python
93 lines
3.7 KiB
Python
from datetime import datetime
|
|
from pathlib import Path
|
|
from string import Template
|
|
|
|
from .config import TMPL_DIR, CSS_PATH, PAGES_CSS_DIR, JS_DIR, _state, DbType
|
|
from .queries.db import e as _e
|
|
|
|
|
|
def _css() -> str:
|
|
return CSS_PATH.read_text(encoding='utf-8')
|
|
|
|
|
|
def _page_subdirs(base: Path) -> list:
|
|
"""Return direct subdirectories of base, excluding 'shared'."""
|
|
return [d for d in sorted(base.iterdir()) if d.is_dir() and d.name != 'shared']
|
|
|
|
|
|
def _page_css(name: str) -> str:
|
|
parts = []
|
|
shared = PAGES_CSS_DIR / 'filters.css'
|
|
if shared.exists():
|
|
parts.append(shared.read_text(encoding='utf-8'))
|
|
for search_dir in [PAGES_CSS_DIR] + _page_subdirs(PAGES_CSS_DIR):
|
|
main = search_dir / f'{name}.css'
|
|
if main.exists():
|
|
parts.append(main.read_text(encoding='utf-8'))
|
|
for f in sorted(search_dir.glob(f'{name}_*.css')):
|
|
parts.append(f.read_text(encoding='utf-8'))
|
|
return '\n'.join(parts)
|
|
|
|
|
|
def _page_js(name: str) -> str:
|
|
parts = []
|
|
shared_dir = JS_DIR / 'shared'
|
|
if shared_dir.exists():
|
|
for f in sorted(shared_dir.glob('*.js')):
|
|
parts.append(f.read_text(encoding='utf-8'))
|
|
for search_dir in [JS_DIR] + _page_subdirs(JS_DIR):
|
|
main = search_dir / f'page_{name}.js'
|
|
if main.exists():
|
|
parts.append(main.read_text(encoding='utf-8'))
|
|
for f in sorted(search_dir.glob(f'page_{name}_*.js')):
|
|
parts.append(f.read_text(encoding='utf-8'))
|
|
return '\n'.join(parts)
|
|
|
|
|
|
def _tmpl(name: str) -> Template:
|
|
return Template((TMPL_DIR / name).read_text(encoding='utf-8'))
|
|
|
|
|
|
def _card(label, value, color='#2563eb') -> str:
|
|
return f'<div class="card"><div class="cv" style="color:{color}">{_e(value)}</div><div class="cl">{_e(label)}</div></div>'
|
|
|
|
|
|
def _base(title: str, content: str, active: str, db_path: str, page_css: str = '', page_js: str = '') -> str:
|
|
now = datetime.now().strftime('%d/%m/%Y %H:%M:%S')
|
|
nav = {'nav_dashboard': '', 'nav_runs': '', 'nav_logs': '', 'nav_report': '', 'nav_schema': ''}
|
|
if active in nav:
|
|
nav[active] = 'active'
|
|
|
|
db_type = _state.get('db_type', 'unknown')
|
|
h1_title = 'RPA — Corsi Intraziendali' if db_type == DbType.INTRAZ else 'RPA — Comunicazioni Regione Lombardia'
|
|
|
|
if db_type == DbType.INTRAZ:
|
|
cls = 'active' if active == 'nav_iscrizioni' else ''
|
|
nav_step_link = f'<a href="/steps" class="{cls}">RPA Steps</a>'
|
|
cls_api_isc = 'active' if active == 'nav_api_iscrizioni' else ''
|
|
nav_report_link = f'<a href="/iscrizioni-api" class="{cls_api_isc}">Iscrizioni</a>'
|
|
cls_sp = 'active' if active == 'nav_sharepoint' else ''
|
|
nav_sharepoint_link = f'<a href="/sharepoint" class="{cls_sp}">SharePoint</a>'
|
|
cls_email = 'active' if active == 'nav_email' else ''
|
|
nav_pec_link = f'<a href="/email" class="{cls_email}">Email</a>'
|
|
else:
|
|
cls = 'active' if active == 'nav_documenti' else ''
|
|
nav_step_link = f'<a href="/documenti" class="{cls}">Documenti</a>'
|
|
cls_pec = 'active' if active == 'nav_pec' else ''
|
|
nav_pec_link = f'<a href="/pec" class="{cls_pec}">PEC</a>'
|
|
cls_rep = nav.get('nav_report', '')
|
|
nav_report_link = f'<a href="/report" class="{cls_rep}">Report</a>'
|
|
nav_sharepoint_link = ''
|
|
|
|
return _tmpl('_base.html').substitute(
|
|
css=_css(), page_css=page_css, page_js=page_js, title=title, now=now,
|
|
db_name=_e(Path(db_path).name),
|
|
h1_title=h1_title,
|
|
content=content,
|
|
nav_step_link=nav_step_link,
|
|
nav_report_link=nav_report_link,
|
|
nav_sharepoint_link=nav_sharepoint_link,
|
|
nav_pec_link=nav_pec_link,
|
|
**nav,
|
|
)
|