"""Install the pinned upstream Event Function through the admin API. Run inside OpenWebUI with this directory available. Never prints credentials. """ import hashlib import json import shutil import sqlite3 import urllib.request import urllib.error from datetime import datetime, timedelta, timezone from pathlib import Path from open_webui.utils.auth import create_token root = Path(__file__).parent manifest = json.loads((root / 'manifest.json').read_text()) for item in manifest['files']: assert hashlib.sha256((root / item['file']).read_bytes()).hexdigest() == item['sha256'] db = sqlite3.connect('/app/backend/data/webui.db') uid = db.execute("select id from user where role='admin' limit 1").fetchone()[0] token = create_token({'id': uid}, timedelta(minutes=15)) def api(path, data=None): req = urllib.request.Request('http://127.0.0.1:8080' + path, data=None if data is None else json.dumps(data).encode(), headers={'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json'}) with urllib.request.urlopen(req, timeout=180) as response: body = response.read() return json.loads(body) if 'application/json' in response.headers.get('Content-Type', '') else body ident = 'theme_designer_pro' assert not any(f['id'] == ident for f in api('/api/v1/functions/')), 'Already installed; not overwritten' stamp = datetime.now(timezone.utc).strftime('%Y%m%dT%H%M%SZ') backup = Path('/app/backend/data/plugin-backups') / ('theme-' + stamp) backup.mkdir(parents=True, mode=0o700) with sqlite3.connect(backup / 'webui.db') as target: db.backup(target) (backup / 'webui.db').chmod(0o600) theme = Path('/app/backend/data/theme') if theme.exists(): shutil.copytree(theme, backup / 'theme') index = Path('/app/build/index.html') before = hashlib.sha256(index.read_bytes()).hexdigest() (backup / 'index.sha256').write_text(before + '\n') created = api('/api/v1/functions/create', {'id': ident, 'name': 'Theme Designer Pro', 'content': (root / 'theme_designer_pro_event.py').read_text(), 'meta': {'description': 'Upstream Theme Designer Pro 1.8.5 — central theme editor'}}) assert created['type'] == 'event', created['type'] api('/api/v1/functions/id/' + ident + '/valves/update', {'draft_mode_default': True, 'enable_canvas_api_access': False}) active = api('/api/v1/functions/id/' + ident + '/toggle', {}) assert active['is_active'] designer = api('/api/v1/theme-designer') assert b'