-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.py
172 lines (133 loc) · 4.56 KB
/
routes.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import os
import functools
import flask
from werkzeug.utils import secure_filename
from app import app,Logger
from auth import flask_login
from models import User
from flask import request, jsonify, g
ALLOWED_EXTENSIONS = {'png', 'jpg', 'txt','html', 'css', 'js'}
UPLOAD_FOLDER = 'projects'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
@app.before_request
def before_request():
g.user = flask_login.current_user
def print_current_user():
app.logger.info('current user: ' + flask_login.current_user.id)
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
def post_only(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
if request.method == 'GET':
return jsonify({'status': False, 'cause': 'only POST is supported'})
else:
return func(*args, **kwargs)
return wrapper
def before_login_only(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
if g.user is not None and g.user.is_authenticated:
return jsonify({'status': False, 'cause': 'already logged in'})
else:
return func(*args, **kwargs)
return wrapper
############################ routes ################################
@app.route('/', methods=['GET', 'POST'])
# @post_only
def home():
return '''
<!doctype html>
<title>Clover</title>
<h1>Homepage For the Clover Project</h1>
<p>Enjoy it with given APIs!</p>
'''
# 注册
@app.route('/register', methods=['GET', 'POST'])
@post_only
@before_login_only
def register():
# if request.method == 'GET':
# return '''
# <!doctype html>
# <title>User Register</title>
# <h1>register</h1>
# <form action="" method=post enctype=multipart/form-data>
# <p><input type=text name=username>
# <input type=password name=password>
# <input type=submit value=register>
# </form>
# '''
username = request.json['username']
password = request.json['password']
return jsonify(User.register(username, password))
# 登入/登出
@app.route('/login', methods=['GET', 'POST'])
@post_only
@before_login_only
def login():
#
#
# if request.method == 'GET':
# # if g.user is not None and g.user.is_authenticated:
# # return jsonify({'status': False, 'cause': 'already logged in'})
# Logger.info('login GET')
# return jsonify({'status': False, 'cause': 'only POST is allowed'})
# # return '''
# # <!doctype html>
# # <title>Login</title>
# # <h1>login</h1>
# # <form action="" method=post enctype=multipart/form-data>
# # <p><input type=text name=username>
# # <input type=password name=password>
# # <input type=submit value=login>
# # </form>
# # '''
username = request.json['username']
password = request.json['password']
Logger.info('login POST')
auth_result = User.auth(username, password)
if auth_result['status']:
Logger.debug('before login_user')
user = User.get(username)
flask_login.login_user(user)
Logger.debug('after login_user')
return flask.jsonify({'status': True})
else:
return flask.jsonify(auth_result)
@app.route('/logout', methods=['GET'])
@flask_login.login_required
def logout():
flask_login.logout_user()
return flask.jsonify({'status': True})
@app.route('/users/<username>/projects/<int:project_id>', methods=['POST', 'DELETE'])
@flask_login.login_required
def get_static(username, project_id):
# print_current_user()
if flask_login.current_user.id == username:
return flask.send_from_directory('projects', str(project_id)+'.png')
@app.route('/users/<username>/projects/', methods=['GET', 'POST'])
@flask_login.login_required
def upload_project(username):
if flask.request.method == 'GET':
return '''
<!doctype html>
<title>Upload new File</title>
<h1>Upload new File</h1>
<form action="" method=post enctype=multipart/form-data>
<p><input type=file name=file>
<input type=submit value=Upload>
</form>
'''
else:
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return jsonify({'status': True})
else:
return jsonify({'status': False})
@app.route('/after_login', methods=['GET'])
@flask_login.login_required
def after_login():
return '<h1>You\'ve logged in<h1>'