class RecipeCategory(db.Model): '''class to represent recipe category table''' __tablename__ = 'recipe_category' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(120), unique=True, nullable=False) description = db.Column(db.Text) user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) def __init__(self, name, description, user_id): self.name = name self.description = description self.user_id = user_id def save(self): db.session.add(self) db.session.commit() @staticmethod def get_all(): return RecipeCategory.query.all() @staticmethod def get_all_limit_offset(userid, lim): return RecipeCategory.query.filter_by(user_id=\ userid).limit(lim).all() def delete(self): db.session.delete(self) db.session.commit() def __repr__(self): return "<Category: {}>".format(self.name)
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 BlacklistToken(db.Model): """ Token Model for storing JWT tokens """ __tablename__ = 'blacklist_tokens' id = db.Column(db.Integer, primary_key=True, autoincrement=True) token = db.Column(db.String(500), unique=True, nullable=False) blacklisted_on = db.Column(db.DateTime, nullable=False) def __init__(self, token): self.token = token self.blacklisted_on = datetime.datetime.now() @staticmethod def check_blacklist(auth_token): # check whether auth token has been blacklisted res = BlacklistToken.query.filter_by(token=str(auth_token)).first() if res: return True else: return False def __repr__(self): return '<id: token: {}'.format(self.token)
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_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 Liked(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100), nullable=False) date_searched = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) recipe_id = db.Column(db.Integer, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) def __repr__(self): return f"Liked('{self.title}', '{self.date_searched}','{self.recipe_id}')"
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 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}"
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 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 Recipe(db.Model): '''Class to represent recipe table''' __tablename__ = 'recipe' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(120), unique=True, nullable=False) ingredients = db.Column(db.String(250)) description = db.Column(db.Text) cat_id = db.Column(db.Integer, db.ForeignKey('recipe_category.id'), nullable=False) user_id = db.Column(db.Integer, nullable=False) def __init__(self, name, cat_id, user_id, ingredients=None, description=None): self.name = name self.ingredients = ingredients self.description = description self.cat_id = cat_id self.user_id = user_id def save(self): db.session.add(self) db.session.commit() @staticmethod def update(): db.session.commit() @staticmethod def get_all(): return Recipe.query.all() @staticmethod def get_all_limit_offset(catid, userid, lim): return Recipe.query.filter_by(cat_id=catid, user_id=\ userid).limit(lim).all() def delete(self): db.session.delete(self) db.session.commit() def __repr__(self): return "<Recipe: {}>".format(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}"
def __str__(self): return self.name def __repr__(self): return self.name 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)) user_recipe_table = db.Table('user_recipe', db.Column('user_id', db.Integer, db.ForeignKey('user.id')), db.Column('recipe_id', db.Integer, db.ForeignKey('recipe.id')) ) # recipe_ingredient_table = db.Table('recipe_ingredient', # db.Column('recipe_id',db.Integer, db.ForeignKey('recipe.id')), # db.Column('ingredient_id', db.Integer, db.ForeignKey('ingredient.id')) # )