Updated Command to allow client_response. First implementation or UserProfile control

This commit is contained in:
2025-11-11 12:07:00 +01:00
parent c641f3fd63
commit cba4f2aab4
11 changed files with 166 additions and 26 deletions

View File

@@ -1,3 +1,4 @@
import inspect
import uuid
from typing import Optional
@@ -40,7 +41,7 @@ class BaseCommand:
"hx-vals": f'{{"c_id": "{self.id}"}}',
} | self._htmx_extra
def execute(self):
def execute(self, client_response: dict = None):
raise NotImplementedError
def htmx(self, target="this", swap="innerHTML"):
@@ -84,6 +85,9 @@ class BaseCommand:
self._htmx_extra["hx-swap"] = "none"
return self
def __str__(self):
return f"Command({self.name})"
class Command(BaseCommand):
@@ -110,8 +114,9 @@ class Command(BaseCommand):
self.callback = callback
self.args = args
self.kwargs = kwargs
self.requires_client_response = 'client_response' in inspect.signature(callback).parameters
def execute(self):
def execute(self, client_response: dict = None):
ret_from_bindings = []
def binding_result_callback(attr, old, new, results):
@@ -120,7 +125,10 @@ class Command(BaseCommand):
for data in self._bindings:
add_event_listener(ObservableEvent.AFTER_PROPERTY_CHANGE, data, "", binding_result_callback)
ret = self.callback(*self.args, **self.kwargs)
if self.requires_client_response:
ret = self.callback(client_response=client_response, *self.args, **self.kwargs)
else:
ret = self.callback(*self.args, **self.kwargs)
for data in self._bindings:
remove_event_listener(ObservableEvent.AFTER_PROPERTY_CHANGE, data, "", binding_result_callback)
@@ -138,9 +146,6 @@ class Command(BaseCommand):
return list(ret) + ret_from_bindings
else:
return [ret] + ret_from_bindings
def __str__(self):
return f"Command({self.name})"
class CommandsManager: