Files

115 lines
3.6 KiB
Python

"""
Hilfsfunktionen für CMBNet
"""
import json
import os
import re
from flask import current_app, g
# Mandantencodes werden als Dateinamensbestandteil verwendet. Nur Zeichen
# zulassen, die keinen Pfadwechsel (../) oder Sonderpfade erzeugen können.
_TENANT_FILE_RE = re.compile(r'^[A-Za-z0-9_-]+$')
def str2pl_filename(tenant_code):
"""Relativer Dateiname der mandantenspezifischen Polyline-Datei."""
tenant_code = (tenant_code or '').strip()
if not _TENANT_FILE_RE.match(tenant_code):
raise ValueError('Ungültiger Mandantencode für den Polyline-Dateinamen')
return f'js/str2pl-{tenant_code}.js'
def str2pl_asset(tenant_code=None):
"""Dateiname des Polyline-Skripts für einen Mandanten.
Existiert ``static/js/str2pl-<tenant>.js``, wird diese mandantenspezifische
Datei verwendet. Andernfalls wird auf die gemeinsame ``str2pl.js``
zurückgegriffen.
"""
if tenant_code is None:
tenant_code = getattr(g, 'tenant_code', None)
tenant_code = (tenant_code or '').strip()
if tenant_code and _TENANT_FILE_RE.match(tenant_code):
candidate = str2pl_filename(tenant_code)
static_folder = current_app.static_folder
if static_folder and os.path.isfile(os.path.join(static_folder, candidate)):
return candidate
return 'js/str2pl.js'
def str2pl_asset_version(tenant_code=None):
"""Cache-Busting-Version der aktuell verwendeten Polyline-Datei."""
filename = str2pl_asset(tenant_code)
static_folder = current_app.static_folder
if not static_folder:
return 0
try:
stat = os.stat(os.path.join(static_folder, filename))
except OSError:
return 0
return f'{int(stat.st_mtime)}-{stat.st_size}'
def get_tenant_id():
"""Gibt die aktuelle Tenant-ID aus dem Request-Kontext zurück"""
return getattr(g, 'tenant_id', None)
def get_current_account():
"""Gibt den aktuellen Account aus dem Request-Kontext zurück"""
return getattr(g, 'current_account', None)
def notify_reload(text, tenant_id=None, sender_id=None):
"""Sendet eine interne Code-99-Nachricht an alle Clients eines Mandanten."""
from cmbnet.ws import broker
from cmbnet.ws.protocol import WSMessage
tenant_id = tenant_id if tenant_id is not None else get_tenant_id()
account = get_current_account()
if sender_id is None and account:
sender_id = account['id']
if not tenant_id or not sender_id:
return False
try:
broker.route_message(
tenant_id,
WSMessage(sender_id=sender_id, code=99, msg=str(text))
)
return True
except Exception as exc:
try:
current_app.logger.warning('Code-99-Nachricht konnte nicht gesendet werden: %s', exc)
except Exception:
pass
return False
def log_transaction(action, entity_type, entity_id, details=None):
"""
Schreibt einen Eintrag ins Transaktionslog
"""
from cmbnet.db import get_db
from cmbnet.core.logging import write_transaction_log
db = get_db()
tenant_id = get_tenant_id()
account = get_current_account()
actor_id = account['id'] if account else None
details_json = json.dumps(details) if details else None
db.execute(
'INSERT INTO transaction_log (tenant_id, actor_account_id, action, entity_type, entity_id, details) VALUES (?, ?, ?, ?, ?, ?)',
(tenant_id, actor_id, action, entity_type, entity_id, details_json)
)
db.commit()
# Auch ins Logfile schreiben
write_transaction_log(tenant_id, actor_id, account['name'] if account else None, action, entity_type, entity_id, details)