class SocialMedia(db.Model): __tablename__ = 'social_media' id_user = db.Column(db.Integer, db.ForeignKey('users.id'), primary_key=True) id_web = db.Column(db.Integer, db.ForeignKey('webs.id'), primary_key=True) username = db.Column(db.String(100), nullable=False) web = db.relationship('Web', backref='media', lazy=True)
class Recipe(db.Model): __tablename__ = 'recipes' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), nullable=False, unique=True) url = db.Column(db.String(100), nullable=False, unique=True) time_prep = db.Column(db.Integer) time_cook = db.Column(db.Integer) intro = db.Column(db.Text) text = db.Column(db.Text) link_video = db.Column(db.String(100)) health = db.Column(db.Text) created_at = db.Column(db.DateTime, nullable=False, default=now()) changed_at = db.Column(db.DateTime) published_at = db.Column(db.DateTime) id_season = db.Column(db.Integer, db.ForeignKey('seasons.id')) id_state = db.Column(db.Integer, db.ForeignKey('recipe_states.id'), nullable=False) id_user = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) steps = db.relationship('RecipeStep', backref='recipe', lazy=True, cascade='delete') ingredients = db.relationship('Quantity', backref='recipe', lazy=True, cascade='delete') def image_path(self, folder): """Returns the path for the recipe image in the given size. """ return url_for('static', filename=f'images/recipes/{folder}/{self.url}.jpg')
class Ingredient(db.Model): __tablename__ = 'ingredients' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), nullable=False, unique=True) url = db.Column(db.String(100), nullable=False, unique=True) health = db.Column(db.Text, nullable=False) created_at = db.Column(db.DateTime, nullable=False, default=now()) created_by = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) changed_at = db.Column( db.DateTime) # nullable not necessary because of default. changed_by = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) is_valid = db.Column(db.Boolean, default=False) creator = db.relationship('User', backref='ingredient', foreign_keys='Ingredient.created_by', lazy=True) def __repr__(self): return self.name def uses(self): """ Returns the amount of recipes and subrecipes that use it. """ recipe_count = Quantity.query.filter_by(id_ingredient=self.id).count() subrecipe_count = Subquantity.query.filter_by( id_ingredient=self.id).count() return recipe_count + subrecipe_count
class Subquantity(db.Model): __tablename__ = 'ingredients_in_subrecipe' id = db.Column(db.Integer, primary_key=True) amount = db.Column(db.Integer, nullable=False) id_subrecipe = db.Column(db.Integer, db.ForeignKey('subrecipes.id'), nullable=False) id_ingredient = db.Column(db.Integer, db.ForeignKey('ingredients.id'), nullable=False) id_unit = db.Column(db.Integer, db.ForeignKey('units.id'), nullable=False) ingredient = db.relationship('Ingredient', lazy=True) unit = db.relationship('Unit', lazy=True)
class RecipeStep(db.Model): __tablename__ = 'recipe_steps' id = db.Column(db.Integer, primary_key=True) step_nr = db.Column(db.Integer, nullable=False) step = db.Column(db.String(500), nullable=False) id_recipe = db.Column(db.Integer, db.ForeignKey('recipes.id'), nullable=False)
class Subrecipe(db.Model): __tablename__ = 'subrecipes' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), nullable=False, unique=True) url = db.Column(db.String(100), nullable=False, unique=True) created_at = db.Column(db.DateTime, nullable=False, default=now()) changed_at = db.Column(db.DateTime) is_feminine = db.Column(db.Boolean, nullable=False) id_user = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) steps = db.relationship('SubrecipeStep', backref='subrecipe', lazy=True, cascade='delete') ingredients = db.relationship('Subquantity', backref='subrecipe', lazy=True, cascade='delete') def uses(self): """ Returns the number of recipes on which the given subrecipe appears. """ return RecipeStep.query.filter_by(step=str(self.id)).count()