class User(UserMixin, db.Model): __tablename__ = "users" id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(100), unique=True) password = db.Column(db.String(100)) name = db.Column(db.String(100)) img_user = db.Column(db.String(250), nullable=False, default="person.png") posts = relationship("BlogPost", back_populates="author") comments = relationship("Comment", back_populates="comment_author") profile = relationship("Profile", uselist=False, back_populates="user_profile") def get_reset_token(self, expires_sec=1800): s = Serializer(app.config['SECRET_KEY'], expires_sec) return s.dumps({'user_id': self.id}).decode('utf-8') @staticmethod def verify_reset_token(token): s = Serializer(app.config['SECRET_KEY']) try: user_id = s.loads(token)['user_id'] except: return None return User.query.get(user_id)
class User(db.Model, UserMixin): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) profile_image = db.Column(db.String(64), nullable=False, default='default_profile.png') email = db.Column(db.String(64), unique=True, nullable=False, index=True) username = db.Column(db.String(64), unique=True, nullable=False, index=True) password_hash = db.Column(db.String(128)) posts = db.relationship('BlogPost', backref='author', lazy=True) def __init__(self, email, username, password): self.email = email self.username = username self.password_hash = generate_password_hash(password) def check_password(self, password): return check_password_hash(self.password_hash, password) def __repr__(self): return f"Username: {self.username}"
class Comment(db.Model): __tablename__ = "comments" id = db.Column(db.Integer, primary_key=True) post_id = db.Column(db.Integer, db.ForeignKey("blog_posts.id")) author_id = db.Column(db.Integer, db.ForeignKey("users.id")) parent_post = relationship("BlogPost", back_populates="comments") comment_author = relationship("User", back_populates="comments") text = db.Column(db.Text, nullable=False)
class blogpost(db.Model): users = db.relationship(User) id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) title = db.Column(db.String(140), nullable=False) text = db.Column(db.Text, nullable=False) def __init__(self, title, text, user_id): self.title = title self.text = text self.user_id = user_id def __repr__(self): return f"Post id: {self.id}--Date: {self.date}---Title: {self.title}"
class Profile(db.Model): __tablename__ = "profile" id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey("users.id")) user_profile = relationship("User", back_populates="profile") designation = db.Column(db.String(100)) bday = db.Column(db.String(20)) address = db.Column(db.String(100)) zipcode = db.Column(db.Integer) phone = db.Column(db.Integer, unique=False) job = db.Column(db.Text)
class BlogPost(db.Model): __tablename__ = "blog_posts" id = db.Column(db.Integer, primary_key=True) author_id = db.Column(db.Integer, db.ForeignKey("users.id")) author = relationship("User", back_populates="posts") title = db.Column(db.String(250), unique=True, nullable=False) subtitle = db.Column(db.String(250), nullable=False) date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) body = db.Column(db.Text, nullable=False) img_url = db.Column(db.String(250), nullable=False) comments = relationship("Comment", back_populates="parent_post")