class User(db.Model, UserMixin): # Create a table in the db __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) profile_image = db.Column(db.String(20), nullable=False, default='default_profile.png') user_email = db.Column(db.String(64), unique=True, index=True) user_name = db.Column(db.String(64), unique=True, index=True) user_password = db.Column(db.String(128)) # This connects BlogPosts to a User Author. posts = db.relationship('BlogPost', backref='author', lazy=True) def check_password(self, password): return bcrypt.check_password_hash(self.user_password, password) @classmethod # used in run.py def create_user(cls, user, email, password): user = cls(user_name=user, user_email=email, user_password=bcrypt.generate_password_hash( password).decode('utf-8')) db.session.add(user) db.session.commit() return user
class Blogpost(db.Model): __tablename__ = 'blog' users = db.relationship(Users) 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 User(db.Model, UserMixin): # Create a table in the db __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) profile_image = db.Column(db.String(20), nullable=False, default='default_profile.png') email = db.Column(db.String(64), unique=True, index=True) username = db.Column(db.String(64), unique=True, index=True) password_hash = db.Column(db.String(128)) # This connects BlogPosts to a User Author. 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): __tabelname__ = "blogposts" users = db.relationship(User) id = db.Column(db.Integer, unique=True, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey("users.id"), nullable=False) title = db.Column(db.String(128), nullable=False) date = db.Column(db.DateTime, nullable=True, default=datetime.utcnow()) body = db.Column(db.Text(), nullable=False) def __init__(self, title, body, user_id): self.title = title self.body = body self.user_id = user_id def __repr__(self): return f"post ID:{self.id} was created at{self.date}. The author of the post was {self.user.user_name}"
class Users(db.Model, UserMixin): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) profile_image = db.Column(db.String(30), nullable=False, default='default-profile-picture-man.jpg') email = db.Column(db.String(68), unique=True, index=True) mobile = db.Column(db.Integer, unique=True, nullable=True) username = db.Column(db.String(34), unique=True, index=True) password_hash = db.Column(db.String(128), nullable=False) sex = db.Column(db.String(10), nullable=True, default='m') posts = db.relationship('Blogpost', backref='author', lazy=True) def __init__(self, email, username, password, sex, mobile): self.email = email self.username = username self.password_hash = generate_password_hash(password) self.sex = sex self.mobile = mobile def check_password(self, password): 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 User(db.Model, UserMixin): __tablename__ = "users" id = db.Column(db.Integer, unique=True, primary_key=True) user_name = db.Column(db.String(64), nullable=False, unique=True, index=True) email = db.Column(db.String(64), nullable=False, unique=True, index=True) hash_password = db.Column(db.String(128), unique=True, nullable=False) profile_image = db.Column(db.String(64), default='default_profile.png') blog_post = db.relationship('BlogPost', backref='Author', lazy=True) def __init__(self, user_name, email, password): self.user_name = user_name self.email = email self.hash_password = generate_password_hash(password) def match_password(self, password): return check_password_hash(self.hash_password, password) def __repr__(self): return f"{self.user_name} this user name create his/her last post {self.BlogPost.title} at {self.logPost.date}"