def view_recipe(request): render_dict = {} ''' This page is built around viewing a single retreived recipe. So if there is no request for one something has gone wrong. ''' if not 'recipe' in request.GET: #redirect to the recipes page return redirect(view_recipes) #get the restaurant rest = InventoryUtils.get_rest(request) #add the restaurant to the render dictionary render_dict["restaurant_name"]= rest try: #get the recipe being queried recipe = Recipe.objects.get(id = request.GET['recipe']) except ObjectDoesNotExist: return redirect(view_recipes) #get all of the ingredients ingredients = list(RecipeIngredient.objects.filter(recipe=recipe)) render_dict["ingredients"] = ingredients #add the recipe to the render dict render_dict['recipe'] = recipe return render_to_response("view_recipe.html", render_dict, context_instance=RequestContext(request))
def view_recipes(request): render_dict = {} #get the restaurant rest = InventoryUtils.get_rest(request) #add the restaurant to the render dictionary render_dict["restaurant_name"]= rest if request.method == "POST": #the only form on the page is the delete form, delete the requested recipes utils.delete_recipe(rest,request.POST) #make the table of recipes table = RecipeTable(Recipe.objects.filter(restaurant = rest.id)) RequestConfig(request).configure(table) render_dict['table'] = table #add the csrf form for deleting recipes render_dict.update(csrf(request)) return render_to_response("view_recipes.html", render_dict, context_instance=RequestContext(request))
def print_view(request): render_dict = {} #get the rest rest = utils.get_rest(request) try: all_ings = Ingredient.objects.filter(restaurant = rest) except ObjectDoesNotExist: all_ings =[] render_dict['ingredients'] = all_ings return render_to_response('print_ingredients.html', render_dict, context_instance=RequestContext(request))
def serve_recipe(request): render_dict = {} #get the restaurant rest = InventoryUtils.get_rest(request) # create the formset and add the form ServiceFormSet = modelformset_factory(RecipeService, extra=3, exclude =("cost_at_serve", "recipe_cost_at_serve")) formset = ServiceFormSet(queryset=RecipeService.objects.none()) #add the queryset so that users can only see their own recipes valid_recipes = Recipe.objects.filter(restaurant = rest) for form in formset: form.fields['recipe'].queryset = valid_recipes if request.method == "POST": formset = ServiceFormSet(request.POST) #if check for validity if formset.is_valid(): service_list = formset.save(commit=False) #add the cost and then save for service in service_list: #add a record to the db service.recipe_cost_at_serve = service.recipe.cost service.cost_at_serve = service.recipe.cost * Decimal(service.number) service.save() #remove the correct quanitity from the inventory InventoryUtils.serve_ingredients(service.recipe, service.number) #redirect to prevent resubmission return redirect(serve_recipe) #add the formset render_dict['formset'] = formset #add the csrf token render_dict.update(csrf(request)) return render_to_response("serve_recipe.html", render_dict, context_instance=RequestContext(request))
def ingredient(request): render_dict = {} #get the restaurant rest = utils.get_rest(request) #add the restaurant to the render dictionary render_dict["restaurant_name"]= rest #create the form early so it can be replaced if a form #comes back bad form = IngredientForm() #Deal with the ingredient add form if request.method == 'POST': # If the form has been submitted... #if the quickadd form was submitted if "quickadd" in request.POST: form=IngredientForm(request.POST) if form.is_valid(): utils.add_ingredient(rest, form) #return to page after redirect return redirect(ingredient_redirect) #if the delete form was submitted elif "delete" in request.POST: utils.delete_ingredients(rest, request.POST) #create the table of ingredients ing_table = IngredientTable(Ingredient.objects.filter(restaurant = rest.id)) RequestConfig(request, paginate={"per_page": 10}).configure(ing_table) render_dict["ing_table"] = ing_table #add the form to the render dictionary render_dict["form"]=form render_dict.update(csrf(request)) return render_to_response("ingredient.html", render_dict, context_instance=RequestContext(request))
def view_service(request): render_dict={} #get the restaurant rest = InventoryUtils.get_rest(request) render_dict['restaurant'] = rest #get the services of all recipes belonging to the current user try: service_list = RecipeService.objects.filter(recipe__restaurant__pk=rest.id) except ObjectDoesNotExist: service_list = [] #make the table of services table = ServiceTable(service_list) RequestConfig(request).configure(table) render_dict['table'] = table return render_to_response("view_service.html", render_dict, context_instance=RequestContext(request))
def add_ingredients(request): render_dict = {} #get the restaurant rest = utils.get_rest(request) #add the restaurant to the render dictionary render_dict["restaurant_name"]= rest #create a formset, no restaurant because we should set that based #on the current login, also you dont want users touching other users' #restaurants IngredientFormset = modelformset_factory(Ingredient, exclude=('restaurant','total_value'), extra=5) #this page should be for adding ingredients only formset = IngredientFormset(queryset=Ingredient.objects.none()) if request.method == 'POST': formset = IngredientFormset(request.POST) if formset.is_valid(): for form in formset: utils.add_ingredient(rest, form) #if everything is valid we are done and should redirect back to #the viewing page return redirect(ingredient) render_dict['formset'] = formset render_dict.update(csrf(request)) #create a formset for ingredients return render_to_response('add_ingredients.html', render_dict, context_instance=RequestContext(request))
def add_recipe(request): render_dict = {} #get the restaurant rest = InventoryUtils.get_rest(request) #add the restaurant to the render dictionary render_dict["restaurant_name"]= rest #create the form recipeform = RecipeForm() RecipeIngredientFormset = inlineformset_factory(Recipe, RecipeIngredient) ing_formset = RecipeIngredientFormset() #if we receive information about a recipe to load if request.method == 'GET' and 'recipe' in request.GET: #get the recipe being requested recipe_key = request.GET['recipe'] recipe = Recipe.objects.get(id = recipe_key) #load the recipe into the form recipeform = RecipeForm(instance = recipe) ing_formset = RecipeIngredientFormset(instance = recipe); # If the form has been submitted if request.method == 'POST': #if we have a recipe id we are editing an old model if 'recipe' in request.GET: recipe = Recipe.objects.get(id = request.GET['recipe']) print(recipe) #parse the recipe form recipeform = RecipeForm(request.POST, instance = recipe) #otherwise we are adding a new model else: recipeform = RecipeForm(request.POST) if recipeform.is_valid(): #add the resaurant then temporarily save recipe = recipeform.save(commit = False) recipe.restaurant = rest recipe = recipeform.save() #create the formset with the recipe ing_formset = RecipeIngredientFormset(request.POST, instance = recipe) #if the formset is valid and therefore both are #valid if ing_formset.is_valid(): #save the ingredients first so the recipe can calculate its cost ing_formset.save() #save the recipe to the database recipe.save() #redirect to the version of this page but without the post data #this prevents the user from accidentally refreshing #and submitting the form twice return redirect(add_recipe) #before the form is added to the render dict change the query set so the user #sees only their own ingredients user_ingredients = Ingredient.objects.filter(restaurant = rest) for form in ing_formset: form.fields['ingredient'].queryset=user_ingredients #add forms and csrf to dict render_dict['recipeform'] = recipeform render_dict['ingredientform'] = ing_formset render_dict.update(csrf(request)) return render_to_response("add_recipe.html", render_dict, context_instance=RequestContext(request))