示例#1
0
def test_custom_message_template(greeting_form):
    """
    Test that custom message templates work as expected.
    """
    greeting_form.message_template = '{{ greeting }}, friend!'
    form = DesignedForm(greeting_form, data={
        'greeting': 'Hello',
    })
    assert form.is_valid()
    greeting_form.log(form)
    mail = greeting_form.send_mail(form)
    assert mail.body == 'Hello, friend!'
def test_custom_message_template(greeting_form):
    """
    Test that custom message templates work as expected.
    """
    greeting_form.message_template = '{{ greeting }}, friend!'
    form = DesignedForm(greeting_form, data={
        'greeting': 'Hello',
    })
    assert form.is_valid()
    greeting_form.log(form)
    mail = greeting_form.send_mail(form)
    assert mail.body == 'Hello, friend!'
示例#3
0
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
示例#4
0
def process_form(request, form_definition, context={}, is_cms_plugin=False):
    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.')
    message = None
    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)
        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():
            # Successful submission
            messages.success(request, success_message)
            message = success_message
            form_success = True
            if form_definition.log_data:
                form_definition.log(form)
            if form_definition.mail_to:
                form_definition.send_mail(form)
            if form_definition.success_redirect and not is_cms_plugin:
                # TODO Redirection does not work for cms plugin
                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)
            message = error_message
    else:
        if form_definition.allow_get_initial:
            form = DesignedForm(form_definition, initial_data=request.GET)
        else:
            form = DesignedForm(form_definition)

    context.update({
        'message': message,
        'form_error': form_error,
        'form_success': form_success,
        'form': form,
        'form_definition': form_definition
    })
    context.update(csrf(request))
    return context
示例#5
0
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:
        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
            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:
                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
            designedform_error.send(sender=process_form,
                                    context=context,
                                    form_definition=form_definition,
                                    request=request)
            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)
        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
示例#6
0
def process_form(request, form_definition, context={}, is_cms_plugin=False):
    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.')
    message = None
    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
            files = []
            if hasattr(request, 'FILES'):
                for file_key in request.FILES:
                    file_obj = request.FILES[file_key]
                    file_name = '%s.%s_%s' % (
                        datetime.now().strftime('%Y%m%d'),
                        random.randrange(0, 10000),
                        file_obj.name,
                    )

                    if not os.path.exists(os.path.join(settings.MEDIA_ROOT, 'contact_form')):
                        os.mkdir(os.path.join(settings.MEDIA_ROOT, 'contact_form'))

                    destination = open(os.path.join(settings.MEDIA_ROOT, 'contact_form', file_name), 'wb+')
                    for chunk in file_obj.chunks():
                        destination.write(chunk)
                    destination.close()

                    form.cleaned_data[file_key] = os.path.join(settings.MEDIA_URL, 'contact_form', file_name)
                    files.append(os.path.join(settings.MEDIA_ROOT, 'contact_form', file_name))

            # Successful submission
            messages.success(request, success_message)
            message = success_message
            form_success = True
            if form_definition.log_data:
                form_definition.log(form)
            if form_definition.mail_to:
                form_definition.send_mail(form, files)
            if form_definition.success_redirect and not is_cms_plugin:
                # TODO Redirection does not work for cms plugin
                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)
            message = error_message
    else:
        if form_definition.allow_get_initial:
            form = DesignedForm(form_definition, initial_data=request.GET)
        else:
            form = DesignedForm(form_definition)

    context.update({
        'message': message,
        'form_error': form_error,
        'form_success': form_success,
        'form': form,
        'form_definition': form_definition
    })
    context.update(csrf(request))
    return context
示例#7
0
def process_form(request, form_definition, context={}, is_cms_plugin=False):
    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.')
    initial_message = form_definition.initial_message or ''
    message = initial_message

    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, request.FILES)
        is_submit = True

    if is_submit:
        if form.is_valid():
            # Handle file uploads
            files = []
            if hasattr(request, 'FILES'):
                for file_key in request.FILES:
                    file_obj = request.FILES[file_key]

                    # Check if its a valid filename, if not, skip this file
                    is_valid_file = _is_valid_file(file_obj)
                    if not is_valid_file:
                        continue

                    file_name = '%s.%s_%s' % (
                        datetime.now().strftime('%Y%m%d'),
                        random.randrange(0, 10000),
                        file_obj.name,
                    )

                    file_name = force_unicode(file_name).encode('ascii', 'ignore')

                    if not os.path.exists(os.path.join(settings.MEDIA_ROOT, 'form_uploads')):
                        os.mkdir(os.path.join(settings.MEDIA_ROOT, 'form_uploads'))
                        logger.debug('Created form uploads directory: %s ' %
                            os.path.join(settings.MEDIA_ROOT, 'form_uploads'))
                    destination = open(os.path.join(settings.MEDIA_ROOT, 'form_uploads', file_name), 'wb+')
                    logger.debug('File upload disk destination: %s ' % destination)
                    for chunk in file_obj.chunks():
                        destination.write(chunk)
                    destination.close()
                    form.cleaned_data[file_key] = os.path.join(settings.MEDIA_URL, 'form_uploads', file_name)
                    files.append(os.path.join(settings.MEDIA_ROOT, 'form_uploads', file_name))

            logger.debug('Files to attach: %s' % files)

            # Successful submission
            messages.success(request, success_message)
            message = success_message
            form_success = True
            if form_definition.log_data:
                form_definition.log(form)
            if form_definition.mail_to:
                form_definition.send_mail(form, files)
            if form_definition.inform_sender:
                for name, field in form.fields.items():
                    if isinstance(field, EmailField):
                        mail_to = form.cleaned_data.get(name)
                        if mail_to:
                            form_definition.send_inform_mail(mail_to, form)
                            break
            if form_definition.success_redirect and not is_cms_plugin:
                # TODO Redirection does not work for cms plugin
                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)
            message = error_message
    else:
        if form_definition.allow_get_initial:
            form = DesignedForm(form_definition, initial_data=request.GET)
        else:
            form = DesignedForm(form_definition)

    context.update({
        'message': message,
        'form_error': form_error,
        'form_success': form_success,
        'form': form,
        'form_definition': form_definition
    })
    context.update(csrf(request))
    return context
示例#8
0
def process_form(request, form_definition, context={}, is_cms_plugin=False):
    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.')
    message = None
    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)
        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():
            # Successful submission
            messages.success(request, success_message)
            message = success_message
            form_success = True
            if form_definition.log_data:
                form_definition.log(form)
            if form_definition.mail_to:
                form_definition.send_mail(form)
            if form_definition.success_redirect and not is_cms_plugin:
                # TODO Redirection does not work for cms plugin
                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)
            message = error_message
    else:
        if form_definition.allow_get_initial:
            form = DesignedForm(form_definition, initial_data=request.GET)
        else:
            form = DesignedForm(form_definition)

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