From 0ccfc6f17de8031bd350f912b6ecf37ec5ba5dc1 Mon Sep 17 00:00:00 2001 From: Daniele Procida Date: Sun, 27 Jan 2013 13:43:30 +0000 Subject: [PATCH 1/9] removed .pyc --- mysite/settings.pyc | Bin 2863 -> 0 bytes mysite/urls.pyc | Bin 280 -> 0 bytes polls/models.pyc | Bin 861 -> 0 bytes 3 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 mysite/settings.pyc delete mode 100644 mysite/urls.pyc delete mode 100644 polls/models.pyc diff --git a/mysite/settings.pyc b/mysite/settings.pyc deleted file mode 100644 index 54c54feae4b9ba9841408cda9c51c6d6c151949f..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 2863 zcmd^BTXWk)6h4X_J8>=zZJ}v-N<+XQRL+gmFu{1 zkOS}wKnvg(AyObJ0RbQioDh^1a3}&<%!X1nl(S(8hh;eN7lBm3N3#|1%RpAaUy=cq zWnYo~3ZNT*RrYJLUk9=V{sSO~;BNpq0{;Ub8{j_#at!_>AYX$27|1609|Cy_{zpJg zz<&bdCHR{VVNmq81t;_=kmqnBkSjp8fqa6ncCdY#F$+LG15%aD0cs$d>;m~5hdpdx z%(V7Eh>@P^V~)rRq*QD*T;?+%``}jVB8ShgBbQ+^NLA8RyJ7Danh>V4t2pBHh?h`o z*e%mG*fLIsw+z{+$%{zYTHD|_f~3w|!{JLvoocRo-f?uvecf?AIXUe(9v53jXg{39 zoUP&9n2ggAtyyV8(u6G{hbAv34A+m)gs&RoA&+`Bc7((fXD?0oIO?jIs2|0Ysr@J+ zbj;KyQtmC|blA)|FaMdHHX7;CnD%%&jwqYj9-?jX6+hg+tn)5C-n+iqne^-Ry>GWJ zFIjJ=9$!~)`fteX;7z*SJKhefZ{MaDTWoh85^;>;5Hl91p~NTAN_?s{W&MqII2r|H z4(rY&7ho6>64RS-Ox0dG9HxodkVAXMXUFpZv&>P(B22iNGj}sy&hRh7uyJ2)VE0o* zZ*toABs!e%i+l(TX`0}6;+CdK&h{RNLxd9!aA$K6+ZiusxPoh&p2^8LX6(tFzF~t% z#RSIFaO}~$c@O~E$M5n*o)9R%9brV_^Uo(Q%N1jf?~!4F+-r2VqI}8CHbkCsc{sSJ zf>9dBOjgS^o@z`M-xQG|7ItGA&|f3Qqht`o>0p3z_IwUw!lRhsaT}-(y_(?J(P?)w zXofMPYys7(yIdq%=1djoY9q z1Ltbuz0`J9?0)_R$sPayVS#5`E?$lQO8GkC)vAg+MZS98ctT}G=*FwF78l3dGuo$? z<{3f85-+T#w@urX_0iUBtz|f_tPfrDv{#yIWRz#Njll2N1{Y7KrP-}BR1F#(-H^rN zn&{Njr5k*V1M{G6&j{;a-I-ZP{k!Ox23)l5D&unB;p**F9 zcw^cSX4ABFu?}f#G#tYVelUKPwzCXa9Zl*y6ktqUw~TYm!4+Bf z1%ZoRMYcc}V9-X#Zc5{Gw`B&}>8WKl5Mala$(VVRP(+q7pPpt}hAwnnxdrkPl$o+R zE&QZu`yWi}D;fX* diff --git a/mysite/urls.pyc b/mysite/urls.pyc deleted file mode 100644 index 916e1768143c8f42a0d3095ea566215810ee5e56..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 280 zcmYL@O$x#=5QQgA{Xr2tfCuP8TEVS|ClJ(K!9{7JR`Mgwbm7_~djNeA#Uc4#W*(Ut z`J9Z7i*#4hHxlRuH_QQrBghb0L8Ty@P??aGR7w(BkV@zQBPAv2kfr$0ybzhhi-*9i zV%Dt-JcypFO{X+Rw`&bUXq)vwo&xGxxwFq}?@l@Fu#f&;M8utV450pNnIWY=!V+n5 s@m3eAa*a07Pebn5w@qb2Veg?~{gXH%zuiA;uB=y`(d#%Y#9_1k8_V}TBLDyZ diff --git a/polls/models.pyc b/polls/models.pyc deleted file mode 100644 index af237dbb3b21b617fde6e907f4a6684058a784ff..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 861 zcmb`F&yLeD5XQ%8n*9R{h#NPK;j+EJ4IyArC4@kb-r`G+~(JR`iH;~%FGho7N9)Eb%@GA<)Fw9M$U zAvdD0L|dXyvk|#`FERd|$jKFSULN2m{}WGXFp>h&_Kri89K-j02!E7+iC}yLVbZFc z(GnGsl`WYWN+B{PnAyTsLDhMwuH_ig)>k%gw~)qv*JJKLTrK=?wYdmBHtdocIYivr zu5Q`dw9#5v+5}81q)3h+XHC7HdJZlv|FYpIzVGA&saH7jqi=baMggmpO5MJb_!!ek z)3-cvb%(?7Ue?{BmyPsmUxzqpucH^9tiS=21m<*gT`4R5uBea-bKfLzXHD Date: Sun, 27 Jan 2013 13:45:52 +0000 Subject: [PATCH 2/9] end of tutorial 2 --- .gitignore | 8 ++++++++ mysite/settings.py | 2 +- mysite/urls.py | 6 +++--- polls/admin.py | 23 +++++++++++++++++++++++ polls/models.py | 3 +++ 5 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 .gitignore create mode 100644 polls/admin.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2d028c7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +*.egg-info +*.pot +*.py[co] +MANIFEST +dist/ +docs/_build/ +tests/coverage_html/ +tests/.coverage \ No newline at end of file diff --git a/mysite/settings.py b/mysite/settings.py index 8e12620..be20f3e 100644 --- a/mysite/settings.py +++ b/mysite/settings.py @@ -117,7 +117,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', diff --git a/mysite/urls.py b/mysite/urls.py index ebbc31d..f10343c 100644 --- a/mysite/urls.py +++ b/mysite/urls.py @@ -1,8 +1,8 @@ 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: @@ -13,5 +13,5 @@ # 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'^admin/', include(admin.site.urls)), ) diff --git a/polls/admin.py b/polls/admin.py new file mode 100644 index 0000000..e144212 --- /dev/null +++ b/polls/admin.py @@ -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) + + diff --git a/polls/models.py b/polls/models.py index f8cc376..bef1b4f 100644 --- a/polls/models.py +++ b/polls/models.py @@ -11,6 +11,9 @@ def __unicode__(self): def was_published_recently(self): return self.pub_date >= timezone.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): From 0c7b31e53cc742aa0ffab0573b8d0e3bd772446d Mon Sep 17 00:00:00 2001 From: Daniele Procida Date: Wed, 30 Jan 2013 23:52:19 +0000 Subject: [PATCH 3/9] end of tutorial 3 --- mysite/settings.py | 8 ++++++-- mysite/templates/admin/base_site.html | 10 ++++++++++ mysite/urls.py | 10 +--------- polls/templates/polls/detail.html | 6 ++++++ polls/templates/polls/index.html | 9 +++++++++ polls/urls.py | 15 +++++++++++++++ polls/views.py | 25 ++++++++++++++++++++++++- 7 files changed, 71 insertions(+), 12 deletions(-) create mode 100644 mysite/templates/admin/base_site.html create mode 100644 polls/templates/polls/detail.html create mode 100644 polls/templates/polls/index.html create mode 100644 polls/urls.py diff --git a/mysite/settings.py b/mysite/settings.py index be20f3e..121685b 100644 --- a/mysite/settings.py +++ b/mysite/settings.py @@ -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', diff --git a/mysite/templates/admin/base_site.html b/mysite/templates/admin/base_site.html new file mode 100644 index 0000000..0717edf --- /dev/null +++ b/mysite/templates/admin/base_site.html @@ -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 %} +

MySite Corp Global Industries {% trans 'Django administration' %}

+{% endblock %} + +{% block nav-global %}{% endblock %} diff --git a/mysite/urls.py b/mysite/urls.py index f10343c..b884696 100644 --- a/mysite/urls.py +++ b/mysite/urls.py @@ -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() 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'^polls/', include('polls.urls', namespace="polls")), url(r'^admin/', include(admin.site.urls)), ) diff --git a/polls/templates/polls/detail.html b/polls/templates/polls/detail.html new file mode 100644 index 0000000..fff52a7 --- /dev/null +++ b/polls/templates/polls/detail.html @@ -0,0 +1,6 @@ +

{{ poll.question }}

+
    +{% for choice in poll.choice_set.all %} +
  • {{ choice.choice_text }}
  • +{% endfor %} +
diff --git a/polls/templates/polls/index.html b/polls/templates/polls/index.html new file mode 100644 index 0000000..4c15ce4 --- /dev/null +++ b/polls/templates/polls/index.html @@ -0,0 +1,9 @@ +{% if latest_poll_list %} + +{% else %} +

No polls are available.

+{% endif %} diff --git a/polls/urls.py b/polls/urls.py new file mode 100644 index 0000000..eb8c809 --- /dev/null +++ b/polls/urls.py @@ -0,0 +1,15 @@ +from django.conf.urls import patterns, url +from django.http import Http404 + +from polls import views + +urlpatterns = patterns('', + # example: /polls/ + url(r'^$', views.index, name='index'), + # example: /polls/5/ + url(r'^(?P\d+)/$', views.detail, name='detail'), + # example: /polls/5/results/ + url(r'^(?P\d+)/results/$', views.results, name='results'), + # example: /polls/5/vote/ + url(r'^(?P\d+)/vote/$', views.vote, name='vote'), +) diff --git a/polls/views.py b/polls/views.py index 60f00ef..98493e3 100644 --- a/polls/views.py +++ b/polls/views.py @@ -1 +1,24 @@ -# Create your views here. +from django.shortcuts import render +from django.shortcuts import render, get_object_or_404 + +from polls.models import Poll + +def index(request): + latest_poll_list = Poll.objects.all().order_by('-pub_date')[:5] + context = {'latest_poll_list': latest_poll_list} + return render(request, 'polls/index.html', context) + +def index(request): + latest_poll_list = Poll.objects.all().order_by('-pub_date')[:5] + context = {'latest_poll_list': latest_poll_list} + return render(request, 'polls/index.html', context) + +def detail(request, poll_id): + poll = get_object_or_404(Poll, pk=poll_id) + return render(request, 'polls/detail.html', {'poll': poll}) + +def results(request, poll_id): + return HttpResponse("You're looking at the results of poll %s." % poll_id) + +def vote(request, poll_id): + return HttpResponse("You're voting on poll %s." % poll_id) From 05ec859a9134a8d9deacd999a2f9691eaff9c637 Mon Sep 17 00:00:00 2001 From: Daniele Procida Date: Wed, 8 May 2013 21:23:54 +0100 Subject: [PATCH 4/9] not sure... --- polls/templates/polls/detail.html | 12 ++++++--- polls/templates/polls/results.html | 9 +++++++ polls/testsettings.py | 13 ++++++++++ polls/urls.py | 30 ++++++++++++++-------- polls/views.py | 40 +++++++++++++++--------------- 5 files changed, 70 insertions(+), 34 deletions(-) create mode 100644 polls/templates/polls/results.html create mode 100644 polls/testsettings.py diff --git a/polls/templates/polls/detail.html b/polls/templates/polls/detail.html index fff52a7..15da227 100644 --- a/polls/templates/polls/detail.html +++ b/polls/templates/polls/detail.html @@ -1,6 +1,12 @@

{{ poll.question }}

-
    + +{% if error_message %}

    {{ error_message }}

    {% endif %} + +
    +{% csrf_token %} {% for choice in poll.choice_set.all %} -
  • {{ choice.choice_text }}
  • + +
    {% endfor %} -
+ + diff --git a/polls/templates/polls/results.html b/polls/templates/polls/results.html new file mode 100644 index 0000000..d6783ba --- /dev/null +++ b/polls/templates/polls/results.html @@ -0,0 +1,9 @@ +

{{ poll.question }}

+ +
    +{% for choice in poll.choice_set.all %} +
  • {{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}
  • +{% endfor %} +
+ +Vote again? diff --git a/polls/testsettings.py b/polls/testsettings.py new file mode 100644 index 0000000..b52b3f0 --- /dev/null +++ b/polls/testsettings.py @@ -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' diff --git a/polls/urls.py b/polls/urls.py index eb8c809..3127d0b 100644 --- a/polls/urls.py +++ b/polls/urls.py @@ -1,15 +1,23 @@ from django.conf.urls import patterns, url -from django.http import Http404 - -from polls import views +from django.views.generic import DetailView, ListView +from polls.models import Poll urlpatterns = patterns('', - # example: /polls/ - url(r'^$', views.index, name='index'), - # example: /polls/5/ - url(r'^(?P\d+)/$', views.detail, name='detail'), - # example: /polls/5/results/ - url(r'^(?P\d+)/results/$', views.results, name='results'), - # example: /polls/5/vote/ - url(r'^(?P\d+)/vote/$', views.vote, name='vote'), + 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\d+)/$', + DetailView.as_view( + model=Poll, + template_name='polls/detail.html'), + name='detail'), + url(r'^(?P\d+)/results/$', + DetailView.as_view( + model=Poll, + template_name='polls/results.html'), + name='results'), + url(r'^(?P\d+)/vote/$', 'polls.views.vote', name='vote'), ) diff --git a/polls/views.py b/polls/views.py index 98493e3..8706599 100644 --- a/polls/views.py +++ b/polls/views.py @@ -1,24 +1,24 @@ -from django.shortcuts import render -from django.shortcuts import render, get_object_or_404 +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 -from polls.models import Poll -def index(request): - latest_poll_list = Poll.objects.all().order_by('-pub_date')[:5] - context = {'latest_poll_list': latest_poll_list} - return render(request, 'polls/index.html', context) - -def index(request): - latest_poll_list = Poll.objects.all().order_by('-pub_date')[:5] - context = {'latest_poll_list': latest_poll_list} - return render(request, 'polls/index.html', context) - -def detail(request, poll_id): - poll = get_object_or_404(Poll, pk=poll_id) - return render(request, 'polls/detail.html', {'poll': poll}) - -def results(request, poll_id): - return HttpResponse("You're looking at the results of poll %s." % poll_id) def vote(request, poll_id): - return HttpResponse("You're voting on poll %s." % 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,))) From 7118d912e212cf04fae690f0fd460c54bc229b4d Mon Sep 17 00:00:00 2001 From: Daniele Procida Date: Wed, 8 May 2013 21:48:44 +0100 Subject: [PATCH 5/9] tutorial4 --- .gitignore | 47 +++++++++++++++++++++++++++++++++++++------- mysite/__init__.pyc | Bin 142 -> 0 bytes mysite/wsgi.pyc | Bin 1036 -> 0 bytes polls/__init__.pyc | Bin 141 -> 0 bytes polls/views.py | 7 +++++-- 5 files changed, 45 insertions(+), 9 deletions(-) delete mode 100644 mysite/__init__.pyc delete mode 100644 mysite/wsgi.pyc delete mode 100644 polls/__init__.pyc diff --git a/.gitignore b/.gitignore index 2d028c7..67e78e9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,41 @@ +# Numerous always-ignore extensions +*.pyc +*.diff +*.err +*.orig +*.log +*.rej +*.swo +*.swp +*.vi +*.cache *.egg-info -*.pot -*.py[co] -MANIFEST -dist/ -docs/_build/ -tests/coverage_html/ -tests/.coverage \ No newline at end of file +*~ +*# + +# 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 diff --git a/mysite/__init__.pyc b/mysite/__init__.pyc deleted file mode 100644 index 30808d05b6a32cfcccda206e1a9a33f031e6e04f..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 142 zcmZSn%*z#Pz!I3u00oRd+5w1*S%5?e14FO|NW@PANHCxg#pXaU{fzwFRQ;61yv)=b zAf1(%m!4lzT9RLsnV3_opIcd+S&|B);^Q;(GE3s)^$IFWIDmR>a`RJ4b5iX!CZv(z)@;0r-5m+m;4UsN^35A!YQ3CL$HV%RB2)tAQ5!T#FzEBuzUQx*`+hdVs z$v*;FW*gmA&N8YSV#%}23rXwTR2BJ%r*ydq?0h)u9vG}8Z_k=#fn|Dpad9^Ja6F%! zPbuG__fi+6(WPs+Qd`)X1~yVDDES)OMCYjjw_?ZXu4pA%d;yEbgY>|aGjS!0(nH0s zktz^-@CGc&!}AVuQyOYTC-Pmqjr1kyl{e0t3XDcN6!)OGM@tP+SkMd(}0Z*a@LtUZzVU`8R=H8f{m&}^C-v6fr@S}aFZMQ^&;`dJ LdN_E}KZ8s|mg diff --git a/polls/__init__.pyc b/polls/__init__.pyc deleted file mode 100644 index 20c92392d32b6f8d2d7724deee6df5e651c3d863..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 141 zcmZSn%*!R|$P$>$00oRd+5w1*S%5?e14FO|NW@PANHCxg#b!V;{fzwFRQ;61yv)=b zAf1(%m!4lzT9RLsnV3_opIcd+S(2(>ke>tO#K&jmWtPOp>lIX%Z~(R1 Date: Sat, 18 May 2013 11:36:20 +0000 Subject: [PATCH 6/9] Nicer formatting --- polls/models.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/polls/models.py b/polls/models.py index bef1b4f..7515905 100644 --- a/polls/models.py +++ b/polls/models.py @@ -2,6 +2,7 @@ import datetime from django.utils import timezone + class Poll(models.Model): question = models.CharField(max_length=200) pub_date = models.DateTimeField('date published') @@ -10,7 +11,9 @@ def __unicode__(self): return self.question def was_published_recently(self): - return self.pub_date >= timezone.now() - datetime.timedelta(days=1) + return self.pub_date >= ( + timezone.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?' @@ -20,6 +23,6 @@ class Choice(models.Model): poll = models.ForeignKey(Poll) choice_text = models.CharField(max_length=200) votes = models.IntegerField() - + def __unicode__(self): return self.choice_text From 5ef8f6fe26d5ffdf5e44e37d548e7aa006c54d83 Mon Sep 17 00:00:00 2001 From: Chris Reeves Date: Sat, 18 May 2013 11:40:33 +0000 Subject: [PATCH 7/9] Do not commit .db files --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 67e78e9..bb95219 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ # Numerous always-ignore extensions +*.db *.pyc *.diff *.err From 5a5af6e2bf53bf1395668909ea5902f78986a5c2 Mon Sep 17 00:00:00 2001 From: Chris Reeves Date: Sat, 18 May 2013 11:40:53 +0000 Subject: [PATCH 8/9] Failing test for testing publish state in future should return False --- polls/tests.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/polls/tests.py b/polls/tests.py index 501deb7..3ef08d1 100644 --- a/polls/tests.py +++ b/polls/tests.py @@ -5,12 +5,17 @@ Replace this with more appropriate tests for your application. """ +import datetime + from django.test import TestCase +from django.utils import timezone + +from polls.models import Poll + +class PollMethodsTest(TestCase): -class SimpleTest(TestCase): - def test_basic_addition(self): - """ - Tests that 1 + 1 always equals 2. - """ - self.assertEqual(1 + 1, 2) + def test_published_recently_with_future_poll(self): + future_poll = Poll(pub_date=timezone.now() + + datetime.timedelta(days=30)) + self.assertEqual(future_poll.was_published_recently(), False) From 4f4cf2896c6de69ba92494ad66e8aaeb29e8f6bc Mon Sep 17 00:00:00 2001 From: Chris Reeves Date: Sat, 18 May 2013 11:52:29 +0000 Subject: [PATCH 9/9] Passing test --- polls/models.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/polls/models.py b/polls/models.py index 7515905..3e4d58d 100644 --- a/polls/models.py +++ b/polls/models.py @@ -1,5 +1,6 @@ -from django.db import models import datetime + +from django.db import models from django.utils import timezone @@ -11,8 +12,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() + if self.pub_date > now: + return False + else: + return self.pub_date >= (now - datetime.timedelta(days=1)) was_published_recently.admin_order_field = 'pub_date' was_published_recently.boolean = True