def get_recipes(search_query): with recipe_db() as db: query = db.query(GET_RECIPES_QUERY) query.bind('$query', search_query or '') recipes = query.execute() return recipes.encode('utf8')
def get_menuplan_display(pk): with recipe_db() as db: q = db.query(GET_MENUPLAN_DISPLAY_QUERY) q.bind('$pk', pk) menuplans = q.execute() return menuplans.encode('utf8')
def get_random_recipes(number_of_recipes=7, rating='Good'): with recipe_db() as db: query = db.query(GET_RANDOM_RECIPES_QUERY) query.bind('$num', str(number_of_recipes)) query.bind('$rating', str(rating or '')) recipes = query.execute() return recipes.encode('utf-8')
def get_menuplans(query=None, offset=0, limit=500): with recipe_db() as db: q = db.query(GET_MENUPLANS_QUERY) q.bind('$offset', str(offset)) q.bind('$limit', str(limit)) q.bind('$query', str(query or '')) menuplans = q.execute() return menuplans.encode('utf8')
def get_recipe(pk): path = recipe_path(pk) with recipe_db() as db: query = db.query(GET_RECIPE_QUERY) query.bind('$path', path) recipe = query.execute() return recipe.encode('utf8')
def add_recipe(data): pk = str(uuid4()) recipe_xml = create_recipe_xml(pk, data['name'], data['people'], data['rating']) # Create a new document with recipe_db() as db: db.add(recipe_path(pk), et.tostring(recipe_xml)) return pk
def get_units(): with recipe_db() as db: query = db.query(GET_UNITS_QUERY) return query.execute().encode('utf8')
def detail(request, pk): with recipe_db() as db: recipe = et.fromstring(get_recipe(pk)) ingredient_options = get_ingredient_options() unit_options = get_unit_options() comment_options = get_comment_options() ingredients_data = [] for ing in recipe.findall('.//ingredient'): ingredients_data.append({ 'amount': ing.findtext('amount/value'), 'unit': ing.findtext('amount/unit'), 'name': ing.findtext('name'), 'comment': ing.findtext('comment'), }) instructions_data = [] for inst in recipe.findall('.//instruction'): instructions_data.append({ 'instruction': inst.findtext('text') }) form_kwargs = {'db': db} ingredients = IngredientFormSet(prefix='ingredient', initial=ingredients_data, form_kwargs=form_kwargs) instructions = InstructionFormSet(prefix='instruction', initial=instructions_data, form_kwargs=form_kwargs) if request.method == 'POST': form = RecipeDetailForm(request.POST) cp = request.POST.copy() if 'add_ingredient' in request.POST: cp['ingredient-TOTAL_FORMS'] = int(cp['ingredient-TOTAL_FORMS']) + 1 elif 'add_instruction' in request.POST: cp = request.POST.copy() cp['instruction-TOTAL_FORMS'] = int(cp['instruction-TOTAL_FORMS']) + 1 elif 'save' in request.POST: ingredients = IngredientFormSet(request.POST, prefix='ingredient', form_kwargs=form_kwargs) instructions = InstructionFormSet(request.POST, prefix='instruction', form_kwargs=form_kwargs) if form.is_valid() and ingredients.is_valid(): data = form.cleaned_data ingredient_data = [f.cleaned_data for f in ingredients.forms if f not in ingredients.deleted_forms] instruction_data = [f.cleaned_data for f in instructions.forms if f not in instructions.deleted_forms] with recipe_db() as db: db.replace(recipe_path(pk), et.tostring( create_recipe_xml( pk, data['name'], data['people'], data['rating'], ingredients=ingredient_data, instructions=instruction_data))) messages.add_message(request, messages.SUCCESS, 'Recipe %s saved' % data['name']) return redirect('recipes.detail', pk=pk) instructions = InstructionFormSet(cp, prefix='instruction', form_kwargs=form_kwargs) ingredients = IngredientFormSet(cp, prefix='ingredient', form_kwargs=form_kwargs) else: form = RecipeDetailForm({ 'name': recipe.findtext('name'), 'people': recipe.findtext('people'), 'rating': recipe.findtext('rating'), }) return render(request, 'recipes/detail.html', {'form': form, 'ingredients': ingredients, 'ingredients_helper': IngredientInlineHelper(), 'instructions': instructions, 'instructions_helper': InstructionInlineHelper(), 'pk': pk, 'ingredient_options': ingredient_options, 'unit_options': unit_options, 'comment_options': comment_options, })
def detail(request, pk): with recipe_db() as db: recipe = et.fromstring(get_recipe(pk)) ingredient_options = get_ingredient_options() unit_options = get_unit_options() comment_options = get_comment_options() ingredients_data = [] for ing in recipe.findall('.//ingredient'): ingredients_data.append({ 'amount': ing.findtext('amount/value'), 'unit': ing.findtext('amount/unit'), 'name': ing.findtext('name'), 'comment': ing.findtext('comment'), }) instructions_data = [] for inst in recipe.findall('.//instruction'): instructions_data.append({'instruction': inst.findtext('text')}) form_kwargs = {'db': db} ingredients = IngredientFormSet(prefix='ingredient', initial=ingredients_data, form_kwargs=form_kwargs) instructions = InstructionFormSet(prefix='instruction', initial=instructions_data, form_kwargs=form_kwargs) if request.method == 'POST': form = RecipeDetailForm(request.POST) cp = request.POST.copy() if 'add_ingredient' in request.POST: cp['ingredient-TOTAL_FORMS'] = int( cp['ingredient-TOTAL_FORMS']) + 1 elif 'add_instruction' in request.POST: cp = request.POST.copy() cp['instruction-TOTAL_FORMS'] = int( cp['instruction-TOTAL_FORMS']) + 1 elif 'save' in request.POST: ingredients = IngredientFormSet(request.POST, prefix='ingredient', form_kwargs=form_kwargs) instructions = InstructionFormSet(request.POST, prefix='instruction', form_kwargs=form_kwargs) if form.is_valid() and ingredients.is_valid(): data = form.cleaned_data ingredient_data = [ f.cleaned_data for f in ingredients.forms if f not in ingredients.deleted_forms ] instruction_data = [ f.cleaned_data for f in instructions.forms if f not in instructions.deleted_forms ] with recipe_db() as db: db.replace( recipe_path(pk), et.tostring( create_recipe_xml( pk, data['name'], data['people'], data['rating'], ingredients=ingredient_data, instructions=instruction_data))) messages.add_message(request, messages.SUCCESS, 'Recipe %s saved' % data['name']) return redirect('recipes.detail', pk=pk) instructions = InstructionFormSet(cp, prefix='instruction', form_kwargs=form_kwargs) ingredients = IngredientFormSet(cp, prefix='ingredient', form_kwargs=form_kwargs) else: form = RecipeDetailForm({ 'name': recipe.findtext('name'), 'people': recipe.findtext('people'), 'rating': recipe.findtext('rating'), }) return render( request, 'recipes/detail.html', { 'form': form, 'ingredients': ingredients, 'ingredients_helper': IngredientInlineHelper(), 'instructions': instructions, 'instructions_helper': InstructionInlineHelper(), 'pk': pk, 'ingredient_options': ingredient_options, 'unit_options': unit_options, 'comment_options': comment_options, })
def get_ingredients(): with recipe_db() as db: query = db.query(GET_INGREDIENTS_QUERY) return query.execute().encode('utf8')
def get_comments(): with recipe_db() as db: query = db.query(GET_COMMENTS_QUERY) return query.execute().encode('utf8')
def add_menuplan(pk, menuplan): with recipe_db() as db: db.add(menuplan_path(pk), menuplan)