Merge pull request #36 from Sosokker/iteration3

Iteration3 Merge Update Test + Readme + Vote view
This commit is contained in:
Sirin Puenggun 2023-09-14 23:52:00 +07:00 committed by GitHub
commit 73b9d3d2b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 643 additions and 271 deletions

View File

@ -5,8 +5,23 @@ An application to conduct online polls and surveys based
on the [Django Tutorial project](https://docs.djangoproject.com/en/4.2/intro/tutorial01/), with additional features.
## Install and Run
### Run Setup.py Method
1. Install [Python 3.11.4 or later](https://www.python.org/downloads/)
Clone this repository and Run `setup.py` to install and run the project
**Don't forget to answer the question from `setup.py` to setup the project**
```bash
git clone https://github.com/Sosokker/ku-polls
cd ku-polls
python setup.py
```
or run `setup.ps1` (For Windows User)
----
### Manual
1. Install [Python 3.11 or later](https://www.python.org/downloads/)
2. Run these commands to clone and install requirements.txt
```bash
git clone https://github.com/Sosokker/ku-polls
@ -33,8 +48,8 @@ or
4. Run these commands
```bash
python manage.py migrate
python manage.py loaddata data/polls.json
python manage.py loaddata data/users.json
python manage.py loaddata data/polls.json
python manage.py runserver
```
@ -83,6 +98,7 @@ python -m virtualenv .venv
|tester2|aa12345678aa|
|tester3|aa12345678aa|
|tester4|aa12345678aa|
|novote |aa12345678aa|
## Project Documents
@ -92,5 +108,6 @@ All project documents are in the [Project Wiki](https://github.com/Sosokker/ku-p
- [Requirements](https://github.com/Sosokker/ku-polls/wiki/Requirements)
- [Iteration1](https://github.com/Sosokker/ku-polls/wiki/Iteration-1-Plan)
- [Iteration2](https://github.com/Sosokker/ku-polls/wiki/Iteration-2-Plan)
- [Iteration3](https://github.com/Sosokker/ku-polls/wiki/Iteration-3-Plan)
[django-tutorial](https://docs.djangoproject.com/en/4.2/intro/tutorial01/)

View File

@ -4,7 +4,7 @@
"pk": 1,
"fields": {
"question_text": "Python vs C++, which one is better in your opinion?",
"pub_date": "2023-09-05T06:31:14Z",
"pub_date": "2023-09-11T06:31:14Z",
"end_date": "2023-09-29T20:31:49Z",
"short_description": "Cool kids have polls",
"long_description": "No description provide for this poll.",
@ -20,7 +20,7 @@
"fields": {
"question_text": "The chicken and the egg, which came first?",
"pub_date": "2023-09-11T02:50:04Z",
"end_date": "2023-09-19T23:50:19Z",
"end_date": "2023-10-18T23:50:19Z",
"short_description": "Cool kids have polls",
"long_description": "No description provide for this poll.",
"up_vote_count": 1,
@ -35,7 +35,7 @@
"fields": {
"question_text": "So far so good?",
"pub_date": "2023-08-03T06:50:43Z",
"end_date": "2023-11-15T19:50:53Z",
"end_date": "2023-11-28T19:50:53Z",
"short_description": "Cool kids have polls",
"long_description": "No description provide for this poll.",
"up_vote_count": 1,
@ -50,7 +50,7 @@
"fields": {
"question_text": "Do you love Django?",
"pub_date": "2023-09-11T19:51:12Z",
"end_date": "2023-09-13T17:51:18Z",
"end_date": "2023-09-29T17:51:18Z",
"short_description": "Cool kids have polls",
"long_description": "No description provide for this poll.",
"up_vote_count": 10,
@ -180,7 +180,7 @@
"model": "polls.vote",
"pk": 6,
"fields": {
"choice": 4,
"choice": 3,
"user": 2,
"question": 2
}
@ -238,5 +238,23 @@
"user": 3,
"question": 2
}
},
{
"model": "polls.vote",
"pk": 13,
"fields": {
"choice": 6,
"user": 6,
"question": 3
}
},
{
"model": "polls.sentimentvote",
"pk": 1,
"fields": {
"user": 1,
"question": 1,
"vote_types": false
}
}
]

View File

@ -4,7 +4,7 @@
"pk": 1,
"fields": {
"password": "pbkdf2_sha256$600000$aDh9a1PXxcXAb8z3YIjAPX$NVH24kt/wMad+0fZcCii738dfojI4vL2ffXOwNRuLz4=",
"last_login": "2023-09-12T04:02:42.758Z",
"last_login": "2023-09-14T16:45:03.576Z",
"is_superuser": true,
"username": "admin",
"first_name": "",
@ -22,7 +22,7 @@
"pk": 2,
"fields": {
"password": "pbkdf2_sha256$600000$quZKLKT8Ec3TQgpdqlCkpX$o+VOOnRDLGf64qjHb239Yvsre74tPkC8hw1qH1un/hk=",
"last_login": "2023-09-12T04:22:38.555Z",
"last_login": "2023-09-14T13:22:50.921Z",
"is_superuser": false,
"username": "tester1",
"first_name": "",
@ -40,7 +40,7 @@
"pk": 3,
"fields": {
"password": "pbkdf2_sha256$600000$1xGp6EDCoaljdTlSdVT1Mn$UID0Woeh8hwW7LtchH+hKzqdKTDeITTxQ/0DGvfG3CY=",
"last_login": "2023-09-11T19:57:39.303Z",
"last_login": "2023-09-12T07:09:55.381Z",
"is_superuser": false,
"username": "tester3",
"first_name": "",
@ -58,7 +58,7 @@
"pk": 4,
"fields": {
"password": "pbkdf2_sha256$600000$fJJcIwAuIESYwZDBOqBv8t$YEDVCgg/xJOqAOiAdvGvvqgi1jgn1YfYHJE9yx2JWTA=",
"last_login": "2023-09-11T19:55:41.583Z",
"last_login": "2023-09-14T11:23:08.948Z",
"is_superuser": false,
"username": "tester2",
"first_name": "",
@ -76,7 +76,7 @@
"pk": 5,
"fields": {
"password": "pbkdf2_sha256$600000$aHyU2gjOR6Vfsh3DBMIvQy$PZwRu+rOLc+N15DDguvy29dks6GUiN5YN/4io8b390o=",
"last_login": null,
"last_login": "2023-09-14T14:49:50.765Z",
"is_superuser": false,
"username": "novote",
"first_name": "",
@ -88,5 +88,23 @@
"groups": [],
"user_permissions": []
}
},
{
"model": "auth.user",
"pk": 6,
"fields": {
"password": "pbkdf2_sha256$600000$5rNKsClojvcsBqzrEmAzy5$XpeAUCrzeLG42H+8o4HBVqifKd0cQuWcEhFax/dxS5M=",
"last_login": "2023-09-14T16:44:29.087Z",
"is_superuser": false,
"username": "tester4",
"first_name": "",
"last_name": "",
"email": "",
"is_staff": false,
"is_active": true,
"date_joined": "2023-09-14T11:37:58.740Z",
"groups": [],
"user_permissions": []
}
}
]

View File

@ -0,0 +1,17 @@
# Generated by Django 4.2.4 on 2023-09-14 12:47
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('polls', '0010_sentimentvote'),
]
operations = [
migrations.RemoveField(
model_name='vote',
name='question',
),
]

View File

@ -0,0 +1,19 @@
# Generated by Django 4.2.4 on 2023-09-14 13:15
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('polls', '0011_remove_vote_question'),
]
operations = [
migrations.AddField(
model_name='vote',
name='question',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='polls.question'),
),
]

View File

@ -0,0 +1,19 @@
# Generated by Django 4.2.4 on 2023-09-14 13:15
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('polls', '0012_vote_question'),
]
operations = [
migrations.AlterField(
model_name='vote',
name='question',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='polls.question'),
),
]

View File

@ -172,8 +172,8 @@ class Question(models.Model):
vote.update(vote_types=True)
self.save()
else:
return 'already_upvoted'
return 'ok'
return False
return True
def downvote(self, user):
@ -187,8 +187,8 @@ class Question(models.Model):
vote.update(vote_types=False)
self.save()
else:
return 'already_downvoted'
return 'ok'
return False
return True
class Choice(models.Model):

View File

@ -6,10 +6,6 @@
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://cdn.tailwindcss.com"></script>
<script
src="https://unpkg.com/htmx.org@1.9.5"
integrity="sha384-xcuj3WpfgjlKF+FXhSQFQ0ZNr39ln+hwjN3npfM9VBnUskLolQAcN80McRIVOPuO"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src="{% static 'polls/js/detail.js' %}"></script>
<script src="{% static 'polls/base.css' %}"></script>

View File

@ -3,8 +3,13 @@
<!-- Navbar -->
<nav class="bg-blue-500 p-4 shadow-xl border-b-2 border-solid border-neutral-700">
<div class="container mx-auto flex items-center justify-between">
<div class="text-2xl font-bold text-white">📝KU POLL</div>
<div class="text-2xl font-bold text-white">
{% if user.is_authenticated %}
📝KU POLL | 👋 Hi! {{ user.username }}
{% else %}
📝KU POLL
{% endif %}
</div>
<!-- Button -->
<div>
{% comment %}

View File

@ -7,7 +7,7 @@
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-100 min-h-screen flex items-center justify-center">
<div class="bg-white p-8 shadow-md rounded-md w-96">
<div class="bg-white p-8 shadow-md rounded-md w-96 border-solid border-neutral-500 border-2">
<h2 class="text-3xl font-semibold text-center mb-6">Sign In</h2>
<form method="post">
{% csrf_token %}

View File

@ -7,7 +7,7 @@
</head>
<body class="bg-gray-100">
<div class="min-h-screen flex items-center justify-center">
<div class="bg-white p-8 shadow-md rounded-md w-96">
<div class="bg-white p-8 shadow-md rounded-md w-96 border-solid border-neutral-500 border-2">
<h2 class="text-2xl font-semibold mb-6">Sign Up</h2>
<form method="post">
{% csrf_token %}

View File

@ -1,224 +0,0 @@
import datetime
from django.test import TestCase
from django.utils import timezone
from django.urls import reverse
from django.contrib.auth.models import User
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)
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_default_pub_date(self):
"""
Questions with the default pub_date (now) are displayed on the index page.
"""
question = Question.objects.create(question_text="Default pub date question.")
response = self.client.get(reverse("polls:index"))
self.assertQuerySetEqual(
response.context["latest_question_list"],
[question],
)
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 True for questions that are published and
the current time is within the allowed voting period.
"""
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(), True)
def create_question(self, question_text, days, pub_date=None):
"""
Create a question with the given `question_text` and published the
given number of `days` offset to now (negative for questions published
in the past, positive for questions that have yet to be published).
"""
time = timezone.now() + timezone.timedelta(days=days)
if pub_date is not None:
time = pub_date
return Question.objects.create(question_text=question_text, pub_date=time)
class QuestionIndexViewTests(TestCase):
def test_no_questions(self):
"""
If no questions exist, an appropriate message is displayed.
"""
response = self.client.get(reverse("polls:index"))
self.assertEqual(response.status_code, 200)
self.assertQuerySetEqual(response.context["latest_question_list"], [])
def test_past_question(self):
"""
Questions with a pub_date in the past are displayed on the
index page.
"""
question = Question.objects.create(question_text="Past question.")
question.pub_date = timezone.now() - timezone.timedelta(days=30)
question.save()
response = self.client.get(reverse("polls:index"))
self.assertQuerySetEqual(
response.context["latest_question_list"],
[question],
)
def test_future_question(self):
"""
Questions with a pub_date in the future aren't displayed on
the index page.
"""
future_question = Question.objects.create(question_text="Future question.")
future_question.pub_date = timezone.now() + timezone.timedelta(days=30)
future_question.save()
response = self.client.get(reverse("polls:index"))
self.assertQuerySetEqual(response.context["latest_question_list"], [])
def test_future_question_and_past_question(self):
"""
Even if both past and future questions exist, only past questions
are displayed.
"""
past_question = Question.objects.create(question_text="Past question.")
past_question.pub_date = timezone.now() - timezone.timedelta(days=30)
past_question.save()
future_question = Question.objects.create(question_text="Future question.")
future_question.pub_date = timezone.now() + timezone.timedelta(days=30)
future_question.save()
response = self.client.get(reverse("polls:index"))
self.assertQuerySetEqual(
response.context["latest_question_list"],
[past_question],
)
def test_two_past_questions(self):
"""
The questions index page may display multiple questions.
"""
question1 = Question.objects.create(question_text="Past question 1.")
question1.pub_date = timezone.now() - timezone.timedelta(days=30)
question1.save()
question2 = Question.objects.create(question_text="Past question 2.")
question2.pub_date = timezone.now() - timezone.timedelta(days=5)
question2.save()
response = self.client.get(reverse("polls:index"))
self.assertQuerySetEqual(
response.context["latest_question_list"],
[question2, question1],
)
class QuestionDetailViewTests(TestCase):
def test_future_question(self):
"""
The detail view of a question with a pub_date in the future
returns a 404 not found.
"""
future_question = Question.objects.create(question_text="Future question.")
future_question.pub_date = timezone.now() + timezone.timedelta(days=5)
future_question.save()
user = User.objects.create_user(username="testcase", password="123test123")
self.client.login(username="testcase", password="123test123")
url = reverse("polls:detail", args=(future_question.id,))
response = self.client.get(url)
self.assertEqual(response.status_code, 404)
def test_past_question(self):
"""
The detail view of a question with a pub_date in the past
displays the question's text.
"""
past_question = Question.objects.create(question_text="Past Question.")
past_question.pub_date = timezone.now() - timezone.timedelta(days=5)
past_question.save()
user = User.objects.create_user(username="testcase", password="123test123")
self.client.login(username="testcase", password="123test123")
url = reverse("polls:detail", args=(past_question.id,))
response = self.client.get(url)
self.assertContains(response, past_question.question_text)

0
polls/tests/__init__.py Normal file
View File

14
polls/tests/base.py Normal file
View File

@ -0,0 +1,14 @@
from django.utils import timezone
from django.contrib.auth.models import User
from ..models import Question, Vote, Choice
def create_question(question_text, day=0):
"""
Create a question with the given `question_text` and published the
given number of `days` offset to now (negative for questions published
in the past, positive for questions that have yet to be published).
"""
time = timezone.now() + timezone.timedelta(days=day)
return Question.objects.create(question_text=question_text, pub_date=time)

View File

@ -0,0 +1,43 @@
from django.test import TestCase, Client
from django.utils import timezone
from django.urls import reverse
from django.contrib.auth.models import User
from .base import create_question
class QuestionDetailViewTests(TestCase):
@classmethod
def setUpTestData(cls):
cls.user = User.objects.create_user(username="test_user_123", password="aaa123321aaa")
cls.client = Client()
def test_future_question(self):
"""
The detail view of a question with a pub_date in the future
returns a 404 not found.
"""
future_question = create_question(question_text="Future question.", day=10)
future_question.save()
self.client.login(username=self.user.username, password="aaa123321aaa")
url = reverse("polls:detail", args=(future_question.id,))
respone = self.client.get(url)
self.assertEqual(respone.status_code, 404)
def test_past_question(self):
"""
The detail view of a question with a pub_date in the past
displays the question's text.
"""
past_question = create_question(question_text="Past Question.", day=-10)
past_question.save()
self.client.login(username=self.user.username, password='aaa123321aaa')
url = reverse("polls:detail", args=(past_question.id,))
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertContains(response, past_question.question_text)

View File

@ -0,0 +1,77 @@
from django.test import TestCase
from django.utils import timezone
from django.urls import reverse
from ..models import Question
class QuestionIndexViewTests(TestCase):
def test_no_questions(self):
"""
If no questions exist, an appropriate message is displayed.
"""
response = self.client.get(reverse("polls:index"))
self.assertEqual(response.status_code, 200)
self.assertQuerySetEqual(response.context["latest_question_list"], [])
def test_past_question(self):
"""
Questions with a pub_date in the past are displayed on the
index page.
"""
question = Question.objects.create(question_text="Past question.")
question.pub_date = timezone.now() - timezone.timedelta(days=30)
question.save()
response = self.client.get(reverse("polls:index"))
self.assertQuerySetEqual(
response.context["latest_question_list"],
[question],
)
def test_future_question(self):
"""
Questions with a pub_date in the future aren't displayed on
the index page.
"""
future_question = Question.objects.create(question_text="Future question.")
future_question.pub_date = timezone.now() + timezone.timedelta(days=30)
future_question.save()
response = self.client.get(reverse("polls:index"))
self.assertQuerySetEqual(response.context["latest_question_list"], [])
def test_future_question_and_past_question(self):
"""
Even if both past and future questions exist, only past questions
are displayed.
"""
past_question = Question.objects.create(question_text="Past question.")
past_question.pub_date = timezone.now() - timezone.timedelta(days=30)
past_question.save()
future_question = Question.objects.create(question_text="Future question.")
future_question.pub_date = timezone.now() + timezone.timedelta(days=30)
future_question.save()
response = self.client.get(reverse("polls:index"))
self.assertQuerySetEqual(
response.context["latest_question_list"],
[past_question],
)
def test_two_past_questions(self):
"""
The questions index page may display multiple questions.
"""
question1 = Question.objects.create(question_text="Past question 1.")
question1.pub_date = timezone.now() - timezone.timedelta(days=30)
question1.save()
question2 = Question.objects.create(question_text="Past question 2.")
question2.pub_date = timezone.now() - timezone.timedelta(days=5)
question2.save()
response = self.client.get(reverse("polls:index"))
self.assertQuerySetEqual(
response.context["latest_question_list"],
[question2, question1],
)

View File

@ -0,0 +1,105 @@
import datetime
from django.test import TestCase
from django.utils import timezone
from django.urls import reverse
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)
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_default_pub_date(self):
"""
Questions with the default pub_date (now) are displayed on the index page.
"""
question = Question.objects.create(question_text="Default pub date question.")
response = self.client.get(reverse("polls:index"))
self.assertQuerySetEqual(
response.context["latest_question_list"],
[question],
)
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 True for questions that are published and
the current time is within the allowed voting period.
"""
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(), True)

View File

@ -0,0 +1,35 @@
from django.contrib.auth.models import User
from django.test import TestCase
from django.urls import reverse
class SignUpTestCase(TestCase):
def test_signup_view(self):
"""Test Sign Up view Load correctly or not"""
signup_url = reverse("polls:signup")
response = self.client.get(signup_url)
self.assertEqual(response.status_code, 200)
def test_signup_success(self):
"""Test the signup System, is it work or not."""
signup_url = reverse('polls:signup')
data = {
'username': 'testuser',
'password1': 'testpassword123',
'password2': 'testpassword123',
}
response = self.client.post(signup_url, data)
self.assertEqual(response.status_code, 302)
self.assertTrue(User.objects.filter(username='testuser').exists())
def test_signup_validation_error(self):
"""Test for data validation of Sign Up form"""
signup_url = reverse('polls:signup')
data = {
'username': '',
'password1': 'testpassword123',
'password2': 'testpassword123',
}
response = self.client.post(signup_url, data)
self.assertEqual(response.status_code, 200)
self.assertFalse(User.objects.filter(username='').exists())

View File

@ -0,0 +1,86 @@
from django.test import TestCase, Client
from django.urls import reverse
from django.contrib.auth.models import User
from .base import create_question
from ..models import Vote, Choice
class VoteViewTest(TestCase):
@classmethod
def setUpTestData(cls):
cls.question = create_question(question_text="Test Question", day=0)
cls.choice1 = Choice.objects.create(question=cls.question, choice_text="Test Choice 1")
cls.choice2 = Choice.objects.create(question=cls.question, choice_text="Test Choice 2")
cls.user = User.objects.create_user(username="test_user_123", password="aaa123321aaa")
cls.client = Client()
def test_vote_with_valid_choice(self):
"""
Test the vote view with a valid choice selection.
"""
self.client.login(username=self.user.username, password="aaa123321aaa")
response = self.client.post(reverse("polls:vote", args=(self.question.id,)),
{'choice' : self.choice1.id})
self.assertRedirects(response, reverse("polls:results", args=(self.question.id,)))
self.assertTrue(Vote.objects.filter(user=self.user, question=self.question).exists())
def test_vote_with_invalid_choice(self):
"""
Test the vote view with an invalid choice selection.
"""
self.client.login(username=self.user.username, password="aaa123321aaa")
response = self.client.post(reverse("polls:vote", args=(self.question.id,)),
{'choice' : 1000})
self.assertRedirects(response, reverse('polls:detail', args=(self.question.id,)))
def test_vote_without_login(self):
"""
Test the vote view when the user is not logged in.
"""
response = self.client.post(reverse("polls:vote", args=(self.question.id,)),
{'choice' : self.choice1})
self.assertRedirects(response, "/accounts/login/?next=/polls/1/vote/")
def test_vote_voting_not_allowed(self):
"""
Test the vote view when voting is not allowed for the question.
"""
self.client.login(username=self.user.username, password="aaa123321aaa")
self.question_2 = create_question(question_text="Test not allow", day=10)
self.choice_2 = Choice.objects.create(question=self.question_2, choice_text="Test Choice 2_2")
response = self.client.post(reverse("polls:vote", args=(self.question_2.id,)),
{"choice" : self.choice_2.id})
self.assertRedirects(response, reverse('polls:index'))
def test_vote_with_no_post_data(self):
"""
Test the vote view when vote with no post data.
"""
self.client.login(username=self.user.username, password="aaa123321aaa")
response = self.client.post(reverse("polls:vote", args=(self.question.id,)))
self.assertRedirects(response, reverse('polls:detail', args=(self.question.id,)))
def test_update_vote_when_vote_on_new_choice(self):
"""
Test the vote when same user vote on same question but change the choice.
"""
self.client.login(username=self.user.username, password="aaa123321aaa")
response_1 = self.client.post(reverse("polls:vote", args=(self.question.id,)), {"choice": self.choice1.id})
self.assertRedirects(response_1, reverse('polls:results', args=(self.question.id,)))
response_2 = self.client.post(reverse("polls:vote", args=(self.question.id,)), {"choice": self.choice2.id})
self.assertRedirects(response_2, reverse('polls:results', args=(self.question.id,)))
self.assertFalse(Vote.objects.filter(user=self.user, question=self.question, choice=self.choice1).exists())
self.assertTrue(Vote.objects.filter(user=self.user, question=self.question, choice=self.choice2).exists())

View File

@ -25,7 +25,7 @@ class IndexView(generic.ListView):
"""
now = timezone.now()
return Question.objects.filter(
Q(pub_date__lte=now) & (Q(end_date__gte=now) | Q(end_date=None))
Q(pub_date__lte=now) & ((Q(end_date__gte=now) | Q(end_date=None)))
).order_by("-pub_date")
@ -42,7 +42,10 @@ class DetailView(LoginRequiredMixin, generic.DetailView):
"""
Excludes any questions that aren't published yet.
"""
return Question.objects.filter(pub_date__lte=timezone.now())
now = timezone.now()
return Question.objects.filter(
Q(pub_date__lte=now) & (Q(end_date__gte=now) | Q(end_date=None))
).order_by("-pub_date")
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
@ -88,6 +91,7 @@ class SignUpView(generic.CreateView):
success_url = reverse_lazy('login')
template_name = 'registration/signup.html'
@login_required
def vote(request, question_id):
"""
@ -95,29 +99,31 @@ def vote(request, question_id):
in a specific question_id.
"""
question = get_object_or_404(Question, pk=question_id)
try:
selected_choice = question.choice_set.get(pk=request.POST["choice"])
except (KeyError, Choice.DoesNotExist):
messages.error(request, "You didn't select a choice.")
return render(request, "polls/detail.html", {"question": question})
else:
if request.method == "POST":
try:
selected_choice = question.choice_set.get(pk=request.POST["choice"])
except (KeyError, Choice.DoesNotExist):
messages.error(request, "You didn't select a choice.")
return redirect("polls:detail", question_id)
if question.can_vote():
if request.method == "POST" and "vote-button" in request.POST:
if Vote.objects.filter(user=request.user, question=question).exists():
old_vote = question.vote_set.get(user=request.user)
old_vote.choice = selected_choice
old_vote.save()
# ! Return 1. object element 2. boolean status of creation
vote, created = Vote.objects.update_or_create(
user=request.user,
question=question,
defaults={'choice' : selected_choice}
)
messages.success(request, "You vote successfully🥳")
return HttpResponseRedirect(reverse("polls:results", args=(question.id,)))
else:
Vote.objects.create(choice=selected_choice, user=request.user, question=question).save()
messages.success(request, "You vote successfully🥳")
return HttpResponseRedirect(reverse("polls:results", args=(question.id,)))
if created:
messages.success(request, "You voted successfully🥳")
else:
messages.error(request, "You cannot vote by typing the URL.")
return render(request, "polls/detail.html", {"question": question})
messages.success(request, "You updated your vote🥳")
return redirect("polls:results", question_id)
else:
messages.error(request, "You can not vote on this question.")
return HttpResponseRedirect(reverse("polls:index"))
messages.error(request, "You cannot vote on this question.")
return redirect("polls:index")
else:
messages.error(request, "Invalid request method.")
return redirect("polls:index")

59
setup.ps1 Normal file
View File

@ -0,0 +1,59 @@
$python_command = (Get-Command python.exe -ErrorAction SilentlyContinue).Source
if ($python_command -eq $null) {
Write-Host "Error: The Python interpreter 'python.exe' is not found in your PATH."
exit 1
}
if (-not (Test-Path $python_command)) {
Write-Host "Error: The specified Python executable path '$python_command' does not exist."
exit 1
}
if (-not (Test-Path .venv)) {
Write-Host "Creating a new virtual environment..."
python -m venv .venv
.\.venv\Scripts\Activate
} else {
Write-Host "Using existing virtual environment."
}
if ($setup_venv -eq "yes") {
if (-not (Test-Path (Get-Command virtualenv -ErrorAction SilentlyContinue))) {
Write-Host "Error: virtualenv is not installed. Please install it and rerun this script."
exit 1
}
python -m venv .venv
.\.venv\Scripts\Activate
}
python -m pip install -r requirements.txt
$secret_key = (python manage.py shell -c 'from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())')
@"
SECRET_KEY=$secret_key
DEBUG=False
ALLOWED_HOSTS=*.ku.th,localhost,127.0.0.1,::1
TIME_ZONE=Asia/Bangkok
EMAIL_HOST_PASSWORD=ineedmorebullets
"@ | Set-Content -Path .env
$text = @"
Django is now running in insecure mode for the static files gathering reason.
You can stop the server and run it again
"@
$boxWidth = ($text | Measure-Object -Property Length -Maximum).Maximum + 4
$topBorder = '+' + ('-' * ($boxWidth - 2)) + '+'
$sideBorder = '| ' + $text + (' ' * ($boxWidth - $text.Length - 4)) + ' |'
$bottomBorder = '+' + ('-' * ($boxWidth - 2)) + '+'
Write-Host $topBorder
Write-Host $sideBorder
Write-Host $bottomBorder
python manage.py migrate
python manage.py loaddata data/users.json
python manage.py loaddata data/polls.json
python manage.py runserver --insecure

62
setup.py Normal file
View File

@ -0,0 +1,62 @@
import os
import subprocess
import sys
is_windows = os.name == 'nt'
is_posix = os.name == 'posix'
def check_python_command():
python_commands = ["python", "py", "python3"]
for command in python_commands:
try:
subprocess.check_output([command, "--version"])
return command
except FileNotFoundError:
continue
return None
python_command = check_python_command()
if python_command is None:
print("Error: Python interpreter not found. Please specify the Python command (e.g., python, py, python3).")
sys.exit(1)
setup_venv = input("Do you want to set up a virtual environment? (yes/no): ").lower()
if setup_venv == "yes":
if not os.path.exists(".venv"):
print("Creating a new virtual environment...")
subprocess.run([python_command, "-m", "venv", ".venv"])
else:
print("Using an existing virtual environment.")
if is_posix:
activate_command = os.path.join(".venv", "bin", "activate")
elif is_windows:
activate_command = os.path.join(".venv", "Scripts", "activate")
subprocess.run([activate_command], shell=True)
subprocess.run([python_command, "-m", "pip", "install", "-r", "requirements.txt"])
secret_key = subprocess.check_output([python_command, "manage.py", "shell", "-c",
'from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())']).decode().strip()
with open(".env", "w") as env_file:
env_file.write(f"""SECRET_KEY={secret_key}
DEBUG=False
ALLOWED_HOSTS=*.ku.th,localhost,127.0.0.1,::1
TIME_ZONE=Asia/Bangkok
EMAIL_HOST_PASSWORD=temppassword
""")
subprocess.run([python_command, "manage.py", "migrate"])
subprocess.run([python_command, "manage.py", "loaddata", "data/users.json"])
subprocess.run([python_command, "manage.py", "loaddata", "data/polls.json"])
start_server = input("Do you want to start the Django server? (yes/no): ").lower()
if start_server == "yes":
print("=================================================")
print("Django run in --insecure mode to load Static File")
print("==================================================")
subprocess.run([python_command, "manage.py", "runserver", "--insecure"])