diff --git a/polls/admin.py b/polls/admin.py index f7f2a32..c2ce0aa 100644 --- a/polls/admin.py +++ b/polls/admin.py @@ -19,4 +19,5 @@ class QuestionAdmin(admin.ModelAdmin): search_fields = ["question_text"] -admin.site.register(Question, QuestionAdmin) \ No newline at end of file +admin.site.register(Question, QuestionAdmin) + \ No newline at end of file diff --git a/polls/apps.py b/polls/apps.py index 5a5f94c..1dffd05 100644 --- a/polls/apps.py +++ b/polls/apps.py @@ -4,3 +4,4 @@ from django.apps import AppConfig class PollsConfig(AppConfig): default_auto_field = 'django.db.models.BigAutoField' name = 'polls' + \ No newline at end of file diff --git a/polls/models.py b/polls/models.py index 1528cf3..7252b32 100644 --- a/polls/models.py +++ b/polls/models.py @@ -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): - return self.choice_text \ No newline at end of file + """ + Returns a string representation of the choice. + """ + return self.choice_text + \ No newline at end of file diff --git a/polls/tests.py b/polls/tests.py index 17d37ca..410f88f 100644 --- a/polls/tests.py +++ b/polls/tests.py @@ -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) + \ No newline at end of file diff --git a/polls/urls.py b/polls/urls.py index 45ebddd..6d2b663 100644 --- a/polls/urls.py +++ b/polls/urls.py @@ -9,3 +9,4 @@ urlpatterns = [ path("/results/", views.ResultsView.as_view(), name="results"), path("/vote/", views.vote, name="vote"), ] + \ No newline at end of file diff --git a/polls/views.py b/polls/views.py index 320229c..6180804 100644 --- a/polls/views.py +++ b/polls/views.py @@ -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"]) @@ -63,4 +86,5 @@ def vote(request, question_id): else: selected_choice.votes += 1 selected_choice.save() - return HttpResponseRedirect(reverse("polls:results", args=(question.id,))) \ No newline at end of file + return HttpResponseRedirect(reverse("polls:results", args=(question.id,))) + \ No newline at end of file