Skip to content

Commit

Permalink
Specify return type of get_queryset
Browse files Browse the repository at this point in the history
  • Loading branch information
dmoisset committed Sep 2, 2016
1 parent 61a147c commit b18aa7b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
24 changes: 23 additions & 1 deletion polls/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,26 @@ def test_index_view_with_two_past_questions(self):
self.assertQuerysetEqual(
response.context['latest_question_list'],
['<Question: Past question 2.>', '<Question: Past question 1.>']
)
)


class QuestionIndexDetailTests(TestCase):
def test_detail_view_with_a_future_question(self):
"""
The detail view of a question with a pub_date in the future should
return a 404 not found.
"""
future_question = create_question(question_text='Future question.', days=5)
url = reverse('polls:detail', args=(future_question.id,))
response = self.client.get(url)
self.assertEqual(response.status_code, 404)

def test_detail_view_with_a_past_question(self):
"""
The detail view of a question with a pub_date in the past should
display the question's text.
"""
past_question = create_question(question_text='Past Question.', days=-5)
url = reverse('polls:detail', args=(past_question.id,))
response = self.client.get(url)
self.assertContains(response, past_question.question_text)
10 changes: 9 additions & 1 deletion polls/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from django.db.models.query import QuerySet

from django.http import HttpResponseRedirect, HttpRequest, HttpResponse
from django.shortcuts import render, get_object_or_404
from django.urls import reverse
Expand All @@ -11,7 +13,7 @@ class IndexView(generic.ListView):
template_name = 'polls/index.html'
context_object_name = 'latest_question_list'

def get_queryset(self):
def get_queryset(self) -> QuerySet:
"""
Return the last five published questions (not including those set to be
published in the future).
Expand All @@ -24,6 +26,12 @@ class DetailView(generic.DetailView):
model = Question
template_name = 'polls/detail.html'

def get_queryset(self) -> QuerySet:
"""
Excludes any questions that aren't published yet.
"""
return Question.objects.filter(pub_date__lte=timezone.now())


class ResultsView(generic.DetailView):
model = Question
Expand Down

0 comments on commit b18aa7b

Please sign in to comment.