def category_exist(self,name): user_id = session['user_id'] categories = Store().get_user_categories(user_id) for category in categories: if (category['name'] == name): return True return False
def recipe_exist(self,name): user_id = session['user_id'] recipes = Store().get_user_recipes(user_id) for recipe in recipes: if (recipe['name'] == name): return True return False
def is_exist(self,id): user_id = session['user_id'] categories = Store().get_user_categories(user_id) for category in categories: if (category['id'] == id): return True return False
def update(self, name,category_id): user_id = session['user_id'] categories = Store().get_user_categories(user_id) for category in categories: if self.is_exist(category_id): self.category_id = category_id self.name = name self.owner_id = category['owner_id'] self.created = category['created_at'] data = { 'id': self.category_id, 'owner_id': self.owner_id, 'name': name, 'created_at': self.created, } if (Store().update_category(data)): return True else: return False return True
def exist(self,email,password): users = Store().get_users() for user in users: # print(check_password_hash(user['password'], password)) if (user['email'] == email and check_password_hash(user['password'], password)): self.user_id = user['id'] self.name = user['name'] self.email = user['email'] self.password = user['password'] return True return False
def update(self, name,description,category_id,id): user_id = session['user_id'] recipes = Store().get_user_recipes(user_id) for recipe in recipes: if self.is_exist(id): self.category_id = category_id self.name = name self.owner_id = recipe['owner_id'] self.created = recipe['created_at'] data = { 'id': id, 'category_id': self.category_id, 'owner_id': self.owner_id, 'name': name, 'description': description, 'created_at': self.created, } if (Store().update_recipe(data)): return True else: return False return True
def recipes(): categories = Category().user_categories() categories_list = [] for category in categories: cat = (category['id'], category['name']) categories_list.append(cat) form = RecipeForm.editRecipe(categories_list) #Edit Recipe form addCategory = CategoryForm.AddForm() #Addcategory form categories = Store().get_user_categories(session['user_id']) recipes = Recipe().user_recipes() return render_template("recipes.html", form=form, categoryForm=addCategory, categories=categories, recipes=recipes)
def save(self, name,owner_id,created,category_id=None): self.name = name self.category_id = uuid.uuid4().hex self.created = created self.owner_id = owner_id data = { 'id': self.category_id, 'owner_id': owner_id, 'name': self.name, 'created_at': self.created, } if (self.category_exist(self.name)): return False Store().add_category(data) return True
def save(self, name,description,category_id): self.id = uuid.uuid4().hex self.name = name self.category_id = category_id self.description = description self.created = datetime.now() self.owner_id = session['user_id'] data = { 'id': self.id, 'name': self.name, 'category_id': self.category_id, 'description': self.description, 'owner_id': self.owner_id, 'created_at': self.created } Store().save_recipe(data) return True
def save(self, name, email, password, created, user_id=None): self.name = name self.email = email self.password = password self.user_id = uuid.uuid4().hex if user_id is None else user_id self.created = created data = { 'id': self.user_id, 'name': self.name, 'email': self.email, 'password': self.password, 'created_at': self.created, 'categories': list(), } if (self.email_exist(self.email)): return False Store().add_user(data) return True
def exist_id(self,id): users = Store().get_users() for user in users: if (user['id'] == id): return True return False
def email_exist(self,email): users = Store().get_users() for user in users: if (user['email'] == email): return True return False
from flask import render_template, session, redirect, flash, request, url_for, abort from functools import wraps from werkzeug.security import generate_password_hash from datetime import datetime from app import app from app.forms import RegisterForm, LoginForm, RecipeForm, CategoryForm from app.models.store import Store from app.models.user import User from app.models.category import Category from app.models.recipe import Recipe store = Store() """ Only logged in user Middleware function """ def auth(n): @wraps(n) def wrap(*args, **kwargs): if 'logged_in' in session and session[ 'logged_in'] == True and 'user_id' in session and session[ 'logged_in'] and User().exist_id(session['user_id']): return n(*args, **kwargs) else: session.clear() return redirect(url_for('login')) return wrap
def exist_twice(self,name): user_id = session['user_id'] recipes = Store().get_user_recipes(user_id) if (len(recipes) > 1): return True return False
def delete(self, category_id): if (Store().delete_category(category_id)): return True else: return False
def user_recipes(self): user_id = session['user_id'] recipes = Store().get_user_recipes(user_id) for recipe in recipes: recipe['category'] = Category().category_name(recipe['category_id']) return recipes
def delete(self, recipe_id): if (Store().delete_recipe(recipe_id)): return True else: return False
def user_categories(self): user_id = session['user_id'] categories = Store().get_user_categories(user_id) return categories
def category_name(self,id): user_id = session['user_id'] categories = Store().get_user_categories(user_id) for category in categories: if (category['id'] == id): return category['name']
def exist_twice(self,name): user_id = session['user_id'] categories = Store().get_user_categories(user_id) if (len(categories) > 1): return True return False
def exist_categories(self): user_id = session['user_id'] categories = Store().get_user_categories(user_id) if (len(categories) > 0): return True return False