1 Star 1 Fork 2

Python_Web_Project/Flask_BBS

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
manage.py 3.40 KB
一键复制 编辑 原始数据 按行查看 历史
Corley 提交于 2020-07-06 11:19 . V1.11
import random
from flask_script import Manager
from bbs import app
from flask_migrate import Migrate, MigrateCommand
from exts import db
from apps.cms.models import CMSUser, CMSRole, CMSPermission, BannerModel, BoardModel, HighlightPostModel
from apps.front.models import FrontUser, PostModel, CommentModel, PraiseModel
manager = Manager(app)
Migrate(app, db)
manager.add_command('db', MigrateCommand)
@manager.option('-u', '--username', dest='username')
@manager.option('-p', '--password', dest='password')
@manager.option('-e', '--email', dest='email')
def create_cms_user(username, password, email):
user = CMSUser(username=username, password=password, email=email)
db.session.add(user)
db.session.commit()
print('CMS用户添加成功')
@manager.command
def create_role():
# 访问者
visitor = CMSRole(name='访问者', desc='只能查看数据,不能修改数据')
visitor.permissions = CMSPermission.VISITOR # 也可以省去,因为默认权限就是VISITOR
# 运营者
operator = CMSRole(name='运营', desc='管理帖子,管理评论,管理前台和后台用户,管理轮播图')
# 有多个权限时,使用或运算表示
operator.permissions = CMSPermission.VISITOR | CMSPermission.POSTER | CMSPermission.CMSUSER | CMSPermission.COMMENTER | CMSPermission.FRONTUSER | CMSPermission.BANNER
# 管理员
admin = CMSRole(name='管理员', desc='拥有本系统大部分权限')
admin.permissions = CMSPermission.VISITOR | CMSPermission.POSTER | CMSPermission.CMSUSER | CMSPermission.COMMENTER | CMSPermission.FRONTUSER | CMSPermission.BOARDER | CMSPermission.BANNER
# 开发人员
developer = CMSRole(name='开发者', desc='拥有所有权限')
developer.permissions = CMSPermission.ALL_PERMISSION
db.session.add_all([visitor, operator, admin, developer])
db.session.commit()
print('角色添加成功')
@manager.command
def test_permission():
user = CMSUser.query.first()
if user.has_permission(CMSPermission.VISITOR):
print('该用户有使用者权限')
else:
print('该用户没有使用者权限')
@manager.option('-e', '--email', dest='email')
@manager.option('-n', '--name', dest='name')
def add_user_to_role(email, name):
user = CMSUser.query.filter_by(email=email).first()
if user:
role = CMSRole.query.filter_by(name=name).first()
if role:
role.users.append(user)
db.session.commit()
print('用户添加到角色添加成功')
else:
print('角色不存在')
else:
print('邮箱不存在')
@manager.option('-t', '--telephone', dest='telephone')
@manager.option('-u', '--username', dest='username')
@manager.option('-p', '--password', dest='password')
def create_front_user(telephone, username, password):
user = FrontUser(telephone=telephone, username=username, password=password)
db.session.add(user)
db.session.commit()
print('前台用户添加成功')
@manager.command
def test_post_page():
for i in range(1, 301):
title = 'Title %d' % i
content = 'Content %d' % i
author = FrontUser.query.first()
post = PostModel(title=title, content=content)
post.author = author
post.board = BoardModel.query.get(random.choice([1, 3, 4]))
db.session.add(post)
db.session.commit()
print('测试文章添加成功')
if __name__ == '__main__':
manager.run()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/Python_Web_Project/Flask_BBS.git
git@gitee.com:Python_Web_Project/Flask_BBS.git
Python_Web_Project
Flask_BBS
Flask_BBS
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385