test: 添加测试数据
This commit is contained in:
@@ -6,6 +6,8 @@ class BaseConfig:
|
||||
|
||||
SQLALCHEMY_DATABASE_URI = ""
|
||||
|
||||
ROOT_PATH = os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
|
||||
class DevelopmentConfig(BaseConfig):
|
||||
"""开发配置"""
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
from flask import Flask
|
||||
|
||||
from .init_db import db, migrate
|
||||
from .init_script import register_script
|
||||
|
||||
|
||||
def register_extensions(app: Flask):
|
||||
db.init_app(app)
|
||||
migrate.init_app(app, db)
|
||||
register_script(app)
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
import csv
|
||||
import os
|
||||
|
||||
from flask import Flask, current_app
|
||||
|
||||
from pear_admin.extensions import db
|
||||
from pear_admin.orms import DepartmentORM, RightsORM, RoleORM, UserORM
|
||||
|
||||
|
||||
def dict_to_orm(d, o):
|
||||
for k, v in d.items():
|
||||
setattr(o, k, v or None)
|
||||
|
||||
|
||||
def csv_to_databases(path, orm):
|
||||
with open(path, encoding="utf-8") as file:
|
||||
for d in csv.DictReader(file):
|
||||
o = orm()
|
||||
dict_to_orm(d, o)
|
||||
db.session.add(o)
|
||||
db.session.flush()
|
||||
db.session.commit()
|
||||
|
||||
|
||||
def register_script(app: Flask):
|
||||
@app.cli.command()
|
||||
def init():
|
||||
db.drop_all()
|
||||
db.create_all()
|
||||
|
||||
root = current_app.config.get("ROOT_PATH")
|
||||
|
||||
rights_data_path = os.path.join(root, "static", "data", "ums_rights.csv")
|
||||
csv_to_databases(rights_data_path, RightsORM)
|
||||
|
||||
role_data_path = os.path.join(root, "static", "data", "ums_role.csv")
|
||||
csv_to_databases(role_data_path, RoleORM)
|
||||
|
||||
with open(role_data_path, encoding="utf-8") as file:
|
||||
for d in csv.DictReader(file):
|
||||
role: RoleORM = RoleORM.query.get(d["id"])
|
||||
id_list = [int(_id) for _id in d["rights_ids"].split(":")]
|
||||
role.rights_list = RightsORM.query.filter(
|
||||
RightsORM.id.in_(id_list)
|
||||
).all()
|
||||
db.session.commit()
|
||||
|
||||
department_data_path = os.path.join(
|
||||
root, "static", "data", "ums_department.csv"
|
||||
)
|
||||
csv_to_databases(department_data_path, DepartmentORM)
|
||||
|
||||
user_data_path = os.path.join(root, "static", "data", "ums_user.csv")
|
||||
csv_to_databases(user_data_path, UserORM)
|
||||
|
||||
with open(user_data_path, encoding="utf-8") as file:
|
||||
for d in csv.DictReader(file):
|
||||
user: UserORM = UserORM.query.get(d["id"])
|
||||
id_list = [int(_id) for _id in d["role_ids"].split(":")]
|
||||
user.role_list = RoleORM.query.filter(RoleORM.id.in_(id_list)).all()
|
||||
db.session.commit()
|
||||
@@ -12,7 +12,7 @@ user_role = db.Table(
|
||||
db.Column("role_id", db.Integer, db.ForeignKey("ums_role.id"), comment="角色编号"),
|
||||
)
|
||||
|
||||
role_permission = db.Table(
|
||||
role_rights = db.Table(
|
||||
"ums_role_rights", # 用户-权限中间表名称
|
||||
db.Column("id", db.Integer, primary_key=True, autoincrement=True, comment="标识"),
|
||||
db.Column("rights_id", db.Integer, db.ForeignKey("ums_rights.id"), comment="用户编号"),
|
||||
|
||||
@@ -11,13 +11,13 @@ class RoleORM(BaseORM):
|
||||
code = db.Column(db.String(20), nullable=False, comment="角色标识符")
|
||||
desc = db.Column(db.Text)
|
||||
|
||||
permission_ids = db.Column(
|
||||
rights_ids = db.Column(
|
||||
db.String(512),
|
||||
comment="权限ids,1,2,5。冗余字段,用户缓存用户权限",
|
||||
)
|
||||
|
||||
permission_list = db.relationship(
|
||||
"RightsORM", secondary="ums_role_permission", backref=db.backref("role")
|
||||
rights_list = db.relationship(
|
||||
"RightsORM", secondary="ums_role_rights", backref=db.backref("role")
|
||||
)
|
||||
|
||||
def json(self):
|
||||
@@ -25,5 +25,5 @@ class RoleORM(BaseORM):
|
||||
"id": self.id,
|
||||
"name": self.name,
|
||||
"desc": self.desc,
|
||||
"permission_ids": self.permission_ids,
|
||||
"rights_ids": self.rights_ids,
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
id,name,leader,phone,email,enable,comment,address,sort,pid
|
||||
1,集团总部,集团负责人,12312345679,123qq.com,,,这是集团总部,1,0
|
||||
2,济南分公司,就眠仪式,12312345678,1234qq.com,,,这是济南,2,1
|
||||
3,唐山分公司,mkg,12312345678,123@qq.com,,,这是唐山,4,1
|
||||
4,济南分公司-开发部,就眠仪式,12312345678,123@qq.com,,,测试,5,4
|
||||
5,唐山分公司-测试部,mkg,12312345678,123@qq.com,,,测试部,6,5
|
||||
6,长沙分公司,正心全栈编程,18675867241,pyxxp@qq.com,,,湖南省长沙市望城区,6,1
|
||||
7,长沙分公司-VIP服务中心,正心全栈编程,18675867241,pyxxp@qq.com,,,,6,6
|
||||
8,长沙分公司-招生中心,正心全栈编程,18675867241,pyxxp@qq.com,,,,6,6
|
||||
9,长沙分公司-运营中心,正心全栈编程,18675867241,pyxxp@qq.com,,,,6,6
|
||||
|
@@ -0,0 +1,27 @@
|
||||
id,name,code,type,url,icon,status,sort,open_type,pid
|
||||
100,系统管理,ums:rights,menu,,layui-iconlayui-icon-set-fill,,2,,0
|
||||
101,权限管理,ums:rights:list,path,/rights,,,1,,100
|
||||
102,新增权限,ums:rights:create,auth,,,,0,,101
|
||||
103,修改权限,ums:rights:update,auth,,,,0,,101
|
||||
104,获取权限,ums:rights:read,auth,,,,0,,101
|
||||
105,删除权限,ums:rights:delete,auth,,,,0,,101
|
||||
106,角色管理,ums:role,path,/role,,,2,,100
|
||||
107,新增角色,ums:role:create,auth,,,,0,,106
|
||||
109,修改角色,ums:role:update,auth,,,,0,,106
|
||||
108,查询角色,ums:role:read,auth,,,,0,,106
|
||||
110,删除角色,ums:role:delete,auth,,,,0,,106
|
||||
111,部门管理,ums:department,path,/department,,,3,,100
|
||||
112,新建部门,ums:department:create,auth,,,,0,,111
|
||||
113,修改部门,ums:department:update,auth,,,,0,,111
|
||||
114,删除部门,ums:department:delete,auth,,,,0,,111
|
||||
115,查询部门,ums:department:read,auth,,,,0,,111
|
||||
116,员工管理,ums:user,path,/user,,,4,,100
|
||||
117,新增员工,ums:user:create,auth,,,,0,,116
|
||||
118,修改员工,ums:user:update,auth,,,,0,,116
|
||||
119,查询员工,ums:user:read,auth,,,,0,,116
|
||||
120,查询员工,ums:user:read,auth,,,,0,,116
|
||||
121,数据图表,,menu,,layui-iconlayui-icon-chart,,3,,0
|
||||
122,折线图,,path,/echarts/line,layui-iconlayui-icon-face-smile,,0,,121
|
||||
123,柱状图,,path,/echarts/column,layui-iconlayui-icon-face-smile,,0,,121
|
||||
124,工作空间,,menu,,layui-iconlayui-icon-console,,1,,0
|
||||
125,控制后台,,path,/person,layui-iconlayui-icon-console,,0,,124
|
||||
|
@@ -0,0 +1,4 @@
|
||||
id,name,code,desc,rights_ids
|
||||
1,超级管理员,super_admin,,100:101:102:103:104:105:106:107:109:108:110:111:112:113:114:115:116:117:118:119:120
|
||||
2,普通管理员,admin,,106:107:109:108:110:111:112:113:114:115:116:117:118:119:120
|
||||
3,普通用户,user,,111:112:113:114:115:116:117:118:119:120
|
||||
|
@@ -0,0 +1,4 @@
|
||||
id,nickname,username,password,mobile,email,avatar,department_id,role_ids
|
||||
1,admin,超级管理员,123456,15543526531,8540854@qq.com,,1,1
|
||||
2,就眠仪式,就眠仪式,123456,155324324234,8540854@qq.com,,1,2
|
||||
3,zhengxinonly,正心全栈编程,123456,18675867241,pyxxponly@gmail.com,,1,3
|
||||
|
Reference in New Issue
Block a user