class PostLike(db.Model): __tablename__ = 'post_likes' id = db.Column(db.Integer, primary_key=True, autoincrement=True) post_id = db.Column(db.Integer, db.ForeignKey('post.id', ondelete='CASCADE')) user_id = db.Column(db.Integer, db.ForeignKey('user.id', ondelete='CASCADE')) def __init__(self, post_id, user_id): self.post_id = post_id self.user_id = user_id def __repr__(self): return '<PostLike:post %s is liked by uesr %s>' % (self.post_id, self.user_id)
class Accounts(db.Model): id = db.Column(db.Integer, primary_key=True) encrypted_social_media = db.Column(db.String(20)) encrypted_username = db.Column(db.String(30)) encrypted_password = db.Column(db.String(30)) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) # getter and setter (encrypting values before saving to database) @property def social_media(self): return Encryption.decrypt(ENCRYPTION_KEY, self.encrypted_social_media) @property def username(self): return Encryption.decrypt(ENCRYPTION_KEY, self.encrypted_username) @property def password(self): return Encryption.decrypt(ENCRYPTION_KEY, self.encrypted_password) @social_media.setter def social_media(self, value): self.encrypted_social_media = Encryption.encrypt(ENCRYPTION_KEY, value) @username.setter def username(self, value): self.encrypted_username = Encryption.encrypt(ENCRYPTION_KEY, value) @password.setter def password(self, value): self.encrypted_password = Encryption.encrypt(ENCRYPTION_KEY, value)
class Post(db.Model): __tablename__ = 'post' id = db.Column(db.Integer, primary_key=True, autoincrement=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) send_time = db.Column(db.DateTime, default=datetime.now()) body = db.Column(db.String(128)) post_like_person_ids = db.relationship('PostLike', backref='post', lazy='dynamic', foreign_keys='PostLike.post_id', cascade="all, delete-orphan", passive_deletes=True) all_comments = db.relationship( 'Comment', backref='post', lazy='dynamic', foreign_keys='Comment.post_id', #class.filed to assignment cascade="all, delete-orphan", passive_deletes=True) def __init__(self, user_id, body): self.body = body self.user_id = user_id def __repr__(self): return '<Post:%s>' % self.body
class Todo(db.Model): __tablename__ = 'todo' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(20)) description = db.Column(db.String(100)) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) def __repr__(self): return 'Id: {}, name: {}'.format(self.id, self.name)
class Comment(db.Model): __tablename__ = 'comments' id = db.Column(db.Integer, primary_key=True, autoincrement=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id', ondelete='CASCADE')) send_time = db.Column(db.DateTime, default=datetime.now()) comment_body = db.Column(db.Text) post_id = db.Column(db.Integer, db.ForeignKey('post.id', ondelete='CASCADE')) parent_id = db.Column( db.Integer ) #if it equals to comments.id that is relpy,otherwise comment comment_like_count = db.Column(db.Integer, default=0) def __init__(self, user_id, comment_body, post_id, parent_id): self.user_id = user_id self.comment_body = comment_body self.post_id = post_id self.parent_id = parent_id def __repr__(self): return '<Comment:user %s \'s comment in comment_id %s in post_id:%s>' % ( self.user_id, self.parent_id, self.post_id)
class Unit(db.Model, BaseModel): __tablename__ = 'units' id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(150), unique=True, nullable=False) manager = db.Column(db.String(150), unique=False, nullable=True) mall_id = db.Column(db.Integer, db.ForeignKey("malls.id", ondelete='SET NULL')) creation_date = db.Column(db.DateTime, default=now, nullable=False) last_modification_date = db.Column(db.DateTime, default=now, onupdate=now, nullable=False) def __init__(self, name, manager=None, mall_id=None, **kwargs): self.name = name self.manager = manager self.manager = mall_id
class Mall(db.Model, BaseModel): __tablename__ = 'malls' id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(150), unique=True, nullable=False) address = db.Column(db.String(300), unique=False, nullable=True) account_id = db.Column(db.Integer, db.ForeignKey("accounts.id", ondelete='SET NULL')) units = db.relationship('Unit', lazy='joined') creation_date = db.Column(db.DateTime, default=now, nullable=False) last_modification_date = db.Column(db.DateTime, default=now, onupdate=now, nullable=False) def __init__(self, name, address=None, account_id=None, **kwargs): self.name = name self.address = address self.account_id = account_id
class TokenBlacklist(db.Model): """Blacklist representation """ id = db.Column(db.Integer, primary_key=True) jti = db.Column(db.String(36), nullable=False, unique=True) token_type = db.Column(db.String(10), nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) revoked = db.Column(db.Boolean, nullable=False) expires = db.Column(db.DateTime, nullable=False) user = db.relationship('User', lazy='joined') def to_dict(self): return { 'token_id': self.id, 'jti': self.jti, 'token_type': self.token_type, 'user_identity': self.user_identity, 'revoked': self.revoked, 'expires': self.expires }
class Stu(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(20), unique=True) grade_id = db.Column(db.Integer, db.ForeignKey("grade.id"))
class Stu(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(20), unique=True) grade_id = db.Column(db.Integer, db.ForeignKey("grade.id")) class Tag(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) title = db.Column(db.String(20)) tags = db.Table( "tags", db.Column("tag_id", db.Integer, db.ForeignKey("tag.id"), primary_key=True), db.Column("book_id", db.Integer, db.ForeignKey("book.id"), primary_key=True), ) class Book(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(20), ) tags = db.relationship("Tag", secondary=tags, backref=db.backref("books", lazy=True), lazy=True)