From 54ec62cae929bafe5537dd8d0645b5531e211dab Mon Sep 17 00:00:00 2001 From: Arne Gudermann Date: Tue, 12 Sep 2023 15:57:43 +0200 Subject: [PATCH] Add docstring --- src/viur/core/decorators.py | 17 +++++++++++++++++ src/viur/core/module.py | 3 +++ 2 files changed, 20 insertions(+) diff --git a/src/viur/core/decorators.py b/src/viur/core/decorators.py index 43d13a789..649df497f 100644 --- a/src/viur/core/decorators.py +++ b/src/viur/core/decorators.py @@ -133,3 +133,20 @@ def decorator(func): return decorator return decorator(func) + + +def run_in_transaction(func: Callable) -> Method: + """ + Decorator, which enforces the Method to run in a transaction. + .. code-block:: python + from viur.core.decorators import run_in_transaction + def outer_method(key) + @run_in_transaction + def transaction_method(key): + db.Get(key) + return transaction_method(key) + """ + + func = Method.ensure(func) + func.run_in_transaction = True + return func diff --git a/src/viur/core/module.py b/src/viur/core/module.py index d5ba32b48..2868a0679 100644 --- a/src/viur/core/module.py +++ b/src/viur/core/module.py @@ -35,6 +35,7 @@ def __init__(self, func: typing.Callable): self.ssl = False self.methods = ("GET", "POST", "HEAD") self.seo_language_map = None + self.run_in_transaction = False # Inspection self.signature = inspect.signature(self._func) @@ -282,6 +283,8 @@ def parse_value_by_annotation(annotation: type, name: str, value: str | list | t raise errors.Forbidden(self.access["message"]) if self.access["message"] else errors.Forbidden() # call with instance when provided + if self.run_in_transaction: + return db.RunInTransaction(self._func, *args, **kwargs) if self._instance: return self._func(self._instance, *args, **kwargs)