class Blog(db.Model): __tablename__ = 'blogs' id = db.Column(db.Integer, primary_key=True) blog_id = db.Column(db.Text) dish_name = db.Column(db.String(60), nullable=False) dish_receipe = db.Column(db.Text) date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) recipe_img = db.Column(db.String(80), nullable=True) email = db.Column(db.String(80), db.ForeignKey('users.email'), nullable=False) user = db.relationship('User', backref=db.backref('blogs', lazy=True, cascade="all,delete")) def __init__(self, email, dish_name, dish_receipe, count): self.blog_id = str(email) + '-' + str(dish_name) + '-' + str(count) self.email = email self.dish_name = dish_name self.dish_receipe = dish_receipe def __repr__(self): return "Id: {} User: {} \n Receipe Name: {} \n Receipe: {} ".format( self.blog_id, self.email, self.dish_name, self.dish_receipe)
class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(20), unique=True, nullable=False) email = db.Column(db.String(120), unique=True, nullable=False) image_file = db.Column(db.String(20), nullable=False, default='default.jpg') password = db.Column(db.String(60), nullable=False) posts = db.relationship('Post', backref='author', lazy=True) def __repr__(self): return f"User('{self.username}', '{self.email}', '{self.image_file}')"
class Post(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100), nullable=False) date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) content = db.Column(db.Text, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) def __repr__(self): return f"Post('{self.title}', '{self.date_posted}')"
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, 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 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}"