Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Numerous always-ignore extensions
*.pyc
*.diff
*.err
*.orig
*.log
*.rej
*.swo
*.swp
*.vi
*.cache
*.egg-info
*~
*#

# OS or Editor folders
.DS_Store
Thumbs.db
.cache
.project
.settings
.tmproj
*.esproj
nbproject
*.sublime-project
*.sublime-workspace
.tm_properties
._*

# Folders to ignore
.hg
.svn
.CVS
.idea
_assets
_design
_content
_tmp
dist
build
_build
Binary file removed mysite/__init__.pyc
Binary file not shown.
10 changes: 7 additions & 3 deletions mysite/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,15 @@
# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = 'mysite.wsgi.application'

import os.path
BASE_PATH = os.path.normpath(os.path.dirname(__file__))

TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)
# Don't forget to use absolute paths, not relative paths.
BASE_PATH + "/templates/",
)

INSTALLED_APPS = (
'django.contrib.auth',
Expand All @@ -117,7 +121,7 @@
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
# 'django.contrib.admin',
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
'polls',
Expand Down
Binary file removed mysite/settings.pyc
Binary file not shown.
10 changes: 10 additions & 0 deletions mysite/templates/admin/base_site.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% extends "admin/base.html" %}
{% load i18n %}

{% block title %}MySite Corp Global Industries {{ title }} | {% trans 'Django site admin' %}e{% endblock %}

{% block branding %}
<h1 id="site-name">MySite Corp Global Industries {% trans 'Django administration' %}</h1>
{% endblock %}

{% block nav-global %}{% endblock %}
16 changes: 4 additions & 12 deletions mysite/urls.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
from django.conf.urls import patterns, include, url

# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
# Examples:
# url(r'^$', 'mysite.views.home', name='home'),
# url(r'^mysite/', include('mysite.foo.urls')),

# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

# Uncomment the next line to enable the admin:
# url(r'^admin/', include(admin.site.urls)),
url(r'^polls/', include('polls.urls', namespace="polls")),
url(r'^admin/', include(admin.site.urls)),
)
Binary file removed mysite/urls.pyc
Binary file not shown.
Binary file removed mysite/wsgi.pyc
Binary file not shown.
Binary file removed polls/__init__.pyc
Binary file not shown.
23 changes: 23 additions & 0 deletions polls/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from django.contrib import admin
from polls.models import Poll
from polls.models import Choice

class ChoiceInline(admin.TabularInline):
model = Choice
extra = 3


class PollAdmin(admin.ModelAdmin):
fieldsets = [
(None, {'fields': ['question']}),
('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
]
list_display = ('question', 'pub_date', 'was_published_recently')
list_filter = ['pub_date']
search_fields = ['question']
date_hierarchy = 'pub_date'
inlines = [ChoiceInline]

admin.site.register(Poll, PollAdmin)


6 changes: 5 additions & 1 deletion polls/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ def __unicode__(self):
return self.question

def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
now = timezone.now()
return now > self.pub_date >= now - datetime.timedelta(days=1)
was_published_recently.admin_order_field = 'pub_date'
was_published_recently.boolean = True
was_published_recently.short_description = 'Published recently?'


class Choice(models.Model):
Expand Down
Binary file removed polls/models.pyc
Binary file not shown.
12 changes: 12 additions & 0 deletions polls/templates/polls/detail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<h1>{{ poll.question }}</h1>

{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}

<form action="{% url 'polls:vote' poll.id %}" method="post">
{% csrf_token %}
{% for choice in poll.choice_set.all %}
<input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" />
<label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br />
{% endfor %}
<input type="submit" value="Vote" />
</form>
9 changes: 9 additions & 0 deletions polls/templates/polls/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% if latest_poll_list %}
<ul>
{% for poll in latest_poll_list %}
<li><a href="{% url 'polls:detail' poll.id %}">{{ poll.question }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}
9 changes: 9 additions & 0 deletions polls/templates/polls/results.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<h1>{{ poll.question }}</h1>

<ul>
{% for choice in poll.choice_set.all %}
<li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}</li>
{% endfor %}
</ul>

<a href="{% url 'polls:detail' poll.id %}">Vote again?</a>
36 changes: 26 additions & 10 deletions polls/tests.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".

Replace this with more appropriate tests for your application.
"""
import datetime

from django.utils import timezone
from django.test import TestCase

from polls.models import Poll

class PollMethodTests(TestCase):

def test_was_published_recently_with_future_poll(self):
"""
was_published_recently() should return False for polls whose
pub_date is in the future
"""
future_poll = Poll(pub_date=timezone.now() + datetime.timedelta(days=30))
self.assertEqual(future_poll.was_published_recently(), False)

def test_was_published_recently_with_old_poll(self):
"""
was_published_recently() should return False for polls whose pub_date
is older than 1 day
"""
old_poll = Poll(pub_date=timezone.now() - datetime.timedelta(days=30))
self.assertEqual(old_poll.was_published_recently(), False)

class SimpleTest(TestCase):
def test_basic_addition(self):
def test_was_published_recently_with_recent_poll(self):
"""
Tests that 1 + 1 always equals 2.
was_published_recently() should return True for polls whose pub_date
is within the last day
"""
self.assertEqual(1 + 1, 2)
recent_poll = Poll(pub_date=timezone.now() - datetime.timedelta(hours=1))
self.assertEqual(recent_poll.was_published_recently(), True)
13 changes: 13 additions & 0 deletions polls/testsettings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'pollstests.db',
# The following settings are not used with sqlite3:
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': '',
}
}

SECRET_KEY = 'i4mth3m0sts3cr3t0f4lls3tti1ngs'
23 changes: 23 additions & 0 deletions polls/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from django.conf.urls import patterns, url
from django.views.generic import DetailView, ListView
from polls.models import Poll

urlpatterns = patterns('',
url(r'^$',
ListView.as_view(
queryset=Poll.objects.order_by('-pub_date')[:5],
context_object_name='latest_poll_list',
template_name='polls/index.html'),
name='index'),
url(r'^(?P<pk>\d+)/$',
DetailView.as_view(
model=Poll,
template_name='polls/detail.html'),
name='detail'),
url(r'^(?P<pk>\d+)/results/$',
DetailView.as_view(
model=Poll,
template_name='polls/results.html'),
name='results'),
url(r'^(?P<poll_id>\d+)/vote/$', 'polls.views.vote', name='vote'),
)
28 changes: 27 additions & 1 deletion polls/views.py
Original file line number Diff line number Diff line change
@@ -1 +1,27 @@
# Create your views here.
from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from polls.models import Choice, Poll

def vote(request, poll_id):
p = get_object_or_404(Poll, pk=poll_id)
try:
selected_choice = p.choice_set.get(pk=request.POST['choice'])
except (KeyError, Choice.DoesNotExist):
# Redisplay the poll voting form.
return render(request, 'polls/detail.html', {
'poll': p,
'error_message': "You didn't select a choice.",
})
else:
selected_choice.votes += 1
selected_choice.save()
# Always return an HttpResponseRedirect after successfully dealing
# with POST data. This prevents data from being posted twice if a
# user hits the Back button.
return HttpResponseRedirect(reverse('polls:results', args=(p.id,)))


def results(request, poll_id):
poll = get_object_or_404(Poll, pk=poll_id)
return render(request, 'polls/results.html', {'poll': poll})