提交 c262db85 编写于 作者: X xmq

Initial commit

上级 b14b859e
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.7" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/woniuNote.iml" filepath="$PROJECT_DIR$/.idea/woniuNote.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
<mapping directory="$PROJECT_DIR$/python-flask" vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
from flask import Flask, abort, render_template
from flask_sqlalchemy import SQLAlchemy
import os
import pymysql
pymysql.install_as_MySQLdb() # 解决 “No module named 'MySQLdb'” 的错误
app = Flask(__name__, template_folder='template',
static_folder='resource')
app.config['SECRET_KEY'] = os.urandom(24) # 生成随机数种子,用于产生SessionID
# 使用集成方式处理SQLAlchemy
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:MySQL666@localhost:3306/woniunote?charset=utf8'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False # True:更新数据库的修改,及时发送信号
# 实例化db对象
db = SQLAlchemy(app)
# 定义404错误页面
@app.errorhandler(404)
def page_notfound(e):
return render_template('error-404.html')
# 定义500错误页面
@app.errorhandler(500)
def page_notfound(e):
return render_template('error-500.html')
# 定义全局拦截器,实现自动登录
@app.before_request
def before():
url = request.path
pass_list = ['/user', '/login', 'logout']
if url in pass_list or url.endswith('.js') or url.endswith('.jpg'):
pass
else:
if session.get('islogin') is None:
username = request.cookies.get('username')
password = request.cookies.get('password')
if username != None and password != None:
user = Users()
result = user.find_by_username(username)
if len(result) == 1 and result[0].password == password:
session['islogin'] = 'true'
session['userid'] = result[0].userid
session['username'] = username
session['nickname'] = result[0].nickname
session['role'] = result[0].role
# 通过自定义过滤器来重构truncate原生过滤器
def mytruncate(s, length, end='...'):
count = 0
new = ''
for c in s:
new += c # 每循环一次,将一个字符添加到new字符串后面
if ord(c) <= 128:
count += 0.5
else:
count += 1
if count > length:
break
return new + end
# 注册mytruncate过滤器
app.jinja_env.filters.update(truncate=mytruncate)
# 上下文处理器
@app.context_processor
def gettype():
"""
定义文章类型函数,供模板页面直接调用
:return: 类型字典
"""
type = {
"1": "PHP开发",
"2": "Java开发",
"3": "Python开发",
"4": "Web前端",
"5": "测试开发",
"6": "数据科学",
"7": "网络安全",
"8": "蜗牛杂谈"
}
return dict(article_type=type)
@app.route('/preupload')
def upload():
return render_template('file-upload.html')
@app.route('/upload', methods=['POST'])
def do_upload():
headline = request.form.get('headline')
content = request.form.get('content')
file = request.form.get('upfile')
print(headline, content)
suffix = file.filename.split('.')[-1]
if suffix.lower() not in ['jpg', 'jpeg', 'png', 'rar', 'zip', 'doc', 'docx']:
return 'Invalid'
file.save('./resource/upload/test001.' + suffix)
return 'Done'
if __name__ == '__main__':
from controller.index import *
app.register_blueprint(index)
from controller.user import *
app.register_blueprint(user)
from controller.article import *
app.register_blueprint(article)
from controller.favorite import *
app.register_blueprint(favorite)
from controller.comment import *
app.register_blueprint(comment)
from controller.ueditor import *
app.register_blueprint(ueditor)
app.run(debug=True)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册