-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecorators.py
47 lines (34 loc) · 971 Bytes
/
decorators.py
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
from functools import wraps
from flask import Flask, request
from flask_restful import Resource, Api
def authorize(func):
@wraps(func)
def authorize_user(*args, **kwargs):
print('executed before login')
if request.headers.get('Authorization', None) is not None:
return func(*args, **kwargs)
else:
print('user should be authorize')
print('executed after')
return authorize_user
def doctor(func):
@wraps(func)
def is_doctor(*args, **kwargs):
if request.headers.get('Role', None) == 'doctor':
return func(*args, **kwargs)
else:
print('user must be doctor')
return is_doctor
@doctor
@authorize
def login():
print('hey login')
class LoginResource(Resource):
@authorize
def post(self):
return 'success'
app = Flask(__name__)
api = Api(app)
api.add_resource(LoginResource, '/auth/login')
if __name__ == '__main__':
app.run()