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))
def test_get_instance_data(self): # check get_contact_msg raises ValueError when form is not valid form = ContactMsgForm() name = "Alice Bloggs" email = "*****@*****.**" data = {"name": name, "email": email, "message": MESSAGE} data.update(form.initial) form = ContactMsgForm(data=data) form.is_valid() data = form.get_instance_data() self.assert_(len(data) == 4) self.assert_(name == data["name"]) self.assert_(email == data["email"]) self.assert_(MESSAGE == data["message"])
def test_get_instance_data(self): # check get_contact_msg raises ValueError when form is not valid form = ContactMsgForm() name = 'Alice Bloggs' email = '*****@*****.**' data = {'name': name, 'email': email, 'message': MESSAGE } data.update(form.initial) form = ContactMsgForm(data=data) form.is_valid() data = form.get_instance_data() self.assert_( len(data) == 4 ) self.assert_( name == data['name'] ) self.assert_( email == data['email'] ) self.assert_( MESSAGE == data['message'] )
def test_get_instance_data(self): # check get_contact_msg raises ValueError when form is not valid form = ContactMsgForm() name = 'Alice Bloggs' email = '*****@*****.**' data = {'name': name, 'email': email, 'message': MESSAGE} data.update(form.initial) form = ContactMsgForm(data=data) form.is_valid() data = form.get_instance_data() self.assert_(len(data) == 4) self.assert_(name == data['name']) self.assert_(email == data['email']) self.assert_(MESSAGE == data['message'])
def render(self, context): context.push() form_str = render_to_string("django_contactme/form.html", {"form": ContactMsgForm()}, context) context.pop() return form_str