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, index=True) username = db.Column(db.String(64), unique=True) password_hash = db.Column(db.String(128)) # This connects BlogPosts to a User Author. many to one 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): # https://stackoverflow.com/questions/23432478/flask-generate-password-hash-not-constant-output return check_password_hash(self.password_hash, password) def __repr__(self): return f"UserName: {self.username}"
class BlogPost(db.Model): # Setup the relationship to the User table users = db.relationship(User) # Model for the Blog Posts on Website id = db.Column(db.Integer, primary_key=True) # Notice how we connect the BlogPost to a particular author 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 Users(db.Model, UserMixin): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) profile_image = db.Column(db.String(20), nullable=False, default='Shreenu.png') email = db.Column(db.String(20)) username = db.Column(db.String(64), unique=True, index=True) blogs = db.relationship('BlogPosts', backref='author', lazy=True) password_hash = db.Column(db.String(128)) def __init__(self, email, username, password): self.email = email self.username = username self.password_hash = generate_password_hash(password) def __repr__(self): return "{} Author".format(self.username) def checkPassword(self, password): boolPasswordCheck = check_password_hash(self.password_hash, password) return boolPasswordCheck
class BlogPosts(db.Model): __tablename__ = 'Blogs' # Setup the relationship to the User table users = db.relationship(Users) post_id = db.Column(db.Integer, primary_key=True) profile_image = db.Column(db.String(20), nullable=False, default='MJPIC.png') # creating a ForeignKey to author_id column which points to User with pseudo column users.id author_id = db.Column(db.Integer, db.ForeignKey('users.id')) timeofPost = db.Column(db.DateTime, nullable=False, default=datetime.utcnow()) post_text = db.Column(db.Text) title = db.Column(db.String(140), nullable=False) def __init__(self, title, post_text, author_id): self.title = title self.author_id = author_id self.post_text = post_text def __repr__(self): return f' {self.title} : by {self.author_id} : wriiten at {self.timeofPost}'