Esempio n. 1
0
def post_contact_form(request, next=None, template_preview="django_contactme/preview.html", template_discarded="django_contactme/discarded.html", template_post="django_contactme/confirmation_sent.html"):
    """
    Post a contact message.

    HTTP POST is required. If ``POST['submit'] == "preview"`` or if there are
    errors a preview template, ``comments/preview.html``, will be rendered.
    """
    data = request.POST.copy()

    # Check to see if the POST data overrides the view's next argument.
    next = data.get("next", next)

    # Do we want to preview the message?
    preview = "preview" in data

    # Construct the ContactMsgForm
    form = ContactMsgForm(data=data)

    # Check security information
    if form.security_errors():
        return ContactMsgPostBadRequest(
            "The contact message form failed security verification: %s" % \
                escape(str(form.security_errors())))

    # If there are errors or if we requested a preview show the comment
    if form.errors or preview:
        return render_to_response(template_preview, 
                                  { "message": form.data.get("message", ""),
                                    "form": form,
                                    "next": next }, 
                                  RequestContext(request, {}))

    contact_msg_data = form.get_instance_data()

    # Signal that a confirmation is about to be requested
    responses = signals.confirmation_will_be_requested.send(
        sender=form.__class__, data=contact_msg_data, request=request)

    # Check whether a signal receiver decides to kill the process
    for (receiver, response) in responses:
        if response == False:
            return render_to_response(template_discarded, 
                                      {'data': contact_msg_data},
                                      context_instance=RequestContext(request))

    # Create key and send confirmation URL by email
    key = signed.dumps(contact_msg_data, compress=True, 
                       extra_key=CONTACTME_SALT)
    send_confirmation_email(contact_msg_data, key)
    
    # Signal that a confirmation has been requested
    signals.confirmation_requested.send(sender=form.__class__, 
                                        data=contact_msg_data, 
                                        request=request)

    if next is not None:
        return HttpResponseRedirect(next)

    return render_to_response(template_post, 
                              context_instance=RequestContext(request))
Esempio n. 2
0
def post_contact_form(request,
                      next=None,
                      template_preview="django_contactme/preview.html",
                      template_discarded="django_contactme/discarded.html",
                      template_post="django_contactme/confirmation_sent.html"):
    """
    Post a contact message.

    HTTP POST is required. If ``POST['submit'] == "preview"`` or if there are
    errors a preview template, ``comments/preview.html``, will be rendered.
    """
    data = request.POST.copy()

    # Check to see if the POST data overrides the view's next argument.
    next = data.get("next", next)

    # Do we want to preview the message?
    preview = "preview" in data

    # Construct the form
    form = get_form()(data=data)

    # Check security information
    if form.security_errors():
        return ContactMsgPostBadRequest(
            "The contact message form failed security verification: %s" %
            escape(str(form.security_errors())))

    # If there are errors or if we requested a preview show the comment
    if form.errors or preview:
        return render_to_response(template_preview, {
            "form": form,
            "next": next
        }, RequestContext(request, {}))

    contact_msg_data = form.get_instance_data()

    # Signal that a confirmation is about to be requested
    responses = signals.confirmation_will_be_requested.send(
        sender=form.__class__, data=contact_msg_data, request=request)

    # Check whether a signal receiver decides to kill the process
    for (receiver, response) in responses:
        if response is False:
            return render_to_response(template_discarded,
                                      {'data': contact_msg_data},
                                      context_instance=RequestContext(request))

    # Create key and send confirmation URL by email
    key = signed.dumps(contact_msg_data,
                       compress=True,
                       extra_key=settings.CONTACTME_SALT)
    send_confirmation_email(contact_msg_data, key)

    # Signal that a confirmation has been requested
    signals.confirmation_requested.send(sender=form.__class__,
                                        data=contact_msg_data,
                                        request=request)

    if next is not None:
        return HttpResponseRedirect(next)

    return render_to_response(template_post,
                              context_instance=RequestContext(request))
Esempio n. 3
0
def post_ajax_contact_form(
        request,
        template_preview="django_contactme/preview_ajax.html",
        template_discarded="django_contactme/discarded_ajax.html",
        template_post="django_contactme/confirmation_sent_ajax.html",
        template_field_errors="django_contactme/field_errors.html"):
    """
    Post a contact message via AJAX.

    HTTP POST is required. If ``POST['submit'] == "preview"`` or if there are
    errors a preview template, ``django_contactme/ajax_preview.html``, will
    be rendered.
    """
    if not request.is_ajax():
        return HttpResponseBadRequest("Bad request, AJAX call expected.")

    post_data = request.POST.copy()
    preview = "preview" in post_data  # want to preview the message?
    form = get_form()(data=post_data)  # Construct the form

    # Check security information
    if form.security_errors():
        return ContactMsgPostBadRequest(
            "The contact message form failed security verification: %s" %
            escape(str(form.security_errors())))

    json_data = {}
    if form.errors:
        json_data.update({'status': 'errors', 'errors': {}})
        for field_name in form.errors:
            json_data['errors'][field_name] = render_to_string(
                template_field_errors, {'field': form[field_name]},
                RequestContext(request, {}))

    if preview or form.errors:
        json_data['html'] = render_to_string(template_preview, {'form': form})
        if not form.errors:
            json_data['status'] = 'preview'
        return HttpResponse(json.dumps(json_data),
                            content_type='application/json')

    contact_msg_data = form.get_instance_data()

    # Signal that a confirmation is about to be requested
    responses = signals.confirmation_will_be_requested.send(
        sender=form.__class__, data=contact_msg_data, request=request)

    # Check whether a signal receiver decides to kill the process
    for (receiver, response) in responses:
        if response is False:
            html = render_to_string(template_discarded,
                                    {'data': contact_msg_data},
                                    context_instance=RequestContext(request))
            payload = json.dumps({'status': 'discarded', 'html': html})
            return HttpResponse(payload, content_type='application/json')

    # Create key and send confirmation URL by email
    key = signed.dumps(contact_msg_data,
                       compress=True,
                       extra_key=settings.CONTACTME_SALT)
    send_confirmation_email(contact_msg_data, key)

    # Signal that a confirmation has been requested
    signals.confirmation_requested.send(sender=form.__class__,
                                        data=contact_msg_data,
                                        request=request)

    html = render_to_string(template_post,
                            context_instance=RequestContext(request))
    payload = json.dumps({'status': 'success', 'html': html})
    return HttpResponse(payload, content_type='application/json')
Esempio n. 4
0
def post_ajax_contact_form(
        request,
        template_preview="django_contactme/preview_ajax.html",
        template_discarded="django_contactme/discarded_ajax.html",
        template_post="django_contactme/confirmation_sent_ajax.html",
        template_field_errors="django_contactme/field_errors.html"):
    """
    Post a contact message via AJAX.

    HTTP POST is required. If ``POST['submit'] == "preview"`` or if there are
    errors a preview template, ``django_contactme/preview_ajax.html``, will
    be rendered.
    """
    if not request.is_ajax():
        return HttpResponseBadRequest("Bad request, AJAX call expected.")

    post_data = request.POST.copy()
    preview = "preview" in post_data  # want to preview the message?
    form = get_form()(data=post_data)  # Construct the form

    # Check security information
    if form.security_errors():
        return ContactMsgPostBadRequest(
            "The contact message form failed security verification: %s" %
            escape(str(form.security_errors())))

    json_data = {}
    if form.errors:
        json_data.update({'status': 'errors', 'errors': {}})
        for field_name in form.errors:
            json_data['errors'][field_name] = render_to_string(
                template_field_errors,
                {'field': form[field_name]},
                RequestContext(request, {}))

    if preview or form.errors:
        json_data['html'] = render_to_string(template_preview, {'form': form})
        if not form.errors:
            json_data['status'] = 'preview'
        return HttpResponse(json.dumps(json_data),
                            content_type='application/json')

    contact_msg_data = form.get_instance_data()

    # Signal that a confirmation is about to be requested
    responses = signals.confirmation_will_be_requested.send(
        sender=form.__class__, data=contact_msg_data, request=request)

    # Check whether a signal receiver decides to kill the process
    for (receiver, response) in responses:
        if response is False:
            html = render_to_string(template_discarded,
                                    {'data': contact_msg_data},
                                    context_instance=RequestContext(request))
            payload = json.dumps({'status': 'discarded', 'html': html})
            return HttpResponse(payload, content_type='application/json')

    # Create key and send confirmation URL by email
    key = signed.dumps(contact_msg_data, compress=True,
                       extra_key=settings.CONTACTME_SALT)
    send_confirmation_email(contact_msg_data, key)

    # Signal that a confirmation has been requested
    signals.confirmation_requested.send(sender=form.__class__,
                                        data=contact_msg_data,
                                        request=request)

    html = render_to_string(template_post,
                            context_instance=RequestContext(request))
    payload = json.dumps({'status': 'success', 'html': html})
    return HttpResponse(payload, content_type='application/json')