Skip to content

Commit

Permalink
[enh] add contact form. fix #127
Browse files Browse the repository at this point in the history
  • Loading branch information
gawel committed Aug 4, 2011
1 parent a104007 commit e485bb3
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ settings_local.py
*.swp
*.swo
matplotlib/
django-contact-form/
memopol2/static/img/trends/meps/*
memopol2/static/img/meps/*
static/img/
6 changes: 5 additions & 1 deletion memopol2/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
DEBUG = True
TEMPLATE_DEBUG = DEBUG

# those emails are used as the contact form recipient
ADMINS = (
('Admin Istrator', '[email protected]'),
('memopol', '[email protected]'),
)

MANAGERS = ADMINS

DEFAULT_FROM_EMAIL='[email protected]'

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
Expand Down Expand Up @@ -110,6 +113,7 @@
# 3rd party
'south',
'flatblocks',
'contact_form',

# memopol
'reps',
Expand Down
12 changes: 12 additions & 0 deletions memopol2/templates/contact_form/contact_form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{% extends "base.html" %}

{% block content %}
<h1>{% trans "Contact form" %}</h1>
<form method="POST" id="contact">
{% csrf_token %}
{{ form.as_p }}
<p class="submit">
<input name="ok" type="submit" value="{% trans 'Send' %}" />
</p>
</form>
{% endblock %}
4 changes: 4 additions & 0 deletions memopol2/templates/contact_form/contact_form.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{{name}} - {{email}}
===============================================================================

{{body}}
1 change: 1 addition & 0 deletions memopol2/templates/contact_form/contact_form_subject.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[Memopol Contact] from {{name}}
1 change: 1 addition & 0 deletions memopol2/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^comments/', include('django.contrib.comments.urls')),
url(r'^contact/', include('contact_form.urls')),
)

# hack to autodiscover static files location in dev mode
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ South==0.7.3
django-debug-toolbar==0.8.4
numpy==1.5.1
git+http://github.com/matplotlib/matplotlib.git@3bf76b9fe6152280950168f9a3791f56c508eeed#egg=matplotlib
hg+https://bitbucket.org/gregmuellegger/django-contact-form-i18n#egg=django-contact-form
django-flatblocks==0.5
lxml
pyquery
Expand Down
20 changes: 19 additions & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
import unittest
import logging
from webtest import TestApp
from webtest import TestApp, TestResponse
from pyquery import PyQuery as pq
import django.core.handlers.wsgi

Expand All @@ -16,7 +16,9 @@
settings.AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.RemoteUserBackend',
)
settings.EMAIL_BACKEND = 'django.core.mail.backends.locmem.EmailBackend'

from django.core import mail
from django.contrib.auth.models import User, Permission
from django.contrib.contenttypes.models import ContentType

Expand All @@ -27,6 +29,7 @@ class TestCase(unittest.TestCase):
visited = set()

def setUp(self):
mail.outbox = []
self.app = TestApp(django_app)

def visit_links(self, links):
Expand All @@ -42,12 +45,27 @@ def visit_links(self, links):
except Exception, e:
log.warn('seems that %r is not a valid url. cant be encoded', l)

@property
def mails(self):
out = []
for m in mail.outbox:
r = TestResponse()
r.content_type = 'text/email'
r.unicode_body = u'From: %s\nTo: %s\nSubject: %s\n\n%s' % (m.from_email, ', '.join(m.to), m.subject, m.body)
out.append(r)
return out

@property
def mail(self):
return self.mails[0]

class UserTestCase(TestCase):

user = None
user_data = dict(id=99, username='garage1', email='[email protected]')

def setUp(self):
mail.outbox = []
self.set_user()

def set_anonymous(self):
Expand Down
26 changes: 26 additions & 0 deletions tests/test_contact.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
from tests import *

class TestContact(TestCase):

def test_contact(self):
"""test comments"""
resp = self.app.get('/contact/')
form = resp.forms['contact']
form['name'] = 'Un Garage'
form['email'] = '[email protected]'
resp = form.submit()
resp.mustcontain('<li>This field is required.</li>')

form = resp.forms['contact']
form['body'] = 'ACTA sucks'
resp = form.submit()

self.mail.mustcontain(
'From: [email protected]',
'To: [email protected]',
'Subject: [Memopol Contact] from Un Garage',
'Un Garage - [email protected]',
'==========',
'ACTA sucks',
)

0 comments on commit e485bb3

Please sign in to comment.