def modifyDish(key, title, subtitle, description, dishCategoryDb, eggFree, milkFree, codeModifier): dishDb = Dish.get(key) client = memcache.Client() dishDb.title = title dishDb.subtitle = subtitle dishDb.description = description dishDb.category = dishCategoryDb dishDb.eggFree = eggFree dishDb.milkFree = milkFree dishDb.codeModifier = codeModifier # Update dishes old category dishObject = getDish(key) categoryKey = None if dishDb.category != None: categoryKey = str(dishDb.category.key()) if dishObject != None and dishObject["category"]!= None and dishObject["category"]['key'] != categoryKey: # Update category objects in cacge removeDishFromCategory(dishObject["category"]['key'], key) # Create object newDishObject = createDishObjectDb(dishDb) dishDb.price = int(newDishObject['price']) if ( dishObject == None or dishObject["category"] != categoryKey ) and categoryKey != None: addDishToCategory(categoryKey, key) client.set(key, newDishObject) dishDb.put()
def get_dish_by_id(self, dish_id): self.cursor.execute('SELECT json_dish FROM Dishes WHERE id = :dish_id', {'dish_id': dish_id}) json_dish = self.cursor.fetchone() if json_dish is not None: return Dish(json.loads(json_dish[0])) return None
def getDish(key): if key == None: return None client = memcache.Client() dish = client.get(key) if dish == None: dishDb = Dish.get(key) if dishDb != None: dish = createDishObjectDb(dishDb) client.set(dish['key'], dish) return dish
def get(self): if not isUserAdmin(self): self.session[LOGIN_NEXT_PAGE_KEY] = self.URL self.redirect("/") return dishes = Dish.all() template_values = { 'dishes' : dishes } template = jinja_environment.get_template('templates/csv/dishList.download') self.response.out.write(template.render(template_values))
def getDishes(): client = memcache.Client() dishKeys = client.get(ALL_DISHES) if dishKeys == None: dishKeys=[] dishes = Dish.gql("ORDER BY title") if dishes != None: for dish in dishes: dishKeys.append(str(dish.key())) client.set(ALL_DISHES, dishKeys) else: return None # Fetch dishes dishes = [] for dishKey in dishKeys: dishes.append(getDish(dishKey)) return dishes
def get(self): if not isUserCook(self): self.session[LOGIN_NEXT_PAGE_KEY] = self.URL self.redirect("/") return dishKey=self.request.get('dishKey') if ((dishKey != None) and (dishKey != "")): # A single dish with editable ingredient list dish=getDish(dishKey) #Check if category exists ingredients = dish['ingredients'] dish['energy'] = 0 for ingredient in ingredients: dish['energy'] = dish['energy'] + ingredient['quantity'] * ingredient['energy'] / 100.0 gotIngredients = getIngredients() availableIngredients = sorted(gotIngredients, key=lambda ingredient:ingredient['name']) gotCategories = getDishCategories() availableCategories = sorted(gotCategories, key=lambda category:category['name']) template_values = { 'dish': dish, 'availableCategories':availableCategories, 'availableIngredients':availableIngredients, 'add_url':"/addIngredientToDish", 'delete_url':"/deleteIngredientFromDish" } template = jinja_environment.get_template('templates/dish.html') self.printPage(dish['title'], template.render(template_values), False, False) else: # All the dishes unprocessedDishes = Dish.gql("ORDER BY title") dishes = [] for dish in unprocessedDishes: try: dish.category except ReferencePropertyResolveError: dish.category = None dishes.append(dish) availableCategories = DishCategory.gql("WHERE isMenu = False ORDER BY index") template_values = { 'dishes': dishes, 'availableCategories': availableCategories } template = jinja_environment.get_template('templates/dish_list.html') self.printPage("Receptek", template.render(template_values), False, False)
def recipe_results(): global results query = request.args['query'] labels = [ label for label in ALL_LABELS if request.args.get(label) is not None ] health_labels = list(filter(lambda l: l in HEALTH_LABELS, labels)) diet_labels = list(filter(lambda l: l in DIET_LABELS, labels)) api_request = build_request(query, health_labels, diet_labels) if user is not None: if query in [entry.query for entry in user.history]: dao.delete_entry(user, query) dao.add_history_entry( user, HistoryEntry(query, datetime.now().strftime('%Y-%m-%d %H:%M:%S'))) response = requests.get(api_request) results = [Dish(hit['recipe']) for hit in response.json()['hits'] ] if response.status_code == 200 else [] return render_template('recipes.html', labels=ALL_LABELS, recipes=results, profile=user)
def get_dishes_by_user(self, user): self.cursor.execute( 'SELECT json_dish FROM Dishes WHERE user_id = :user_id', {'user_id': user.id}) json_dishes = self.cursor.fetchall() return [Dish(json.loads(json_dish[0])) for json_dish in json_dishes]
def post(self): if not isUserCook(self): self.session[LOGIN_NEXT_PAGE_KEY] = self.URL self.redirect("/") return dishKey=self.request.get('dishKey') if ((dishKey != None) and (dishKey != "")): #Modification of basic data title = self.request.get('title') subtitle = self.request.get('subtitle') description = self.request.get('description') eggFree = (self.request.get('eggFree') == "yes") milkFree = (self.request.get('milkFree') == "yes") dishCategoryKey=self.request.get(DISH_CATEGORY_KEY) codeModifier=self.request.get(DISH_CODE_MODIFIER) dishCategory=None if ((dishCategoryKey != None) and (dishCategoryKey != "")): dishCategory = db.get(dishCategoryKey) modifyDish(dishKey, title, subtitle, description, dishCategory, eggFree, milkFree, codeModifier) self.redirect('/dish?dishKey=%s' % self.request.get('dishKey')) return else: dish = Dish() dish.creationDate = datetime.datetime.today().date() dish.title = self.request.get('title') dish.subtitle = self.request.get('subtitle') dish.description = self.request.get('description') dish.category = DishCategory.get(self.request.get('dishCategoryKey')) eggFree = (self.request.get('eggFree') == "yes") milkFree = (self.request.get('milkFree') == "yes") dish.milkFree = milkFree dish.eggFree = eggFree dish.put() addDish(dish) self.redirect('/dish?dishKey=%s' % str(dish.key()))