mirror of
https://github.com/Sosokker/ku-polls.git
synced 2025-12-19 05:24:05 +01:00
Add docstring to classes and methods and clean the code.
This commit is contained in:
parent
477903dfd3
commit
9f3aba25cd
@ -19,4 +19,5 @@ class QuestionAdmin(admin.ModelAdmin):
|
|||||||
search_fields = ["question_text"]
|
search_fields = ["question_text"]
|
||||||
|
|
||||||
|
|
||||||
admin.site.register(Question, QuestionAdmin)
|
admin.site.register(Question, QuestionAdmin)
|
||||||
|
|
||||||
@ -4,3 +4,4 @@ from django.apps import AppConfig
|
|||||||
class PollsConfig(AppConfig):
|
class PollsConfig(AppConfig):
|
||||||
default_auto_field = 'django.db.models.BigAutoField'
|
default_auto_field = 'django.db.models.BigAutoField'
|
||||||
name = 'polls'
|
name = 'polls'
|
||||||
|
|
||||||
@ -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
|
import datetime
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
@ -6,10 +17,24 @@ from django.contrib import admin
|
|||||||
|
|
||||||
|
|
||||||
class Question(models.Model):
|
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)
|
question_text = models.CharField(max_length=200)
|
||||||
pub_date = models.DateTimeField("date published")
|
pub_date = models.DateTimeField("date published")
|
||||||
|
|
||||||
def was_published_recently(self):
|
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()
|
now = timezone.now()
|
||||||
return now - datetime.timedelta(days=1) <= self.pub_date <= 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
|
return now - datetime.timedelta(days=1) <= self.pub_date <= now
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
|
"""
|
||||||
|
Returns a string representation of the question.
|
||||||
|
"""
|
||||||
return self.question_text
|
return self.question_text
|
||||||
|
|
||||||
|
|
||||||
class Choice(models.Model):
|
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)
|
question = models.ForeignKey(Question, on_delete=models.CASCADE)
|
||||||
choice_text = models.CharField(max_length=200)
|
choice_text = models.CharField(max_length=200)
|
||||||
votes = models.IntegerField(default=0)
|
votes = models.IntegerField(default=0)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.choice_text
|
"""
|
||||||
|
Returns a string representation of the choice.
|
||||||
|
"""
|
||||||
|
return self.choice_text
|
||||||
|
|
||||||
@ -126,3 +126,4 @@ class QuestionDetailViewTests(TestCase):
|
|||||||
url = reverse("polls:detail", args=(past_question.id,))
|
url = reverse("polls:detail", args=(past_question.id,))
|
||||||
response = self.client.get(url)
|
response = self.client.get(url)
|
||||||
self.assertContains(response, past_question.question_text)
|
self.assertContains(response, past_question.question_text)
|
||||||
|
|
||||||
@ -9,3 +9,4 @@ urlpatterns = [
|
|||||||
path("<int:pk>/results/", views.ResultsView.as_view(), 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"),
|
||||||
]
|
]
|
||||||
|
|
||||||
@ -9,6 +9,10 @@ from .models import Choice, Question
|
|||||||
|
|
||||||
|
|
||||||
class HomeView(TemplateView):
|
class HomeView(TemplateView):
|
||||||
|
"""
|
||||||
|
Provide a view for Home page(first page).
|
||||||
|
"""
|
||||||
|
|
||||||
template_name = 'polls/home.html'
|
template_name = 'polls/home.html'
|
||||||
|
|
||||||
def get_context_data(self, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
@ -19,6 +23,10 @@ class HomeView(TemplateView):
|
|||||||
|
|
||||||
|
|
||||||
class IndexView(generic.ListView):
|
class IndexView(generic.ListView):
|
||||||
|
"""
|
||||||
|
Provide a view for Index page that list all polls.
|
||||||
|
"""
|
||||||
|
|
||||||
template_name = "polls/index.html"
|
template_name = "polls/index.html"
|
||||||
context_object_name = "latest_question_list"
|
context_object_name = "latest_question_list"
|
||||||
|
|
||||||
@ -33,6 +41,11 @@ class IndexView(generic.ListView):
|
|||||||
|
|
||||||
|
|
||||||
class DetailView(generic.DetailView):
|
class DetailView(generic.DetailView):
|
||||||
|
"""
|
||||||
|
Provide a view for detail page, a detail for each poll contain poll question
|
||||||
|
and poll choices.
|
||||||
|
"""
|
||||||
|
|
||||||
model = Question
|
model = Question
|
||||||
template_name = "polls/detail.html"
|
template_name = "polls/detail.html"
|
||||||
|
|
||||||
@ -42,12 +55,22 @@ class DetailView(generic.DetailView):
|
|||||||
"""
|
"""
|
||||||
return Question.objects.filter(pub_date__lte=timezone.now())
|
return Question.objects.filter(pub_date__lte=timezone.now())
|
||||||
|
|
||||||
|
|
||||||
class ResultsView(generic.DetailView):
|
class ResultsView(generic.DetailView):
|
||||||
|
"""
|
||||||
|
Provide a view for result page that show up when user submit on of the choices.
|
||||||
|
"""
|
||||||
|
|
||||||
model = Question
|
model = Question
|
||||||
template_name = "polls/results.html"
|
template_name = "polls/results.html"
|
||||||
|
|
||||||
|
|
||||||
def vote(request, question_id):
|
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)
|
question = get_object_or_404(Question, pk=question_id)
|
||||||
try:
|
try:
|
||||||
selected_choice = question.choice_set.get(pk=request.POST["choice"])
|
selected_choice = question.choice_set.get(pk=request.POST["choice"])
|
||||||
@ -63,4 +86,5 @@ def vote(request, question_id):
|
|||||||
else:
|
else:
|
||||||
selected_choice.votes += 1
|
selected_choice.votes += 1
|
||||||
selected_choice.save()
|
selected_choice.save()
|
||||||
return HttpResponseRedirect(reverse("polls:results", args=(question.id,)))
|
return HttpResponseRedirect(reverse("polls:results", args=(question.id,)))
|
||||||
|
|
||||||
Loading…
Reference in New Issue
Block a user