-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest_api_stuff.py
62 lines (51 loc) · 2.52 KB
/
rest_api_stuff.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
61
62
from flask import jsonify
from flask_login import current_user
from flask_restful import Resource, reqparse
from data import db_session
from data.user import User
parser = reqparse.RequestParser()
parser.add_argument('username', required=True)
parser.add_argument('password', required=True)
class UserResource(Resource):
def get(self, id_user):
"""get user by id by get request e.g http://localhost:5000/api/user/121231"""
db_sess = db_session.create_session()
a = db_sess.query(User).get(id_user)
return jsonify({"user": [a.to_dict(only=('username', 'score', 'medium', 'games_played', 'created_date'))]})
def delete(self, id_user):
"""delete user by delete request e.g http://localhost:5000/api/user/346342.
Only if it is your account, so your id must be 346342"""
if current_user.id == id_user:
db_sess = db_session.create_session()
a = db_sess.query(User).get(id_user)
if a:
db_sess.delete(a)
db_sess.commit()
return jsonify({'status': ['success']})
else:
return jsonify({'status': ['not found']})
class AllUsersResource(Resource):
def get(self):
"""get top ten users by their scores e.g http://localhost:5000/api/users"""
db_sess = db_session.create_session()
a = db_sess.query(User).order_by(User.score.desc()).limit(10)
return jsonify(
{'users': [i.to_dict(only=('username', 'score', 'medium', 'games_played', 'created_date')) for i in a]})
def post(self):
"""adding new user (if data is correct) by post request
e.g post('http://localhost:5000/api/users', json={'username': 'artem', 'password': 'artem22'})"""
args = parser.parse_args()
session = db_session.create_session()
user = User()
user.username = args['username']
if len(args['password']) <= 6:
return jsonify({'success': 'Bad password'})
user.create_password_hash(args['password'])
session.add(user)
session.commit()
return jsonify({'success': 'OK'})
# from requests import post, get
# print(get('http://localhost:5000/api/users').json()) # get top 10 users by score print(post(
# 'http://localhost:5000/api/users', json={'username': 'artem', 'password': 'artem22'}).json() # add new user.
# Note that password must be 6 or more symbols
# print(get('http://localhost:5000/api/users/2').json()) # get user by id