text: import jwt
  from datetime import datetime
  from time import time
  
  from werkzeug.security import generate_password_hash, check_password_hash
  from flask_login import UserMixin
  
  from app import app, db
  
  
  followers = db.Table(
      'followers',
      db.Column('follower_id', db.Integer, db.ForeignKey('user.id')),
      db.Column('followed_id', db.Integer, db.ForeignKey('user.id'))
  )
  
  class User(db.Model, UserMixin):
      id = db.Column(db.Integer, primary_key=True)
      avatar = db.Column(
          db.String(200), nullable=False, default='images/default.png'
      )
      username = db.Column(db.String(120))
      email = db.Column(db.String(120), index=True, unique=True)
      password = db.Column(db.String(128))
      posts = db.relationship(
          'Post', backref='author', cascade="all,delete", lazy='dynamic'
      )
      profile = db.relationship(
          'Profile', backref='user', cascade="all,delete", uselist=False
      )
      comments = db.relationship(
          'Comment', cascade="all,delete", backref='username'
      )
      followed = db.relationship(
          'User', secondary=followers,
          primaryjoin=(followers.c.follower_id == id),
          secondaryjoin=(followers.c.followed_id == id),
          backref=db.backref('followers', lazy='dynamic'), lazy='dynamic')
  
      liked = db.relationship(
          'PostLike',
          foreign_keys='PostLike.user_id',
          backref='user', lazy='dynamic')
  
      def __repr__(self):
          return '<User {}>'.format(self.username)
  
      def set_password(self, password):
          self.password = generate_password_hash(password)
  
      def check_password(self, password):
          return check_password_hash(self.password, password)
  
      def like(self, post):
          if not self.has_liked(post):
              like = PostLike(user_id=self.id, post_id=post.id)
              db.session.add(like)
  
      def unlike(self, post):
          if self.has_liked(post):
              PostLike.query.filter_by(
                  user_id=self.id,
                  post_id=post.id).delete()
  
      def has_liked(self, post):
          return PostLike.query.filter(
              PostLike.user_id == self.id,
              PostLike.post_id == post.id).count() > 0
  
      def liked_posts(self):
          liked = Post.query.join(PostLike, (PostLike.user_id == PostLike.user_id)).filter(PostLike.post_id == Post.id)
          return liked
  
      def follow(self, user):
          if not self.is_following(user):
              self.followed.append(user)
  
      def unfollow(self, user):
          if self.is_following(user):
              self.followed.remove(user)
  
      def is_following(self, user):
          return self.followed.filter(
              followers.c.followed_id == user.id).count() > 0
  
      def followed_posts(self):
          followed = Post.query.join(
              followers, (followers.c.followed_id == Post.user_id)).filter(
                  followers.c.follower_id == self.id)
          own = Post.query.filter_by(user_id=self.id)
          return followed.union(own).order_by(Post.create_date.desc())
  
      def get_reset_password_token(self, expires_in=600):
          return jwt.encode(
              {'reset_password': self.id, 'exp': time() + expires_in},
              app.config['SECRET_KEY'], algorithm='HS256')
  
      @staticmethod
      def verify_reset_password_token(token):
          try:
              id = jwt.decode(token, app.config['SECRET_KEY'],
                              algorithms=['HS256'])['reset_password']
          except:
              return
          return User.query.get(id)
  
  
  
  class PostLike(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
      post_id = db.Column(db.Integer, db.ForeignKey('post.id'))
  
  
  class Profile(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      fname = db.Column(db.String(120))
      lname = db.Column(db.String(120))
      about = db.Column(db.String(250))
      user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
      
      def __repr__(self):
          return '<{} {}>'.format(self.fname, self.lname)
  
  
  class Comment(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      comment = db.Column(db.String(300))
      create_date = db.Column(db.DateTime, index=True, default=datetime.utcnow)
      post_id = db.Column(db.Integer, db.ForeignKey('post.id'))
      user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
      
  
  class Post(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      image = db.Column(db.String(200))
      title = db.Column(db.String(300))
      tags = db.Column(db.String(600))
      content = db.Column(db.String(800))
      view = db.Column(db.Integer, default=0)
      create_date = db.Column(db.DateTime, index=True, default=datetime.utcnow)
      comment = db.relationship('Comment', cascade="all,delete", backref='post')
      user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
      category_id = db.Column(db.Integer, db.ForeignKey('category.id'))
      likes = db.relationship('PostLike', backref='post', lazy='dynamic')
  
      def __repr__(self):
          return '<{}>'.format(self.title)
  
  
  class Category(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      title = db.Column(db.String(200))
  
      def __repr__(self):
          return '{}'.format(self.title)