-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
585 lines (492 loc) · 18.7 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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
from base64 import decode
import math
import time
import uuid
import hashlib
import os
from flask import g, request, send_from_directory
from flask import json
from werkzeug.exceptions import HTTPException
from PIL import Image
import geoip2.database
from models import *
from captcha import captcha
from gitscratch_init import app, db
from flask_migrate import Migrate
migrate = Migrate(app, db)
geoip2reader = geoip2.database.Reader(
'geolite2/GeoLite2-City.mmdb')
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
ASSETS_FOLDER = 'assets'
COMMITS_FOLDER = 'commits'
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
def error(message="Unknown error"):
return json.dumps(
{"status": "error",
"message": message}
), 500
def unauthorized(message="Unauthorized"):
return json.dumps(
{"status": "error",
"message": message}
), 401
def not_found(message="Not found"):
return json.dumps({
"status": "error",
"message": message
}), 404
def success(data=None):
return json.dumps(
{"status": "success",
"data": data}
)
@app.route("/")
def index():
"""For test only"""
if g.user:
return g.user.name
return success()
@app.before_request
def load_logged_in_user():
"""Check if user is logged in"""
# print(request.headers)
if 'X-Real-IP' in request.headers:
g.ip = request.headers['X-Real-IP']
else:
g.ip = request.remote_addr
try:
region = geoip2reader.city(g.ip)
g.ip_region = region.country.names['zh-CN']+" " + \
region.subdivisions.most_specific.names['zh-CN']
# +region.city.names['zh-CN']
except:
g.ip_region = '未知'
# print(g.ip_region)
if 'X-Gitscratch-Session' in request.headers:
_session = request.headers['X-Gitscratch-Session']
else:
_session = None
if _session is not None: # if headers has session
user = User.query.filter_by(_session=_session).first()
if user is not None: # if session is valid
if time.time() - user._session_time <= 60 * 60: # 1 hour
user._session_time = int(time.time())
db.session.commit()
g.user = user
else: # session expired
g.user = None
else: # if session is invalid
g.user = None
else: # if headers has no session
g.user = None
@app.errorhandler(HTTPException)
def handle_exception(e):
"""Return JSON for HTTP errors. Only available for production environment."""
return error(str(e.code) + " " + e.name)
@app.route("/auth/captcha", methods=["GET"])
def auth_captcha():
"""Get captcha image"""
_captcha = captcha.getCaptcha()
_uuid = str(uuid.uuid4())
db.session.add(Captcha(
uuid=_uuid,
id=_captcha['id'],
created_at=int(time.time())))
db.session.commit()
# print(_captcha)
return success(
{
"captcha_uuid": _uuid,
"captcha_base64": _captcha['base64']
}
)
# return error()
@app.route("/auth/register", methods=["POST"])
def auth_register():
"""Register new user"""
email = request.json['email']
name = request.json['username']
password = request.json['password']
captcha_uuid = request.json['captcha_uuid']
captcha_value = request.json['captcha_value']
_captcha = db.session.query(Captcha).filter_by(uuid=captcha_uuid).first()
if (not _captcha) or (not captcha.checkCaptcha(_captcha.id, captcha_value)) or (time.time() - _captcha.created_at > 15*60): # 15min
return error("Invalid captcha")
if db.session.query(
User.query.filter_by(email=email).exists()
).scalar():
return error("Email already exists")
db.session.add(User(
email=email,
name=name,
password=password,
created_at=int(time.time())))
db.session.commit()
return success()
@app.route("/auth/login", methods=["POST"])
def auth_login():
"""Login user"""
email = request.json['email']
password = request.json['password']
user = db.session.query(User).filter_by(email=email).first()
if not user or not user.checkPassword(password):
return error("Invalid username or password")
_uuid = str(uuid.uuid4())
user._session = _uuid
user._session_time = int(time.time())
db.session.commit()
# session['session'] = _uuid
return success({'session': _uuid})
@app.route("/auth/session", methods=["GET"])
def auth_session():
"""Get user session"""
if g.user:
return success({'data': g.user.to_json(is_user=True)})
return unauthorized()
@app.route("/auth/logout", methods=["POST"])
def auth_logout():
"""Logout user"""
if g.user:
g.user._session = ""
g.user._session_time = 0
db.session.commit()
return success()
@app.route("/users/<id>/info", methods=["GET"])
def users_info(id):
"""Get user info"""
user = db.session.query(User).filter_by(id=id).first()
if not user:
return not_found("User does not exist")
if g.user.id == id:
return success(user.to_json(is_user=True))
return success(user.to_json())
@app.route("/users/<id>/info", methods=["POST"])
def users_info_update(id):
"""Update user info"""
user = db.session.query(User).filter_by(id=id).first()
if not user or g.user == None:
return not_found("User does not exist")
elif(user.id == g.user.id):
if 'name' in request.json:
user.name = request.json['name']
if 'email' in request.json:
user.email = request.json['email']
if 'website' in request.json:
user.website = request.json['website']
if 'bio' in request.json:
user.bio = request.json['bio']
if 'avatar' in request.json:
user.avatar = request.json['avatar']
if 'readme' in request.json:
user.readme = request.json['readme']
db.session.commit()
return success()
return unauthorized()
def getProjects(target_id):
# query = db.session.query(Project).filter_by(_author=target_id).order_by(
# Project.created_at)
query = db.session.query(Project).filter_by(_author=target_id)
projects = query.all()
_projects = [project.to_json() for project in projects]
# print(time.time()-startTime)
return success({'projects': _projects, 'totalProjects': query.count()})
@app.route("/users/<id>/projects", methods=["GET"])
def users_projects(id):
"""Get user projects"""
return getProjects(target_id=id)
@app.route("/users/<id>/projects/new", methods=["POST"])
def users_projects_new(id):
"""Create new user project"""
if(g.user == None):
return unauthorized()
with open("default.json", "rb") as default_project:
file_md5 = hashlib.md5(default_project.read()).hexdigest()
with open(os.path.join(COMMITS_FOLDER, file_md5), "wb") as new_file:
new_file.write(default_project.read())
new_file.flush()
new_project = Project(
_author=g.user.id,
_head=file_md5
)
db.session.add(new_project)
db.session.flush()
new_commit = Commit(
hash=file_md5,
time=int(time.time()),
_author=g.user.id,
_project_id=new_project.id
)
db.session.add(new_commit)
db.session.flush()
db.session.commit()
return success({"id": new_project.id})
@app.route("/projects/<id>/info", methods=["GET"])
def projects_info(id):
"""Get project info"""
project = db.session.query(Project).filter_by(id=id).first()
if not project:
return not_found("Project does not exist")
return success(project.to_json(user=g.user))
@app.route("/projects/<id>/operation", methods=["POST"])
def projects_operation(id):
"""Add project operation"""
project = db.session.query(Project).filter_by(id=id).first()
if not project:
return not_found("Project does not exist")
if not g.user:
return unauthorized()
operation_type = request.json['type']
if (operation_type in ['project.view', 'project.star', 'project.like']):
operation = UserOperation.query.filter_by(
_target_type="project", _target_id=id, type=operation_type, _user=g.user.id) # project.view, project.star, project.like
if operation.count() > 0:
operation.delete()
db.session.commit()
return success()
operation = UserOperation(
_target_type="project", _target_id=id, type=operation_type, _user=g.user.id)
db.session.add(operation)
db.session.commit()
return success()
return error("Invalid operation type")
@app.route("/projects/<id>/info", methods=["POST"])
def projects_info_update(id):
"""Update project info"""
project = db.session.query(Project).filter_by(id=id).first()
if not project or g.user is None:
return not_found("Project does not exist")
elif project.author.id == g.user.id:
if 'title' in request.json:
project.title = request.json['title']
if 'readme' in request.json:
project.readme = request.json['readme']
if 'source' in request.json:
project.source = request.json['source']
if 'public' in request.json:
project.public = request.json['public']
if 'status' in request.json:
project.status = request.json['status']
db.session.commit()
return success()
return unauthorized()
@app.route("/projects/<id>/json", methods=["GET"])
def projects_json(id):
"""Get project source"""
commit_hash = request.args.get("commit")
project = db.session.query(Project).filter_by(id=id).first()
if not project:
return not_found("Project does not exist")
if commit_hash is None:
commit_hash = project.head.hash
commit = db.session.query(Commit).filter_by(
_project_id=id, hash=commit_hash)
if not commit:
return error("Invalid commit")
return send_from_directory(COMMITS_FOLDER, commit_hash)
@app.route("/projects/<id>/json", methods=["POST"])
def projects_json_commit(id):
"""Update project source"""
project = db.session.query(Project).filter_by(id=id).first()
if not project:
return not_found("Project does not exist")
f = request.files['file']
md5 = hashlib.md5(f.read()).hexdigest()
filename = md5
filepath = os.path.join(COMMITS_FOLDER, filename)
if not os.path.isfile(filepath):
f.seek(0)
f.save(filepath)
new_commit = Commit(
hash=md5,
time=int(time.time()),
_author=g.user.id,
_project_id=id
)
db.session.add(new_commit)
return success({'filename': filename})
if(commit_hash == None):
commit_hash = project.head.hash
commit = db.session.query(Commit).filter_by(
_project_id=id, hash=commit_hash)
if not commit:
return error("Invalid commit")
return send_from_directory(COMMITS_FOLDER, commit_hash)
# def getPosts(target_id):
# # query = db.session.query(Project).filter_by(_author=target_id).order_by(
# # Project.created_at)
# query = db.session.query(Post).filter_by(_user=target_id)
# posts = query.all()
# _posts = [post.to_json() for post in posts]
# # print(time.time()-startTime)
# return success({'posts': _posts, 'totalPosts': query.count()})
def getPosts():
query = db.session.query(Post).filter_by(id > 1 , _user = 1)
_posts = [post.to_json() for post in query.all()]
return success({'posts':_posts})
# 需要重构分页算法
# 传入一个时间戳 缺省值为当前时间
# 返回时间戳前最近的n篇帖子
# 要加载下一页只需要传入n篇中的最后一篇的时间戳
# 也可以传入时间范围 比如要获取最新的帖子
# 只要传回已加载的最新一篇的时间戳 查找更新的帖子
# 其他接口同理 比如获取作品啊 获取评论之类
# pageSize = 10 # once this value is changed, the database should be reset
# query = db.session.query(Post).filter_by(_user=target_id).order_by(
# Post.created_at)
# pageId = int(args['pageId']
# ) if 'pageId' in args else query.paginate(per_page=10, error_out=False).pages
# pagination = query.paginate(page=pageId, per_page=10, error_out=False)
# posts = pagination.items
# _posts = [post.to_json() for post in posts]
# # print(time.time()-startTime)
# return success({'posts': _posts, 'pageId': pageId, 'pageSize': pageSize, 'totalPages': pagination.pages, 'totalPosts': query.count()})
@app.route("/users/<id>/posts/new", methods=["POST"])
def users_posts_new(id):
"""Create new user post"""
if(g.user == None):
return unauthorized()
new_post = Post(
_user=g.user.id,
page_id=db.session.query(Post).order_by(
Post.time).count()//10+1,
created_at=int(time.time())
)
db.session.add(new_post)
db.session.commit()
return success({"id": new_post.id})
@app.route("/posts",methods=['GET'])
def posts_get():
return getPosts()
@app.route("/posts/<id>/info", methods=["GET"])
def posts_info(id):
"""Get post info"""
post = db.session.query(Post).filter_by(id=id).first()
if not post:
return error("Invalid id")
return success(post.to_json())
@app.route("/posts/<id>/info", methods=["POST"])
def posts_info_update(id):
"""Update post info"""
post = db.session.query(Post).filter_by(id=id).first()
if not post or g.user == None:
return error("Invalid id")
elif post._user == g.user.id:
if 'title' in request.json:
post.title = request.json['title']
if 'content' in request.json:
post.content = request.json['content']
if 'status' in request.json:
post.status = request.json['status']
post.updated_at = int(time.time())
db.session.commit()
return success()
return unauthorized()
def getComments(target_type, target_id, args):
pageSize = 10 # once this value is changed, the database should be reset
query = db.session.query(Comment).filter_by(target_type=target_type, target_id=target_id).order_by(
Comment.time)
pageId = int(args['pageId']
) if 'pageId' in args else query.paginate(per_page=10, error_out=False).pages
pagination = query.paginate(page=pageId, per_page=10, error_out=False)
comments = pagination.items
_comments = [comment.to_json() for comment in comments]
# print(time.time()-startTime)
return success({'comments': _comments, 'pageId': pageId, 'pageSize': pageSize, 'totalPages': pagination.pages, 'totalComments': query.count()})
def newComment(target_type, target_id, request_json):
if g.user is None:
return unauthorized()
db.session.add(Comment(
comment=request_json['comment'],
page_id=db.session.query(Comment).filter_by(target_type=target_type, target_id=target_id).order_by(
Comment.time).count()//10+1,
target_type=target_type,
target_id=target_id,
_reply=request_json['reply'] if 'reply' in request_json else 0,
_user=g.user.id,
time=int(time.time()),
region=g.ip_region,
_ip=g.ip
))
db.session.commit()
return success()
def updateComment(target_type, target_id, request_json):
if target_type == 'user':
user = db.session.query(User).filter_by(id=target_id).first()
elif target_type == 'project':
user = db.session.query(Project).filter_by(id=target_id).first().author
if not user or g.user is None:
return error("Invalid id")
if user.id == g.user.id:
comment_id = request_json['id']
comment = db.session.query(Comment).filter_by(id=comment_id).first()
if not comment:
return error("Invalid comment id")
if 'comment' in request_json:
comment.comment = request_json['comment']
if 'status' in request_json:
comment.status = request_json['status']
db.session.commit()
return success()
return unauthorized()
@app.route("/users/<id>/comments", methods=["GET"])
def users_comments(id):
"""Get user comments"""
return getComments(target_type="user", target_id=id, args=request.args)
@app.route("/users/<id>/comments/new", methods=["POST"])
def users_comments_new(id):
"""Create new comment"""
return newComment(target_type='user', target_id=id, request_json=request.json)
@app.route("/users/<id>/comments", methods=["POST"])
def users_comments_update(id):
"""Update comment"""
return updateComment(target_type='user', target_id=id, request_json=request.json)
@app.route("/projects/<id>/comments", methods=["GET"])
def projects_comments(id):
"""Get project comments"""
return getComments(target_type="project", target_id=id, args=request.args)
@app.route("/projects/<id>/comments/new", methods=["POST"])
def projects_comments_new(id):
"""Create new comment"""
return newComment(target_type='project', target_id=id, request_json=request.json)
@app.route("/projects/<id>/comments", methods=["POST"])
def projects_comments_update(id):
"""Update comment"""
return updateComment(target_type='project', target_id=id, request_json=request.json)
@app.route("/forum/<id>/comments", methods=["GET"])
def posts_comments(id):
"""Get post comments"""
return getComments(target_type="post", target_id=id, args=request.args)
@app.route("/forum/<id>/comments/new", methods=["POST"])
def posts_comments_new(id):
"""Create new comment"""
return newComment(target_type='post', target_id=id, request_json=request.json)
@app.route("/forum/<id>/comments", methods=["POST"])
def posts_comments_update(id):
"""Update comment"""
return updateComment(target_type='post', target_id=id, request_json=request.json)
@app.route('/assets/upload', methods=['POST'])
def assets_upload():
f = request.files['file']
ext = f.filename.rsplit('.', 1)[1]
md5 = hashlib.md5(f.read()).hexdigest()
filename = md5 + '.' + ext
filepath = os.path.join(ASSETS_FOLDER, filename)
if not os.path.isfile(filepath):
f.seek(0)
f.save(filepath)
return success({'filename': filename})
@app.route('/assets/<filename>', methods=['GET'])
def assets_get(filename):
return send_from_directory(ASSETS_FOLDER, filename)
@app.after_request
def apply_caching(response):
response.headers["Access-Control-Allow-Origin"] = "*"
response.headers["Access-Control-Allow-Method"] = "*"
response.headers["Access-Control-Allow-Headers"] = "*"
response.headers["Server"] = "Python with Super Cow Powers"
if g.user is None:
response.headers['X-GitScratch-User'] = 'None'
return response
if __name__ == "__main__":
app.run(debug=True, port=3000, host="0.0.0.0")