-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathusers.py
60 lines (45 loc) · 1.4 KB
/
users.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
48
49
50
51
52
53
54
55
56
57
58
59
60
from flask import Flask, request
from flask_restful import Resource, Api, reqparse
from db import User
from helpers import JSONDataEncoder
app = Flask('users')
api = Api(app)
class MyConfig(object):
RESTFUL_JSON = {
'indent': 2,
'cls': JSONDataEncoder
}
app.config.from_object(MyConfig)
class Users(Resource):
def __init__(self, *args, **kwargs):
self.parser = reqparse.RequestParser()
self.parser.add_argument('username', type=str, required=True)
self.parser.add_argument('password', type=str, required=True)
self.parser.add_argument('github', type=str, required=True)
self.parser.add_argument('groups', type=str, required=True, action="append")
super().__init__(*args, **kwargs)
def get(self):
response = []
for user in User.scan():
response.append(user.token_payload)
return {
'users': response
}
def post(self):
args = self.parser.parse_args()
print(args)
user = User(
username=args.get('username'),
password=args.get('password'),
github=args.get('github'),
groups=set(args.get('groups'))
)
user.save()
user.refresh()
return {
"user": user.token_payload
}
api.add_resource(Users, '/')
# local run
if __name__ == '__main__':
app.run(debug=True)