Add is_published, can_vote to QuestionModel/ Add test for can_vote, is_published

is_published -> Check if the specific question able to be published
can_vote -> Can vote or not, check it from pub time

Delete some unnecessary files
This commit is contained in:
sosokker 2023-09-05 21:25:04 +07:00
parent 4853f8d4fa
commit e0c7995f28
7 changed files with 115 additions and 4 deletions

View File

@ -0,0 +1,24 @@
# Generated by Django 4.2.4 on 2023-09-05 13:47
from django.db import migrations, models
import django.utils.timezone
class Migration(migrations.Migration):
dependencies = [
('polls', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='question',
name='end_date',
field=models.DateTimeField(null=True, verbose_name='date ended'),
),
migrations.AlterField(
model_name='question',
name='pub_date',
field=models.DateTimeField(default=django.utils.timezone.now, verbose_name='date published'),
),
]

View File

@ -26,7 +26,8 @@ class Question(models.Model):
""" """
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", default=timezone.now)
end_date = models.DateTimeField("date ended", null=True)
def was_published_recently(self): def was_published_recently(self):
""" """
@ -53,6 +54,28 @@ class Question(models.Model):
""" """
return self.question_text return self.question_text
def is_published(self):
"""
Checks if the question is published or not.
Returns:
bool: True if the question is published, else False.
"""
now = timezone.now()
return now >= self.pub_date
def can_vote(self):
"""
Checks if the question can be voted on or not.
Returns:
bool: True if the question is published and not ended, else False.
"""
now = timezone.now()
if self.end_date is None:
return self.pub_date <= now
return self.pub_date <= now <= self.end_date
class Choice(models.Model): class Choice(models.Model):
""" """

View File

@ -10,7 +10,7 @@
</section> </section>
<section class="polls-section"> <section class="polls-section">
<h2>Recent Polls</h2> <h2>Recent Polls</h2>
<p class="total-polls">Total number of polls: {{ total_polls }}</p> <p class="total-polls">Total number of polls: {{ total_open_polls }}</p>
<div class="poll-cards"> <div class="poll-cards">
{% if latest_question_list %} {% if latest_question_list %}
{% for question in latest_question_list %} {% for question in latest_question_list %}

View File

@ -37,6 +37,64 @@ class QuestionModelTests(TestCase):
self.assertIs(recent_question.was_published_recently(), True) self.assertIs(recent_question.was_published_recently(), True)
def test_is_published_with_future_question(self):
"""
is_published() should return False for questions whos pub_date is in the
future.
"""
future_date = timezone.now() + datetime.timedelta(days=30)
future_question = Question(pub_date=future_date)
self.assertIs(future_question.is_published(), False)
def test_is_published_with_past_question(self):
"""
is_published() should return True for questions whose pub_date is in the
past.
"""
past_date = timezone.now() - datetime.timedelta(days=1)
past_question = Question(pub_date=past_date)
self.assertIs(past_question.is_published(), True)
def test_can_vote_with_question_not_ended(self):
"""
can_vote() should return True for questions that are published and have not
ended.
"""
pub_date = timezone.now() - datetime.timedelta(hours=1)
end_date = timezone.now() + datetime.timedelta(hours=1)
question = Question(pub_date=pub_date, end_date=end_date)
self.assertIs(question.can_vote(), True)
def test_can_vote_with_question_ended(self):
"""
can_vote() should return False for questions that are published but have
ended.
"""
pub_date = timezone.now() - datetime.timedelta(hours=2)
end_date = timezone.now() - datetime.timedelta(hours=1)
question = Question(pub_date=pub_date, end_date=end_date)
self.assertIs(question.can_vote(), False)
def test_can_vote_with_question_no_end_date(self):
"""
can_vote() should return True for questions that are published and have no
specified end date.
"""
pub_date = timezone.now() - datetime.timedelta(hours=1)
question = Question(pub_date=pub_date, end_date=None)
self.assertIs(question.can_vote(), True)
def test_can_vote_with_question_ending_in_future(self):
"""
can_vote() should return False for questions that are published but have
an end date in the future.
"""
pub_date = timezone.now() - datetime.timedelta(hours=1)
end_date = timezone.now() + datetime.timedelta(hours=2)
question = Question(pub_date=pub_date, end_date=end_date)
self.assertIs(question.can_vote(), False)
def create_question(question_text, days): def create_question(question_text, days):
""" """
Create a question with the given `question_text` and published the Create a question with the given `question_text` and published the

View File

@ -17,8 +17,14 @@ class HomeView(TemplateView):
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
context['latest_question_list'] = Question.objects.filter(pub_date__lte=timezone.now()).order_by("-pub_date")[:5] all_questions = Question.objects.all()
context['total_polls'] = Question.objects.count() #* Check if the question is published and can be voted. Then, sort by pub_date
published_questions = [q for q in all_questions if q.is_published() and q.can_vote()]
latest_published_questions = sorted(published_questions, key=lambda q: q.pub_date, reverse=True)[:5]
context['latest_question_list'] = latest_published_questions
context['total_open_polls'] = sum(1 for q in published_questions if q.end_date is None)
context['total_polls'] = all_questions.count()
return context return context

Binary file not shown.