Skip to content

Commit

Permalink
create read only mode for ui
Browse files Browse the repository at this point in the history
  • Loading branch information
pbrochar committed Mar 9, 2024
1 parent d11eb92 commit e6ce7c2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
4 changes: 0 additions & 4 deletions blitz/ui/blitz_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
13 changes: 9 additions & 4 deletions blitz/ui/components/json_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 9 additions & 3 deletions blitz/ui/components/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

0 comments on commit e6ce7c2

Please sign in to comment.