File: decorators.py

package info (click to toggle)
django-stronghold 0.4.0%2Bdebian-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 208 kB
  • sloc: python: 413; makefile: 135
file content (24 lines) | stat: -rw-r--r-- 887 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from functools import partial
from stronghold.utils import set_view_func_public


def public(function):
    """
    Decorator for public views that do not require authentication
    Sets an attribute in the fuction STRONGHOLD_IS_PUBLIC to True
    """
    orig_func = function
    outer_partial_wrapper = None
    while isinstance(orig_func, partial):
        outer_partial_wrapper = orig_func
        orig_func = orig_func.func
    # For Django >= 2.1.x:
    # If `function` is a bound method, django will wrap it in a partial
    # to allow setting attributes on a bound method.
    # Bound methods have the attr "__self__". If this is the case,
    # we reapply the partial wrapper before setting the attribute.
    if hasattr(orig_func, "__self__") and outer_partial_wrapper != None:
        orig_func = outer_partial_wrapper
    set_view_func_public(orig_func)

    return function