Skip to content

Commit

Permalink
Merge pull request #5 from phorward/fix/custom_decorators
Browse files Browse the repository at this point in the history
fix: Proposal for use of `additional_descr`
  • Loading branch information
ArneGudermann authored Jan 31, 2025
2 parents 4d40966 + 837ceb5 commit ee41054
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
12 changes: 10 additions & 2 deletions src/viur/core/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,14 @@ def validate(*args, **kwargs):

if not ok:
raise errors.Forbidden(access_config["message"]) if access_config["message"] else errors.Forbidden()

def decorator(func):
meth = Method.ensure(func)
meth.access = access_config
meth.guards.append(validate)

# extend additional access descr, must be a list to be JSON-serializable
meth.additional_descr["access"] = [str(access) for access in access_config["access"]]

return meth

return decorator
Expand Down Expand Up @@ -223,8 +227,12 @@ def validate(args, kwargs, varargs, varkwargs):

def decorator(func):
meth = Method.ensure(func)
meth.skey = skey_config
meth.skey = skey_config["name"]
meth.guards.append(validate)

# extend additional access descr, must be a list to be JSON-serializable
meth.additional_descr["access"] = skey_config["name"]

return meth

if func is None:
Expand Down
20 changes: 6 additions & 14 deletions src/viur/core/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ def __init__(self, func: t.Callable):
self.methods = ("GET", "POST", "HEAD", "OPTIONS")
self.seo_language_map = None
self.cors_allow_headers = None
self.additional_descr = {}
self.skey = None

# Inspection
self.signature = inspect.signature(self._func)

# Guards
self.skey = False
self.access = None
self.guards = []

def __get__(self, obj, objtype=None):
Expand Down Expand Up @@ -215,8 +215,8 @@ def parse_value_by_annotation(annotation: type, name: str, value: str | list | t
args = tuple(parsed_args + varargs)

# always take "skey"-parameter name, when configured, as parsed_kwargs
if self.skey and self.skey["name"] in kwargs:
parsed_kwargs[self.skey["name"]] = kwargs.pop(self.skey["name"])
if self.skey and self.skey in kwargs:
parsed_kwargs[self.skey] = kwargs.pop(self.skey)

# When varkwargs are accepted, merge parsed_kwargs and kwargs, otherwise just use parsed_kwargs
if varkwargs := varkwargs and bool(kwargs):
Expand All @@ -243,7 +243,7 @@ def describe(self) -> dict:
"""
return_doc = t.get_type_hints(self._func).get("return")

ret = {
return {
"args": {
param.name: {
"type": str(param.annotation) if param.annotation is not inspect.Parameter.empty else None,
Expand All @@ -255,15 +255,7 @@ def describe(self) -> dict:
"accepts": self.methods,
"docs": self._func.__doc__.strip() if self._func.__doc__ else None,
"aliases": tuple(self.seo_language_map.keys()) if self.seo_language_map else None,
}

if self.skey:
ret["skey"] = self.skey["name"]

if self.access:
ret["access"] = [str(access) for access in self.access["access"]] # must be a list to be JSON-serializable

return ret
} | self.additional_descr

def register(self, target: dict, name: str, language: str | None = None):
"""
Expand Down

0 comments on commit ee41054

Please sign in to comment.