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}
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 override_mezzanine_form_processor(request, page): """ Override mezzanine.forms.page_processors.form_processor """ if request.method == 'POST': form = FormForForm(page.form, RequestContext(request), request.POST or None, request.FILES or None) if form.is_valid() and page.form.downloads.exists(): return process_promotion(request, page)
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}
def form_processor(request, page): """ Display a built form and handle submission. """ if request.method == 'POST': form = FormForForm(page.form, request.POST or None, request.FILES or None) if form.is_valid() and request.regstage: request.regstage.anketa = True request.regstage.save() return {'anketa': True, 'stage': True}
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}
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_from_page(request, slug): try: page = Page.objects.get(slug=slug) except Page.DoesNotExist: return None return FormForForm(page.form, request, None, None)
def form_from_page(slug): #pdb.set_trace() try: page = Page.objects.get(slug=slug) except Page.DoesNotExist: return None return FormForForm(page.form, None, None)
def test_custom_email_type(self): class CustomEmailField(forms.EmailField): pass fields.CLASSES[16] = CustomEmailField fields.NAMES += ((16, 'Custom email field'), ) form_page = Form.objects.create(title="Email form tests") form_page.fields.create(label="Email field test", field_type=16) test_email = '*****@*****.**' request = self._request_factory.post('/', {'field_1': test_email}) form = FormForForm(form_page, RequestContext(request), request.POST or None, request.FILES or None) self.assertTrue(form.is_valid()) self.assertEqual(form.email_to(), test_email)
def render_in_place(context, page, request): """ renders the content of the given page in place. """ context['page'] = page template_name = u"includes/%s_content_only.html" % page.content_model if page.content_model == "form": # adapted from Josh Cartmell's gist : https://gist.github.com/joshcartme/5130702 context['form'] = FormForForm(page.form, RequestContext(request), None) return get_template(template_name).render(Context(context))
def form_processor(request, page): """ Display a built form and handle submission. """ if request.method == 'POST': form = FormForForm(page.form, request.POST or None, request.FILES or None) if form.is_valid(): if form.cleaned_data['field_5'] == 'Are_you_attentive?': if request.regstage: request.regstage.task_2 = True request.regstage.save() else: errors = form._errors.setdefault("field_5", ErrorList()) errors.append(u'wrong key!') return render(request, 'pages/form.html', {'form': form}) return {'stage': True}
def test_custom_email_type(self): class CustomEmailField(forms.EmailField): pass fields.CLASSES[16] = CustomEmailField fields.NAMES += ((16, 'Custom email field'),) form_page = Form.objects.create(title="Email form tests") form_page.fields.create(label="Email field test", field_type=16) test_email = '*****@*****.**' request = self._request_factory.post('/', {'field_1': test_email}) form = FormForForm(form_page, RequestContext(request), request.POST or None, request.FILES or None) self.assertTrue(form.is_valid()) self.assertEqual(form.email_to(), test_email)
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 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})
def get_email_form(self): if self.form: return FormForForm(self.form, RequestContext(current_request()), None, None) return None