view changes and tests
This commit is contained in:
parent
82b2af8d6b
commit
97dee49b4f
@ -10,8 +10,8 @@ class Question(models.Model):
|
|||||||
return self.question_text
|
return self.question_text
|
||||||
|
|
||||||
def was_published_recently(self):
|
def was_published_recently(self):
|
||||||
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
|
now = timezone.now()
|
||||||
|
return now - datetime.timedelta(days=1) <= self.pub_date <= now
|
||||||
|
|
||||||
class Choice(models.Model):
|
class Choice(models.Model):
|
||||||
question = models.ForeignKey(Question, on_delete=models.CASCADE)
|
question = models.ForeignKey(Question, on_delete=models.CASCADE)
|
||||||
|
12
LCAdmin/polls/templates/polls/detail.html
Normal file
12
LCAdmin/polls/templates/polls/detail.html
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
<form action="{% url 'polls:vote' question.id %}" method="post">
|
||||||
|
{% csrf_token %}
|
||||||
|
<fieldset>
|
||||||
|
<legend><h1>{{ question.question_text }}</h1></legend>
|
||||||
|
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
|
||||||
|
{% for choice in question.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 %}
|
||||||
|
</fieldset>
|
||||||
|
<input type="submit" value="Vote">
|
||||||
|
</form>
|
@ -1,6 +0,0 @@
|
|||||||
<h1>{{ question.question_text }}</h1>
|
|
||||||
<ul>
|
|
||||||
{% for choice in question.choice_set.all %}
|
|
||||||
<li>{{ choice.choice_text }}</li>
|
|
||||||
{% endfor %}
|
|
||||||
</ul>
|
|
9
LCAdmin/polls/templates/polls/results.html
Normal file
9
LCAdmin/polls/templates/polls/results.html
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
<h1>{{ question.question_text }}</h1>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
{% for choice in question.choice_set.all %}
|
||||||
|
<li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}</li>
|
||||||
|
{% endfor %}
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<a href="{% url 'polls:detail' question.id %}">Vote again?</a>
|
@ -1,3 +1,36 @@
|
|||||||
from django.test import TestCase
|
import datetime
|
||||||
|
|
||||||
# Create your tests here.
|
from django.test import TestCase
|
||||||
|
from django.utils import timezone
|
||||||
|
|
||||||
|
from .models import Question
|
||||||
|
|
||||||
|
|
||||||
|
class QuestionModelTests(TestCase):
|
||||||
|
|
||||||
|
def test_was_published_recently_with_future_question(self):
|
||||||
|
"""
|
||||||
|
was_published_recently() returns False for questions whose pub_date
|
||||||
|
is in the future.
|
||||||
|
"""
|
||||||
|
time = timezone.now() + datetime.timedelta(days=30)
|
||||||
|
future_question = Question(pub_date=time)
|
||||||
|
self.assertIs(future_question.was_published_recently(), False)
|
||||||
|
|
||||||
|
def test_was_published_recently_with_old_question(self):
|
||||||
|
"""
|
||||||
|
was_published_recently() returns False for questions whose pub_date
|
||||||
|
is older than 1 day.
|
||||||
|
"""
|
||||||
|
time = timezone.now() - datetime.timedelta(days=1, seconds=1)
|
||||||
|
old_question = Question(pub_date=time)
|
||||||
|
self.assertIs(old_question.was_published_recently(), False)
|
||||||
|
|
||||||
|
def test_was_published_recently_with_recent_question(self):
|
||||||
|
"""
|
||||||
|
was_published_recently() returns True for questions whose pub_date
|
||||||
|
is within the last day.
|
||||||
|
"""
|
||||||
|
time = timezone.now() - datetime.timedelta(hours=23, minutes=59, seconds=59)
|
||||||
|
recent_question = Question(pub_date=time)
|
||||||
|
self.assertIs(recent_question.was_published_recently(), True)
|
@ -1,10 +1,11 @@
|
|||||||
from django.urls import path
|
from django.urls import path
|
||||||
|
|
||||||
from . import views
|
from . import views
|
||||||
|
|
||||||
app_name = 'polls'
|
app_name = 'polls'
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('', views.index, name='index'),
|
path('', views.IndexView.as_view(), name='index'),
|
||||||
path('<int:question_id>/', views.detail, name='detail'),
|
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
|
||||||
path('<int:question_id>/results/', views.results, name='results'),
|
path('<int:pk>/results/', views.ResultsView.as_view(), name='results'),
|
||||||
path('<int:question_id>/vote/', views.vote, name='vote'),
|
path('<int:question_id>/vote/', views.vote, name='vote'),
|
||||||
]
|
]
|
@ -1,22 +1,47 @@
|
|||||||
from django.shortcuts import render
|
from django.http import HttpResponseRedirect
|
||||||
from django.http import HttpResponse
|
|
||||||
from django.shortcuts import get_object_or_404, render
|
from django.shortcuts import get_object_or_404, render
|
||||||
|
from django.urls import reverse
|
||||||
|
from django.views import generic
|
||||||
|
|
||||||
from .models import Question
|
from .models import Choice, Question
|
||||||
|
|
||||||
|
|
||||||
def index(request):
|
class IndexView(generic.ListView):
|
||||||
latest_question_list = Question.objects.order_by('-pub_date')[:5]
|
template_name = 'polls/index.html'
|
||||||
context = {'latest_question_list': latest_question_list}
|
context_object_name = 'latest_question_list'
|
||||||
return render(request, 'polls/index.html', context)
|
|
||||||
|
def get_queryset(self):
|
||||||
|
"""Return the last five published questions."""
|
||||||
|
return Question.objects.order_by('-pub_date')[:5]
|
||||||
|
|
||||||
|
|
||||||
|
class DetailView(generic.DetailView):
|
||||||
|
model = Question
|
||||||
|
template_name = 'polls/detail.html'
|
||||||
|
|
||||||
|
|
||||||
|
class ResultsView(generic.DetailView):
|
||||||
|
model = Question
|
||||||
|
template_name = 'polls/results.html'
|
||||||
|
|
||||||
def detail(request, question_id):
|
|
||||||
question = get_object_or_404(Question, pk=question_id)
|
|
||||||
return render(request, 'polls/detail.html', {'question': question})
|
|
||||||
|
|
||||||
def results(request, question_id):
|
|
||||||
response = "You're looking at the results of question %s."
|
|
||||||
return HttpResponse(response % question_id)
|
|
||||||
|
|
||||||
def vote(request, question_id):
|
def vote(request, question_id):
|
||||||
return HttpResponse("You're voting on question %s." % question_id)
|
question = get_object_or_404(Question, pk=question_id)
|
||||||
|
try:
|
||||||
|
## added type ignore to get rid of choice _set error
|
||||||
|
selected_choice = question.choice_set.get(pk=request.POST['choice']) # type: ignore
|
||||||
|
except (KeyError, Choice.DoesNotExist):
|
||||||
|
# Redisplay the question voting form.
|
||||||
|
return render(request, 'polls/detail.html', {
|
||||||
|
'question': question,
|
||||||
|
'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.
|
||||||
|
## type ignore bs
|
||||||
|
return HttpResponseRedirect(reverse('polls:results', args=(question.id,))) # type: ignore # type: ignore)
|
Loading…
Reference in New Issue
Block a user