Skip to content

Commit 9621ed3

Browse files
committed
Add profiler timed decorator on more functions.
1 parent 4b3c324 commit 9621ed3

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

django_unicorn/components.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ def __init__(
148148
self.component = component
149149
self.init_js = init_js
150150

151+
@timed
151152
def render(self):
152153
response = super().render()
153154

@@ -157,7 +158,6 @@ def render(self):
157158
content = response.content.decode("utf-8")
158159

159160
frontend_context_variables = self.component.get_frontend_context_variables()
160-
161161
frontend_context_variables_dict = orjson.loads(frontend_context_variables)
162162
checksum = generate_checksum(orjson.dumps(frontend_context_variables_dict))
163163

@@ -253,6 +253,7 @@ def __init__(self, **kwargs):
253253
self._set_default_template_name()
254254
self._set_caches()
255255

256+
@timed
256257
def _set_default_template_name(self) -> None:
257258
"""
258259
Sets a default template name based on component's name if necessary.
@@ -272,6 +273,7 @@ def _set_default_template_name(self) -> None:
272273
template_name = self.component_name.replace(".", "/")
273274
self.template_name = f"unicorn/{template_name}.html"
274275

276+
@timed
275277
def _set_caches(self) -> None:
276278
"""
277279
Setup some initial "caches" to prevent Python from having to introspect
@@ -282,6 +284,7 @@ def _set_caches(self) -> None:
282284
self._methods_cache = self._methods()
283285
self._set_resettable_attributes_cache()
284286

287+
@timed
285288
def reset(self):
286289
for (
287290
attribute_name,
@@ -337,6 +340,7 @@ def called(self, name, args):
337340
"""
338341
pass
339342

343+
@timed
340344
def render(self, init_js=False) -> str:
341345
"""
342346
Renders a UnicornView component with the public properties available. Delegates to a
@@ -369,6 +373,7 @@ def render(self, init_js=False) -> str:
369373

370374
return rendered_component
371375

376+
@timed
372377
def get_frontend_context_variables(self) -> str:
373378
"""
374379
Get publicly available properties and output them in a string-encoded JSON object.
@@ -407,6 +412,7 @@ def get_frontend_context_variables(self) -> str:
407412

408413
return encoded_frontend_context_variables
409414

415+
@timed
410416
def _get_form(self, data):
411417
if hasattr(self, "form_class"):
412418
try:
@@ -417,6 +423,7 @@ def _get_form(self, data):
417423
except Exception as e:
418424
logger.exception(e)
419425

426+
@timed
420427
def get_context_data(self, **kwargs):
421428
"""
422429
Overrides the standard `get_context_data` to add in publicly available
@@ -432,9 +439,11 @@ def get_context_data(self, **kwargs):
432439

433440
return context
434441

442+
@timed
435443
def is_valid(self, model_names: List = None) -> bool:
436444
return len(self.validate(model_names).keys()) == 0
437445

446+
@timed
438447
def validate(self, model_names: List = None) -> Dict:
439448
"""
440449
Validates the data using the `form_class` set on the component.
@@ -481,6 +490,7 @@ def validate(self, model_names: List = None) -> Dict:
481490

482491
return self.errors
483492

493+
@timed
484494
def _attribute_names(self) -> List[str]:
485495
"""
486496
Gets publicly available attribute names. Cached in `_attribute_names_cache`.
@@ -492,6 +502,7 @@ def _attribute_names(self) -> List[str]:
492502

493503
return attribute_names
494504

505+
@timed
495506
def _attributes(self) -> Dict[str, Any]:
496507
"""
497508
Get publicly available attributes and their values from the component.
@@ -505,6 +516,7 @@ def _attributes(self) -> Dict[str, Any]:
505516

506517
return attributes
507518

519+
@timed
508520
def _set_property(self, name, value):
509521
# Get the correct value type by using the form if it is available
510522
data = self._attributes()
@@ -535,6 +547,7 @@ def _set_property(self, name, value):
535547
f"'{name}' attribute on '{self.component_name}' component could not be set. Is it a @property without a setter?"
536548
)
537549

550+
@timed
538551
def _methods(self) -> Dict[str, Callable]:
539552
"""
540553
Get publicly available method names and their functions from the component.
@@ -553,6 +566,7 @@ def _methods(self) -> Dict[str, Callable]:
553566

554567
return methods
555568

569+
@timed
556570
def _set_hook_methods_cache(self) -> None:
557571
"""
558572
Caches the updating/updated attribute function names defined on the component.
@@ -568,6 +582,7 @@ def _set_hook_methods_cache(self) -> None:
568582
if hasattr(self, function_name):
569583
self._hook_methods_cache.append(function_name)
570584

585+
@timed
571586
def _set_resettable_attributes_cache(self) -> None:
572587
"""
573588
Caches the attributes that are "resettable" in `_resettable_attributes_cache`.

django_unicorn/views.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
from .call_method_parser import InvalidKwarg, parse_call_method_name, parse_kwarg
1414
from .components import UnicornField, UnicornView
15+
from .decorators import timed
1516
from .errors import UnicornViewError
1617
from .message import ComponentRequest, Return
1718
from .serializer import loads
@@ -33,6 +34,7 @@ def wrapped_view(*args, **kwargs):
3334
return wraps(view_func)(wrapped_view)
3435

3536

37+
@timed
3638
def _set_property_from_data(
3739
component_or_field: Union[UnicornView, UnicornField, Model], name: str, value,
3840
) -> None:
@@ -59,6 +61,7 @@ def _set_property_from_data(
5961
setattr(component_or_field, name, value)
6062

6163

64+
@timed
6265
def _set_property_value(
6366
component: UnicornView, property_name: str, property_value: Any, data: Dict = {}
6467
) -> None:
@@ -130,6 +133,7 @@ class TestView(UnicornView):
130133
component.updated(property_name, property_value)
131134

132135

136+
@timed
133137
def _get_property_value(component: UnicornView, property_name: str) -> Any:
134138
"""
135139
Gets property value from the component based on the property name.
@@ -159,6 +163,7 @@ def _get_property_value(component: UnicornView, property_name: str) -> Any:
159163
component_or_field = component_or_field[property_name_part]
160164

161165

166+
@timed
162167
def _call_method_name(
163168
component: UnicornView, method_name: str, params: List[Any]
164169
) -> Any:
@@ -180,6 +185,7 @@ def _call_method_name(
180185
return func()
181186

182187

188+
@timed
183189
@handle_error
184190
@csrf_protect
185191
@require_POST

0 commit comments

Comments
 (0)