def recipe_add(request, weeklymenu_id, menu_date, menu_type, recipe_id): menu = get_object_or_404(WeeklyMenu, pk=weeklymenu_id, owner=request.user) recipe = get_object_or_404(Recipe, pk=recipe_id, owner=request.user) dt = datetime.datetime.strptime(menu_date, "%Y%m%d").date() mi = MenuItem(menu=menu, menu_date=dt, menu_type=menu_type, recipe=recipe, owner=request.user) mi.save() recipe.made_count += 1 recipe.save() next = request.GET.get('next') return redirect(next)
def ajax_add_to_menu(request): if request.is_ajax(): if request.method == 'POST': #Get POST variables recipe_id = request.POST.get('recipe_id') menu_id = request.POST.get('menu_id') menu_date = request.POST.get('menu_date') menu_type = request.POST.get('menu_type') #Get menus and recipes #If non-numeric "id", this is an ad-hoc recipe title to be added to DB if is_int(recipe_id): recipe = get_object_or_404(Recipe, pk=recipe_id, owner=request.user) else: log.debug("Recipe is not an id, creating new.") recipe = Recipe(title=recipe_id, source='ad-hoc', owner=request.user, url='') recipe.save() log.debug('Added new recipe %s' % recipe_id) menu = get_object_or_404(WeeklyMenu, pk=menu_id, owner=request.user) dt = datetime.datetime.strptime(menu_date, "%Y%m%d").date() mi = MenuItem(menu=menu, menu_date=dt, menu_type=menu_type, recipe=recipe, owner=request.user) mi.save() recipe.made_count += 1 recipe.save() response_data = { 'menuitem_id': mi.id, 'menu_id': menu_id, 'menu_date': menu_date, 'menu_type': menu_type, } return HttpResponse(json.dumps(response_data), content_type="application/json") else: return HttpResponseBadRequest("Bad request, must be POST") else: return HttpResponseBadRequest("Bad request, must be AJAX")
def menu_edit(request, weeklymenu_id, menu_date, menu_type): if request.method == 'POST': recipe_search_form = RecipeAjaxForm(request.POST) if recipe_search_form.is_valid(): menu = get_object_or_404(WeeklyMenu, pk=weeklymenu_id, owner=request.user) recipe = get_object_or_404(Recipe, title=recipe_search_form.cleaned_data['title'], owner=request.user) dt = datetime.datetime.strptime(menu_date, "%Y%m%d").date() mi = MenuItem(menu=menu, menu_date=dt, menu_type=menu_type, recipe=recipe, owner=request.user) mi.save() return redirect(request.path) else: recipe_search_form = RecipeAjaxForm() dt = datetime.datetime.strptime(menu_date, "%Y%m%d").date() current_recipes = MenuItem.objects.filter(menu=weeklymenu_id, menu_date=dt, menu_type=menu_type, owner=request.user) recent_recipes = Recipe.objects.filter(owner=request.user).order_by('-added')[:5] popular_recipes = Recipe.objects.filter(owner=request.user).order_by('-made_count')[:5] info_data = { 'date': dt, 'type': menumanager.models.type_mapping[int(menu_type)] } return render_to_response( 'menu_items_modal.html', { 'current_recipes': current_recipes, 'chunked_recipes': chunk_list(current_recipes), 'recent_recipes': recent_recipes, 'popular_recipes': popular_recipes, 'info_data': info_data, 'recipe_search_form': recipe_search_form, 'menu_id': weeklymenu_id, 'menu_date': menu_date, 'menu_type': menu_type, }, context_instance=RequestContext(request) )