class RecipeIngredient(db.Model): __tablename__ = 'ingredient' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(250), nullable=False) recipe_id = db.Column(db.Integer, db.ForeignKey('recipe.id', ondelete='CASCADE'), nullable=False) pre_measure = db.Column(db.String(250), nullable=True) post_measure = db.Column(db.String(250), nullable=True) amount = db.Column(db.Float, nullable=True) amount_units_id = db.Column(db.Integer, db.ForeignKey('unit.id'), nullable=True) amount_units = db.relationship("IngredientUnit") divided = db.Column(db.Boolean, nullable=False, default=False) def __repr__(self): return "{} {} {}".format(self.amount, self.amount_units, self.name) @property def serialize(self): return _serialize(self) @classmethod def deserialize(cls, recipe_dict): return _deserialize(cls, recipe_dict) @property def amount_fract(self): """ Converts float to fraction string Returns: str: fraction """ return convert_to_mixed_numeral(self.amount)
class RecipeNote(db.Model): __tablename__ = "recipe_note" id = db.Column(db.Integer, primary_key=True) recipe_id = db.Column(db.Integer, db.ForeignKey('recipe.id', ondelete='CASCADE'), nullable=False) create_date = db.Column(db.DateTime, default=datetime.datetime.utcnow) note_text = db.Column(db.String(250), nullable=False) @property def serialize(self): return _serialize(self) @classmethod def deserialize(cls, recipe_dict): return _deserialize(cls, recipe_dict)
class RecipeStep(db.Model): __tablename__ = "recipe_step" id = db.Column(db.Integer, primary_key=True) recipe_id = db.Column(db.Integer, db.ForeignKey('recipe.id', ondelete='CASCADE'), nullable=False) step_number = db.Column(db.Integer, nullable=False) description = db.Column(db.String(250), nullable=False) @property def serialize(self): return _serialize(self) @classmethod def deserialize(cls, recipe_dict): return _deserialize(cls, recipe_dict)
class Recipe(db.Model): __tablename__ = 'recipe' # Here we define db.Columns for the table recipe # Notice that each db.Column is also a normal Python instance attribute. id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(250), nullable=False) description = db.Column(db.String(250)) source = db.Column(db.String(250), nullable=True) source_url = db.Column(db.String(250), nullable=True) total_served = db.Column(db.Integer, nullable=True) recipe_type_id = db.Column(db.Integer, db.ForeignKey('recipe_type.id'), nullable=True) recipe_type = db.relationship("RecipeType") ingredients = db.relationship("RecipeIngredient", cascade="all, delete-orphan") steps = db.relationship("RecipeStep", cascade="all, delete-orphan") notes = db.relationship("RecipeNote", cascade="all, delete-orphan") create_date = db.Column(db.DateTime, default=datetime.datetime.utcnow) @property def serialize(self): d = _serialize(self) d['recipe_type'] = self.recipe_type.serialize d['steps'] = [step.serialize for step in self.steps] d['ingredients'] = [ ingredient.serialize for ingredient in self.ingredients ] d['notes'] = [note.serialize for note in self.notes] return d @classmethod def deserialize(cls, recipe_dict): d_recipe = _deserialize(cls, recipe_dict) existing_recipe_type = db.session.query(RecipeType).filter( RecipeType.name == 'Stove Top').first() if existing_recipe_type is None: d_recipe.recipe_type = RecipeType.deserialize( recipe_dict['recipe_type']) else: d_recipe.recipe_type = existing_recipe_type if 'ingredients' in recipe_dict: d_recipe.ingredients = [ RecipeIngredient.deserialize(i) for i in recipe_dict['ingredients'] ] if 'steps' in recipe_dict: d_recipe.steps = [ RecipeStep.deserialize(i) for i in recipe_dict['steps'] ] if 'notes' in recipe_dict: d_recipe.notes = [ RecipeNote.deserialize(i) for i in recipe_dict['notes'] ] return d_recipe