def deleteShopList(): """ It deletes the shopping list by name """ name = request.args.get("lname") date_created = request.args.get("date") all_names = '' if 'User' in session: if name is not None: ShoppingList.deleteShoppingList(name, session['User'], date_created) else: ShoppingList.deleteShoppingListByDate(session['User'], date_created) all_names = ShoppingList.getShoppingListNames(session['User']) return redirect ('/getShoppingLists') else: flash = [] flash = "You need to login" return render_template("/error.html",url="homepage.html")
def add_missing_ingredients(): """ Displays shopping list with missing ingredients.""" new_recipes_to_add = db.session.query(UserRecipe.recipe_id).filter( UserRecipe.user_id == session['user_id'], UserRecipe.status == 'needs_missing_ingredients').all() new_recipe_list = [] for recipe in new_recipes_to_add: new_recipe_list.append(recipe[0]) new_shopping_list = ShoppingList( user_id=session['user_id'], has_shopped=False, ) db.session.add(new_shopping_list) current_user = User.query.get(session['user_id']) results_recipes = current_user.get_used_and_missing_ingredients( new_recipe_list) for recipe in results_recipes: for missing_ingredient in results_recipes[recipe]['missing_ing']: ingredient = Ingredient.query.filter( Ingredient.ingredient_id == missing_ingredient[0]).first() if not ingredient: new_missing_ingredient = Ingredient( ingredient_id=missing_ingredient[0], ingredient_name=missing_ingredient[3], base_unit=missing_ingredient[2], ingredient_aisle=missing_ingredient[4], ) db.session.add(new_missing_ingredient) new_list_ingredient = ListIngredient( shopping_list_id=new_shopping_list.list_id, ingredient_id=missing_ingredient[0], aggregate_quantity=missing_ingredient[1], ) db.session.add(new_list_ingredient) # Update status of recipes added to shopping list to 'in progress' update_recipes = UserRecipe.query.filter( UserRecipe.user_id == session['user_id'], UserRecipe.status == 'needs_missing_ingredients').all() for recipe in update_recipes: recipe.status = 'in_progress' db.session.commit() user_ingredients = new_shopping_list.get_ingredients() return render_template("shopping.html", ingredients=user_ingredients)
def show_shopping_list(): """ Creates shopping list of missing ingredients with aggregated quantities and base units.""" all_user_recipes = db.session.query(UserRecipe.recipe_id).filter( UserRecipe.user_id == session['user_id'], UserRecipe.status == 'needs_ingredients').all() new_shopping_list = ShoppingList( user_id=session['user_id'], has_shopped=False, ) db.session.add(new_shopping_list) aggregated_ingredients = aggregate_ingredients(all_user_recipes) for ingredient_id in aggregated_ingredients: ingredient = db.session.query(Ingredient).filter( Ingredient.ingredient_id == ingredient_id).first() if not ingredient: new_ingredient = Ingredient( ingredient_id=ingredient_id, ingredient_name=aggregated_ingredients[ingredient_id]['name'], base_unit=aggregated_ingredients[ingredient_id]['unit'], ingredient_aisle=aggregated_ingredients[ingredient_id] ['aisle'], ) db.session.add(new_ingredient) new_list_ingredient = ListIngredient( shopping_list_id=new_shopping_list.list_id, ingredient_id=ingredient_id, aggregate_quantity=aggregated_ingredients[ingredient_id] ['quantity'], ) db.session.add(new_list_ingredient) # Update status of recipes added to shopping list to 'in progress' update_recipes = UserRecipe.query.filter( UserRecipe.user_id == session['user_id'], UserRecipe.status == 'needs_ingredients').all() for recipe in update_recipes: recipe.status = 'in_progress' db.session.commit() user_ingredients = new_shopping_list.get_ingredients() return render_template("shopping.html", ingredients=user_ingredients)
def getAllShopListIngredients(user): """ It gets all the ingredients in the shopping lists saved """ ingredients = ShoppingList.getAllIngredientsInShopList(user) graphData = makeJson(ingredients) return graphData
def checkOrUncheck(check): my_view = View() my_database = ShoppingList() user_input = my_view.takeInput("Enter an ID of an ite in your list: ") try: user_input = int(user_input) except ValueError: return "Not a number. Try again" found_result = my_database.checkID(user_input) if len(found_result) == 0: return "ID not found" else: my_database.updateChecked(check, user_input) return check
def getShoppingList(name=None): """ It searches a shopping list by name or finds the latest saved""" shop_list = '' all_names = '' i_list = {} date_created = '' if 'User' in session: if name: shop_list = ShoppingList.getShoppingListByName(name, session['User']) else: shop_list = ShoppingList.getLatestShoppingList(session['User']) if shop_list: name = shop_list[0].list_name if len(shop_list) > 0: date_created = shop_list[0].date_created all_names = ShoppingList.getShoppingListNames(session['User']) i_list = helpFunctions.makeShoppingList(shop_list) return render_template("view_shopping_list.html", list=i_list, names=all_names, date_created=date_created, name=name ) else: flash = [] flash = "You need to login" return render_template("/error.html", url="/getShoppingLists")
def saveShoppingList(): """ It saves the shopping list with the ingredients for the recipes in the planner """ list_name = request.form.get('name') ingredients = request.form.get('ingredients') ingr_aisles = request.form.get('list') list_ingredients = ingredients.split(",") # Saves each ingredient in the shop_lists table for ingr in list_ingredients: ShoppingList.addItem(ingr, session['User'], list_name) db.session.commit return redirect("/getShoppingLists/"+list_name)
def _display_list(self, list_id): """ Displays the list with the given id if it belongs to the current user """ try: current_user = User.getUser(self.user_email) current_list = ShoppingList.get_by_id(int(list_id), current_user) if current_list is None: raise ValueError self._api_display_list_(current_list) except (TypeError, ValueError, ProtocolBufferEncodeError) as exc: logging.error(str(exc)) error_message = self.gettext("There's not such list, sorry.") self.set_error(constants.STATUS_BAD_REQUEST, message=error_message, url="/")
def post(self, api=None, list_id=None): """ POST request handler """ current_user = User.getUser(self.user_email) if api is not None: try: if list_id is None: list_name = self.request.get('name', None) new_list = ShoppingList.create_list(current_user, list_name) self._api_display_list_(new_list) except (ValueError) as exc: self.set_error(constants.STATUS_BAD_REQUEST) if list_id is not None: # Add item to list try: current_list = ShoppingList.get_by_id(int(list_id), current_user) if current_list is None: raise ValueError item = current_list.add_item(self.request.get('description', None), self.request.get('key', None), int(self.request.get('quantity', 1))) self.response.out.write(json.dumps(item.to_dict())) self.response.headers['Content-Type'] = 'application/json' self.ok('/Lists/'+str(list_id)) except (TypeError, ValueError, BadKeyError, BadValueError, ProtocolBufferEncodeError) as exc: logging.error('Exception: ' + str(exc)) error_message = self.gettext("There's not such list, sorry.") self.set_error(constants.STATUS_BAD_REQUEST, message=error_message, url="/")
def _list_lists(self, api): """ Lists all shopping lists of current user """ current_user = User.getUser(self.user_email) query = ShoppingList.all() query.ancestor(current_user) if api is not None: all_lists = query.run() self._api_list_lists_(all_lists) else: template = self.jinja2_env.get_template('ang-base.html') template_values = { 'user_email': self.user_email } self.response.out.write(template.render(template_values))
def put(self, api, list_id): """ PUT method is used to change item rankings """ try: current_user = User.getUser(self.user_email) current_list = ShoppingList.get_by_id(int(list_id), current_user) items_json = self.request.get('items') self.response.out.write('Items json: ' + str(items_json)+ '\n') items_check = json.loads(items_json) current_list.items = items_json current_list.put() self.response.out.write('Ran with success') except (BadValueError, AttributeError) as exc: logging.error('Exception: ' + str(exc)) error_message = self.gettext('Error while storing new order.') self.set_error(constants.STATUS_BAD_REQUEST, message=error_message, url="/")
def delete(self, api=None, list_id=None, item_id=None): """ DELETE request handler """ if api is not None: try: current_user = User.getUser(self.user_email) current_list = ShoppingList.get_by_id(int(list_id), current_user) current_list.delete_item(item_id) except (TypeError, ValueError, BadKeyError, BadValueError, ProtocolBufferEncodeError) as exc: logging.error('Exception: ' + str(exc)) error_message = self.gettext("There's no such item, sorry.") self.set_error(constants.STATUS_BAD_REQUEST, message=error_message, url="/")
def create_shopping_list(name): """ It creates a shopping list with the ingredients for the recipes in the meal planner and returns it in a dictionary : {'aisle':[ingr1, ingr2,...],...} """ print name i_ingr = [] if 'User' in session: list_ingr = ShoppingList.getListIngrName(session['User']) dict_ingr = helpFunctions.makeShoppingListNoQty(list_ingr) return render_template("grocery_list.html", list=dict_ingr, name=name)
def sendShoppingList(): """ It sends the shopping list by SMS with Twilio API """ from twilio.rest import TwilioRestClient client = TwilioRestClient() name = request.form.get('listIng') # It finds the shopping list by name and creates a dictionary of ingredients # and aisle if 'User' in session: shop_list = ShoppingList.getShoppingListByName(name, session['User']) i_list = helpFunctions.makeShoppingList(shop_list) else: flash = [] flash = "You need to login" return render_template("/error.html", url="homepage.html") message = '' # It creates a string message with all the ingredients and aisles for aisle in i_list: message += '\n'+aisle.upper() + ':\n' message += '\n'.join(i_list[aisle]) message = client.messages.create(to="+14156018298", from_="+16506810994", body=message) return redirect("/getShoppingLists")
user_input = int(user_input) except ValueError: return "Not a number. Try again" found_result = my_database.checkID(user_input) if len(found_result) == 0: return "ID not found" else: my_database.updateChecked(check, user_input) return check my_view = View() my_database = ShoppingList() running = True while running == True: user_input = my_view.menu() if user_input == "1": item = my_view.takeInput("Enter an item to add to the list: ") my_database.insertRow(item) elif user_input == "2": rows = my_database.selectTable() my_view.printList(rows)