@ -1,4 +1,7 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
|
<component name="Black">
|
||||||
|
<option name="sdkName" value="Python 3.12" />
|
||||||
|
</component>
|
||||||
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.12" project-jdk-type="Python SDK" />
|
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.12" project-jdk-type="Python SDK" />
|
||||||
</project>
|
</project>
|
@ -0,0 +1,22 @@
|
|||||||
|
# app.py
|
||||||
|
from main import app
|
||||||
|
from controller.index import index as index_blueprint
|
||||||
|
from controller.user import user as user_blueprint
|
||||||
|
from controller.article import article as article_blueprint
|
||||||
|
from controller.favorite import favorite as favorite_blueprint
|
||||||
|
from controller.comment import comment as comment_blueprint
|
||||||
|
from controller.admin import admin as admin_blueprint
|
||||||
|
from controller.ucenter import ucenter as ucenter_blueprint
|
||||||
|
from controller.ueditor import ueditor as ueditor_blueprint
|
||||||
|
#
|
||||||
|
app.register_blueprint(index_blueprint)
|
||||||
|
app.register_blueprint(user_blueprint)
|
||||||
|
app.register_blueprint(article_blueprint)
|
||||||
|
app.register_blueprint(favorite_blueprint)
|
||||||
|
app.register_blueprint(comment_blueprint)
|
||||||
|
app.register_blueprint(admin_blueprint)
|
||||||
|
app.register_blueprint(ucenter_blueprint)
|
||||||
|
app.register_blueprint(ueditor_blueprint)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
app.run(debug=True)
|
@ -0,0 +1,106 @@
|
|||||||
|
from flask import Blueprint, render_template, session, request, jsonify
|
||||||
|
from module.article import Article
|
||||||
|
from module.user import Users
|
||||||
|
import math
|
||||||
|
|
||||||
|
admin = Blueprint("admin", __name__)
|
||||||
|
|
||||||
|
# @admin.before_request
|
||||||
|
# def before_admin():
|
||||||
|
# if session.get('islogin') != 'true' or session.get('role') != 'admin':
|
||||||
|
# return 'perm-denied'
|
||||||
|
|
||||||
|
# 为系统管理首页填充文章列表,并绘制分页栏
|
||||||
|
@admin.route('/admin')
|
||||||
|
def sys_admin():
|
||||||
|
pagesize = 50
|
||||||
|
article = Article()
|
||||||
|
result = article.find_all_except_draft(0, pagesize)
|
||||||
|
total = math.ceil(article.get_count_except_draft() / pagesize)
|
||||||
|
return render_template('system-admin.html', page=1, result=result, total=total)
|
||||||
|
@admin.route('/admin/user')
|
||||||
|
def sys_admin_user():
|
||||||
|
pagesize = 50
|
||||||
|
user = Users()
|
||||||
|
result = user.find_all_users()
|
||||||
|
total = math.ceil(user.get_total_count_user() / pagesize)
|
||||||
|
return render_template('system-admin-user.html', page=1, result=result, total=total)
|
||||||
|
|
||||||
|
# 为系统管理首页的文章列表进行分页查询
|
||||||
|
@admin.route('/admin/article/<int:page>')
|
||||||
|
def admin_article(page):
|
||||||
|
pagesize = 50
|
||||||
|
start = (page - 1) * pagesize
|
||||||
|
article = Article()
|
||||||
|
result = article.find_all_except_draft(start, pagesize)
|
||||||
|
total = math.ceil(article.get_count_except_draft() / pagesize)
|
||||||
|
return render_template('system-admin.html', page=page, result=result, total=total)
|
||||||
|
|
||||||
|
|
||||||
|
# 按照文章标题进行模糊查询的后台接口
|
||||||
|
@admin.route('/admin/search/<keyword>')
|
||||||
|
def admin_search_headline(keyword):
|
||||||
|
result = Article().find_by_headline_except_draft(keyword)
|
||||||
|
return render_template('system-admin.html', page=1, result=result, total=1)
|
||||||
|
|
||||||
|
# 文章的隐藏切换接口
|
||||||
|
@admin.route('/admin/article/hide/<int:articleid>')
|
||||||
|
def admin_article_hide(articleid):
|
||||||
|
print("articleid=",articleid)
|
||||||
|
hidden = Article().switch_hidden(articleid)
|
||||||
|
print("hidden=",hidden)
|
||||||
|
return str(hidden)
|
||||||
|
|
||||||
|
# 文章的推荐切换接口
|
||||||
|
@admin.route('/admin/article/recommend/<int:articleid>')
|
||||||
|
def admin_article_recommend(articleid):
|
||||||
|
recommended = Article().switch_recommended(articleid)
|
||||||
|
return str(recommended)
|
||||||
|
|
||||||
|
# 文章的审核切换接口
|
||||||
|
@admin.route('/admin/article/check/<int:articleid>')
|
||||||
|
def admin_article_check(articleid):
|
||||||
|
checked = Article().switch_checked(articleid)
|
||||||
|
return str(checked)
|
||||||
|
|
||||||
|
|
||||||
|
# 人员身份切换接口
|
||||||
|
@admin.route('/admin/user/role/<int:userid>')
|
||||||
|
def admin_user_check(userid):
|
||||||
|
role = Users().switch_role(userid)
|
||||||
|
return str(role)
|
||||||
|
|
||||||
|
# 删除文章
|
||||||
|
@admin.route('/admin/article/delete/<int:articleid>', methods=['GET'])
|
||||||
|
def delete_article(articleid):
|
||||||
|
article = Article()
|
||||||
|
try:
|
||||||
|
# 查找文章
|
||||||
|
result = article.find_by_id(articleid)
|
||||||
|
if result is None:
|
||||||
|
return jsonify({"success": False, "message": "文章不存在"})
|
||||||
|
|
||||||
|
# 删除文章
|
||||||
|
article.do_deletesign(articleid)
|
||||||
|
return jsonify({"success": True, "message": "删除成功"})
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
return jsonify({"success": False, "message": str(e)})
|
||||||
|
|
||||||
|
|
||||||
|
# 删除人员
|
||||||
|
@admin.route('/admin/user/delete/<int:userid>', methods=['GET'])
|
||||||
|
def delete_user(userid):
|
||||||
|
user = Users()
|
||||||
|
try:
|
||||||
|
# 查找人员
|
||||||
|
result = user.find_user_by_id(userid)
|
||||||
|
if result is None:
|
||||||
|
return jsonify({"success": False, "message": "用户不存在"})
|
||||||
|
|
||||||
|
# 删除人员
|
||||||
|
user.do_deletesign_user(userid)
|
||||||
|
return jsonify({"success": True, "message": "删除成功"})
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
return jsonify({"success": False, "message": str(e)})
|
@ -0,0 +1,26 @@
|
|||||||
|
from flask import Blueprint, render_template, request, session
|
||||||
|
from module.favorite import Favorite
|
||||||
|
|
||||||
|
favorite = Blueprint("favorite",__name__)
|
||||||
|
|
||||||
|
# 添加收藏
|
||||||
|
@favorite.route('/favorite',methods = ['post'])
|
||||||
|
def add_favorite():
|
||||||
|
articleid = request.form.get('articleid') # 通过post从前端页面那边拿取数据
|
||||||
|
if session.get('islogin') is None:
|
||||||
|
return 'not-login'
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
Favorite.insert_favorite(articleid = articleid)
|
||||||
|
return 'favorite-pass'
|
||||||
|
except:
|
||||||
|
return 'favorite-fail'
|
||||||
|
|
||||||
|
# 取消收藏
|
||||||
|
@favorite.route('/favorite/<int:articleid>',methods = ['DELETE'])
|
||||||
|
def cancle_favorite(articleid):
|
||||||
|
try:
|
||||||
|
Favorite.cancel_favorite(articleid)
|
||||||
|
return 'cancle-pass'
|
||||||
|
except:
|
||||||
|
return 'cancle-fail'
|
@ -0,0 +1,18 @@
|
|||||||
|
from flask import Blueprint, render_template
|
||||||
|
from module.favorite import Favorite
|
||||||
|
|
||||||
|
ucenter = Blueprint("ucenter", __name__)
|
||||||
|
|
||||||
|
@ucenter.route('/ucenter')
|
||||||
|
def user_center():
|
||||||
|
result = Favorite().find_my_favorite()
|
||||||
|
return render_template('user-center.html', result=result)
|
||||||
|
|
||||||
|
@ucenter.route('/user/favorite/<int:favoriteid>')
|
||||||
|
def user_favorite(favoriteid):
|
||||||
|
canceled = Favorite().switch_favorite(favoriteid)
|
||||||
|
return str(canceled)
|
||||||
|
|
||||||
|
@ucenter.route('/user/post')
|
||||||
|
def user_post():
|
||||||
|
return render_template('user-post.html')
|
@ -0,0 +1,113 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
from flask import Flask, render_template, request, session
|
||||||
|
from flask_sqlalchemy import SQLAlchemy
|
||||||
|
|
||||||
|
|
||||||
|
app = Flask(__name__, template_folder='template', static_url_path='/resource', static_folder='resource')
|
||||||
|
# app = Flask(__name__)
|
||||||
|
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:220113@localhost:3306/woniu?charset=utf8'
|
||||||
|
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
||||||
|
app.config['SECRET_KEY'] = os.urandom(24)
|
||||||
|
|
||||||
|
db = SQLAlchemy(app)
|
||||||
|
|
||||||
|
# 定义404错误页面
|
||||||
|
@app.errorhandler(404)
|
||||||
|
def page_not_found(e):
|
||||||
|
return render_template('error-404.html')
|
||||||
|
|
||||||
|
# 定义500错误页面
|
||||||
|
@app.errorhandler(500)
|
||||||
|
def server_error(e):
|
||||||
|
return render_template('error-500.html')
|
||||||
|
|
||||||
|
|
||||||
|
# 定义过滤器,side的长度保持一致
|
||||||
|
def do_truncate(
|
||||||
|
str,
|
||||||
|
length = 100,
|
||||||
|
end = '...',
|
||||||
|
):
|
||||||
|
count = 0
|
||||||
|
new_str = ''
|
||||||
|
for c in str:
|
||||||
|
if count > length: break
|
||||||
|
new_str += c
|
||||||
|
if ord(c) <= 255: # 这里认为一个汉字的长度是两个字符,一个其他字母的长度是一个字符
|
||||||
|
count += 1
|
||||||
|
else:
|
||||||
|
count += 2
|
||||||
|
if count < length: end = ''
|
||||||
|
return new_str + end
|
||||||
|
# 注册过滤器
|
||||||
|
app.jinja_env.filters.update(truncate = do_truncate)
|
||||||
|
|
||||||
|
# # 定义全局拦截器,实现自动登录
|
||||||
|
# @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
|
||||||
|
#
|
||||||
|
# elif session.get('islogin') != 'true':
|
||||||
|
# username = request.cookies.get('username')
|
||||||
|
# password = request.cookies.get('password')
|
||||||
|
# if username != None and password != None:
|
||||||
|
# from module.user import Users
|
||||||
|
# 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
|
||||||
|
#
|
||||||
|
|
||||||
|
@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
|
||||||
|
|
||||||
|
elif session.get('islogin') != 'true':
|
||||||
|
# 检查用户是否已经登录
|
||||||
|
if session.get('islogin') is not None:
|
||||||
|
# 如果用户已经登录,则跳过自动登录
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
# 如果用户没有登录,则尝试从 session 中获取用户登录信息
|
||||||
|
username = session.get('username')
|
||||||
|
if username is not None:
|
||||||
|
# 如果 session 中存在用户名,则尝试自动登录
|
||||||
|
from module.user import Users
|
||||||
|
user = Users()
|
||||||
|
result = user.find_by_username(username)
|
||||||
|
if len(result) == 1:
|
||||||
|
# 如果用户存在,则更新 session 信息
|
||||||
|
session['islogin'] = 'true'
|
||||||
|
session['userid'] = result[0].userid
|
||||||
|
session['nickname'] = result[0].nickname
|
||||||
|
session['role'] = result[0].role
|
||||||
|
|
||||||
|
|
||||||
|
# 全文可以直接使用article_type
|
||||||
|
@app.context_processor
|
||||||
|
def gettype():
|
||||||
|
type={
|
||||||
|
'1':'情感',
|
||||||
|
'2':'商业',
|
||||||
|
'3':'健康',
|
||||||
|
}
|
||||||
|
return dict(article_type=type)
|
||||||
|
|
||||||
|
|
||||||
|
# 确保在应用上下文中调用,例如在命令行脚本或测试中
|
||||||
|
with app.app_context():
|
||||||
|
db.create_all() # 创建所有数据库表
|
@ -0,0 +1,71 @@
|
|||||||
|
from flask import session
|
||||||
|
from datetime import datetime
|
||||||
|
from main import db
|
||||||
|
class Thumb(db.Model):
|
||||||
|
__tablename__ = 'thumb'
|
||||||
|
thumbid = db.Column(db.Integer, primary_key=True, autoincrement=True)
|
||||||
|
userid = db.Column(db.Integer, db.ForeignKey('user.userid'))
|
||||||
|
articleid = db.Column(db.Integer, db.ForeignKey('article.articleid'))
|
||||||
|
commentid = db.Column(db.Integer, db.ForeignKey('comment.commentid'))
|
||||||
|
upcomment = db.Column(db.Integer, nullable=False)
|
||||||
|
downcomment = db.Column(db.Integer, nullable=False)
|
||||||
|
hide = db.Column(db.Integer, nullable=False)
|
||||||
|
createtime = db.Column(db.DateTime, default=datetime.utcnow)
|
||||||
|
updatetime = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
||||||
|
|
||||||
|
|
||||||
|
# 插入点赞明细
|
||||||
|
@classmethod
|
||||||
|
def insert_up_detail(cls,articleid,commentid):
|
||||||
|
thumb = Thumb(userid=session.get('userid'),articleid=articleid,commentid=commentid,upcomment=1,downcomment=0,hide=0,createtime=datetime.utcnow(),updatetime=datetime.utcnow())
|
||||||
|
db.session.add(thumb)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
# 取消点赞明细
|
||||||
|
@classmethod
|
||||||
|
def insert_sub_up_detail(cls, commentid):
|
||||||
|
userid = session.get('userid')
|
||||||
|
cls.query.filter_by(userid=userid,commentid=commentid).update({cls.upcomment: 0})
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
# 插入反对明细
|
||||||
|
@classmethod
|
||||||
|
def insert_down_detail(cls, articleid, commentid):
|
||||||
|
thumb = Thumb(userid=session.get('userid'), articleid=articleid, commentid=commentid, upcomment=0,
|
||||||
|
downcomment=1, hide=0,createtime=datetime.utcnow(), updatetime=datetime.utcnow())
|
||||||
|
db.session.add(thumb)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
# 取消反对明细
|
||||||
|
@classmethod
|
||||||
|
def insert_sub_down_detail(cls, commentid):
|
||||||
|
userid = session.get('userid')
|
||||||
|
cls.query.filter_by(userid=userid, commentid=commentid).update({cls.downcomment: 0})
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
# 插入隐藏记录
|
||||||
|
@classmethod
|
||||||
|
def insert_hide_detail(cls, articleid, commentid):
|
||||||
|
thumb = Thumb(userid=session.get('userid'), articleid=articleid, commentid=commentid, upcomment=0,
|
||||||
|
downcomment=1, hide=1,createtime=datetime.utcnow(), updatetime=datetime.utcnow())
|
||||||
|
db.session.add(thumb)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
# 判断当前点赞状态
|
||||||
|
@classmethod
|
||||||
|
def is_upComment(cls,articleid,commentid):
|
||||||
|
result = cls.query.filter_by(userid=session.get('userid'),articleid=articleid,commentid=commentid,upcomment=1).first()
|
||||||
|
if result is None: # 如果当前文章不属于点赞状态
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
return True
|
||||||
|
|
||||||
|
# 判断当前反对状态
|
||||||
|
@classmethod
|
||||||
|
def is_downComment(cls, articleid, commentid):
|
||||||
|
result = cls.query.filter_by(userid=session.get('userid'), articleid=articleid, commentid=commentid,
|
||||||
|
downcomment=1).first()
|
||||||
|
if result is None: # 如果当前文章不属于反对状态
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
return True
|
After Width: | Height: | Size: 42 KiB |
After Width: | Height: | Size: 43 KiB |
After Width: | Height: | Size: 44 KiB |
After Width: | Height: | Size: 40 KiB |
After Width: | Height: | Size: 42 KiB |
After Width: | Height: | Size: 46 KiB |
After Width: | Height: | Size: 48 KiB |
After Width: | Height: | Size: 40 KiB |
After Width: | Height: | Size: 37 KiB |
After Width: | Height: | Size: 51 KiB |
After Width: | Height: | Size: 54 KiB |
After Width: | Height: | Size: 79 KiB |
After Width: | Height: | Size: 61 KiB |
After Width: | Height: | Size: 76 KiB |
After Width: | Height: | Size: 55 KiB |
After Width: | Height: | Size: 63 KiB |
After Width: | Height: | Size: 69 KiB |
After Width: | Height: | Size: 52 KiB |
Before Width: | Height: | Size: 231 KiB After Width: | Height: | Size: 231 KiB |
Before Width: | Height: | Size: 54 KiB After Width: | Height: | Size: 54 KiB |
Before Width: | Height: | Size: 191 B After Width: | Height: | Size: 191 B |
Before Width: | Height: | Size: 217 B After Width: | Height: | Size: 217 B |
Before Width: | Height: | Size: 246 B After Width: | Height: | Size: 246 B |
Before Width: | Height: | Size: 310 B After Width: | Height: | Size: 310 B |
Before Width: | Height: | Size: 391 B After Width: | Height: | Size: 391 B |
Before Width: | Height: | Size: 140 B After Width: | Height: | Size: 140 B |
Before Width: | Height: | Size: 186 B After Width: | Height: | Size: 186 B |
Before Width: | Height: | Size: 211 B After Width: | Height: | Size: 211 B |