implement contact form

- implement contact_form.forms.ContactForm
- implement contact_form.views.ContactFormView and
  contact_form.views.ContactSuccessView
- add new URL patterns 'contact_form' and 'contact_success' in
  contact_form.urls
- add contact_form templates base.html, contact_form.html, contact_form.txt,
  contact_form_subject.txt and contact_success.html
- add german translation for new strings
- add contact_form to .coveragerc
- add generated code documentation for contact_form app
- add changelog entry
This commit is contained in:
Jan Dittberner 2015-02-01 18:55:24 +01:00
parent 2b0f1f9f89
commit 385838580b
15 changed files with 256 additions and 4 deletions

View file

@ -1,6 +1,7 @@
Changelog
=========
* :feature:`-` add contact form
* :feature:`-` add imprint as flatpage
* :support:`-` mark active menu item as active via context_processor and
corresponding template markup

View file

@ -36,6 +36,7 @@ Django app code
.. toctree::
code/gnuviechadmin
code/contact_form
code/dashboard
code/domains
code/hostingpackages

View file

@ -0,0 +1,24 @@
:py:mod:`contact_form` app
==========================
.. automodule:: contact_form
:py:mod:`forms <contact_form.forms>`
------------------------------------
.. automodule:: contact_form.forms
:members:
:py:mod:`urls <contact_form.urls>`
----------------------------------
.. automodule:: contact_form.urls
:py:mod:`views <contact_form.views>`
------------------------------------
.. automodule:: contact_form.views
:members:

View file

@ -1,5 +1,5 @@
[run]
source = gnuviechadmin,managemails,osusers,domains,taskresults,gvawebcore,userdbs
source = gnuviechadmin,contact_form,dashboard,domains,gvawebcore,managemails,osusers,taskresults,userdbs
[report]
omit = */migrations/*,*/tests/*.py,*/tests.py,gnuviechadmin/settings/local.py,gnuviechadmin/settings/production.py

View file

@ -0,0 +1,4 @@
"""
Contact form app.
"""

View file

@ -0,0 +1,73 @@
"""
This module contains the form class for the contact_form app.
"""
from __future__ import absolute_import, unicode_literals
from django import forms
from django.conf import settings
from django.core.mail import send_mail
from django.core.urlresolvers import reverse
from django.template import RequestContext
from django.template import loader
from django.utils.translation import ugettext as _
from django.contrib.sites.models import RequestSite
from django.contrib.sites.models import Site
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit
class ContactForm(forms.Form):
"""
This is the contact form class.
"""
name = forms.CharField(max_length=100, label=_('Your name'))
email = forms.EmailField(max_length=200, label=_('Your email address'))
body = forms.CharField(widget=forms.Textarea, label=_('Your message'))
subject_template_name = "contact_form/contact_form_subject.txt"
template_name = 'contact_form/contact_form.txt'
from_email = settings.DEFAULT_FROM_EMAIL
recipient_list = [mail_tuple[1] for mail_tuple in settings.MANAGERS]
def __init__(self, **kwargs):
self.request = kwargs.pop('request')
super(ContactForm, self).__init__(**kwargs)
self.helper = FormHelper()
self.helper.form_action = reverse('contact_form')
self.helper.add_input(Submit('submit', _('Send message')))
def get_context(self):
if not self.is_valid():
raise ValueError(
'Cannot generate context from invalid contact form')
if Site._meta.installed:
site = Site.objects.get_current()
else:
site = RequestSite(self.request)
return RequestContext(
self.request, dict(self.cleaned_data, site=site))
def message(self):
return loader.render_to_string(self.template_name, self.get_context())
def subject(self):
subject = loader.render_to_string(
self.subject_template_name, self.get_context())
return ''.join(subject.splitlines())
def save(self, fail_silently=False):
"""
Build and send the email.
"""
send_mail(
fail_silently=fail_silently,
from_email=self.from_email,
recipient_list=self.recipient_list,
subject=self.subject(),
message=self.message()
)

View file

@ -0,0 +1,36 @@
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
msgid ""
msgstr ""
"Project-Id-Version: contact_form\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2015-02-01 19:02+0100\n"
"PO-Revision-Date: 2015-02-01 19:03+0100\n"
"Last-Translator: Jan Dittberner <jan@dittberner.info>\n"
"Language-Team: Jan Dittberner <jan@dittberner.info>\n"
"Language: de\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"X-Generator: Poedit 1.6.10\n"
"X-Poedit-SourceCharset: UTF-8\n"
#: forms.py:27
msgid "Your name"
msgstr "Ihr Name"
#: forms.py:28
msgid "Your email address"
msgstr "Ihre E-Mailadresse"
#: forms.py:29
msgid "Your message"
msgstr "Ihre Nachricht"
#: forms.py:41
msgid "Send message"
msgstr "Nachricht senden"

View file

@ -0,0 +1,19 @@
"""
URL patterns for the contact_form views.
"""
from __future__ import absolute_import, unicode_literals
from django.conf.urls import patterns, url
from .views import (
ContactFormView,
ContactSuccessView,
)
urlpatterns = patterns(
'',
url(r'^$', ContactFormView.as_view(), name='contact_form'),
url(r'^success/$', ContactSuccessView.as_view(), name='contact_success'),
)

View file

@ -0,0 +1,50 @@
"""
This module defines the views of the contact_form app.
"""
from __future__ import absolute_import, unicode_literals
from django.shortcuts import redirect
from django.core.urlresolvers import reverse_lazy
from django.views.generic import (
FormView,
TemplateView,
)
from .forms import ContactForm
class ContactFormView(FormView):
"""
This is the contact form view.
"""
form_class = ContactForm
template_name = 'contact_form/contact_form.html'
success_url = reverse_lazy('contact_success')
def get_form_kwargs(self, **kwargs):
kwargs = super(ContactFormView, self).get_form_kwargs(**kwargs)
kwargs['request'] = self.request
return kwargs
def get_initial(self):
initial = super(ContactFormView, self).get_initial()
currentuser = self.request.user
if currentuser.is_authenticated():
initial['name'] = (
currentuser.get_full_name() or currentuser.username)
initial['email'] = currentuser.email
return initial
def form_valid(self, form):
form.save(False)
return redirect(self.get_success_url())
class ContactSuccessView(TemplateView):
"""
This view is shown after successful contact form sending.
"""
template_name = 'contact_form/contact_success.html'

View file

@ -7,8 +7,8 @@ msgid ""
msgstr ""
"Project-Id-Version: gnuviechadmin\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2015-02-01 16:31+0100\n"
"PO-Revision-Date: 2015-02-01 16:31+0100\n"
"POT-Creation-Date: 2015-02-01 19:04+0100\n"
"PO-Revision-Date: 2015-02-01 19:04+0100\n"
"Last-Translator: Jan Dittberner <jan@dittberner.info>\n"
"Language-Team: Jan Dittberner <jan@dittberner.info>\n"
"Language: de\n"
@ -477,7 +477,10 @@ msgstr "phpPgAdmin"
msgid "Imprint"
msgstr "Impressum"
#: templates/base.html:68
#: templates/base.html:68 templates/contact_form/contact_form.html:4
#: templates/contact_form/contact_form.html:5
#: templates/contact_form/contact_success.html:4
#: templates/contact_form/contact_success.html:5
msgid "Contact"
msgstr "Kontakt"
@ -514,6 +517,10 @@ msgstr ""
msgid "Close"
msgstr "Schließen"
#: templates/contact_form/contact_success.html:8
msgid "Your message has been sent successfully."
msgstr "Ihre Nachricht wurde erfolgreich übermittelt."
#: templates/dashboard/index.html:3
msgid "Welcome"
msgstr "Willkommen"

View file

@ -0,0 +1 @@
{% extends "base.html" %}

View file

@ -0,0 +1,21 @@
{% extends "contact_form/base.html" %}
{% load i18n crispy_forms_tags %}
{% block title %}{{ block.super }} - {% trans "Contact" %}{% endblock title %}
{% block page_title %}{% trans "Contact" %}{% endblock page_title %}
{% block content %}
{% crispy form %}
{% endblock %}
{% block extra_js %}
<script type="text/javascript">
$(document).ready(function() {
if ($('input[type=text]').first().val() != '') {
$('textarea').first().focus();
} else {
$('input[type=text]').first().focus();
}
});
</script>
{% endblock extra_js %}

View file

@ -0,0 +1,5 @@
User {{ name }} <{{ email }}> from IP address {{ request.META.REMOTE_ADDR }}
sent the following message via the contact form at
{{ site }}{% url 'contact_form' %}:
{{ body }}

View file

@ -0,0 +1 @@
[{{ site.name }}] message from {{ name }} via contact form

View file

@ -0,0 +1,9 @@
{% extends "contact_form/base.html" %}
{% load i18n %}
{% block title %}{{ block.super }} - {% trans "Contact" %}{% endblock title %}
{% block page_title %}{% trans "Contact" %}{% endblock page_title %}
{% block content %}
<p class="text-success">{% trans "Your message has been sent successfully." %}</p>
{% endblock %}