diff --git a/README.md b/README.md index 865fbf9..c0c51cb 100644 --- a/README.md +++ b/README.md @@ -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/) diff --git a/data/polls.json b/data/polls.json index 4935eaa..d4dcbcd 100644 --- a/data/polls.json +++ b/data/polls.json @@ -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 + } } ] diff --git a/data/users.json b/data/users.json index cc88812..5f4166d 100644 --- a/data/users.json +++ b/data/users.json @@ -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": [] + } } ] diff --git a/polls/migrations/0011_remove_vote_question.py b/polls/migrations/0011_remove_vote_question.py new file mode 100644 index 0000000..ab6c9e9 --- /dev/null +++ b/polls/migrations/0011_remove_vote_question.py @@ -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', + ), + ] diff --git a/polls/migrations/0012_vote_question.py b/polls/migrations/0012_vote_question.py new file mode 100644 index 0000000..4b4c620 --- /dev/null +++ b/polls/migrations/0012_vote_question.py @@ -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'), + ), + ] diff --git a/polls/migrations/0013_alter_vote_question.py b/polls/migrations/0013_alter_vote_question.py new file mode 100644 index 0000000..9bb9277 --- /dev/null +++ b/polls/migrations/0013_alter_vote_question.py @@ -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'), + ), + ] diff --git a/polls/models.py b/polls/models.py index a9c0508..10be79f 100644 --- a/polls/models.py +++ b/polls/models.py @@ -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): diff --git a/polls/templates/polls/base.html b/polls/templates/polls/base.html index fc6124b..fa30709 100644 --- a/polls/templates/polls/base.html +++ b/polls/templates/polls/base.html @@ -6,10 +6,6 @@ - diff --git a/polls/templates/polls/index.html b/polls/templates/polls/index.html index d870900..fb8fdb2 100644 --- a/polls/templates/polls/index.html +++ b/polls/templates/polls/index.html @@ -3,8 +3,13 @@ - 📝KU POLL - + + {% if user.is_authenticated %} + 📝KU POLL | 👋 Hi! {{ user.username }} + {% else %} + 📝KU POLL + {% endif %} + {% comment %} diff --git a/polls/templates/registration/login.html b/polls/templates/registration/login.html index 99361b9..398cecb 100644 --- a/polls/templates/registration/login.html +++ b/polls/templates/registration/login.html @@ -7,7 +7,7 @@ - + Sign In {% csrf_token %} diff --git a/polls/templates/registration/signup.html b/polls/templates/registration/signup.html index 0bac13d..6f9ba91 100644 --- a/polls/templates/registration/signup.html +++ b/polls/templates/registration/signup.html @@ -7,7 +7,7 @@ - + Sign Up {% csrf_token %} diff --git a/polls/tests.py b/polls/tests.py deleted file mode 100644 index db9a67f..0000000 --- a/polls/tests.py +++ /dev/null @@ -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) diff --git a/polls/tests/__init__.py b/polls/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/polls/tests/base.py b/polls/tests/base.py new file mode 100644 index 0000000..1e2db85 --- /dev/null +++ b/polls/tests/base.py @@ -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) \ No newline at end of file diff --git a/polls/tests/test_detail_views.py b/polls/tests/test_detail_views.py new file mode 100644 index 0000000..db2f3bc --- /dev/null +++ b/polls/tests/test_detail_views.py @@ -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) diff --git a/polls/tests/test_index_views.py b/polls/tests/test_index_views.py new file mode 100644 index 0000000..4cf0ad2 --- /dev/null +++ b/polls/tests/test_index_views.py @@ -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], + ) diff --git a/polls/tests/test_question_model.py b/polls/tests/test_question_model.py new file mode 100644 index 0000000..6514de0 --- /dev/null +++ b/polls/tests/test_question_model.py @@ -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) \ No newline at end of file diff --git a/polls/tests/test_signup.py b/polls/tests/test_signup.py new file mode 100644 index 0000000..8b17805 --- /dev/null +++ b/polls/tests/test_signup.py @@ -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()) diff --git a/polls/tests/test_vote_views.py b/polls/tests/test_vote_views.py new file mode 100644 index 0000000..393650e --- /dev/null +++ b/polls/tests/test_vote_views.py @@ -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()) \ No newline at end of file diff --git a/polls/views.py b/polls/views.py index 3cd85c0..bcdf518 100644 --- a/polls/views.py +++ b/polls/views.py @@ -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")) \ No newline at end of file + messages.error(request, "You cannot vote on this question.") + return redirect("polls:index") + else: + messages.error(request, "Invalid request method.") + return redirect("polls:index") \ No newline at end of file diff --git a/setup.ps1 b/setup.ps1 new file mode 100644 index 0000000..c2b48af --- /dev/null +++ b/setup.ps1 @@ -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 \ No newline at end of file diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..290e9ac --- /dev/null +++ b/setup.py @@ -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"])