コード例 #1
0
ファイル: views.py プロジェクト: ASpringut/blade
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))
コード例 #2
0
ファイル: views.py プロジェクト: ASpringut/blade
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))