Skip to content

Commit

Permalink
Merge branch '2018-update' of https://github.com/okfn/foundation into…
Browse files Browse the repository at this point in the history
… 2018-update
  • Loading branch information
smth committed May 9, 2018
2 parents 7420d79 + 36cb5f9 commit 9d4beef
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 11 deletions.
1 change: 1 addition & 0 deletions foundation/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ def _parse_email_list(varname):
'spurl',
'standard_form',
'formtools',
'sendemail',

# Asset pipeline
'compressor',
Expand Down
3 changes: 2 additions & 1 deletion foundation/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
url(r'^admin/', include(admin.site.urls)),
url(r'^robots.txt$', TemplateView.as_view(template_name="robots.txt",
content_type="text/plain"),
name="robots_file")
name="robots_file"),
url('', include('sendemail.urls')),
)

# Allow testing of error pages in development
Expand Down
Empty file added sendemail/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions sendemail/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# from django.contrib import admin

# Register your models here.
23 changes: 23 additions & 0 deletions sendemail/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from django import forms


# define form fields and specify what data types to expect
class ContactForm(forms.Form):
email = forms.EmailField(required=True)
name = forms.CharField(required=True)
organisation = forms.CharField(required=True)
message = forms.CharField(widget=forms.Textarea, required=True)
type = forms.CharField(widget=forms.HiddenInput(), required=True)

# check if filled form data is valid
def is_valid(self):
valid = super(ContactForm, self).is_valid()

if not valid:
return False

# ensure that form data that's emailed is from one of the two forms
if self.cleaned_data['type'] not in ['Press', 'Service']:
return False

return True
Empty file.
3 changes: 3 additions & 0 deletions sendemail/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# from django.db import models

# Create your models here.
3 changes: 3 additions & 0 deletions sendemail/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# from django.test import TestCase

# Create your tests here.
8 changes: 8 additions & 0 deletions sendemail/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.conf.urls import url
# from django.contrib import admin

from . import views

urlpatterns = [
url('contact/', views.contactview, name='contact'),
]
51 changes: 51 additions & 0 deletions sendemail/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from __future__ import unicode_literals

from django.shortcuts import render
from django.contrib import messages
from django.core.mail import send_mail, BadHeaderError
from django.http import HttpResponse
from .forms import ContactForm


# check if form data is valid
def contactview(request):
if request.method == 'GET':
form = ContactForm()
else:
form = ContactForm(request.POST)
if form.is_valid():
name = form.cleaned_data['name']
email = form.cleaned_data['email']
message = form.cleaned_data['message']
organisation = form.cleaned_data['organisation']
type = form.cleaned_data['type']

# collate and send filled form data in one email
email_message = 'From: ' + email + '\n' + 'Name: ' + name + '\n'
+ 'Organisation: ' + organisation + '\n'
+ 'Message: ' + message + '\n'
email_subject = type + ' Enquiry from ' + name

# specify where form data is sent, depending on the type of enquiry
if type == 'Service':
recepients = ['oks@okfn.org']
else:
recepients = ['press@okfn.org']

# all form data will originate from the same email address
try:
send_mail(
email_subject, email_message,
"info@okfn.org", recepients)
except BadHeaderError:
return HttpResponse('Invalid header found.')

# pop up message once form data has been sent successfully
messages.info(
request,
'Thank you for your message. ' +
'Someone from Open Knowledge International' +
'will be in touch soon.')

# reload the contact page after form data has been sent successfully
return render(request, "cms_contact.html", {'form': form})
24 changes: 14 additions & 10 deletions templates/cms_contact.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,22 @@
{% placeholder "service" %}
</div>

<form class="contact">
<form id="service_form" class="contact" method="post">
{% csrf_token %}
<div class="fieldset">
<input id="type" type='hidden' name="type" value="Service"/>
<label>Name
<input type="text" class="form-control input-lg">
<input id="name" name="name" type="text" class="form-control input-lg">
</label>
<label>Organisation
<input type="text" class="form-control input-lg">
<input id="organisation" name="organisation" type="text" class="form-control input-lg">
</label>
<label>Email address
<input type="email" class="form-control input-lg">
<input id="email" name="email" type="email" class="form-control input-lg">
</label>
</div>
<label>How can we help?
<textarea class="form-control" rows="10"></textarea>
<textarea id="message" name="message" class="form-control" rows="10"></textarea>
</label>
<button type="submit" class="btn btn-primary btn-lg">Send</button>
</form>
Expand All @@ -51,20 +53,22 @@
{% placeholder "press" %}
</div>

<form class="contact">
<form id="press_form" class="contact" method="post">
{% csrf_token %}
<div class="fieldset">
<input id="type" type='hidden' name="type" value="Press"/>
<label>Name
<input type="text" class="form-control input-lg">
<input id="name" name="name" type="text" class="form-control input-lg">
</label>
<label>Organisation
<input type="text" class="form-control input-lg">
<input id="organisation" name="organisation" type="text" class="form-control input-lg">
</label>
<label>Email address
<input type="email" class="form-control input-lg">
<input id="email" name="email" type="email" class="form-control input-lg">
</label>
</div>
<label>How can we help?
<textarea class="form-control" rows="10"></textarea>
<textarea id="message" name="message" class="form-control" rows="10"></textarea>
</label>
<button type="submit" class="btn btn-primary btn-lg">Send</button>
</form>
Expand Down

0 comments on commit 9d4beef

Please sign in to comment.