Skip to content

Commit 8cfc7d7

Browse files
author
weining.zhu
committed
Ginger
1 parent 715bbb0 commit 8cfc7d7

3 files changed

Lines changed: 90 additions & 10 deletions

File tree

app/libs/redprint.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def register(self, bp, url_prefix=None):
1616
if url_prefix is None:
1717
url_prefix = '/' + self.name
1818
for f, rule, options in self.mound:
19-
endpoint = options.pop('endpoint', f.__name__)
19+
endpoint = self.name + '+' + options.pop('endpoint', f.__name__)
2020
bp.add_url_rule(url_prefix+rule, endpoint, f, **options)
2121

2222

app/libs/scope.py

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,62 @@
11
#encoding:utf-8
2-
import importlib
2+
#关于 继承 变量寻找关系
3+
#子类继承父类 若某个方法在父类中 子类执行此方法时 执行某个变量的操作
4+
#此变量的寻找 (若此变量在父类中存在) 先从子类中寻找 若子类中没有 再去父类中寻找
5+
#变量寻找关系 先从子类中寻找 再去父类中寻找
36

7+
class Scope:
8+
allow_api = []
9+
allow_module = []
10+
forbidden = []
411

5-
class SuperScope:
6-
allow_api = ['v1.super_get_user']
12+
def __add__(self, other):
13+
#寻找allow_api 线路 先从继承此类的 子类当中寻找allow_api
14+
#若子类当中没有 allow_api 在从当前父类中寻找
15+
self.allow_api += other.allow_api
16+
self.allow_api = list(set(self.allow_api))
717

8-
class UserScope:
9-
allow_api = ['v1.get_user']
18+
self.allow_module += other.allow_module
19+
self.allow_module = list(set(self.allow_module))
20+
21+
self.forbidden += other.forbidden
22+
self.forbidden = list(set(self.forbidden))
23+
24+
return self
25+
26+
class UserScope(Scope):
27+
forbidden = ['v1.user+super_get_user',
28+
'v1.user+super_delete_user']
29+
def __init__(self):
30+
self + AdminScope()
31+
32+
class AdminScope(Scope):
33+
allow_module = ['v1.user']
34+
35+
def __init__(self):
36+
pass
37+
38+
class SuperScope(Scope):
39+
allow_module = ['v1.user']
40+
41+
def __init__(self):
42+
#运算符重载 直接调用基类的 __add__
43+
pass
44+
45+
AdminScope()
46+
SuperScope()
1047

1148
def is_in_scope(scope, endpoint):
12-
cls = globals()[scope]
13-
print(cls.allow_api)
14-
print(endpoint)
15-
if endpoint in cls().allow_api:
49+
scope = globals()[scope]()
50+
splits = endpoint.split('+')
51+
red_name = splits[0]
52+
53+
if endpoint in scope.forbidden:
54+
return False
55+
56+
if endpoint in scope.allow_api:
57+
return True
58+
59+
if red_name in scope.allow_module:
1660
return True
1761
else:
1862
return False

app/notes/test/s2_04.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#encoding:utf-8
2+
#关于 继承 变量寻找关系
3+
#子类继承父类 若某个方法在父类中 子类执行此方法时 执行某个变量的操作
4+
#此变量的寻找 (若此变量在父类中存在) 先从子类中寻找 若子类中没有 再去父类中寻找
5+
#变量寻找关系 先从子类中寻找 再去父类中寻找
6+
7+
class Scope:
8+
# allow_api = ['v2.googogogoogogog']
9+
# def __init__(self):
10+
# self.allow_api = ['v2.googogogoogogog']
11+
def __add__(self, other):
12+
#寻找allow_api 线路 先从继承此类的 子类当中寻找allow_api
13+
#若子类当中没有 allow_api 在从当前父类中寻找
14+
self.allow_api += other.allow_api
15+
return self
16+
17+
class UserScope(Scope):
18+
allow_api = ['v1.get_user']
19+
20+
class AdminScope(Scope):
21+
allow_api = ['v1.A']
22+
23+
def __init__(self):
24+
self + UserScope
25+
print(self.allow_api)
26+
27+
class SuperScope(Scope):
28+
allow_api = ['v1.super_get_user']
29+
30+
def __init__(self):
31+
#运算符重载 直接调用基类的 __add__
32+
self + AdminScope + UserScope
33+
print(self.allow_api)
34+
35+
AdminScope()
36+
SuperScope()

0 commit comments

Comments
 (0)