Add docstring to classes and methods and clean the code.

This commit is contained in:
sosokker 2023-08-28 23:04:08 +07:00
parent 477903dfd3
commit 9f3aba25cd
6 changed files with 72 additions and 3 deletions

View File

@ -20,3 +20,4 @@ class QuestionAdmin(admin.ModelAdmin):
admin.site.register(Question, QuestionAdmin)

View File

@ -4,3 +4,4 @@ from django.apps import AppConfig
class PollsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'polls'

View File

@ -1,3 +1,14 @@
"""
This module defines the models for the polls app.
It includes the Question and Choice models, which represent poll questions
and the choices associated with them. These models are used to store and
get poll data in the database.
Attributes:
None
"""
import datetime
from django.db import models
@ -6,10 +17,24 @@ from django.contrib import admin
class Question(models.Model):
"""
Represents a poll question.
Attributes:
question_text (str): The text of the poll question.
pub_date (datetime): The date and time when the question was published.
"""
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField("date published")
def was_published_recently(self):
"""
Checks if the question was published recently or not.
Returns:
bool: True if the question was published within the last day, else False.
"""
now = timezone.now()
return now - datetime.timedelta(days=1) <= self.pub_date <= now
@ -23,13 +48,29 @@ class Question(models.Model):
return now - datetime.timedelta(days=1) <= self.pub_date <= now
def __str__(self):
"""
Returns a string representation of the question.
"""
return self.question_text
class Choice(models.Model):
"""
Represents a choice for a poll question.
Attributes:
question (Question): The poll question to which the choice belongs.
choice_text (str): The text of the choice.
votes (int): The number of votes the choice has received.
"""
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
def __str__(self):
"""
Returns a string representation of the choice.
"""
return self.choice_text

View File

@ -126,3 +126,4 @@ class QuestionDetailViewTests(TestCase):
url = reverse("polls:detail", args=(past_question.id,))
response = self.client.get(url)
self.assertContains(response, past_question.question_text)

View File

@ -9,3 +9,4 @@ urlpatterns = [
path("<int:pk>/results/", views.ResultsView.as_view(), name="results"),
path("<int:question_id>/vote/", views.vote, name="vote"),
]

View File

@ -9,6 +9,10 @@ from .models import Choice, Question
class HomeView(TemplateView):
"""
Provide a view for Home page(first page).
"""
template_name = 'polls/home.html'
def get_context_data(self, **kwargs):
@ -19,6 +23,10 @@ class HomeView(TemplateView):
class IndexView(generic.ListView):
"""
Provide a view for Index page that list all polls.
"""
template_name = "polls/index.html"
context_object_name = "latest_question_list"
@ -33,6 +41,11 @@ class IndexView(generic.ListView):
class DetailView(generic.DetailView):
"""
Provide a view for detail page, a detail for each poll contain poll question
and poll choices.
"""
model = Question
template_name = "polls/detail.html"
@ -42,12 +55,22 @@ class DetailView(generic.DetailView):
"""
return Question.objects.filter(pub_date__lte=timezone.now())
class ResultsView(generic.DetailView):
"""
Provide a view for result page that show up when user submit on of the choices.
"""
model = Question
template_name = "polls/results.html"
def vote(request, question_id):
"""
A function that update the database. Add vote count to choice that user vote
in specific question_id.
"""
question = get_object_or_404(Question, pk=question_id)
try:
selected_choice = question.choice_set.get(pk=request.POST["choice"])
@ -64,3 +87,4 @@ def vote(request, question_id):
selected_choice.votes += 1
selected_choice.save()
return HttpResponseRedirect(reverse("polls:results", args=(question.id,)))