def test_contact_msg_is_created_and_email_sent(self): key = self.url.split("/")[-1] self.get_confirm_contact_url(key) data = signed.loads(key, extra_key=settings.CONTACTME_SALT) try: cmsg = ContactMsg.objects.get(email=data["email"], name=data["name"], submit_date=data["submit_date"]) except: cmsg = None self.assert_(cmsg is not None) # be sure that settings module contains either ADMINS or # CONTACTME_NOTIFY_TO, otherwise there won't be 2 mails self.assertEqual(len(mail.outbox), 2)
def confirm_contact(request, key, template_accepted="django_contactme/accepted.html", template_discarded="django_contactme/discarded.html"): try: data = signed.loads(key, extra_key=settings.CONTACTME_SALT) except (ValueError, signed.BadSignature): raise Http404 # Check that the URL is not confirmed yet otherwise return a Http404 exists = (ContactMsg.objects.filter( name=data['name'], email=data['email'], submit_date=data['submit_date']).count() > 0) if exists: raise Http404 # Signal that the contact_message is about to be saved responses = signals.confirmation_received.send(sender=ContactMsg, data=data, request=request) # Check whether a signal receiver decides to discard the contact_msg for (receiver, response) in responses: if response is False: return render_to_response(template_discarded, {'data': data}, context_instance=RequestContext(request)) # Create ContactMsg object # - note: The submit_date read in the key may be used as well to discard # messages older than a certain date. Read the docs for an example. # http://readthedocs.org/projects/django-contactme contact_msg = ContactMsg.objects.create(site_id=settings.SITE_ID, name=data['name'], email=data['email'], message=data['message'], submit_date=data['submit_date']) contact_msg.ip_address = request.META.get("REMOTE_ADDR", None) contact_msg.save() # Notify Admins about the new ContactMsg send_contact_received_email(contact_msg) return render_to_response(template_accepted, {'data': data}, context_instance=RequestContext(request))
def confirm_contact(request, key, template_accepted="django_contactme/accepted.html", template_discarded="django_contactme/discarded.html"): try: data = signed.loads(key, extra_key=settings.CONTACTME_SALT) except (ValueError, signed.BadSignature): raise Http404 # Check that the URL is not confirmed yet otherwise return a Http404 exists = (ContactMsg.objects.filter( name=data['name'], email=data['email'], submit_date=data['submit_date']).count() > 0) if exists: raise Http404 # Signal that the contact_message is about to be saved responses = signals.confirmation_received.send(sender = ContactMsg, data = data, request = request ) # Check whether a signal receiver decides to discard the contact_msg for (receiver, response) in responses: if response == False: return render_to_response(template_discarded, {'data': data}, context_instance=RequestContext(request)) # Create ContactMsg object # - note: The submit_date read in the key may be used as well to discard # messages older than a certain date. Read the docs for an example. # http://readthedocs.org/projects/django-contactme contact_msg = ContactMsg.objects.create(site_id = settings.SITE_ID, name = data['name'], email = data['email'], message = data['message'], submit_date = data['submit_date']) contact_msg.ip_address = request.META.get("REMOTE_ADDR", None) contact_msg.save() # Notify Admins about the new ContactMsg send_contact_received_email(contact_msg) return render_to_response(template_accepted, {'data':data}, context_instance=RequestContext(request))