class Recipe(db.Model, CRUDModel): __tablename__ = 'recipes' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String) rating = db.Column(db.Integer) date_added = db.Column(db.String) steps = db.relationship('Step', backref='recipe', lazy='dynamic') notes = db.relationship('Note', backref='recipe', lazy='dynamic') recipeingredients = db.relationship('RecipeIngredient', backref='recipe', lazy='dynamic') def __repr__(self): return '<Recipe {:d} {}>'.format(self.id, self.name) def __str__(self): return self.name def update_from(self, new_obj): if new_obj.name: self.name = new_obj.name if new_obj.rating: self.rating = new_obj.rating if new_obj.date_added: self.date_added = new_obj.date_added self.save()
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): return check_password_hash(self.password_hash, password) def __str__(self): return "Username: %s" % self.username
class Product(db.Model): __tablename__ = "products" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(128), nullable=False, unique=True) shop_link = db.Column(db.String(256), nullable=False) recipes = db.relationship( "Recipe", secondary=recipe_product_association, backref=db.backref("products"), ) @classmethod def get_by_name(cls, name): return cls.query.filter_by(name=name).first() @classmethod def get_all(cls): return cls.query.all() @classmethod def add(cls, name, shop_link=None): product = cls(name=name, shop_link=shop_link) db.session.add(product) db.session.commit() return product
class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(15), unique=True, nullable=False) email = db.Column(db.String(120), unique=True, nullable=False) password = db.Column(db.String(60), nullable=False) recipes = db.relationship('Recipe', backref='author', lazy=True) def __repr__(self): return f"User('{self.username}', '{self.email}')"
class Chef(db.Model): __tablename__ = "chefs" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(30), nullable=False, unique=True) password_hash = db.Column(db.String(128)) cookbooks = db.relationship("Cookbook", backref="chef") @classmethod def get_all(cls): return cls.query.all() @classmethod def get_by_name(cls, name): return cls.query.filter_by(name=name).first()
class Recipe(db.Model): __tablename__ = "recipes" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(128), nullable=False, unique=True) instruction = db.Column(db.String(2048), nullable=False) cookbooks = db.relationship( "Cookbook", secondary=cookbook_recipe_association, backref=db.backref("recipes"), ) @classmethod def add(cls, name, instruction, categories=None, products=None): if categories is None: categories = [] if products is None: products = [] product_entities = [ Product.get_by_name(product_name) for product_name in products ] category_entities = [ Category.get_by_name(category_name) for category_name in categories ] recipe = cls( name=name, instruction=instruction, categories=category_entities, products=product_entities, ) db.session.add(recipe) db.session.commit() return recipe @classmethod def get_by_name(cls, name): return cls.query.filter_by(name=name).first() @classmethod def get_all(cls): return cls.query.all()
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) 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 __str__(self): return "Recipe Id: %s --- Title: %d" % (self.id, self.title)
class Unit(db.Model, CRUDModel): __tablename__ = 'units' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String, unique=True) recipeingredients = db.relationship('RecipeIngredient', backref='unit', lazy='dynamic') def __repr__(self): return '<Unit {} {}>'.format(self.id, self.name) def __str__(self): return self.name def update_from(self, new_obj): if new_obj.name: self.name = new_obj.name self.save()
class Department(db.Model, CRUDModel): __tablename__ = 'departments' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String) ingredients = db.relationship('Ingredient', backref='department', lazy='dynamic') def __repr__(self): return '<Department {} {}>'.format(self.id, self.name) def __str__(self): return self.name def update_from(self, new_obj): app.logger.debug('Updating {} from {}'.format(self, new_obj)) self.name = new_obj.name self.save()
class Category(db.Model): __tablename__ = "categories" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(30), nullable=False, unique=True) recipes = db.relationship( "Recipe", secondary=recipe_category_association, backref=db.backref("categories"), ) @classmethod def get_by_name(cls, name): return cls.query.filter_by(name=name).first() @classmethod def get_all(cls): return cls.query.all()
class Ingredient(db.Model, CRUDModel): __tablename__ = 'ingredients' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String, unique=True) department_id = db.Column(db.Integer, db.ForeignKey('departments.id')) recipeingredients = db.relationship('RecipeIngredient', backref='ingredient', lazy='dynamic') def __repr__(self): return '<Ingredient {} {}>'.format(self.id, self.name) def __str__(self): return self.name def update_from(self, new_obj): if new_obj.name: self.name = new_obj.name if new_obj.department: self.department = new_obj.department self.save()