Skip to content

Commit

Permalink
Merge pull request #1 from maxicecilia/big_refactor
Browse files Browse the repository at this point in the history
Big refactor
  • Loading branch information
maxicecilia committed Apr 25, 2015
2 parents ac1af58 + 4c589ae commit 049576a
Show file tree
Hide file tree
Showing 23 changed files with 291 additions and 48 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ nosetests.xml
.mr.developer.cfg
.project
.pydevproject

# Sqlite3
*.sqlite3
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
language: python
install:
- pip install tox
- pip install coveralls
script:
- tox
env:
- TOXENV=django17
- TOXENV=django18
- TOXENV=coverage
after_success: coveralls
7 changes: 7 additions & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# file GENERATED by distutils, do NOT edit
setup.py
contact_us/__init__.py
contact_us/admin.py
contact_us/forms.py
contact_us/models.py
contact_us/views.py
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@

# Contact Us Project #
[![Build Status](https://travis-ci.org/maxicecilia/django_contact_us.svg)](https://travis-ci.org/maxicecilia/django_contact_us)
[![Coverage Status](https://coveralls.io/repos/maxicecilia/django_contact_us/badge.svg?branch=big_refactor)](https://coveralls.io/r/maxicecilia/django_contact_us?branch=big_refactor)


## About ##

Expand All @@ -23,14 +26,16 @@ Add 'contact_us' to your INSTALLED_APPS setting

Add the view to your urls.py

from contact_us.views import contact_us
from contact_us.views import ContactUsFormView
...
urlpatterns = patterns(
...
url(r'^contact_us/', contact_us, name='contact_us'),
url(r'^contact_us/', ContactUsFormView.as_view(), name='contact_us'),
...

You can set the template using **{'template': 'my_template.html'}** or redefine **contact_us/contact_form.html**
You can set the template using **ContactUsFormView.as_view(template='my_template.html')** or redefine **contact_us/contact_form.html**

By default the form will redirect to /thanks/ unless you override the success_url or send a next attribute in the GET.

Optionally you can add the model to the admin, so you can see the messages:

Expand Down
2 changes: 1 addition & 1 deletion contact_us/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# coding: utf-8
from django.contrib.admin import ModelAdmin
from django.contrib.admin.util import unquote
from django.contrib.admin.utils import unquote


class ContactUsAdmin(ModelAdmin):
Expand Down
1 change: 1 addition & 0 deletions contact_us/forms.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# coding: utf-8
from django.forms import ModelForm
from models import SimpleContact

Expand Down
2 changes: 2 additions & 0 deletions contact_us/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# coding: utf-8
from django.conf import settings
from django.contrib.sites.models import Site
from django.contrib.admin.models import LogEntry
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
Expand All @@ -18,6 +19,7 @@ class SimpleContact(models.Model):
from_name = models.CharField(_(u'Nombre'), max_length=64, blank=True, null=True)
from_phone = models.CharField(_(u'Teléfono'), max_length=64, blank=True, null=True)
message = models.TextField(_(u'Mensaje'))
site = models.ForeignKey(Site)
ts = models.DateTimeField(_(u'Timestamp'), auto_now=True, editable=False)

class Meta:
Expand Down
28 changes: 11 additions & 17 deletions contact_us/templates/contact_us/contact_form.html
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
<html>
<head>
<title>{{ title }}</title>
</head>
<body>
<h1>{{ title }}</h1>
{% if form %}
<form action="" method="POST">
{% csrf_token %}
<ul>
{{ form.as_ul }}
</ul>
<input type="submit" value="Enviar" />
</form>
{% endif %}
</body>
</html>
{% load i18n %}

{% if form %}
<form action="" method="POST">
{% csrf_token %}
<ul>
{{ form.as_ul }}
</ul>
<input type="submit" value="{% trans "Enviar" %}" />
</form>
{% endif %}
49 changes: 25 additions & 24 deletions contact_us/views.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
# coding: utf-8
from django.shortcuts import redirect
from django.contrib.sites.shortcuts import get_current_site
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.utils.translation import ugettext as _
from django.views.generic.edit import FormView
from forms import ContactForm

from forms import SimpleContactForm

class ContactUsFormView(FormView):
form_class = ContactForm
success_url = '/thanks/'
template_name = 'contact_us/contact_form.html'

def contact_us(request, extra_context=None, template=None, redirect_on_success=None):
title = _(u"Contáctese con nosotros")
if request.method == 'POST':
form = SimpleContactForm(request.POST)
if form.is_valid():
form.save()
form.instance.notify_users()
form = None
title = _(u"Muchas gracias")
if redirect_on_success:
return redirect(redirect_on_success)
else:
form = SimpleContactForm()
def get(self, request, *args, **kwargs):
form_class = self.get_form_class()
form = self.get_form(form_class)
context = self.get_context_data(**kwargs)
context['form'] = form
return self.render_to_response(context)

context = {
'title': title,
'form': form,
}
context.update(extra_context or {})
context_instance = RequestContext(request, current_app="contact_us")
return render_to_response(template or 'contact_us/contact_form.html', context, context_instance=context_instance)
def form_valid(self, form):
obj = form.save(commit=False)
obj.site = get_current_site(self.request)
obj.save()
form.instance.notify_users()
return super(ContactUsFormView, self).form_valid(form)

def get_success_url(self):
next_url = self.request.GET.get('next', None)
if next_url:
return "{}".format(next_url)
return super(ContactUsFormView, self).get_success_url()
13 changes: 13 additions & 0 deletions example_project/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Django contact_us's example project
====================

Simple project to display how to use django-contact-us.

### Installation
Install using pip
```
$ pip install -r requirements.txt
$ python manage.py migrate # Django 1.7+ and sqllite3 by default
$ python manage.py runserver 0.0.0.0:8000
```
Go to http://localhost:8000
Empty file.
6 changes: 6 additions & 0 deletions example_project/example_project/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# coding: utf-8
from django.contrib import admin
from contact_us.admin import ContactUsAdmin
from contact_us.models import SimpleContact

admin.site.register(SimpleContact, ContactUsAdmin)
79 changes: 79 additions & 0 deletions example_project/example_project/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'd+h*!fsihehbe$fh+t6k$h1f(%3d&4nxup=x+)3cz7u_@y7!t!'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []

# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',

'django.contrib.sites',
'contact_us',
'example_project',
)

MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)

ROOT_URLCONF = 'example_project.urls'

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
'templates'
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

WSGI_APPLICATION = 'example_project.wsgi.application'

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}


# Internationalization
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True

STATIC_URL = '/static/'

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

SITE_ID = 1
15 changes: 15 additions & 0 deletions example_project/example_project/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# coding: utf-8
from django.conf.urls import include, url
from django.contrib import admin
from django.views.generic import TemplateView
from contact_us.views import ContactUsFormView
from contact_us.forms import SimpleContactForm

urlpatterns = [
url(r'^contact_us/', ContactUsFormView.as_view(), name='contact_us'),
url(r'^contact_us_simplified/', ContactUsFormView.as_view(
form_class=SimpleContactForm), name='contact_us_simplified'),
url(r'^admin/', include(admin.site.urls)),
url(r'^thanks/$', TemplateView.as_view(template_name="thanks.html")),
url(r'^(/)*$', TemplateView.as_view(template_name="index.html")),
]
16 changes: 16 additions & 0 deletions example_project/example_project/wsgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
WSGI config for example_project project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/1.8/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "example_project.settings")

application = get_wsgi_application()
10 changes: 10 additions & 0 deletions example_project/manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "example_project.settings")

from django.core.management import execute_from_command_line

execute_from_command_line(sys.argv)
1 change: 1 addition & 0 deletions example_project/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Django==1.8
15 changes: 15 additions & 0 deletions example_project/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<title>Django contact_us's example project</title>
</head>
<body>
<h2>Welcome!</h2>
<p>You can take a look at the different form options here:</p>
<ul>
<li><a href="/contact_us">Go to default form</a></li>
<li><a href="/contact_us_simplified">Go to simplified form</a></li>
<li><a href="/contact_us?next=/">Go to default form that will return to the home</a></li>
</ul>
</body>
</html>
10 changes: 10 additions & 0 deletions example_project/templates/thanks.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<title>Thanks</title>
</head>
<body>
<p>Thanks for testing django contact_us!!</p>
<p><a href="/">Back to home</a></p>
</body>
</html>
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
setup(name='django-contact-us',
version='1.0',
description='Generic contact-us application for Django',
long_description=open(os.path.join(os.path.dirname(__file__), 'README.md')).read(),
long_description='Generic contact-us application for Django',
author='Maximiliano Cecilia',
author_email='[email protected]',
url='https://github.com/maxicecilia/django_contact_us/',
Expand Down
1 change: 1 addition & 0 deletions tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.sites',
'contact_us',
)

Expand Down
Loading

0 comments on commit 049576a

Please sign in to comment.