class recipe_ingredient_association(db.Model): """ASSOCIATION BETWEEN RECIPES AND INGREDIENTS TO INCLUDE AMOUMT""" __tablename__ = 'recipe_ingredient_association' recipe_id = db.Column(db.Integer, db.ForeignKey( 'recipe.id'), primary_key=True) ingredient_id = db.Column(db.Integer, db.ForeignKey( 'ingredient.id'), primary_key=True) recipe = db.relationship('Recipe', back_populates="ingredients") ingredient = db.relationship('Ingredient', back_populates='recipes') amount = db.Column(db.String(40))
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) password = db.Column(db.String(60), nullable=False) history = db.relationship('History', backref='searcher', lazy=True) likes = db.relationship('Liked', backref='liker', lazy=True) def __repr__(self): return f"User('{self.username}', '{self.email}'"
class Recipe(db.Model): """RECIPE MODEL""" id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(80), nullable=False) description = db.Column(db.Text()) servings = db.Column(db.Integer) instructions = db.Column(db.Text()) ingredients = db.relationship( 'recipe_ingredient_association', back_populates='recipe') favorites = db.relationship( "User", secondary="user_recipe", back_populates='favorite_recipes') image = db.Column(db.String(200), nullable=True)
class User(db.Model, UserMixin): """User Model""" id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(80), nullable=False, unique=True) password = db.Column(db.String(300), nullable=False) favorite_recipes = db.relationship( 'Recipe', secondary='user_recipe', back_populates='favorites')
class User(db.Model): '''Class to represent the users table''' __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) public_id = db.Column(db.String(100), unique=True, nullable=False) first_name = db.Column(db.String(120), nullable=False) last_name = db.Column(db.String(120), nullable=False) email = db.Column(db.String(120), unique=True, nullable=False) password = db.Column(db.String(250), nullable=False) category = db.relationship('RecipeCategory', backref='users', lazy=True) def __init__(self, public_id, email, password, first_name, last_name): '''initialize with email''' self.public_id = public_id self.email = email self.password = password self.first_name = first_name self.last_name = last_name def save(self): db.session.add(self) db.session.commit() @staticmethod def get_all(): return User.query.all() def delete(self): db.session.delete(self) db.session.commit() @staticmethod def decode_auth_token(auth_token, secret_key): """ Validates the auth token :param auth_token: :return: integer|string """ try: payload = jwt.decode(auth_token, secret_key) is_blacklisted_token = BlacklistToken.\ check_blacklist(auth_token) if is_blacklisted_token: return 'Token blacklisted. Please log in again.' else: return payload['user_id'] except jwt.ExpiredSignatureError: return 'Signature expired. Please log in again.' except jwt.InvalidTokenError: return 'Invalid token. Please log in again.' def __repr__(self): return "<User: {}>".format(self.email)
class Ingredient(db.Model): """INGREDIENT MODEL """ id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(80), nullable=False) recipes = db.relationship( 'recipe_ingredient_association', back_populates='ingredient') def __str__(self): return self.name def __repr__(self): return self.name
class RecipePost(db.Model): # Search __searchable__ = ['recipe_name'] # Setup the relationship to the User table users = db.relationship(User) # Model for the Recipe Posts on Website id = db.Column(db.Integer, primary_key=True) # we connect the Recipe Posts to a particular author user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) ############################################################################## date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) ######################################################################## recipe_image = db.Column(db.String(128), default=None, nullable=True) ######################################################################## recipe_name = db.Column(db.String(164), nullable=False) ######################################################################## course = db.Column(db.String(164), nullable=False) food_category = db.Column(db.String(164), nullable=False) cuisine = db.Column(db.String(164), nullable=False) prep_time = db.Column(db.String(164), nullable=False) cook_time = db.Column(db.String(164), nullable=False) allergens = db.Column(db.String(164), nullable=False) serving = db.Column(db.String(164), nullable=False) ############################################################################# ingredients = db.Column(db.Text, nullable=False) recipe_description = db.Column(db.Text, nullable=False) def __init__(self, recipe_image, recipe_name, ingredients, recipe_description, user_id, course, food_category, cuisine, prep_time, cook_time, allergens, serving): self.recipe_image = recipe_image self.recipe_name = recipe_name self.ingredients = ingredients self.recipe_description = recipe_description self.user_id = user_id ########################## self.course = course self.food_category = food_category self.cuisine = cuisine self.prep_time = prep_time self.cook_time = cook_time self.allergens = allergens self.serving = serving def __repr__(self): return f"Recipe Id: {self.id} --- Date: {self.date} --- Title: {self.recipe_name}"
class User(db.Model, UserMixin): # Create a table in the db __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) picture = db.Column(db.String(128), default='default_profile.png', nullable=False) 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 RecipePost to a User Author. recipe = db.relationship('RecipePost', 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 set_password(self, password): self.password_hash = generate_password_hash(password) def get_reset_password_token(self, expires_in=600): return jwt.encode( { 'reset_password': self.id, 'exp': time() + expires_in }, app.config['SECRET_KEY'], algorithm='HS256').decode('utf-8') @staticmethod def verify_reset_password_token(token): try: id = jwt.decode(token, app.config['SECRET_KEY'], algorithms=['HS256'])['reset_password'] except: return return User.query.get(id) def __repr__(self): return f"UserName: {self.username}"