class Book(db.Model, TimestampMixin): def __init__(self, **kwargs): for k, v in kwargs.items(): setattr(self, k, v) __tablename__ = 'book' id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(255, convert_unicode=True), nullable=False, unique=True) author = db.Column(db.Text(convert_unicode=True), nullable=True) description = db.Column(db.Text(convert_unicode=True), nullable=True) is_active = db.Column(db.Boolean, default=True) star = db.Column(db.SmallInteger, default=5) price = db.Column(db.Float, nullable=False) quantity_in_stock = db.Column(db.Integer, nullable=False) quantity_sold = db.Column(db.Integer, default=0) users = relationship("UserBookAction", back_populates="book") categories = relationship("Category", secondary=category_book_table, back_populates="books") def update_attr(self, **kwargs): for k, v in kwargs.items(): setattr(self, k, v) self.updated_at = datetime.now() def to_dict(self): from ducttapp.models import User, UserBookAction user_created = db.session.query(User).filter( self.id == UserBookAction.book_id).filter( UserBookAction.log_name == LogAction.CREATED).filter( User.id == UserBookAction.user_id).first().username or "" return { 'id': self.id, 'name': self.name, 'description': self.description, 'is_active': self.is_active, 'created_at': self.created_at, 'updated_at': self.updated_at, 'quantity_in_stock': self.quantity_in_stock, 'price': self.price, 'user_created': user_created, 'categories': [x.id for x in self.categories] }
class Action(db.Model): def __init__(self, **kwargs): for k, v in kwargs.items(): setattr(self, k, v) __tablename__ = 'action' name = db.Column(db.String(256, collation='utf8mb4_general_ci', convert_unicode=True), primary_key=True) description = db.Column(db.Text(collation='utf8mb4_general_ci', convert_unicode=True), nullable=True) created_at = db.Column(db.TIMESTAMP, default=datetime.now)
class Signup_Request(db.Model): def __init__(self, **kwargs): for k, v in kwargs.items(): setattr(self, k, v) self.expired_time = (datetime.now() + timedelta(minutes=30)) __tablename__ = 'signup_request' id = db.Column(db.Integer, primary_key=True, autoincrement=True) username = db.Column(db.String(128, convert_unicode=True), nullable=False, unique=True) email = db.Column(db.String(128, convert_unicode=True), nullable=False, unique=True) password_hash = db.Column(db.Text(convert_unicode=True), nullable=False) is_admin = db.Column(db.Boolean, default=False) expired_time = db.Column(db.TIMESTAMP, default=(datetime.now() + timedelta(minutes=30))) user_token_confirm = db.Column(db.Text(), nullable=False) @property def password(self): raise AttributeError('password: write-only field') @password.setter def password(self, password): self.password_hash = bcrypt.generate_password_hash(password).decode( 'utf-8') def token_verify_expired(self): return self.expired_time < datetime.now() def to_dict(self): return { "username": self.username, "email": self.email, "user_token_confirm": self.user_token_confirm }
class Category(db.Model, TimestampMixin): def __init__(self, **kwargs): for k, v in kwargs.items(): setattr(self, k, v) __tablename__ = 'category' id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(255, convert_unicode=True), nullable=False, unique=True) description = db.Column(db.Text(convert_unicode=True), nullable=True) is_active = db.Column(db.Boolean, default=True) users = relationship("UserCategoryAction", back_populates="category", cascade="all, delete-orphan") books = relationship( "Book", secondary=category_book_table, back_populates="categories") def update_attr(self, **kwargs): for k, v in kwargs.items(): setattr(self, k, v) self.updated_at = datetime.now() def to_dict(self): from ducttapp.models import User, UserCategoryAction return { 'id': self.id, 'name': self.name, 'description': self.description, 'is_active': self.is_active, 'created_at': self.created_at, 'updated_at': self.updated_at, 'user_created': db.session.query( User ).filter( self.id == UserCategoryAction.category_id ).filter( UserCategoryAction.log_name == LogAction.CREATED ).filter( User.id == UserCategoryAction.user_id ).first().username or "" }
class User(db.Model, TimestampMixin): def __init__(self, **kwargs): for k, v in kwargs.items(): setattr(self, k, v) __tablename__ = 'user' id = db.Column(db.Integer, primary_key=True, autoincrement=True) username = db.Column(db.String(128, convert_unicode=True), nullable=False) email = db.Column(db.String(128, convert_unicode=True), nullable=False, unique=True) fullname = db.Column(db.String(128, convert_unicode=True), nullable=True) gender = db.Column(db.Boolean, nullable=True) phone_number = db.Column(db.String(20), nullable=True) birthday = db.Column(db.DateTime, nullable=True) avatar = db.Column(db.String(256), nullable=True) password_hash = db.Column(db.Text(convert_unicode=True), nullable=False) is_admin = db.Column(db.Boolean, default=False) is_active = db.Column(db.Boolean, default=True) time_unlock = db.Column(db.TIMESTAMP, default=datetime.now) history_pass_change = relationship("HistoryPassChange", cascade="save-update, merge, delete") history_wrong_pass = relationship("HistoryWrongPass", cascade="save-update, merge, delete") user_action = relationship("UserAction", cascade="save-update, merge, delete") social_login = relationship("SocialLogin", cascade="save-update, merge, delete") roles = relationship("Role", secondary=user_role_table, back_populates="users") categories = relationship("UserCategoryAction", back_populates="user", cascade="all, delete-orphan") books = relationship("UserBookAction", back_populates="user", cascade="all, delete-orphan") @property def password(self): raise AttributeError('password: write-only field') @password.setter def password(self, password): self.password_hash = bcrypt.generate_password_hash(password).decode( 'utf-8') def check_password(self, password): return bcrypt.check_password_hash(self.password_hash, password) def update_attr(self, **kwargs): for k, v in kwargs.items(): setattr(self, k, v) self.updated_at = datetime.now() def to_dict(self): return { 'id': self.id, 'email': self.email, 'username': self.username, 'is_active': self.is_active, 'updated_at': self.updated_at, 'fullname': self.fullname, 'phone_number': self.phone_number, 'gender': self.gender, 'birthday': self.birthday, 'avatar': self.avatar, 'roles': [r.id for r in self.roles] }