class Menu(db.Model): __tablename__ = 'menus' id = db.Column(db.Integer, primary_key=True) exclude_columns = ['created_at', 'updated_at'] auth_name = Column(db.String(32), nullable=False, index=True) path = Column(db.String(256), nullable=False) level = Column(db.Integer, nullable=True) created_at = Column(db.Date, nullable=True, default=datetime.now) updated_at = Column(db.Date, nullable=True, default=datetime.now) parent_id = db.Column(db.Integer, db.ForeignKey('menus.id')) children = db.relationship('Menu', back_populates='parent') parent = db.relationship('Menu', back_populates='children', remote_side=[id]) # role_id = Column(db.Integer, db.ForeignKey('roles.id')) # role = relationship('Role', backref=db.backref('menus', order_by=id)) # roles = relationship('Role', secondary=association_table, back_populates="menus") # 将back_populates修改为db.backref() 指定 lazy = 'dynamic' 参数,关系两侧返回的查询都可接受额外的过滤器 roles = relationship('Role', secondary=association_table, backref=db.backref("menus", lazy='dynamic')) def __init__(self, auth_name, path, **kwargs): db.Model.__init__(self, auth_name=auth_name, path=path, **kwargs)
class Photo(db.Model): __tablename__ = 'photos' id = db.Column(db.Integer, primary_key=True) photo_name = Column(db.String(500), nullable=False, index=True) # photo_url = Column(db.Url(), nullable=False, index=True) photo_url = Column(db.String(500), nullable=False, index=True) good_id = Column(db.Integer, db.ForeignKey('goods.id')) good = relationship('Good', back_populates='photos')
class Role(db.Model): __tablename__ = 'roles' id = Column(db.Integer, primary_key=True) # exclude_columns = ['created_at', 'updated_at'] role_name = Column(db.String(32), nullable=False, index=True) role_desc = Column(db.String(256), nullable=True, index=False) created_at = Column(db.Date, nullable=True, default=datetime.now) updated_at = Column(db.Date, nullable=True, default=datetime.now) # menus = relationship('Menu', secondary=association_table, back_populates="roles") def __init__(self, role_name, role_desc, **kwargs): db.Model.__init__(self, role_name=role_name, role_desc=role_desc, **kwargs) @property def role_menus(self): # TODO 优化角色对应权限的筛选逻辑 menus = [] menus_1 = self.menus.filter_by(level=0).all() # logging.error(menus_1) for menu_1 in menus_1: # menu_1 = copy.deepcopy(menu_1_o) item_1 = {} children_1 = [] # logging.error((menu_1.children, self.menus.all())) for menu_2 in menu_1.children: # menu_2 = copy.deepcopy(menu_2_o) item_2 = {} children_2 = [] if menu_2 in self.menus.all(): # children_1.append(menu_2) for menu_3 in menu_2.children: if menu_3 in self.menus.all(): children_2.append(menu_3) item_2 = { 'children': children_2, 'auth_name': menu_2.auth_name, 'created_at': menu_2.created_at, 'id': menu_2.id, 'level': menu_2.level, 'path': menu_2.path, 'updated_at': menu_2.updated_at } children_1.append(item_2) item_1 = { 'children': children_1, 'auth_name': menu_1.auth_name, 'created_at': menu_1.created_at, 'id': menu_1.id, 'level': menu_1.level, 'path': menu_1.path, 'updated_at': menu_1.updated_at } menus.append(item_1) return menus
class BulkModelMixin: x = Column(db.String(50), nullable=False) y = Column(db.String(50), nullable=False) z = Column(db.String(50), nullable=False) __table_args__ = (UniqueConstraint('x', 'y', 'z', name='bulk_model_main_unique_key'), )
class BulkModel2Mixin: id = Column(BigInteger, primary_key=True) update = Column(DateTime, index=True, nullable=False, server_default=func.now(), onupdate=func.now()) x = Column(db.String(50), nullable=False) y = Column(db.String(50), nullable=False) z = Column(db.String(50), nullable=False) __table_args__ = (UniqueConstraint('x', 'y', 'z', name='bulk_model2_main_unique_key'), )
class TestUser(BaseModel): __bind_key__ = 'oracle' __tablename__ = f'{random.random()}' sequence_name, primary_key_name, exclude_columns = conf username = Column(db.String(32), nullable=False, index=True)
class User(db.Model): # 包含在exclude_columns 的列不会新建 # exclude_columns = ['created_at', 'updated_at'] __tablename__ = 'users' id = Column(db.Integer, primary_key=True) created_at = Column(db.Date, nullable=True, default=datetime.now) updated_at = Column(db.Date, nullable=True, default=datetime.now) username = Column(db.String(32), nullable=False, index=True) password = Column(db.String(256), nullable=False) email = Column(db.String(32), nullable=False, index=True) # status = Column(db.Integer(), default=1, doc='1代表激活') status = Column(db.Boolean(), default=True, doc='1代表激活') del_flag = Column(db.Integer(), default=0, doc='0代表未删除') role_id = Column(db.Integer, db.ForeignKey('roles.id')) role = relationship('Role', backref=db.backref('users', order_by=username)) orders = db.relationship('Order', back_populates='user', lazy='dynamic') def __init__(self, username, email, password=None, **kwargs): db.Model.__init__(self, username=username, email=email, **kwargs) if password: self.set_password(password) else: self.password = None @classmethod def s_query(cls): """查询激活状态的user,未被删除 """ return cls.query.filter_by(del_flag=0) def set_password(self, password): """Set password.""" self.password = bcrypt.generate_password_hash(password) def check_password(self, value): """Check password.""" return bcrypt.check_password_hash(self.password, value) @property def token(self): return _default_jwt_encode_handler(self).decode('utf-8')
class Attribute(db.Model): __tablename__ = 'attributes' class Sel(enum.Enum): only = 0 many = 1 class Write(enum.Enum): manual = 0 list = 1 id = db.Column(db.Integer, primary_key=True) attribute_name = Column(db.String(32), nullable=False, index=True) # attribute_sel = Column(db.Enum(Sel), default=Sel.only, nullable=False) attribute_sel = Column(db.Enum('only', 'many'), default='only', nullable=False) attribute_write = Column(db.Enum('manual', 'list'), default='list', nullable=False) # attribute_write = Column(db.Enum(Write), default=Write.list, nullable=False) attribute_values = Column(db.String(200), nullable=True, index=True) created_at = Column(db.Date, nullable=True, default=datetime.now) updated_at = Column(db.Date, nullable=True, default=datetime.now) category_id = Column(db.Integer, db.ForeignKey('categories.id')) category = relationship('Category', back_populates='attributes') def __init__(self, attribute_name, attribute_sel, attribute_write, attribute_values='', **kwargs): db.Model.__init__(self, attribute_name=attribute_name, attribute_sel=attribute_sel, attribute_write=attribute_write, attribute_values=attribute_values, **kwargs)
class OtherUser(BaseModel): username = Column(db.String(20), unique=True, nullable=False, doc='username') nick = Column(db.String(20), unique=True, nullable=False, doc='nick name')
class Role(BaseModel): # just for assert multi model worked name = Column(db.String(50), nullable=False, unique=True)
class Order(db.Model): __tablename__ = 'orders' id = Column(db.Integer, primary_key=True) order_number = Column(db.String(200), nullable=False, index=True) trad_no = Column(db.String(200), nullable=True) invoice_titile = Column(db.String(500), nullable=True) invoice_company = Column(db.String(500), nullable=True) invoice_content = Column(db.String(500), nullable=True) consignee_address = Column(db.String(500), nullable=True) order_price = Column(db.Float, nullable=False, default=1.0) pay_status = Column( db.Enum('未支付', '已支付', '已取消'), default='未支付', doc='订单状态:未支付, 已支付, 已取消') is_send = Column(db.Boolean(), default=False, doc='False代表非热销商品') created_at = Column(db.Date, nullable=True, default=datetime.now) updated_at = Column(db.Date, nullable=True, default=datetime.now) user_id = Column(db.Integer, db.ForeignKey('users.id')) user = relationship('User', back_populates='orders') goods = relationship('Good', secondary=goods_orders, back_populates="orders", lazy='dynamic') def __init__( self, order_number, trad_no, invoice_titile, invoice_company, invoice_content, consignee_address, order_price, pay_status, is_send, **kwargs): db.Model.__init__( self, order_number=order_number, trad_no=trad_no, invoice_titile=invoice_titile, invoice_company=invoice_company, invoice_content=invoice_content, consignee_address=consignee_address, order_price=order_price, pay_status=pay_status, is_send=is_send, **kwargs)
import logging import enum from datetime import datetime from hobbit_core.db import Column, BaseModel, reference_col, SurrogatePK from sqlalchemy.orm import relationship, exc from app.exts import db goods_orders = db.Table( 'association_order', Column('order_id', db.Integer, db.ForeignKey('orders.id')), Column('good_id', db.Integer, db.ForeignKey('goods.id')) ) class Order(db.Model): __tablename__ = 'orders' id = Column(db.Integer, primary_key=True) order_number = Column(db.String(200), nullable=False, index=True) trad_no = Column(db.String(200), nullable=True) invoice_titile = Column(db.String(500), nullable=True) invoice_company = Column(db.String(500), nullable=True) invoice_content = Column(db.String(500), nullable=True) consignee_address = Column(db.String(500), nullable=True) order_price = Column(db.Float, nullable=False, default=1.0) pay_status = Column( db.Enum('未支付', '已支付', '已取消'), default='未支付', doc='订单状态:未支付, 已支付, 已取消') is_send = Column(db.Boolean(), default=False, doc='False代表非热销商品')
class Good(db.Model): __tablename__ = 'goods' id = Column(db.Integer, primary_key=True) good_name = Column(db.String(32), nullable=False, index=True) good_desc = Column(db.String(200), nullable=True) good_price = Column(db.Float, nullable=False, default=1.0) good_weight = Column(db.Float, nullable=False, default=1.0) good_number = Column(db.Integer, nullable=False, default=1) hot_number = Column(db.Integer, nullable=False, default=1) good_state = Column(db.Enum('ReviewFaild', 'UnderReview', 'Reviewed'), default='UnderReview', doc='商品状态:未通过, 审核中, 已审核') is_promote = Column(db.Boolean(), default=False, doc='False代表非热销商品') created_at = Column(db.Date, nullable=True, default=datetime.now) updated_at = Column(db.Date, nullable=True, default=datetime.now) category_id = Column(db.Integer, db.ForeignKey('categories.id')) category = relationship('Category', back_populates='goods') photos = relationship('Photo', back_populates='good', lazy='dynamic') orders = relationship('Order', secondary=goods_orders, back_populates="goods", lazy='dynamic') def __init__(self, good_name, good_price, good_weight, good_number, good_desc, hot_number, good_state, is_promote, **kwargs): db.Model.__init__(self, good_name=good_name, good_state=good_state, good_price=good_price, good_number=good_number, good_weight=good_weight, good_desc=good_desc, hot_number=hot_number, is_promote=is_promote, **kwargs)
class Category(db.Model): __tablename__ = 'categories' id = db.Column(db.Integer, primary_key=True) category_name = Column(db.String(32), nullable=False, index=True) category_desc = Column(db.String(200), nullable=True) category_level = Column(db.Integer, nullable=True) created_at = Column(db.Date, nullable=True, default=datetime.now) updated_at = Column(db.Date, nullable=True, default=datetime.now) parent_id = db.Column(db.Integer, db.ForeignKey('categories.id')) children = db.relationship('Category', back_populates='parent') attributes = db.relationship('Attribute', back_populates='category', lazy='dynamic') goods = db.relationship('Good', back_populates='category', lazy='dynamic') parent = db.relationship('Category', back_populates='children', remote_side=[id]) # parent = db.relationship('Category', backref=db.backref("children", lazy='dynamic'), remote_side=[id]) # role_id = Column(db.Integer, db.ForeignKey('roles.id')) # role = relationship('Role', backref=db.backref('menus', order_by=id)) # roles = relationship('Role', secondary=association_table, back_populates="menus") # 将back_populates修改为db.backref() 指定 lazy = 'dynamic' 参数,关系两侧返回的查询都可接受额外的过滤器 # roles = relationship('Role', secondary=association_table, backref=db.backref("menus", lazy='dynamic')) def __init__(self, category_name, category_level, category_desc, **kwargs): db.Model.__init__(self, category_name=category_name, category_level=category_level, **kwargs) @property def children_f(self, type=2): children = [] if type == 1: return children categories_1 = self.children # logging.error(categories_1) for category_1 in categories_1: if type == 2: item_1 = {} children_1 = [] for category_2 in category_1.children: children_1.append(category_2) item_1 = { 'children_f': [], # 'children_f': [], 'category_name': category_1.category_name, 'category_desc': category_1.category_desc, 'category_level': category_1.category_level, 'id': category_1.id, 'parent_id': category_1.parent_id, 'parent': category_1.parent, 'updated_at': category_1.updated_at, 'created_at': category_1.created_at } if item_1['children_f'] == []: del (item_1['children_f']) children.append(item_1) else: children.append(category_1) return children @property def children_p(self): children = [] categories_1 = self.children logging.error(f'all: {categories_1}') for category_1 in categories_1: children_1 = [] for category_2 in category_1.children: item_2 = { 'category_name': category_2.category_name, 'category_desc': category_2.category_desc, 'category_level': category_2.category_level, 'id': category_2.id, 'parent_id': category_2.parent_id, 'parent': category_2.parent, 'updated_at': category_2.updated_at, 'created_at': category_2.created_at } children_1.append(item_2) item_1 = { 'children_p': children_1, 'category_name': category_1.category_name, 'category_desc': category_1.category_desc, 'category_level': category_1.category_level, 'id': category_1.id, 'parent_id': category_1.parent_id, 'parent': category_1.parent, 'updated_at': category_1.updated_at, 'created_at': category_1.created_at } logging.error(f'{category_1.category_name}: {item_1}') if item_1['children_p'] == []: del (item_1['children_p']) logging.error(f'{category_1.category_name}: {item_1}') children.append(item_1) logging.error(f'aaaaa: {children}') return children
class User(SurrogatePK, db.Model): # type: ignore username = Column(db.String(20), unique=True, nullable=False, doc='username') nick = Column(db.String(20), unique=True, nullable=False, doc='nick name')
class User(BaseModel): username = Column(db.String(50), nullable=False, unique=True) email = Column(db.String(50), nullable=False, unique=True) password = Column(db.String(255), nullable=False, server_default='') role = Column(db.Enum(RoleEnum), doc='角色', default=RoleEnum.admin)
# __tablename__ = 'submenus' # exclude_columns = ['created_at', 'updated_at'] # auth_name = Column(db.String(32), nullable=False, index=True) # path = Column(db.String(256), nullable=False) # level = Column(db.Integer, nullable=True) # parent_id = Column(db.Integer, db.ForeignKey('menus.id')) # parent = relationship('Menu', backref=db.backref('children', order_by=auth_name)) # def __init__(self, auth_name, path, **kwargs): # db.Model.__init__(self, auth_name=auth_name, path=path, **kwargs) association_table = db.Table( 'association', # Column('menu_id', db.Integer, db.ForeignKey('menus.id', ondelete='CASCADE')), # Column('role_id', db.Integer, db.ForeignKey('roles.id', ondelete='CASCADE')) Column('menu_id', db.Integer, db.ForeignKey('menus.id')), Column('role_id', db.Integer, db.ForeignKey('roles.id'))) class Menu(db.Model): __tablename__ = 'menus' id = db.Column(db.Integer, primary_key=True) exclude_columns = ['created_at', 'updated_at'] auth_name = Column(db.String(32), nullable=False, index=True) path = Column(db.String(256), nullable=False) level = Column(db.Integer, nullable=True) created_at = Column(db.Date, nullable=True, default=datetime.now) updated_at = Column(db.Date, nullable=True, default=datetime.now) parent_id = db.Column(db.Integer, db.ForeignKey('menus.id')) children = db.relationship('Menu', back_populates='parent') parent = db.relationship('Menu',