-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.py
135 lines (115 loc) · 4.35 KB
/
main.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
from flask import Flask, jsonify, request, send_from_directory
from flask_cors import CORS
from flask_jwt_extended import create_access_token, get_jwt_identity, jwt_required, JWTManager
from models import User, Todo, db
from sqlalchemy.exc import IntegrityError
from datetime import timedelta
app = Flask(__name__)
''' Begin boilerplate code '''
def create_app():
app = Flask(__name__, static_url_path='', static_folder='static')
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
app.config['JWT_SECRET_KEY'] = "MYSECRET"
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['JWT_EXPIRATION_DELTA'] = timedelta(days = 7)
CORS(app)
db.init_app(app)
return app
app = create_app()
jwt = JWTManager(app)
app.app_context().push()
db.create_all()
''' End Boilerplate Code '''
def get_user(username):
return User.query.filter_by(username=username).first()
@app.route('/', methods=['GET'])
def index():
return app.send_static_file('index.html')
@app.route("/login", methods=["POST"])
def login():
data = request.get_json()
user = get_user(data['username'])
if user and user.check_password(data['password']):
access_token = create_access_token(identity=data['username'])
return jsonify(access_token=access_token)
return jsonify({"msg": "Bad username or password"}), 401
@app.route('/signup', methods=['POST'])
def signup():
userdata = request.get_json() # get userdata
newuser = User(username=userdata['username'], email=userdata['email']) # create user object
newuser.set_password(userdata['password']) # set password
try:
db.session.add(newuser)
db.session.commit() # save user
except IntegrityError: # attempted to insert a duplicate user
db.session.rollback()
return jsonify({ "error" : "username or email already exists"}) # error message
return jsonify({ "message" : "user created"}) # success
@app.route('/identify')
@jwt_required()
def protected():
user = get_user(get_jwt_identity())
return jsonify(user.toDict())
@app.route('/users', methods=['GET'])
def get_users():
users = User.query.all()
users_list = [ user.toDict() for user in users ]
# convert user objects to list of dictionaries
return jsonify({ "num_users": len(users_list), "users": users_list })
@app.route('/todos', methods=['POST'])
@jwt_required()
def create_todo():
data = request.get_json()
todo = Todo(text=data['text'], userid=get_user(get_jwt_identity()).id, done=False)
db.session.add(todo)
db.session.commit()
return jsonify({ 'id' : todo.id}), 201 # return data and set the message code
@app.route('/todos', methods=['GET'])
@jwt_required()
def get_todos():
todos = Todo.query.filter_by(userid=get_user(get_jwt_identity()).id).all()
todos = [todo.toDict() for todo in todos] # list comprehension which converts todo objects to dictionaries
return jsonify(todos)
@app.route('/todos/<id>', methods=['GET'])
@jwt_required()
def get_todo(id):
todo = Todo.query.filter_by(userid=get_user(get_jwt_identity()).id, id=id).first()
if todo == None:
return jsonify({'error':'Invalid id or unauthorized'})
return jsonify(todo.toDict())
@app.route('/todos/<id>', methods=['PUT'])
@jwt_required()
def update_todo(id):
todo = Todo.query.filter_by(userid=get_user(get_jwt_identity()).id, id=id).first()
if todo == None:
return jsonify({'error':'Invalid id or unauthorized'})
data = request.get_json()
if 'text' in data: # we can't assume what the user is updating so we check for the field
todo.text = data['text']
if 'done' in data:
todo.done = data['done']
db.session.add(todo)
db.session.commit()
return jsonify({'message':'Updated'}), 201
@app.route('/todos/<id>', methods=['DELETE'])
@jwt_required()
def delete_todo(id):
todo = Todo.query.filter_by(userid=get_user(get_jwt_identity()).id, id=id).first()
if todo == None:
return jsonify(message='Invalid id or unauthorized')
db.session.delete(todo) # delete the object
db.session.commit()
return jsonify({'message':'Deleted'}), 200
@app.route('/stats/todos', methods=['GET'])
@jwt_required()
def get_todo_stats():
user = User.query.get(get_user(get_jwt_identity()).id)
if user:
return jsonify({
"num_todos": user.get_num_todos(),
"num_done": user.get_done_todos()
})
else :
return jsonify({'message': 'User not found'}), 404
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080, debug=True)