diff --git a/etc/environments/deployment/procfile b/etc/environments/deployment/procfile index c90b8d4..59e131d 100644 --- a/etc/environments/deployment/procfile +++ b/etc/environments/deployment/procfile @@ -1 +1,2 @@ web: waitress-serve --port $PORT project.wsgi:application +worker: manage.py worker diff --git a/etc/environments/development/procfile b/etc/environments/development/procfile index dbc4b47..2b2fb45 100644 --- a/etc/environments/development/procfile +++ b/etc/environments/development/procfile @@ -1,2 +1,3 @@ web: manage.py runserver 0.0.0.0:$PORT -watcher: npm run watch \ No newline at end of file +watcher: npm run watch +worker: manage.py worker diff --git a/project/common/forms.py b/project/common/forms.py index f237969..90c0586 100644 --- a/project/common/forms.py +++ b/project/common/forms.py @@ -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' + }) diff --git a/project/common/jobs.py b/project/common/jobs.py new file mode 100644 index 0000000..f40f0ae --- /dev/null +++ b/project/common/jobs.py @@ -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) diff --git a/project/common/tests.py b/project/common/tests.py index 465c435..de2a749 100644 --- a/project/common/tests.py +++ b/project/common/tests.py @@ -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) diff --git a/project/pages/tests.py b/project/pages/tests.py index 288d784..e987f7d 100644 --- a/project/pages/tests.py +++ b/project/pages/tests.py @@ -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): diff --git a/project/pages/views.py b/project/pages/views.py index 22d6cc0..9c65540 100644 --- a/project/pages/views.py +++ b/project/pages/views.py @@ -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' diff --git a/project/settings.py b/project/settings.py index a569ad4..cdea934 100644 --- a/project/settings.py +++ b/project/settings.py @@ -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'], + } +} diff --git a/requirements.txt b/requirements.txt index 67ee25f..c3d29fc 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 diff --git a/templates/about/index.html b/templates/about/index.html index 2fefe29..f1e0037 100644 --- a/templates/about/index.html +++ b/templates/about/index.html @@ -59,7 +59,7 @@
-
+ {% csrf_token %} {{ form|bootstrap }}
diff --git a/templates/email/base.html b/templates/email/base.html new file mode 100644 index 0000000..f031be1 --- /dev/null +++ b/templates/email/base.html @@ -0,0 +1,11 @@ +{% block subject %} + {% block subjectcontent %}{% endblock %} +{% endblock %} + +{% block body %} + {% block bodycontent %}{% endblock %} +{% endblock %} + +{% block html %} + {% block htmlcontent %}{% endblock %} +{% endblock %} diff --git a/templates/email/contact_message.html b/templates/email/contact_message.html new file mode 100644 index 0000000..a996dd3 --- /dev/null +++ b/templates/email/contact_message.html @@ -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 %} +

You have recieved a message from {{ name }}.

+

The message reads:

+
{{ message }}
+{% endblock %}