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 User(db.Model, UserMixin): __tablename__ = 'users' 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) pw = db.Column(db.String(60), nullable=False) birth_date = db.Column(db.DateTime) intro = db.Column(db.Text) recipes = db.relationship('Recipe', backref='user', lazy=True) media = db.relationship('SocialMedia', backref='user', lazy=True) def image_path(self): """Returns the path for the user image. """ return url_for('static', filename=f'images/users/{self.username}.jpg')
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 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()
class Web(db.Model): __tablename__ = 'webs' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), unique=True, nullable=False) link = db.Column(db.String(200), unique=True, nullable=False) icon = db.Column(db.String(100), nullable=False)
class RecipeState(db.Model): __tablename__ = 'recipe_states' id = db.Column(db.Integer, primary_key=True) state = db.Column(db.String(100), nullable=False) label = db.Column(db.String(100)) recipes = db.relationship('Recipe', backref='state', lazy=True)
class Unit(db.Model): __tablename__ = 'units' id = db.Column(db.Integer, primary_key=True) singular = db.Column(db.String(20), nullable=False) plural = db.Column(db.String(20), nullable=False)