コード例 #1
0
ファイル: views.py プロジェクト: thepythonic/kidzania
def process_form(request, form_definition, extra_context={}, disable_redirection=False):
    context = extra_context
    success_message = form_definition.success_message or _('Thank you, the data was submitted successfully.')
    error_message = form_definition.error_message or _('The data could not be submitted, please try again.')
    form_error = False
    form_success = False
    is_submit = False
    # If the form has been submitted...
    if request.method == 'POST' and request.POST.get(form_definition.submit_flag_name):
        form = DesignedForm(form_definition, None, request.POST, request.FILES)
        is_submit = True
    if request.method == 'GET' and request.GET.get(form_definition.submit_flag_name):
        form = DesignedForm(form_definition, None, request.GET)
        is_submit = True

    if is_submit:
        if form.is_valid():
            # Handle file uploads using storage object
            form_definition.action = request.path
            files = handle_uploaded_files(form_definition, form)
            # Successful submission
            messages.success(request, success_message)
            form_success = True
            if form_definition.log_data:
                form_definition.log(form, request.user)
            if form_definition.mail_to:
                form_definition.send_mail(form, files)
            if form_definition.success_redirect and not disable_redirection:
                return HttpResponseRedirect(form_definition.action or '?')
            if form_definition.success_clear:
                form = DesignedForm(form_definition)

                
        else:
            form_error = True
            #print request
            messages.error(request, error_message)
            form_definition.action = request.path
    else:
        if form_definition.allow_get_initial:
            form = DesignedForm(form_definition, initial_data=request.GET)
            
        else:
            form = DesignedForm(form_definition)
           

    context.update({
        'form_error': form_error,
        'form_success': form_success,
        'messages_success' :messages.success(request,success_message),
        'form': form,
        'form_definition': form_definition
    })
    context.update(csrf(request))
    
    if form_definition.display_logged:
        logs = form_definition.logs.all().order_by('created')
        context.update({'logs': logs})
        
    return context
コード例 #2
0
ファイル: views.py プロジェクト: taarifa/django-form-designer
def process_form(request,
                 form_definition,
                 extra_context={},
                 disable_redirection=False):
    context = extra_context
    success_message = form_definition.success_message or _(
        'Thank you, the data was submitted successfully.')
    error_message = form_definition.error_message or _(
        'The data could not be submitted, please try again.')
    form_error = False
    form_success = False
    is_submit = False
    # If the form has been submitted...
    if request.method == 'POST' and request.POST.get(
            form_definition.submit_flag_name):
        form = DesignedForm(form_definition, None, request.POST, request.FILES)
        is_submit = True
    if request.method == 'GET' and request.GET.get(
            form_definition.submit_flag_name):
        form = DesignedForm(form_definition, None, request.GET)
        is_submit = True

    if is_submit:
        if form.is_valid():
            # Handle file uploads using storage object
            files = handle_uploaded_files(form_definition, form)

            # Successful submission
            messages.success(request, success_message)
            form_success = True
            if form_definition.log_data:
                form_definition.log(form, request.user)
            if form_definition.mail_to:
                form_definition.send_mail(form, files)
            if form_definition.success_redirect and not disable_redirection:
                return HttpResponseRedirect(form_definition.action or '?')
            if form_definition.success_clear:
                form = DesignedForm(form_definition)  # clear form
        else:
            form_error = True
            messages.error(request, error_message)
    else:
        if form_definition.allow_get_initial:
            form = DesignedForm(form_definition, initial_data=request.GET)
        else:
            form = DesignedForm(form_definition)

    context.update({
        'form_error': form_error,
        'form_success': form_success,
        'form': form,
        'form_definition': form_definition
    })
    context.update(csrf(request))

    if form_definition.display_logged:
        logs = form_definition.logs.all().order_by('created')
        context.update({'logs': logs})

    return context
コード例 #3
0
def process_form(
    request, form_definition, extra_context=None, disable_redirection=False, push_messages=True,
    form_class=None
):
    if extra_context is None:
        extra_context = {}
    if form_class is None:
        form_class = get_designed_form_class()
    context = extra_context
    success_message = form_definition.success_message or _('Thank you, the data was submitted successfully.')
    error_message = form_definition.error_message or _('The data could not be submitted, please try again.')
    form_error = False
    form_success = False
    is_submit = False
    # If the form has been submitted...
    if request.method == 'POST' and request.POST.get(form_definition.submit_flag_name):
        form = form_class(form_definition, None, request.POST, request.FILES)
        is_submit = True
    if request.method == 'GET' and request.GET.get(form_definition.submit_flag_name):
        form = form_class(form_definition, None, request.GET)
        is_submit = True

    if is_submit:
        designedform_submit.send(sender=process_form, context=context,
                                 form_definition=form_definition, request=request)
        recaptcha_is_valid = check_recaptcha(request, context, push_messages)
        if form.is_valid() and recaptcha_is_valid:
            # Handle file uploads using storage object
            files = handle_uploaded_files(form_definition, form)

            # Successful submission
            if push_messages:
                messages.success(request, success_message)
            form_success = True

            designedform_success.send(sender=process_form, context=context,
                                      form_definition=form_definition, request=request)

            if form_definition.log_data:
                context['form_log'] = form_definition.log(form, request.user)
            if form_definition.mail_to:
                context['form_mail_message'] = form_definition.send_mail(form, files)
            if form_definition.success_redirect and not disable_redirection:
                return HttpResponseRedirect(form_definition.action or '?')
            if form_definition.success_clear:
                form = form_class(form_definition)  # clear form
        else:
            form_error = True
            designedform_error.send(sender=process_form, context=context,
                                    form_definition=form_definition, request=request)
            if push_messages:
                messages.error(request, error_message)
    else:
        if form_definition.allow_get_initial:
            form = form_class(form_definition, initial_data=request.GET)
        else:
            form = form_class(form_definition)
        designedform_render.send(sender=process_form, context=context,
                                 form_definition=form_definition, request=request)

    context.update({
        'form_error': form_error,
        'form_success': form_success,
        'form_success_message': success_message,
        'form_error_message': error_message,
        'form': form,
        'form_definition': form_definition
    })
    context.update(csrf(request))
    update_recaptcha_context(context)

    if form_definition.display_logged:
        logs = form_definition.logs.all().order_by('created')
        context.update({'logs': logs})

    return context
コード例 #4
0
def process_form(
    request, form_definition, extra_context=None, disable_redirection=False, push_messages=True,
    form_class=None
):
    if extra_context is None:
        extra_context = {}
    if form_class is None:
        form_class = get_designed_form_class()
    context = extra_context
    success_message = form_definition.success_message or _('Thank you, the data was submitted successfully.')
    error_message = form_definition.error_message or _('The data could not be submitted, please try again.')
    form_error = False
    form_success = False
    is_submit = False
    # If the form has been submitted...
    if request.method == 'POST' and request.POST.get(form_definition.submit_flag_name):
        form = form_class(form_definition, None, request.POST, request.FILES)
        is_submit = True
    if request.method == 'GET' and request.GET.get(form_definition.submit_flag_name):
        form = form_class(form_definition, None, request.GET)
        is_submit = True

    if is_submit:
        designedform_submit.send(sender=process_form, context=context,
                                 form_definition=form_definition, request=request)
        if form.is_valid():
            # Handle file uploads using storage object
            files = handle_uploaded_files(form_definition, form)

            # Successful submission
            if push_messages:
                messages.success(request, success_message)
            form_success = True

            designedform_success.send(sender=process_form, context=context,
                                      form_definition=form_definition, request=request)

            if form_definition.log_data:
                context['form_log'] = form_definition.log(form, request.user)
            if form_definition.mail_to:
                context['form_mail_message'] = form_definition.send_mail(form, files)
            if form_definition.success_redirect and not disable_redirection:
                return HttpResponseRedirect(form_definition.action or '?')
            if form_definition.success_clear:
                form = form_class(form_definition)  # clear form
        else:
            form_error = True
            designedform_error.send(sender=process_form, context=context,
                                    form_definition=form_definition, request=request)
            if push_messages:
                messages.error(request, error_message)
    else:
        if form_definition.allow_get_initial:
            form = form_class(form_definition, initial_data=request.GET)
        else:
            form = form_class(form_definition)
        designedform_render.send(sender=process_form, context=context,
                                 form_definition=form_definition, request=request)

    context.update({
        'form_error': form_error,
        'form_success': form_success,
        'form_success_message': success_message,
        'form_error_message': error_message,
        'form': form,
        'form_definition': form_definition
    })
    context.update(csrf(request))

    if form_definition.display_logged:
        logs = form_definition.logs.all().order_by('created')
        context.update({'logs': logs})

    return context