From e6ce7c21772a7567feb98355c1c6e9fb59cc10e6 Mon Sep 17 00:00:00 2001 From: pbrochar Date: Sat, 9 Mar 2024 13:27:18 +0100 Subject: [PATCH] create read only mode for ui --- blitz/ui/blitz_ui.py | 4 ---- blitz/ui/components/json_editor.py | 13 +++++++++---- blitz/ui/components/logger.py | 12 +++++++++--- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/blitz/ui/blitz_ui.py b/blitz/ui/blitz_ui.py index 08f85fd..1ff1593 100644 --- a/blitz/ui/blitz_ui.py +++ b/blitz/ui/blitz_ui.py @@ -7,10 +7,6 @@ from blitz.settings import Settings, get_settings from blitz.tools.erd import generate_mermaid_erd -# @lru_cache -# def get_erd(app: BlitzApp) -> str: -# return generate_mermaid_erd(app._base_resource_model.metadata) - class BlitzUI: def __init__(self, settings: Settings = get_settings()) -> None: diff --git a/blitz/ui/components/json_editor.py b/blitz/ui/components/json_editor.py index 2d5478e..7fdbdf9 100644 --- a/blitz/ui/components/json_editor.py +++ b/blitz/ui/components/json_editor.py @@ -44,7 +44,8 @@ def __init__( else: self.content = content self.mode = mode - self._read_only = True + self.read_only = blitz_ui.read_only + self._editor_read_only = True async def get_data(self) -> None: raw_content: dict[str, str] = await self.editor.run_editor_method("get") @@ -56,8 +57,8 @@ async def get_data(self) -> None: app.storage.user["blitz_file_content"] = self.content def enable_editor(self) -> None: - self._read_only = not self._read_only - self.editor.run_editor_method("updateProps", {"readOnly": self._read_only}) + self._editor_read_only = not self._editor_read_only + self.editor.run_editor_method("updateProps", {"readOnly": self._editor_read_only}) def reset_content(self) -> None: self.content = self._original_content @@ -75,6 +76,9 @@ def validate(self) -> None: notify.success("Valid Blitz File") def save(self) -> None: + if self.read_only: + notify.error("Read Only Mode") + return try: BlitzFile.from_dict(self.content) except ValidationError: @@ -104,12 +108,13 @@ def render(self) -> None: FlatButton("Reset", on_click=self.reset_content, icon="restart_alt") with JustifyBetweenRow(): FlatButton("Validate", on_click=self.validate, icon="verified") + FlatButton("Save", on_click=self.save, icon="save") self.editor = ( ui.json_editor( { "content": {"json": self.content}, - "readOnly": self._read_only, + "readOnly": self._editor_read_only, "mode": self.mode, }, on_change=self.get_data, diff --git a/blitz/ui/components/logger.py b/blitz/ui/components/logger.py index 59c7c79..a8c8349 100644 --- a/blitz/ui/components/logger.py +++ b/blitz/ui/components/logger.py @@ -8,23 +8,29 @@ class LogComponent(BaseComponent[ui.log]): class LogHandler(InterceptHandler): + ANONYMISED_MESSAGE = "[ANONYMISED] *****" + """A logging handler that emits messages to a log element.""" - def __init__(self, log: ui.log, level: int = logging.NOTSET) -> None: + def __init__(self, log: ui.log, level: int = logging.NOTSET, is_anonymised: bool = False) -> None: self.log = log + self._is_anonymised = is_anonymised super().__init__(level) def emit(self, record: logging.LogRecord) -> None: try: if record.name != "uvicorn.access.ui": - self.log.push(record.getMessage()) + message = record.getMessage() if not self._is_anonymised else self.ANONYMISED_MESSAGE + self.log.push(message) except Exception: self.handleError(record) def __init__(self) -> None: self._logger = logging.getLogger("uvicorn.access") + self._anonymize_log = self.blitz_ui.read_only super().__init__() + def render(self) -> None: self.ng = ui.log(max_lines=None).classes("w-full h-64 text-sm") - self._logger.addHandler(self.LogHandler(self.ng)) + self._logger.addHandler(self.LogHandler(self.ng, is_anonymised=self._anonymize_log))