-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecorators2.py
More file actions
77 lines (47 loc) · 1.5 KB
/
decorators2.py
File metadata and controls
77 lines (47 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# ---------------------------------------------------
class User:
def __init__(self, name, role):
self.name = name
self.role = role
user = User('just_user', ' user')
admin = User('just_admin', 'admin')
current_user = admin
def do_admin_work():
if current_user.role != 'admin':
raise Exception('Access Forbidden!')
return 'Doing somth'
# print(do_admin_work())
# ---------------------------------------------------
def do_admin_work2():
return 'Doing somth2'
def check_access(func):
if current_user.role != 'admin':
raise Exception('Access Forbidden2!')
return func()
print(check_access(do_admin_work2))
# ---------------------------------------------------
def check_access2(func):
def wrapper():
if current_user.role != 'admin':
raise Exception('Access Forbidden2!')
return func()
return wrapper
do_admin_work2 = check_access2(do_admin_work2)
print(do_admin_work2.__name__)
# ---------------------------------------------------
@check_access2
def do_admin_work3():
return 'Doing somth3'
print(do_admin_work3())
print(do_admin_work3.__name__)
# ---------------------------------------------------
def check_access3(func):
def wrapper(*args, **kwargs):
if current_user.role != 'admin':
raise Exception('Access Forbidden2!')
return func(*args, **kwargs)
return wrapper
@check_access3
def do_admin_work4(input):
return f'Doing somth4 {input}'
print(do_admin_work4(1))