From e1ee8f6a203479112676bba64ac6d14874f9b546 Mon Sep 17 00:00:00 2001 From: Tdxdxoz Date: Fri, 25 Dec 2020 21:49:52 +0800 Subject: [PATCH] feature: notes & anonymous & change UI --- app.py | 20 ++++-- templates/list.html | 145 +++++++++++++++++++++++--------------------- 2 files changed, 92 insertions(+), 73 deletions(-) diff --git a/app.py b/app.py index 1314097..a221bc5 100644 --- a/app.py +++ b/app.py @@ -30,6 +30,8 @@ class Paper(db.Model): teacher = db.Column(db.String(10), index=True) year = db.Column(db.Integer, index=True) author = db.Column(db.String(30), index=True) + notes = db.Column(db.String(200)) + anon = db.Column(db.Boolean) create_date = db.Column(db.Date) like_num = db.Column(db.Integer, index=True, default=0) file_hash = db.Column(db.String(64)) @@ -50,6 +52,9 @@ def list(): ipfs_base_url = C.ipfs_base_url return render_template('list.html', **locals()) +def check_length(x, limit=30, allow_null=False): + return (x and len(x) <= limit) or (allow_null and not x) + @app.route('/pastExam/upload', methods=['POST']) @limiter.limit("10 / hour") def upload(): @@ -58,8 +63,14 @@ def upload(): teacher = request.form.get('teacher') year = request.form.get('year') year = year and year.isdigit() and int(year) or 0 - - # TODO 检查长度 + notes = request.form.get('notes', '') + anon = request.form.get('anon') == 'on' + + if not (check_length(name) and check_length(teacher, 10) and check_length(notes, 200, True)): + abort(422) + + print(request.form) + files = request.files.getlist('files[]') dir_name = username + str(datetime.now()) @@ -67,7 +78,6 @@ def upload(): os.mkdir(base_path) for f in files: filename = f.filename.replace('/','_') - print(f, filename) f.save(os.path.join(base_path, filename)) res = ipfs_client.add(base_path) @@ -83,6 +93,8 @@ def upload(): course=name, teacher=teacher, year=year, + notes=notes, + anon=anon, author=username, create_date=date.today(), file_hash=file_hash @@ -90,5 +102,5 @@ def upload(): db.session.add(paper) db.session.commit() - return redirect('.') + return redirect('.#part2') diff --git a/templates/list.html b/templates/list.html index 2fb07d1..f428fa3 100644 --- a/templates/list.html +++ b/templates/list.html @@ -13,12 +13,12 @@ 华清大学课程攻略