Moved contact form model in order to be reusable by ungleich app, Created unit test for digitalglarus contact page, Created ungleich contact page, Created unit test for ungleich contact page, Created unit test for forms .

This commit is contained in:
Levi 2016-04-10 16:12:43 -05:00
parent cf0fec706c
commit 942464f6f6
37 changed files with 425 additions and 31 deletions

View file

@ -1,5 +1,6 @@
from django.contrib import admin
from .models import Message, Supporter, DGGallery, DGPicture
from .models import Supporter, DGGallery, DGPicture
from utils.models import ContactMessage
#
class DGPictureInline(admin.StackedInline):
model = DGPicture
@ -8,5 +9,5 @@ class DGGalleryAdmin(admin.ModelAdmin):
inlines = [DGPictureInline]
admin.site.register(DGGallery, DGGalleryAdmin)
admin.site.register(Message)
admin.site.register(ContactMessage)
admin.site.register(Supporter)

View file

@ -0,0 +1,18 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.4 on 2016-04-10 17:10
from __future__ import unicode_literals
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('digitalglarus', '0005_auto_20160407_0519'),
]
operations = [
migrations.DeleteModel(
name='Message',
),
]

View file

@ -3,17 +3,6 @@ from cms.models import CMSPlugin
from filer.fields.image import FilerImageField
class Message(models.Model):
name = models.CharField(max_length=200)
email = models.EmailField()
phone_number = models.CharField(max_length=200)
message = models.TextField()
received_date = models.DateTimeField(auto_now_add=True)
def __str__(self):
return "%s - %s - %s" % (self.name, self.email, self.received_date)
class Supporter(models.Model):
name = models.CharField(max_length=200)
description = models.TextField(null=True, blank=True)

View file

@ -40,7 +40,7 @@
</h2>
<hr>
<p>{% static_placeholder "digital_glarus_contact_form_text" %}</p>
<form action="" method="post" >
<form action="{% url 'digitalglarus:contact' %}" method="post" >
{% csrf_token %}
<div class="row">
<div autofocus class="form-group col-lg-4 {% if form.name.errors %}has-error text-danger{% endif %}">

View file

@ -0,0 +1,23 @@
from django.test import TestCase
from django.core.urlresolvers import reverse
from django.core.urlresolvers import resolve
class ContactViewTest(TestCase):
def setUp(self):
self.url = reverse('digitalglarus:contact')
self.data = {
'name': 'test',
'email': 'test@gmail.com',
'phone_number': '32123123123123',
'message': 'This is a message',
}
def url_resolve_to_view_correctly(self):
found = resolve(self.url)
self.assertEqual(found.func.__name__, self.view.__name__)
def test_any_user_should_contact_us(self):
response = self.client.post(self.url, self.data, follow=True)
self.assertEqual(response.status_code, 200)

View file

@ -3,23 +3,22 @@ import datetime
from django.shortcuts import get_object_or_404, render
from django.forms import ModelForm
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.core.urlresolvers import reverse_lazy
from django.utils.translation import get_language
from djangocms_blog.models import Post
from django.core.urlresolvers import resolve
from django.contrib import messages
from django.utils.translation import ugettext as _
from .models import Message, Supporter
from .forms import ContactUsForm
from .models import Supporter
from utils.forms import ContactUsForm
from django.views.generic.edit import FormView
class ContactView(FormView):
template_name = 'contact.html'
form_class = ContactUsForm
success_url = '/digitalglarus/contact/'
success_url = reverse_lazy('digitalglarus:contact')
success_message = _('Message Successfully Sent')
def form_valid(self, form):

View file

@ -52,6 +52,8 @@ INSTALLED_APPS = (
'django.contrib.staticfiles',
'django.contrib.sites',
'easy_thumbnails',
'utils',
'ungleich_page',
'mptt',
'parler',
'taggit',
@ -125,6 +127,7 @@ MIDDLEWARE_CLASSES = (
ROOT_URLCONF = 'dynamicweb.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',

View file

@ -20,6 +20,7 @@ urlpatterns = [
urlpatterns += i18n_patterns('',
# url(r'^$',include('ungleich.urls')),
url(r'^blog/',include('ungleich.urls',namespace='ungleich')),
url(r'^',include('ungleich_page.urls',namespace='ungleich_page')),
url(r'^login/',include(membership_urls)),
url(r'^admin/', include(admin.site.urls)),
url(r'^digitalglarus/', include('digitalglarus.urls',

View file

@ -7,6 +7,7 @@ from filer.fields.image import FilerImageField
# Create your models here.
class UngleichPage(PageExtension):
#image_header = models.ImageField(upload_to='image_header')
image = FilerImageField(null=True, blank=True,

View file

@ -4,6 +4,6 @@ from . import views
urlpatterns = [
url(r'^$', views.PostListViewUngleich.as_view()),
# url(r'^$',views.PostListView.as_view()),
url(r'^(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/(?P<slug>\w[-\w]*)/$',views.details)
url(r'^(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/(?P<slug>\w[-\w]*)/$',
views.details)
]

View file

@ -1,14 +1,11 @@
from django.shortcuts import render
from django.utils.translation import get_language
from djangocms_blog.models import Post
from django.views.generic import ListView
from djangocms_blog.views import PostListView,BaseBlogView
from django.core.paginator import Paginator
from django.core.paginator import PageNotAnInteger
from django.core.paginator import EmptyPage
from djangocms_blog.views import PostListView
from djangocms_blog.settings import get_setting
def blog(request):
posts = Post.objects.all()
print(posts)

View file

3
ungleich_page/admin.py Normal file
View file

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

5
ungleich_page/apps.py Normal file
View file

@ -0,0 +1,5 @@
from django.apps import AppConfig
class UngleichPageConfig(AppConfig):
name = 'ungleich_page'

View file

3
ungleich_page/models.py Normal file
View file

@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

View file

@ -0,0 +1,40 @@
{% load cms_tags %}
<hr />
<!-- Footer -->
<footer>
<div class="container">
<div class="row">
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
<ul class="list-inline text-center">
<li>
<a href="https://twitter.com/ungleich">
<span class="fa-stack fa-lg">
<i class="fa fa-circle fa-stack-2x"></i>
<i class="fa fa-twitter fa-stack-1x fa-inverse"></i>
</span>
</a>
</li>
<li>
<a href="https://www.linkedin.com/company/ungleich-gmbh">
<span class="fa-stack fa-lg">
<i class="fa fa-circle fa-stack-2x"></i>
<i class="fa fa-linkedin fa-stack-1x fa-inverse"></i>
</span>
</a>
</li>
<li>
<a href="https://github.com/ungleich">
<span class="fa-stack fa-lg">
<i class="fa fa-circle fa-stack-2x"></i>
<i class="fa fa-github fa-stack-1x fa-inverse"></i>
</span>
</a>
</li>
</ul>
<p class="copyright text-muted text-centered">
{% static_placeholder "footer_copyright" %}
</p>
</div>
</div>
</div>
</footer>

View file

@ -0,0 +1,17 @@
{% load cms_tags staticfiles %}
<!-- Page Header -->
<!-- Set your background image for this header on the line below. -->
<header class="intro-header" style="background-image: url('{% static 'blog.ungleich.ch/img/home-bg.jpg' %}');">
<div class="container">
<div class="row">
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
<div class="site-heading">
<h1> {{ page_title }} </h1>
<hr class="small">
<span class="subheading"> {{page_subtitle}} </span>
</div>
</div>
</div>
</div>
</header>

View file

@ -0,0 +1,36 @@
{% load menu_tags staticfiles cms_tags %}
<!-- Navigation -->
<nav class="navbar navbar-default navbar-custom navbar-fixed-top">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header page-scroll">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/">
<img src="{% static "blog.ungleich.ch/img/logo_white.svg" %}" />
</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-right">
{% for child in children %}
<li class="child{% if child.selected %} selected{% endif %}{% if child.ancestor %} ancestor{% endif %}{% if child.sibling %} sibling{% endif %}{% if child.descendant %} descendant{% endif %}">
<a href="{{ child.attr.redirect_url|default:child.get_absolute_url }}">{{ child.get_menu_title }}</a>
{% if child.children %}
<ul>
{% show_menu from_level to_level extra_inactive extra_active template "" "" child %}
</ul>
{% endif %}
</li>
{% endfor %}
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container -->
</nav>

View file

@ -0,0 +1,78 @@
{% load cms_tags menu_tags sekizai_tags staticfiles bootstrap3 %}
<!doctype html>
<html>
<head>
<title>
{% block title %}
{% page_attribute "page_title" %}
{% endblock %}
</title>
{% addtoblock "external-css" %}
{% bootstrap_css %}
<link href='//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css' rel="stylesheet" type="text/css">
<link href='//fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
<link href='//fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800'
rel='stylesheet' type='text/css'>
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
{% endaddtoblock %}
{% addtoblock "css" %}
<link href="{% static 'blog.ungleich.ch/css/clean-blog.css' %}" type="text/css"
rel="stylesheet" medial="all" />
{% endaddtoblock %}
{% block meta %}
<meta charset="UTF-8">
<meta name="description" content="{% page_attribute 'meta_description' %}">
{% include 'meta.html' %}
{% endblock %}
{% render_block "external-css" %}
{% render_block "css" postprocessor "compressor.contrib.sekizai.compress" %}
</head>
<body>
{% cms_toolbar %}
{% show_menu 0 0 0 1 "cms/ungleichch/_menu.html" %}
<!-- body -->
<!-- Main Content -->
{% block base_header %}
{% include "ungleich_page/_header_base.html" %}
{% endblock %}
<div class="container">
<div class="row">
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
{% block base_content %}
{% placeholder "default" %}
{% endblock %}
</div>
</div>
</div>
<!-- end body -->
{% include "cms/ungleichch/_footer.html" %}
{% addtoblock "external-js" %}
{% bootstrap_javascript %}
{% endaddtoblock %}
{% addtoblock "js" %}
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-62285904-4', 'auto');
ga('send', 'pageview');
</script>
<script src="{% static 'blog.ungleich.ch/vendor/js/jquery.min.js' %}" type="text/javascript" />
<script src="{% static 'blog.ungleich.ch/js/navbar-scroll-up.js' %}" type="text/javascript" />
{% endaddtoblock %}
{% render_block "js" postprocessor "compressor.contrib.sekizai.compress" %}
{% render_block "external-js" %}
</body>
</html>

View file

@ -0,0 +1,38 @@
{% extends "ungleich_page/base_ungleich.html" %}
{% block base_content %}
<form action="{% url 'ungleich_page:contact' %}" method="post" >
{% csrf_token %}
<div class="row">
<div autofocus class="form-group col-lg-4 {% if form.name.errors %}has-error text-danger{% endif %}">
{{ form.name.label_tag }}
{{ form.name }}
{{ form.name.errors|striptags}}
</div>
<div class="form-group col-lg-4 {% if form.email.errors %}has-error text-danger {% endif %}">
{{ form.email.label_tag }}
{{ form.email }}
{{ form.email.errors|striptags}}
</div>
<div class="form-group col-lg-4 {% if form.phone_number.errors %}has-error text-danger {% endif %}">
{{ form.phone_number.label_tag }}
{{ form.phone_number }}
{{ form.phone_number.errors|striptags}}
</div>
<div class="clearfix"></div>
<div class="form-group col-lg-12 {% if form.message.errors %}has-error text-danger {% endif %}">
{{ form.message.label_tag }}
{{ form.message }}
{{ form.message.errors|striptags}}
</div>
{{ form.non_field_errors }}
<div class="form-group col-lg-12">
<input type="hidden" name="save" value="contact">
<button type="submit" class="btn btn-default" {% if form.name.errors %} autofocus {% endif %}>Submit</button>
</div>
</div>
</form>
{% block content %}
{% endblock %}
{% endblock %}

View file

@ -0,0 +1,3 @@
{% extends "base_glarus.html" %}
{% block base_content %}
{% endblock %}

View file

@ -0,0 +1,5 @@
{% extends "base_ungleich.html" %}
{% load cms_tags %}
{% block base_content %}
{% placeholder "page_content" %}
{% endblock %}

View file

@ -0,0 +1,23 @@
from django.test import TestCase
from django.core.urlresolvers import reverse
from django.core.urlresolvers import resolve
class ContactViewTest(TestCase):
def setUp(self):
self.url = reverse('ungleich_page:contact')
self.data = {
'name': 'test',
'email': 'test@gmail.com',
'phone_number': '32123123123123',
'message': 'This is a message',
}
def url_resolve_to_view_correctly(self):
found = resolve(self.url)
self.assertEqual(found.func.__name__, self.view.__name__)
def test_any_user_should_contact_us(self):
response = self.client.post(self.url, self.data, follow=True)
self.assertEqual(response.status_code, 200)

6
ungleich_page/urls.py Normal file
View file

@ -0,0 +1,6 @@
from django.conf.urls import url
from .views import ContactView
urlpatterns = [
url(r'contact/?$', ContactView.as_view(), name='contact'),
]

25
ungleich_page/views.py Normal file
View file

@ -0,0 +1,25 @@
from django.contrib import messages
from django.views.generic.edit import FormView
from django.utils.translation import ugettext as _
from django.core.urlresolvers import reverse_lazy
from utils.forms import ContactUsForm
class ContactView(FormView):
template_name = 'ungleich_page/contact.html'
form_class = ContactUsForm
success_url = reverse_lazy('digitalglarus:contact')
success_message = _('Message Successfully Sent')
def form_valid(self, form):
form.save()
form.send_email()
messages.add_message(self.request, messages.SUCCESS, self.success_message)
return super(ContactView, self).form_valid(form)
def get_context_data(self, **kwargs):
context = super(ContactView, self).get_context_data(**kwargs)
context['page_title'] = _('Contact Us')
context['page_subtitle'] = _('If you have any question, just send us an email.')
return context

0
utils/__init__.py Normal file
View file

3
utils/admin.py Normal file
View file

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

5
utils/apps.py Normal file
View file

@ -0,0 +1,5 @@
from django.apps import AppConfig
class UtilsConfig(AppConfig):
name = 'utils'

View file

@ -1,5 +1,5 @@
from django import forms
from .models import Message
from .models import ContactMessage
from django.template.loader import render_to_string
from django.core.mail import EmailMultiAlternatives
from django.utils.translation import ugettext_lazy as _
@ -9,7 +9,7 @@ class ContactUsForm(forms.ModelForm):
error_css_class = 'autofocus'
class Meta:
model = Message
model = ContactMessage
fields = ['name', 'email', 'phone_number', 'message']
widgets = {
'name': forms.TextInput(attrs={'class': u'form-control'}),

View file

@ -0,0 +1,27 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.4 on 2016-04-10 17:04
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='ContactMessage',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=200)),
('email', models.EmailField(max_length=254)),
('phone_number', models.CharField(max_length=200)),
('message', models.TextField()),
('received_date', models.DateTimeField(auto_now_add=True)),
],
),
]

View file

14
utils/models.py Normal file
View file

@ -0,0 +1,14 @@
from django.db import models
# Create your models here.
class ContactMessage(models.Model):
name = models.CharField(max_length=200)
email = models.EmailField()
phone_number = models.CharField(max_length=200)
message = models.TextField()
received_date = models.DateTimeField(auto_now_add=True)
def __str__(self):
return "%s - %s - %s" % (self.name, self.email, self.received_date)

25
utils/test_forms.py Normal file
View file

@ -0,0 +1,25 @@
from django.test import TestCase
from .forms import ContactUsForm
class ContactUsFormTest(TestCase):
def setUp(self):
self.completed_data = {
'name': 'test',
'email': 'test@gmail.com',
'phone_number': '32123123123123',
'message': 'This is a message',
}
self.incompleted_data = {
'name': 'test',
}
def test_valid_form(self):
form = ContactUsForm(data=self.completed_data)
self.assertTrue(form.is_valid())
def test_invalid_form(self):
form = ContactUsForm(data=self.incompleted_data)
self.assertFalse(form.is_valid())

3
utils/tests.py Normal file
View file

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

3
utils/views.py Normal file
View file

@ -0,0 +1,3 @@
from django.shortcuts import render
# Create your views here.