feature: 新增数据模型
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
FLASK_DEBUG=True # 开启调试模式
|
||||
FLASK_RUN_PORT=5050 # 设置运行的端口
|
||||
FLASK_RUN_HOST=0.0.0.0 # 设置监听的 ip
|
||||
FLASK_APP="apps:create_app()" # 运行程序
|
||||
FLASK_APP="pear_admin:create_app()" # 运行程序
|
||||
+3
-1
@@ -1,3 +1,5 @@
|
||||
.idea/
|
||||
.venv/
|
||||
*.py[cod]
|
||||
*.py[cod]
|
||||
instance/
|
||||
migrations/
|
||||
@@ -2,6 +2,7 @@ from flask import Flask
|
||||
|
||||
from configs import config
|
||||
from pear_admin.extensions import register_extensions
|
||||
from pear_admin.orms import UserORM
|
||||
|
||||
|
||||
def create_app(config_name="dev"):
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
from pear_admin.extensions import db
|
||||
|
||||
from .department import DepartmentORM
|
||||
from .rights import RightsORM
|
||||
from .role import RoleORM
|
||||
from .user import UserORM
|
||||
|
||||
user_role = db.Table(
|
||||
"ums_user_role", # 用户-角色中间表名称
|
||||
db.Column("id", db.Integer, primary_key=True, autoincrement=True, comment="标识"),
|
||||
db.Column("user_id", db.Integer, db.ForeignKey("ums_user.id"), comment="用户编号"),
|
||||
db.Column("role_id", db.Integer, db.ForeignKey("ums_role.id"), comment="角色编号"),
|
||||
)
|
||||
|
||||
role_permission = 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="用户编号"),
|
||||
db.Column("role_id", db.Integer, db.ForeignKey("ums_role.id"), comment="角色编号"),
|
||||
)
|
||||
@@ -0,0 +1,13 @@
|
||||
from pear_admin.extensions import db
|
||||
|
||||
|
||||
class BaseORM(db.Model):
|
||||
__abstract__ = True
|
||||
|
||||
def save(self):
|
||||
db.session.add(self)
|
||||
db.session.commit()
|
||||
|
||||
def delete(self):
|
||||
db.session.delete(self)
|
||||
db.session.commit()
|
||||
@@ -0,0 +1,33 @@
|
||||
from pear_admin.extensions import db
|
||||
|
||||
from ._base import BaseORM
|
||||
|
||||
|
||||
class DepartmentORM(BaseORM):
|
||||
__tablename__ = "ums_department"
|
||||
id = db.Column(db.Integer, primary_key=True, comment="部门ID")
|
||||
name = db.Column(db.String(50), comment="部门名称")
|
||||
leader = db.Column(db.String(50), comment="负责人")
|
||||
phone = db.Column(db.String(20), comment="联系方式")
|
||||
email = db.Column(db.String(50), comment="邮箱")
|
||||
enable = db.Column(db.Boolean, comment="状态(1开启,0关闭)")
|
||||
comment = db.Column(db.Text, comment="备注")
|
||||
address = db.Column(db.String(255), comment="详细地址")
|
||||
sort = db.Column(db.Integer, comment="排序")
|
||||
|
||||
users = db.relationship("UserORM", backref="department")
|
||||
|
||||
pid = db.Column(db.Integer, db.ForeignKey("ums_department.id"))
|
||||
parent = db.relationship("DepartmentORM", remote_side=[id], backref="child") # 自关联
|
||||
|
||||
def json(self):
|
||||
return {
|
||||
"id": self.id,
|
||||
"pid": self.pid,
|
||||
"name": self.name,
|
||||
"leader": self.leader,
|
||||
"email": self.email,
|
||||
"phone": self.phone,
|
||||
"sort": self.sort,
|
||||
"enable": self.enable,
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
from pear_admin.extensions import db
|
||||
|
||||
from ._base import BaseORM
|
||||
|
||||
|
||||
class RightsORM(BaseORM):
|
||||
__tablename__ = "ums_rights"
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
name = db.Column(db.String(20), nullable=False, comment="权限名称")
|
||||
code = db.Column(db.String(30), comment="权限标识")
|
||||
type = db.Column(db.String(30), comment="权限类型")
|
||||
url = db.Column(db.String(30), comment="路径地址")
|
||||
|
||||
icon = db.Column(db.String(128), comment="图标")
|
||||
status = db.Column(db.Boolean, default=True, comment="是否开启")
|
||||
sort = db.Column(db.Integer, default=1)
|
||||
open_type = db.Column(db.String(128), comment="打开方式")
|
||||
pid = db.Column(
|
||||
db.Integer,
|
||||
db.ForeignKey("ums_rights.id"),
|
||||
default=0,
|
||||
comment="父类编号",
|
||||
)
|
||||
|
||||
# parent = db.relationship("RightsORM", remote_side=[pid]) # 自关联
|
||||
|
||||
def json(self):
|
||||
return {
|
||||
"id": self.id,
|
||||
"name": self.name,
|
||||
"code": self.code,
|
||||
"type": self.type,
|
||||
"url": self.url,
|
||||
"icon": self.icon,
|
||||
"status": self.status,
|
||||
"sort": self.sort,
|
||||
"open_type": self.open_type,
|
||||
"pid": self.pid,
|
||||
}
|
||||
|
||||
def menu_json(self):
|
||||
type_map_dict = {"menu": 0, "path": 1}
|
||||
|
||||
return {
|
||||
"id": self.id,
|
||||
"pid": self.pid,
|
||||
"rights_id": self.id,
|
||||
"title": self.name,
|
||||
"type": type_map_dict[self.type],
|
||||
"href": self.url,
|
||||
"icon": self.icon,
|
||||
"sort": self.sort,
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
from pear_admin.extensions import db
|
||||
|
||||
from ._base import BaseORM
|
||||
|
||||
|
||||
class RoleORM(BaseORM):
|
||||
__tablename__ = "ums_role"
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
name = db.Column(db.String(20), nullable=False, comment="角色名称")
|
||||
code = db.Column(db.String(20), nullable=False, comment="角色标识符")
|
||||
desc = db.Column(db.Text)
|
||||
|
||||
permission_ids = db.Column(
|
||||
db.String(512),
|
||||
comment="权限ids,1,2,5。冗余字段,用户缓存用户权限",
|
||||
)
|
||||
|
||||
permission_list = db.relationship(
|
||||
"RightsORM", secondary="ums_role_permission", backref=db.backref("role")
|
||||
)
|
||||
|
||||
def json(self):
|
||||
return {
|
||||
"id": self.id,
|
||||
"name": self.name,
|
||||
"desc": self.desc,
|
||||
"permission_ids": self.permission_ids,
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
from datetime import datetime
|
||||
|
||||
from werkzeug.security import check_password_hash, generate_password_hash
|
||||
|
||||
from pear_admin.extensions import db
|
||||
|
||||
from ._base import BaseORM
|
||||
|
||||
|
||||
class UserORM(BaseORM):
|
||||
__tablename__ = "ums_user"
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True, comment="自增id")
|
||||
nickname = db.Column(db.String(128), nullable=False, comment="昵称")
|
||||
username = db.Column(db.String(128), nullable=False, comment="登录名")
|
||||
password_hash = db.Column(db.String(102), nullable=False, comment="登录密码")
|
||||
mobile = db.Column(db.String(32), nullable=False, comment="手机")
|
||||
email = db.Column(db.String(64), nullable=False, comment="邮箱")
|
||||
avatar = db.Column(db.Text, comment="头像地址")
|
||||
create_at = db.Column(
|
||||
db.DateTime,
|
||||
nullable=False,
|
||||
comment="创建时间",
|
||||
default=datetime.now,
|
||||
)
|
||||
department_id = db.Column(
|
||||
db.Integer, db.ForeignKey("ums_department.id"), default=1, comment="部门id"
|
||||
)
|
||||
|
||||
role_list = db.relationship(
|
||||
"RoleORM",
|
||||
secondary="ums_user_role",
|
||||
backref=db.backref("user"),
|
||||
lazy="dynamic",
|
||||
)
|
||||
|
||||
def json(self):
|
||||
return {
|
||||
"id": self.id,
|
||||
"username": self.username,
|
||||
"nickname": self.nickname,
|
||||
"mobile": self.mobile,
|
||||
"email": self.email,
|
||||
"create_at": self.create_at,
|
||||
}
|
||||
|
||||
@property
|
||||
def password(self):
|
||||
return self.password_hash
|
||||
|
||||
@password.setter
|
||||
def password(self, password):
|
||||
self.password_hash = generate_password_hash(password)
|
||||
|
||||
def check_password(self, password):
|
||||
return check_password_hash(self.password_hash, password=password)
|
||||
Reference in New Issue
Block a user