commit 774f489f80f58596c6f192024720e26b3e1f1e73 Author: Tdxdxoz Date: Fri Dec 25 00:22:34 2020 +0800 basic diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e52575b --- /dev/null +++ b/.gitignore @@ -0,0 +1,149 @@ +# ---> Python + +#sqlite +*.db + +#config +config.py + +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# custom generated files +tmp/ diff --git a/app.py b/app.py new file mode 100644 index 0000000..76588fa --- /dev/null +++ b/app.py @@ -0,0 +1,91 @@ +from flask import Flask, request, render_template, send_from_directory, abort, redirect, session +from flask_sqlalchemy import SQLAlchemy +from flask_limiter import Limiter +from flask_limiter.util import get_remote_address + +import ipfshttpclient + +from datetime import date, datetime +from werkzeug.utils import secure_filename +import os +from config import C + +app = Flask(__name__) +app.config.from_object('config.C') +app.secret_key = C.session_key + +limiter = Limiter( + app, + key_func=get_remote_address, + default_limits=["50 / minute"], +) + +db = SQLAlchemy(app) + +ipfs_client = ipfshttpclient.connect() + +class Paper(db.Model): + id = db.Column(db.Integer, primary_key=True) + course = db.Column(db.String(30), index=True) + teacher = db.Column(db.String(10), index=True) + year = db.Column(db.Integer, index=True) + author = db.Column(db.String(30), index=True) + create_date = db.Column(db.Date) + like_num = db.Column(db.Integer, index=True, default=0) + file_hash = db.Column(db.String(64)) + +db.create_all() + +# TODO 登陆 + +@app.route('/pastExam/') +def list(): + ps = Paper.query + ps = ps.order_by(db.desc('like_num')) + pagination = ps.paginate(max_per_page=100) + curr_year = date.today().year + ipfs_base_url = C.ipfs_base_url + return render_template('list.html', **locals()) + +@app.route('/pastExam/upload', methods=['POST']) +@limiter.limit("10 / hour") +def upload(): + username = 'guest' # TODO + name = request.form.get('name') + teacher = request.form.get('teacher') + year = request.form.get('year') + year = year and year.isdigit() and int(year) or 0 + + # TODO 检查长度 + files = request.files.getlist('files[]') + + dir_name = username + str(datetime.now()) + base_path = os.path.join('/tmp', dir_name) + 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) + file_hash = '' + for r in res: + if r.get('Name') == dir_name: + file_hash = r.get('Hash') + + if not file_hash: + abort(500) + + paper = Paper( + course=name, + teacher=teacher, + year=year, + author=username, + create_date=date.today(), + file_hash=file_hash + ) + db.session.add(paper) + db.session.commit() + + return redirect('.') + diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..1ba69ec --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +Flask==1.1.2 +Flask_Limiter==1.3.1 +Flask_SQLAlchemy==2.4.4 +ipfshttpclient==0.7.0a1 diff --git a/templates/list.html b/templates/list.html new file mode 100644 index 0000000..34d8309 --- /dev/null +++ b/templates/list.html @@ -0,0 +1,351 @@ + + + + + + + + + + + + + 华清大学课程攻略 + + + + +
+
+

华清大学
 课程攻略

+
+ +
+ +
+
+
+

上传

+

欢迎分享你手上的往年考题,大家一起实现信息共享,消除信息不对等。课程名请与已有的保持一致,方便查询。

+ +
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+
+ + +
+

TODO: 设置是否匿名展示

+ +

*发布内容的版权属于上传者/原作者/公有领域,基于上传前的版权情况。文件可以多选,电脑端一般是按住ctrl选择,移动端一般是直接勾选多个。对文件数量和大小没有限制,总大小不要太过分就行。时间请填写该学期开始时的年份。

+
+
+
+ +
+ + +
+
+
+
+ +
+
+ +
+
+ #课程1 #课程2 TODO +
+
+
+
+
+
+ +
+
+
+
+ #教师1 #教师2 TODO +
+
+
+
+
+
+ +
+
+
+
+ #年份1 #年份2 TODO +
+
+
+
+ + + {% for p in pagination.items %} +
+ {% if showPrivate %} +
+ + +
+ {% endif %} +

+ {{p.course}} - {{p.teacher}} - {{p.year or '/'}} + 查看 +

+
+ @{{p.author}} | {{p.create_date}} + + + + + + {{p.like_num}} + + +
+
+ {% endfor %} + + + +
+ +
+ + + + + + + + + +