Added emailing to contact form
This commit is contained in:
parent
db5029ff6a
commit
1d174bac2c
12 changed files with 121 additions and 4 deletions
|
@ -1 +1,2 @@
|
||||||
web: waitress-serve --port $PORT project.wsgi:application
|
web: waitress-serve --port $PORT project.wsgi:application
|
||||||
|
worker: manage.py worker
|
||||||
|
|
|
@ -1,2 +1,3 @@
|
||||||
web: manage.py runserver 0.0.0.0:$PORT
|
web: manage.py runserver 0.0.0.0:$PORT
|
||||||
watcher: npm run watch
|
watcher: npm run watch
|
||||||
|
worker: manage.py worker
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
from django import forms
|
from django import forms
|
||||||
|
from django_dbq.models import Job
|
||||||
|
|
||||||
|
|
||||||
class ContactForm(forms.Form):
|
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'}))
|
message = forms.CharField(label="Message", widget=forms.Textarea(attrs={'placeholder': 'Enter your message here'}))
|
||||||
|
|
||||||
def send_email(self):
|
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
13
project/common/jobs.py
Normal 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)
|
|
@ -1,6 +1,9 @@
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from django.core.urlresolvers import reverse
|
from django.core.urlresolvers import reverse
|
||||||
import os.path
|
import os.path
|
||||||
|
from django_dbq.models import Job
|
||||||
|
from . import jobs
|
||||||
|
from collections import namedtuple
|
||||||
|
|
||||||
PATH = os.path.dirname(os.path.abspath(__file__))
|
PATH = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
|
||||||
|
@ -48,3 +51,46 @@ class MarkdownViewTestCase(TestCase):
|
||||||
def test_invalid_template(self):
|
def test_invalid_template(self):
|
||||||
response = self.client.get(reverse('pages:projects', args=['not-a-project']))
|
response = self.client.get(reverse('pages:projects', args=['not-a-project']))
|
||||||
self.assertEqual(response.status_code, 404)
|
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)
|
||||||
|
|
|
@ -19,6 +19,15 @@ class AboutIndexTestCase(TestCase):
|
||||||
response = self.client.get(reverse('pages:about'))
|
response = self.client.get(reverse('pages:about'))
|
||||||
self.assertEqual(response.status_code, 200)
|
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):
|
class Custom404TestCase(TestCase):
|
||||||
def test_accessable(self):
|
def test_accessable(self):
|
||||||
|
|
|
@ -33,8 +33,13 @@ class AboutWebsiteView(CustomTemplate):
|
||||||
class AboutIndexView(CustomFormTemplate):
|
class AboutIndexView(CustomFormTemplate):
|
||||||
template_name = 'about/index.html'
|
template_name = 'about/index.html'
|
||||||
html_title = "About"
|
html_title = "About"
|
||||||
|
success_url = '/about/?sent'
|
||||||
form_class = ContactForm
|
form_class = ContactForm
|
||||||
|
|
||||||
|
def form_valid(self, form):
|
||||||
|
form.send_email()
|
||||||
|
return super().form_valid(form)
|
||||||
|
|
||||||
|
|
||||||
class AboutMeView(CustomTemplate):
|
class AboutMeView(CustomTemplate):
|
||||||
template_name = 'about/me.html'
|
template_name = 'about/me.html'
|
||||||
|
|
|
@ -27,6 +27,8 @@ INSTALLED_APPS = (
|
||||||
|
|
||||||
'bootstrapform',
|
'bootstrapform',
|
||||||
'django_client_reverse',
|
'django_client_reverse',
|
||||||
|
'mail_templated',
|
||||||
|
'django_dbq',
|
||||||
|
|
||||||
'project.pages',
|
'project.pages',
|
||||||
'project.common',
|
'project.common',
|
||||||
|
@ -87,3 +89,9 @@ STATIC_ROOT = os.path.join(BASE_DIR, 'collected-static')
|
||||||
STATICFILES_DIRS = (
|
STATICFILES_DIRS = (
|
||||||
os.path.join(BASE_DIR, 'static', 'build'),
|
os.path.join(BASE_DIR, 'static', 'build'),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
JOBS = {
|
||||||
|
'send_email': {
|
||||||
|
'tasks': ['project.common.jobs.send_email'],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -2,11 +2,13 @@ coverage==4.0.3
|
||||||
colorama==0.3.6
|
colorama==0.3.6
|
||||||
Django==1.8.7
|
Django==1.8.7
|
||||||
dj-database-url==0.3.0
|
dj-database-url==0.3.0
|
||||||
|
django-db-queue==0.0.2
|
||||||
django-bootstrap-form==3.2
|
django-bootstrap-form==3.2
|
||||||
git+https://github.com/RealOrangeOne/django-client-reverse
|
git+https://github.com/RealOrangeOne/django-client-reverse
|
||||||
django-flat-theme==1.1.3
|
django-flat-theme==1.1.3
|
||||||
djangorestframework==3.3.2
|
djangorestframework==3.3.2
|
||||||
markdown2==2.3.0
|
|
||||||
flake8==2.5.0
|
flake8==2.5.0
|
||||||
|
markdown2==2.3.0
|
||||||
|
max-django-mail-templated==1.2
|
||||||
whitenoise==2.0.6
|
whitenoise==2.0.6
|
||||||
waitress==0.8.10
|
waitress==0.8.10
|
||||||
|
|
|
@ -59,7 +59,7 @@
|
||||||
</div>
|
</div>
|
||||||
<div id="collapse1" class="panel-collapse collapse" role="tabpanel" aria-labelledby="heading1">
|
<div id="collapse1" class="panel-collapse collapse" role="tabpanel" aria-labelledby="heading1">
|
||||||
<div class="panel-body">
|
<div class="panel-body">
|
||||||
<form role="form">
|
<form role="form" method="post">
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
{{ form|bootstrap }}
|
{{ form|bootstrap }}
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
|
|
11
templates/email/base.html
Normal file
11
templates/email/base.html
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
{% block subject %}
|
||||||
|
{% block subjectcontent %}{% endblock %}
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
|
{% block body %}
|
||||||
|
{% block bodycontent %}{% endblock %}
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
|
{% block html %}
|
||||||
|
{% block htmlcontent %}{% endblock %}
|
||||||
|
{% endblock %}
|
15
templates/email/contact_message.html
Normal file
15
templates/email/contact_message.html
Normal 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 %}
|
Reference in a new issue