Exemplo n.º 1
0
def script_new(request):
    ParamFormSet = formset_factory(ParamForm, can_delete = True )
    if request.method == 'POST':
        script_form = ScriptForm( request.POST )
        param_formset = ParamFormSet( data = request.POST, prefix='param')
        
        if script_form.is_valid() and param_formset.is_valid():
            script = script_form.save()
            for parameter_data in param_formset.cleaned_data:
                if parameter_data:
                    parameter = Parameter.objects.create( **parameter_data )
                    script.parameters.add( parameter )
            script.save()
            
            info_msg(request, 'Script added successfully')
            
            if 'save_and_add' in request.POST.keys():
                return redirect('script_new')
            else:            
                return redirect('script_list')
    else:
        script_form = ScriptForm()
        param_formset = ParamFormSet( prefix='param' )
    
    return render(request, 'script_new.html', {
        'form'   : script_form,
        'formset': param_formset,
    })
Exemplo n.º 2
0
def script_edit(request, script_id):
    script = get_object_or_404(Script, id = script_id)
    
    ParamFormSet = formset_factory(ParamForm)
    if request.method == 'POST':
        script_form = ScriptForm( data = request.POST, initial = script )
        param_formset = ParamFormset( request.POST, prefix='param' )
        if script_form.is_valid() and param_formset.is_valid():
            script = script_form.save()
            
            return redirect('script_list')
    else:
        form = ScriptForm(initial = script)
        param_formset = ParamFormSet( prefix='param' )
    
    return render(request, 'script_new.html', {
        'form'   : form,
        'formset': param_formset,
    })