class Recipe: def __init__(self): self.db = MyDB() def save(self, name, amount, base, comment, desired_strength, flavor, nicotine, pg, vg, sleep_time, vape_ready, wvpqa): nicotine_obj = json.loads(nicotine) flavor_obj = json.loads(flavor) flavor = flavor_obj nicotine_pg = nicotine_obj["pg"] nicotine_vg = nicotine_obj["vg"] nicotine_strength = nicotine_obj["strength"] # recipe goes in recipe table and flavors goes in flavors table sql = """INSERT INTO `recipe` (name, amount, desired_strength, pg, vg, nicotine_strength, nicotine_pg, nicotine_vg, wvpga, sleep_time, vape_ready, comment) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""" recipe_id = self.db.insert( sql, (name, amount, desired_strength, pg, vg, nicotine_strength, nicotine_pg, nicotine_vg, wvpqa, sleep_time, vape_ready, comment)) self.save_flavors(recipe_id) return recipe_id def save_flavors(self, recipe_id): for flavor in self.flavor: sql = """INSERT INTO `recipe_flavors` (recipe_id, name, amount, percentage, `type`, grams) VALUES (%s, %s, %s, %s, %s, %s)""" self.db.insert( sql, (recipe_id, flavor["name"], flavor["amount"], flavor["percentage"], flavor["type"], flavor["grams"])) # Load recipe by id from database def load(self): pass def get(self, recipe_id): sql = """SELECT * FROM recipe WHERE id = %s""" data = self.db.select_one(sql, recipe_id) print(data) if data: self.set(data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7], data[8], data[9], data[10], data[11]) return self.data() else: return False
class Recipes: def __init__(self): self.db = MyDB() def getAllRecipesByUserId(self, user_id): sql = """SELECT * FROM recipe WHERE user_id = %s""" data = self.db.select(sql, user_id) if data: recipes = list() for recipe in data: recipes.append({ "id": recipe[0], "name": recipe[1], "amount": recipe[2], "desired_strength": recipe[3], "pg": recipe[4], "vg": recipe[5], "nicotine_strength": recipe[6], "nicotine_pg": recipe[7], "nicotine_vg": recipe[8], "wvpga": recipe[9], "sleep_time": recipe[10], "vape_ready": recipe[11], "comment": recipe[12] }) return recipes else: return False
def __init__(self): self.db = MyDB() self.user = User self.userModel = UserModel self.recipe = Recipe
def __init__(self): self.db = MyDB()
def __init__(self): self.db = MyDB() self.user = User self.userModel = UserModel
class User: def __init__(self): self.db = MyDB() def get_user_roles(self, userId): sql = """SELECT * FROM roles r INNER JOIN user_roles ur ON ur.role_id = r.id WHERE user_id = %s""" data = self.db.select(sql, (userId)) if data: roles = list() for role in data: roles.append({"role_id": role[0], "role_name": role[1]}) return roles else: return False def get_active_user(self, args): sql = """SELECT * FROM users WHERE email = %s AND password = %s AND active = 1""" data = self.db.select_one(sql, (args['email'], args["password"])) if data: userData = { "user_id": data[0], "username": data[1], "email": data[3], "firstname": data[4], "lastname": data[5] } return userData else: return False # Check by username def is_username_taken(self, name): sql = """SELECT * FROM users WHERE username = %s""" data = self.db.select_one(sql, name) if data: return True else: return False def is_email_taken(self, email): sql = """SELECT * FROM users WHERE email = %s""" data = self.db.select_one(sql, email) if data: return True else: return False def create_user(self, username, password, email, firstname, lastname, newsletter): sql = """INSERT INTO `users` (`username`, `password`, `email`, `firstname`, `lastname`, `active`, `newsletter`) VALUES (%s, %s, %s, %s, %s, %s, %s)""" result = self.db.insert( sql, (username, password, email, firstname, lastname, 1, newsletter)) return result def add_user_role(self, user_id, role_id): sql = """INSERT INTO user_roles (user_id, role_id) VALUES (%s, %s);""" result = self.db.insert(sql, (user_id, role_id)) return result def update_user(self, name, age, occupation): user = {"name": name, "age": age, "occupation": occupation} sql = """UPDATE USERS SET name = %s, age = %s, occupation = %s WHERE name = %s""" self.db.query(sql, (name, age, occupation, name)) return user def deactivate_user(self, name): sql = """UPDATE USERS SET active = 0 WHERE name = %s""" self.db.query(sql, name) return True