Ejemplo n.º 1
0
def modularpage_processor(request, page):
    formblock_ct = ContentType.objects.get(app_label='clubhouse_forms',\
            model='formblock')

    # TODO: Fix this to find form objects in any of the BlockContexts registered here
    formblocks = page.modularpage.content.filter(block_type=formblock_ct)
    from clubhouse.forms import Initialised

    for formblock in formblocks:
        registry_key = 'form:%s' % formblock.block_object.pk
        form_model = formblock.block_object.form
        form = FormForForm(formblock.block_object.form, RequestContext(request),
                           None, None)

        post_data = request.POST or {}
        if form.submit_key in post_data:
            form = FormForForm(formblock.block_object.form,
                    RequestContext(request), request.POST, request.FILES or None)

        if form.is_valid():
            url = page.get_absolute_url()
            if is_spam(request, form, url):
                return redirect(url)
            attachments = []
            for f in form.files.values():
                f.seek(0)
                attachments.append((f.name, f.read()))
            entry = form.save()
            subject = form_model.email_subject
            if not subject:
                subject = "%s - %s" % (form_model.title, entry.entry_time)
            fields = [(v.label, format_value(form.cleaned_data[k]))
                      for (k, v) in form.fields.items()]
            context = {
                "fields": fields,
                "message": form_model.email_message,
                "request": request,
            }
            email_from = form_model.email_from or settings.DEFAULT_FROM_EMAIL
            email_to = form.email_to()
            if email_to and form_model.send_email:
                send_mail_template(subject, "email/form_response", email_from,
                                   email_to, context)
            headers = None
            if email_to:
                # Add the email entered as a Reply-To header
                headers = {'Reply-To': email_to}
            email_copies = split_addresses(form_model.email_copies)
            if email_copies:
                send_mail_template(subject, "email/form_response_copies",
                                   email_from, email_copies, context,
                                   attachments=attachments, headers=headers)
            form_valid.send(sender=request, form=form, entry=entry)
            form.is_submitted = True
        else:
            form_invalid.send(sender=request, form=form)

        Initialised.register(registry_key, form)

    return {}
Ejemplo n.º 2
0
def form_processor(request, page, response_redirect=True):
    """
    Display a built form and handle submission.
    """
    form = FormForForm(
        page.form, RequestContext(request), request.POST or None, request.FILES or None
    )
    if form.is_valid():
        url = page.get_absolute_url() + "?sent=1"
        if is_spam(request, form, url):
            return redirect(url)
        attachments = []
        for f in form.files.values():
            f.seek(0)
            attachments.append((f.name, f.read()))
        entry = form.save()
        subject = page.form.email_subject
        if not subject:
            subject = "%s - %s" % (page.form.title, entry.entry_time)
        fields = [
            (v.label, format_value(form.cleaned_data[k]))
            for (k, v) in form.fields.items()
        ]
        context = {
            "fields": fields,
            "message": page.form.email_message,
            "request": request,
        }
        email_from = page.form.email_from or settings.DEFAULT_FROM_EMAIL
        email_to = form.email_to()
        if email_to and page.form.send_email:
            send_mail_template(
                subject, "email/form_response", email_from, email_to, context
            )
        headers = None
        if email_to:
            # Add the email entered as a Reply-To header
            headers = {"Reply-To": email_to}
        email_copies = split_addresses(page.form.email_copies)
        if email_copies:
            send_mail_template(
                subject,
                "email/form_response_copies",
                email_from,
                email_copies,
                context,
                attachments=attachments,
                headers=headers,
            )
        form_valid.send(sender=request, form=form, entry=entry)
        if response_redirect:
            return redirect(url)
    form_invalid.send(sender=request, form=form)
    return {"form": form}
Ejemplo n.º 3
0
def form_processor(request, page):
    """
    Display a built form and handle submission.
    """
    form = FormForForm(page.form, request.POST or None, request.FILES or None)
    if form.is_valid():
        url = page.get_absolute_url() + "?sent=1"
        if is_spam(request, form, url):
            return redirect(url)
        entry = form.save()
        subject = page.form.email_subject
        if not subject:
            subject = "%s - %s" % (page.form.title, entry.entry_time)
        fields = [(v.label, format_value(form.cleaned_data[k]))
                  for (k, v) in form.fields.items()]
        context = {
            "fields": fields,
            "message": page.form.email_message,
            "request": request,
        }
        email_from = page.form.email_from or settings.DEFAULT_FROM_EMAIL
        email_to = form.email_to()
        if email_to and page.form.send_email:
            send_mail_template(subject,
                               "email/form_response",
                               email_from,
                               email_to,
                               context,
                               fail_silently=settings.DEBUG)
        if not settings.FORMS_DISABLE_SEND_FROM_EMAIL_FIELD:
            # Send from the email entered,
            # unless FORMS_DISABLE_SEND_FROM_EMAIL_FIELD is True.
            email_from = email_to or email_from
        email_copies = page.form.email_copies.split(",")
        email_copies = [e.strip() for e in email_copies if e.strip()]
        if email_copies:
            attachments = []
            for f in form.files.values():
                f.seek(0)
                attachments.append((f.name, f.read()))
            send_mail_template(subject,
                               "email/form_response",
                               email_from,
                               email_copies,
                               context,
                               attachments=attachments,
                               fail_silently=settings.DEBUG)
        form_valid.send(sender=request, form=form, entry=entry)
        return redirect(url)
    form_invalid.send(sender=request, form=form)
    return {"form": form}
Ejemplo n.º 4
0
def form_processor(request, page):
    """
    Display a built form and handle submission.
    """
    form = FormForForm(page.form, RequestContext(request),
                       request.POST or None, request.FILES or None)
    if form.is_valid():
        url = page.get_absolute_url() + "?sent=1"
        if is_spam(request, form, url):
            return redirect(url)
        attachments = []
        for f in form.files.values():
            f.seek(0)
            attachments.append((f.name, f.read()))
        entry = form.save()
        subject = page.form.email_subject
        if not subject:
            subject = "%s - %s" % (page.form.title, entry.entry_time)
        fields = [(v.label, format_value(form.cleaned_data[k]))
                  for (k, v) in form.fields.items()]
        context = {
            "fields": fields,
            "message": page.form.email_message,
            "request": request,
            "page": page,
            "entry_id": entry.id,
        }

        url += '&entry_id=' + str(entry.pk)

        email_from = page.form.email_from or settings.DEFAULT_FROM_EMAIL
        email_to = form.email_to()
        if email_to and page.form.send_email:
            send_mail_template(subject, "email/form_response", email_from,
                               email_to, context)
        headers = None
        if email_to:
            # Add the email entered as a Reply-To header
            headers = {'Reply-To': email_to}
        email_copies = split_addresses(page.form.email_copies)
        if email_copies:
            send_mail_template(subject, "email/form_response_copies",
                               email_from, email_copies, context,
                               attachments=attachments, headers=headers)
        form_valid.send(sender=request, form=form, entry=entry)
        return redirect(url)
    form_invalid.send(sender=request, form=form)
    return {"form": form}
Ejemplo n.º 5
0
def ajax_form_processor(request, page):
    """
    Handle a Mezzanine form submissions if and only if the request
    is ajax, otherwise the default processor will run.
    """
    if request.is_ajax():
        form = FormForForm(page.form, RequestContext(request),
                           request.POST or None, request.FILES or None)
        if form.is_valid():
            form.save()
            url = page.get_absolute_url() + "?sent=1"
            if is_spam(request, form, url):
                return redirect(url)
            attachments = []
            for f in form.files.values():
                f.seek(0)
                attachments.append((f.name, f.read()))
            entry = form.save()
            subject = page.form.email_subject
            if not subject:
                subject = "%s - %s" % (page.form.title, entry.entry_time)
            fields = [(v.label, format_value(form.cleaned_data[k]))
                      for (k, v) in form.fields.items()]
            context = {
                "fields": fields,
                "message": page.form.email_message,
                "request": request,
            }
            email_from = page.form.email_from or settings.DEFAULT_FROM_EMAIL
            email_to = form.email_to()
            if email_to and page.form.send_email:
                send_mail_template(subject, "email/form_response", email_from,
                                   email_to, context)
            headers = None
            if email_to:
                # Add the email entered as a Reply-To header
                headers = {'Reply-To': email_to}
            email_copies = split_addresses(page.form.email_copies)
            if email_copies:
                send_mail_template(subject, "email/form_response_copies",
                                   email_from, email_copies, context,
                                   attachments=attachments, headers=headers)
            form_valid.send(sender=request, form=form, entry=entry)
            return render(request, 'includes/form_content_only.html', {'form': form, 'ok': True})
        else:
            form_invalid.send(sender=request, form=form)
            return  render(request, 'includes/form_content_only.html', {'form': form})
Ejemplo n.º 6
0
def ajax_form_processor(request, page):
    """
    Handle a Mezzanine form submissions if and only if the request
    is ajax, otherwise the default processor will run.
    """
    if request.is_ajax():
        form = FormForForm(page.form, RequestContext(request), request.POST or None, request.FILES or None)
        if form.is_valid():
            form.save()
            url = page.get_absolute_url() + "?sent=1"
            if is_spam(request, form, url):
                return redirect(url)
            attachments = []
            for f in form.files.values():
                f.seek(0)
                attachments.append((f.name, f.read()))
            entry = form.save()
            subject = page.form.email_subject
            if not subject:
                subject = "%s - %s" % (page.form.title, entry.entry_time)
            fields = [(v.label, format_value(form.cleaned_data[k])) for (k, v) in form.fields.items()]
            context = {"fields": fields, "message": page.form.email_message, "request": request}
            email_from = page.form.email_from or settings.DEFAULT_FROM_EMAIL
            email_to = form.email_to()
            if email_to and page.form.send_email:
                send_mail_template(subject, "email/form_response", email_from, email_to, context)
            headers = None
            if email_to:
                # Add the email entered as a Reply-To header
                headers = {"Reply-To": email_to}
            email_copies = split_addresses(page.form.email_copies)
            if email_copies:
                send_mail_template(
                    subject,
                    "email/form_response_copies",
                    email_from,
                    email_copies,
                    context,
                    attachments=attachments,
                    headers=headers,
                )
            form_valid.send(sender=request, form=form, entry=entry)
            return render(request, "includes/form_content_only.html", {"form": form, "ok": True})
        else:
            form_invalid.send(sender=request, form=form)
            return render(request, "includes/form_content_only.html", {"form": form})
Ejemplo n.º 7
0
def form_processor(request, page):
    """
    Display a built form and handle submission.
    """
    form = FormForForm(page.form, request.POST or None, request.FILES or None)
    if form.is_valid():
        url = page.get_absolute_url() + "?sent=1"
        if is_spam(request, form, url):
            return redirect(url)
        entry = form.save()
        subject = page.form.email_subject
        if not subject:
            subject = "%s - %s" % (page.form.title, entry.entry_time)
        fields = [(v.label, format_value(form.cleaned_data[k]))
                  for (k, v) in form.fields.items()]
        context = {
            "fields": fields,
            "message": page.form.email_message,
            "request": request,
        }
        email_from = page.form.email_from or settings.DEFAULT_FROM_EMAIL
        email_to = form.email_to()
        if email_to and page.form.send_email:
            send_mail_template(subject, "email/form_response", email_from,
                               email_to, context, fail_silently=settings.DEBUG)
        if not settings.FORMS_DISABLE_SEND_FROM_EMAIL_FIELD:
            # Send from the email entered,
            # unless FORMS_DISABLE_SEND_FROM_EMAIL_FIELD is True.
            email_from = email_to or email_from
        email_copies = page.form.email_copies.split(",")
        email_copies = [e.strip() for e in email_copies if e.strip()]
        if email_copies:
            attachments = []
            for f in form.files.values():
                f.seek(0)
                attachments.append((f.name, f.read()))
            send_mail_template(subject, "email/form_response", email_from,
                               email_copies, context, attachments=attachments,
                               fail_silently=settings.DEBUG)
        form_valid.send(sender=request, form=form, entry=entry)
        return redirect(url)
    form_invalid.send(sender=request, form=form)
    return {"form": form}