-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrp_3.py
More file actions
58 lines (44 loc) · 1.39 KB
/
rp_3.py
File metadata and controls
58 lines (44 loc) · 1.39 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
# flask_marshmallow
# slqalchemy
from flask import Flask, request, jsonify, Blueprint
from flask_restplus import Resource, Api, fields
from flask_marshmallow import Marshmallow
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
users = Blueprint('users', __name__, url_prefix='/api')
api = Api(users, doc='/doc')
app.register_blueprint(users)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root@localhost:3306/tc'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
ma = Marshmallow(app)
from app.models.users_dua import User
class UserSchema(ma.ModelSchema):
class Meta:
model = User
a_user = api.model('User',
{
'email' : fields.String('email.'),
'username' : fields.Integer('username')
}
)
@app.route('/')
def index():
return 'hallo'
@api.route('/users')
class UserRest(Resource):
def get(self):
one_user = User.query.all()
user_schema = UserSchema(many=True)
output = user_schema.dump(one_user).data
return jsonify({'user':output})
@api.expect(a_user)
def post(self):
username = request.json.get('username')
email = request.json.get('email')
user = User(username=username, email=email)
db.session.add(user)
db.session.commit()
return {'result':'User added.'}
if __name__ == '__main__':
app.run(debug=True)