1
Fork 0

Added emailing to contact form

This commit is contained in:
Jake Howard 2016-01-24 19:54:57 +00:00
parent db5029ff6a
commit 1d174bac2c
12 changed files with 121 additions and 4 deletions

View file

@ -1 +1,2 @@
web: waitress-serve --port $PORT project.wsgi:application
worker: manage.py worker

View file

@ -1,2 +1,3 @@
web: manage.py runserver 0.0.0.0:$PORT
watcher: npm run watch
watcher: npm run watch
worker: manage.py worker

View file

@ -1,4 +1,5 @@
from django import forms
from django_dbq.models import Job
class ContactForm(forms.Form):
@ -7,4 +8,9 @@ class ContactForm(forms.Form):
message = forms.CharField(label="Message", widget=forms.Textarea(attrs={'placeholder': 'Enter your message here'}))
def send_email(self):
print("Sending email with", self.cleaned_data)
Job.objects.create(name='send_email', workspace={
'context': self.cleaned_data,
'to_email': 'info@theorangeone.net',
'from_email': self.cleaned_data['email'],
'template': 'email/contact_message.html'
})

13
project/common/jobs.py Normal file
View file

@ -0,0 +1,13 @@
from mail_templated import send_mail
def send_email(job):
template = job.workspace['template']
context = job.workspace['context'] or {}
to_email = job.workspace['to_email']
from_email = job.workspace['from_email']
if type(to_email) != list:
to_email = [to_email]
send_mail(template, context, from_email, to_email)

View file

@ -1,6 +1,9 @@
from django.test import TestCase
from django.core.urlresolvers import reverse
import os.path
from django_dbq.models import Job
from . import jobs
from collections import namedtuple
PATH = os.path.dirname(os.path.abspath(__file__))
@ -48,3 +51,46 @@ class MarkdownViewTestCase(TestCase):
def test_invalid_template(self):
response = self.client.get(reverse('pages:projects', args=['not-a-project']))
self.assertEqual(response.status_code, 404)
MockJob = namedtuple('MockJob', {'workspace': {}})
class WorkerTestCase(TestCase):
def test_mail_job_creation(self):
data = {
'name': 'Person',
'email': '123@123.123',
'message': 'Hi there, things.'
}
workspace = {
'template': 'email/contact_message.html',
'from_email': data['email'],
'to_email': 'info@theorangeone.net',
'context': data
}
self.client.post(reverse('pages:about'), data)
self.assertEqual(Job.objects.count(), 1)
job = Job.objects.all()[0]
self.assertEqual(job.workspace, workspace)
def test_email_error(self):
data = {
'name': 'Person',
'email': '123@123.123',
'message': 'Hi there, things.'
}
workspace = {
'template': 'email/contact_message.html',
'from_email': 'me@123.123',
'to_email': data['email'],
'context': data
}
job = MockJob(workspace)
errors = None
try:
jobs.send_email(job)
except Exception as e:
errors = e
self.assertFalse(errors)

View file

@ -19,6 +19,15 @@ class AboutIndexTestCase(TestCase):
response = self.client.get(reverse('pages:about'))
self.assertEqual(response.status_code, 200)
def test_email_send(self):
data = {
'email': '123@123.123',
'name': 'Person',
'message': 'Hi there, things.'
}
response = self.client.post(reverse('pages:about'), data)
self.assertRedirects(response, '/about/?sent')
class Custom404TestCase(TestCase):
def test_accessable(self):

View file

@ -33,8 +33,13 @@ class AboutWebsiteView(CustomTemplate):
class AboutIndexView(CustomFormTemplate):
template_name = 'about/index.html'
html_title = "About"
success_url = '/about/?sent'
form_class = ContactForm
def form_valid(self, form):
form.send_email()
return super().form_valid(form)
class AboutMeView(CustomTemplate):
template_name = 'about/me.html'

View file

@ -27,6 +27,8 @@ INSTALLED_APPS = (
'bootstrapform',
'django_client_reverse',
'mail_templated',
'django_dbq',
'project.pages',
'project.common',
@ -87,3 +89,9 @@ STATIC_ROOT = os.path.join(BASE_DIR, 'collected-static')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static', 'build'),
)
JOBS = {
'send_email': {
'tasks': ['project.common.jobs.send_email'],
}
}

View file

@ -2,11 +2,13 @@ coverage==4.0.3
colorama==0.3.6
Django==1.8.7
dj-database-url==0.3.0
django-db-queue==0.0.2
django-bootstrap-form==3.2
git+https://github.com/RealOrangeOne/django-client-reverse
django-flat-theme==1.1.3
djangorestframework==3.3.2
markdown2==2.3.0
flake8==2.5.0
markdown2==2.3.0
max-django-mail-templated==1.2
whitenoise==2.0.6
waitress==0.8.10

View file

@ -59,7 +59,7 @@
</div>
<div id="collapse1" class="panel-collapse collapse" role="tabpanel" aria-labelledby="heading1">
<div class="panel-body">
<form role="form">
<form role="form" method="post">
{% csrf_token %}
{{ form|bootstrap }}
<div class="form-group">

11
templates/email/base.html Normal file
View file

@ -0,0 +1,11 @@
{% block subject %}
{% block subjectcontent %}{% endblock %}
{% endblock %}
{% block body %}
{% block bodycontent %}{% endblock %}
{% endblock %}
{% block html %}
{% block htmlcontent %}{% endblock %}
{% endblock %}

View file

@ -0,0 +1,15 @@
{% extends 'email/base.html' %}
{% block subjectcontent %}Message from {{ name }}{% endblock %}
{% block bodycontent %}
You have recieved a message from {{ name }}.
The message reads:
{{ message }}
{% endblock %}
{% block htmlcontent %}
<p>You have recieved a message from {{ name }}.</p>
<p>The message reads:</p>
<blockquote>{{ message }}</blockquote>
{% endblock %}