Beispiel #1
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():
        entry = form.save()
        fields = [
            "%s: %s" % (v.label, format_value(form.cleaned_data[k]))
            for (k, v) in form.fields.items()
        ]
        subject = page.form.email_subject
        if not subject:
            subject = "%s - %s" % (page.form.title, entry.entry_time)
        body = "\n".join(fields)
        if page.form.email_message:
            body = "%s\n\n%s" % (page.form.email_message, body)
        email_from = page.form.email_from or settings.DEFAULT_FROM_EMAIL
        email_to = form.email_to()
        if email_to and page.form.send_email:
            msg = EmailMessage(subject, body, email_from, [email_to])
            msg.send()
        email_from = email_to or email_from  # Send from the email entered.
        email_copies = [
            e.strip() for e in page.form.email_copies.split(",") if e.strip()
        ]
        if email_copies:
            msg = EmailMessage(subject, body, email_from, email_copies)
            for f in form.files.values():
                f.seek(0)
                msg.attach(f.name, f.read())
            msg.send()
        return redirect(page.get_absolute_url() + "?sent=1")
    return {"form": form}
Beispiel #2
0
 def get_context(self, request):
     from mezzanine.forms.forms import FormForForm
     sent = False
     if request.method == 'POST':
         form = FormForForm(self, request.POST, request.FILES)
         if form.is_valid():
             entry = form.save()
             fields = ["%s: %s" % (v.label, form.format_value(form.cleaned_data[k]))
                       for (k, v) in form.fields.items()]
             subject = self.email_subject
             if not subject:
                 subject = "%s - %s" % (self.title, entry.entry_time)
             body = "\n".join(fields)
             if self.email_message:
                 body = "%s\n\n%s" % (self.email_message, body)
             email_from = self.email_from or settings.DEFAULT_FROM_EMAIL
             email_to = form.email_to()
             if email_to and self.send_email:
                 msg = EmailMessage(subject, body, email_from, [email_to])
                 msg.send()
             email_from = email_to or email_from  # Send from the email entered.
             email_copies = [e.strip() for e in self.email_copies.split(",") if e.strip()]
             if email_copies:
                 msg = EmailMessage(subject, body, email_from, email_copies)
                 for f in form.files.values():
                     f.seek(0)
                     msg.attach(f.name, f.read())
                 msg.send()
             sent = True
     else:
         form = FormForForm(self)
     return {"form": form, "sent": sent}
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():
        entry = form.save()
        fields = ["%s: %s" % (v.label, form.cleaned_data[k])
            for (k, v) in form.fields.items()]
        subject = page.form.email_subject
        if not subject:
            subject = "%s - %s" % (page.form.title, entry.entry_time)
        body = "\n".join(fields)
        if page.form.email_message:
            body = "%s\n\n%s" % (page.form.email_message, body)
        email_from = page.form.email_from or settings.DEFAULT_FROM_EMAIL
        email_to = form.email_to()
        if email_to and page.form.send_email:
            msg = EmailMessage(subject, body, email_from, [email_to])
            msg.send()
        email_from = email_to or email_from  # Send from the email entered.
        email_copies = [e.strip() for e in page.form.email_copies.split(",")
            if e.strip()]
        if email_copies:
            msg = EmailMessage(subject, body, email_from, email_copies)
            for f in form.files.values():
                f.seek(0)
                msg.attach(f.name, f.read())
            msg.send()
        return redirect(page.get_absolute_url() + "?sent=1")
    return {"form": form}
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})
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})
Beispiel #6
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}
Beispiel #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}
Beispiel #8
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}
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}