diff --git a/.gitignore b/.gitignore index 68bc17f..4513501 100644 --- a/.gitignore +++ b/.gitignore @@ -58,7 +58,7 @@ cover/ # Django stuff: *.log local_settings.py -db.sqlite3 +# db.sqlite3 db.sqlite3-journal # Flask stuff: diff --git a/db.sqlite3 b/db.sqlite3 new file mode 100644 index 0000000..4249e6f Binary files /dev/null and b/db.sqlite3 differ diff --git a/mysite/settings.py b/mysite/settings.py index ed45396..7a27f5c 100644 --- a/mysite/settings.py +++ b/mysite/settings.py @@ -11,6 +11,7 @@ https://docs.djangoproject.com/en/4.2/ref/settings/ """ from pathlib import Path +from decouple import config # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent @@ -20,7 +21,7 @@ BASE_DIR = Path(__file__).resolve().parent.parent # See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = 'django-insecure-ij$9jv9o30j(51=l)ho^l+x&q9)n77i1vt%j()%9=(ohh(b*!^' +SECRET_KEY = config('SECRET_KEY', default='fake-secret-key') # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True @@ -31,6 +32,7 @@ ALLOWED_HOSTS = [] # Application definition INSTALLED_APPS = [ + "polls.apps.PollsConfig", 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', @@ -53,15 +55,15 @@ ROOT_URLCONF = 'mysite.urls' TEMPLATES = [ { - 'BACKEND': 'django.template.backends.django.DjangoTemplates', - 'DIRS': [], - 'APP_DIRS': True, - 'OPTIONS': { - 'context_processors': [ - 'django.template.context_processors.debug', - 'django.template.context_processors.request', - 'django.contrib.auth.context_processors.auth', - 'django.contrib.messages.context_processors.messages', + "BACKEND": "django.template.backends.django.DjangoTemplates", + "DIRS": [BASE_DIR / "templates"], + "APP_DIRS": True, + "OPTIONS": { + "context_processors": [ + "django.template.context_processors.debug", + "django.template.context_processors.request", + "django.contrib.auth.context_processors.auth", + "django.contrib.messages.context_processors.messages", ], }, }, @@ -115,7 +117,8 @@ USE_TZ = True # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/4.2/howto/static-files/ -STATIC_URL = 'static/' +STATIC_URL = '/static/' +STATICFILES_DIRS = [BASE_DIR] # Default primary key field type # https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field diff --git a/mysite/urls.py b/mysite/urls.py index 79d5460..b5d5113 100644 --- a/mysite/urls.py +++ b/mysite/urls.py @@ -1,22 +1,10 @@ -""" -URL configuration for mysite project. - -The `urlpatterns` list routes URLs to views. For more information please see: - https://docs.djangoproject.com/en/4.2/topics/http/urls/ -Examples: -Function views - 1. Add an import: from my_app import views - 2. Add a URL to urlpatterns: path('', views.home, name='home') -Class-based views - 1. Add an import: from other_app.views import Home - 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') -Including another URLconf - 1. Import the include() function: from django.urls import include, path - 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) -""" from django.contrib import admin -from django.urls import path +from django.urls import include, path + +from polls.views import HomeView urlpatterns = [ + path('', HomeView.as_view(), name='home'), + path("polls/", include("polls.urls")), path('admin/', admin.site.urls), ] diff --git a/polls/__init__.py b/polls/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/polls/admin.py b/polls/admin.py new file mode 100644 index 0000000..c2ce0aa --- /dev/null +++ b/polls/admin.py @@ -0,0 +1,23 @@ +from django.contrib import admin + +from .models import Choice, Question + + +class ChoiceInline(admin.TabularInline): + model = Choice + extra = 3 + + +class QuestionAdmin(admin.ModelAdmin): + fieldsets = [ + (None, {"fields": ["question_text"]}), + ("Date information", {"fields": ["pub_date"], "classes": ["collapse"]}), + ] + list_display = ["question_text", "pub_date", "was_published_recently"] + inlines = [ChoiceInline] + list_filter = ["pub_date"] + search_fields = ["question_text"] + + +admin.site.register(Question, QuestionAdmin) + \ No newline at end of file diff --git a/polls/apps.py b/polls/apps.py new file mode 100644 index 0000000..1dffd05 --- /dev/null +++ b/polls/apps.py @@ -0,0 +1,7 @@ +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/migrations/0001_initial.py b/polls/migrations/0001_initial.py new file mode 100644 index 0000000..1993212 --- /dev/null +++ b/polls/migrations/0001_initial.py @@ -0,0 +1,32 @@ +# Generated by Django 4.2.4 on 2023-08-28 11:51 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Question', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('question_text', models.CharField(max_length=200)), + ('pub_date', models.DateTimeField(verbose_name='date published')), + ], + ), + migrations.CreateModel( + name='Choice', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('choice_text', models.CharField(max_length=200)), + ('votes', models.IntegerField(default=0)), + ('question', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='polls.question')), + ], + ), + ] diff --git a/polls/migrations/__init__.py b/polls/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/polls/models.py b/polls/models.py new file mode 100644 index 0000000..7252b32 --- /dev/null +++ b/polls/models.py @@ -0,0 +1,76 @@ +""" +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 +from django.utils import timezone +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 + + @admin.display( + boolean=True, + ordering="pub_date", + description="Published recently?", + ) + def was_published_recently(self): + now = timezone.now() + 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): + """ + Returns a string representation of the choice. + """ + return self.choice_text + \ No newline at end of file diff --git a/polls/static/polls/base.css b/polls/static/polls/base.css new file mode 100644 index 0000000..afd26f9 --- /dev/null +++ b/polls/static/polls/base.css @@ -0,0 +1,191 @@ +/*! NAVBAR */ + +header { + background-color: #1C1C1C; + color: #fff; + padding: 20px; + text-align: center; + display: flex; + justify-content: center; + align-items: center; + box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1); + border-radius: 10px; +} + +.nav-container { + display: flex; + justify-content: center; + align-items: center; +} + +.nav-left h1 a { + text-decoration: none; + color: #fff; + font-size: 24px; +} + + +.nav-right ul { + list-style: none; + padding: 0; + margin: 0; + display: flex; + align-items: center; +} + +.nav-right li { + margin: 0 10px; +} + +.nav-right a { + text-decoration: none; + color: #fff; + font-weight: bold; + padding: 10px 20px; + border: 2px solid #fff; + border-radius: 5px; + transition: background-color 0.3s ease, color 0.3s ease; +} + +.nav-right a:hover { + background-color: #fff; + color: #007bff; +} + +/*! HOME AND POLL CARD */ + +.hero-section { + background-size: cover; + background-position: center; + text-align: center; + color: #1c1c1c; +} + +.hero-content { + max-width: 800px; + margin: 0 auto; +} + +h1 { + font-size: 36px; + margin-bottom: 20px; +} + +.polls-section { + background-size: cover; + background-position: center; + text-align: center; + color: #1c1c1c; +} + +.poll-cards { + display: flex; + flex-wrap: wrap; + gap: 20px; + justify-content: center; + margin-top: 30px; +} + +.poll-card { + background-color: #fff; + border: 1px solid #e0e0e0; + padding: 20px; + border-radius: 5px; + box-shadow: 0px 3px 6px rgba(0, 0, 0, 0.1); + transition: transform 0.3s ease; + cursor: pointer; +} + +.poll-card:hover { + transform: translateY(-5px); +} + +/*! DETAILED */ + +.poll-details { + padding: 30px; + background-color: #fff; + border-radius: 10px; + box-shadow: 0px 3px 6px rgba(0, 0, 0, 0.1); +} + +.poll-form { + text-align: center; +} + +.poll-question { + font-size: 24px; + margin-bottom: 20px; +} + +.error-message { + color: red; + margin-bottom: 10px; +} + +.choice { + display: flex; + align-items: center; + margin: 10px 0; +} + +.choice input[type="radio"] { + margin-right: 10px; +} + +.choice-text { + font-size: 18px; +} + +.vote-button { + background-color: #007bff; + color: #fff; + padding: 10px 20px; + border: none; + border-radius: 5px; + cursor: pointer; + transition: background-color 0.3s ease, color 0.3s ease; +} + +.vote-button:hover { + background-color: #0056b3; +} + +/*! RESULT */ + +.poll-results { + text-align: center; + padding: 30px; + background-color: #fff; + border-radius: 10px; + box-shadow: 0px 3px 6px rgba(0, 0, 0, 0.1); +} + +.poll-question { + font-size: 24px; + margin-bottom: 20px; +} + +.choice-list { + list-style: none; + padding: 0; + margin: 20px 0; + text-align: left; +} + +.choice-item { + font-size: 18px; + margin: 10px 0; +} + +.vote-again { + display: inline-block; + margin-top: 20px; + text-decoration: none; + color: #007bff; + transition: color 0.3s ease; +} + +.vote-again:hover { + color: #0056b3; +} \ No newline at end of file diff --git a/polls/static/polls/images/background.jpg b/polls/static/polls/images/background.jpg new file mode 100644 index 0000000..c47c004 Binary files /dev/null and b/polls/static/polls/images/background.jpg differ diff --git a/polls/static/polls/style.css b/polls/static/polls/style.css new file mode 100644 index 0000000..e69de29 diff --git a/polls/templates/admin/base_site.html b/polls/templates/admin/base_site.html new file mode 100644 index 0000000..7997e66 --- /dev/null +++ b/polls/templates/admin/base_site.html @@ -0,0 +1,12 @@ +{% extends "admin/base.html" %} + +{% block title %}{% if subtitle %}{{ subtitle }} | {% endif %}{{ title }} | {{ site_title|default:_('Django site admin') }}{% endblock %} + +{% block branding %} +
Polls Administration Page
+{% if user.is_anonymous %} + {% include "admin/color_theme_toggle.html" %} +{% endif %} +{% endblock %} + +{% block nav-global %}{% endblock %} \ No newline at end of file diff --git a/polls/templates/polls/base.html b/polls/templates/polls/base.html new file mode 100644 index 0000000..ae54f87 --- /dev/null +++ b/polls/templates/polls/base.html @@ -0,0 +1,25 @@ +{% load static %} + + + MySite Polls + + + + +
+
+ + +
+ {% block content %} + {% endblock content %} +
+ + diff --git a/polls/templates/polls/detail.html b/polls/templates/polls/detail.html new file mode 100644 index 0000000..8ca20b6 --- /dev/null +++ b/polls/templates/polls/detail.html @@ -0,0 +1,24 @@ +{% extends 'polls/base.html' %} + +{% block content %} +
+
+
+ {% csrf_token %} +
+ {{ question.question_text }} + {% if error_message %} +

{{ error_message }}

+ {% endif %} + {% for choice in question.choice_set.all %} +
+ + +
+ {% endfor %} +
+ +
+
+
+{% endblock content %} diff --git a/polls/templates/polls/home.html b/polls/templates/polls/home.html new file mode 100644 index 0000000..ad8a1c8 --- /dev/null +++ b/polls/templates/polls/home.html @@ -0,0 +1,26 @@ +{% extends 'polls/base.html' %} + +{% block content %} +
+
+
+

Welcome to KU Polls

+

Explore and participate in our weird poll questions.

+
+
+
+

Recent Polls

+

Total number of polls: {{ total_polls }}

+
+ {% if latest_question_list %} + {% for question in latest_question_list %} + + {% endfor %} + {% else %} +

No polls are available.

+ {% endif %} +
+
+{% endblock content %} diff --git a/polls/templates/polls/index.html b/polls/templates/polls/index.html new file mode 100644 index 0000000..f07875d --- /dev/null +++ b/polls/templates/polls/index.html @@ -0,0 +1,19 @@ +{% extends 'polls/base.html' %} + +{% block content %} +
+

Recent Polls

+
+ {% if latest_question_list %} + {% for question in latest_question_list %} +
+

{{ question.question_text }}

+

Published on: {{ question.pub_date|date:"F j, Y" }}

+
+ {% endfor %} + {% else %} +

No polls are available.

+ {% endif %} +
+
+{% endblock content %} diff --git a/polls/templates/polls/results.html b/polls/templates/polls/results.html new file mode 100644 index 0000000..dd55c8d --- /dev/null +++ b/polls/templates/polls/results.html @@ -0,0 +1,17 @@ +{% extends 'polls/base.html' %} + +{% block content %} +
+
+
+

{{ question.question_text }}

+
+
    + {% for choice in question.choice_set.all %} +
  • {{ choice.choice_text }} — {{ choice.votes }} vote{{ choice.votes|pluralize }}
  • + {% endfor %} +
+ Vote again? +
+
+{% endblock content %} diff --git a/polls/tests.py b/polls/tests.py new file mode 100644 index 0000000..410f88f --- /dev/null +++ b/polls/tests.py @@ -0,0 +1,129 @@ +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 create_question(question_text, days): + """ + 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() + datetime.timedelta(days=days) + 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.assertContains(response, "No polls are available.") + 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 = create_question(question_text="Past question.", days=-30) + 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. + """ + create_question(question_text="Future question.", days=30) + response = self.client.get(reverse("polls:index")) + self.assertContains(response, "No polls are available.") + 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. + """ + question = create_question(question_text="Past question.", days=-30) + create_question(question_text="Future question.", days=30) + response = self.client.get(reverse("polls:index")) + self.assertQuerySetEqual( + response.context["latest_question_list"], + [question], + ) + + def test_two_past_questions(self): + """ + The questions index page may display multiple questions. + """ + question1 = create_question(question_text="Past question 1.", days=-30) + question2 = create_question(question_text="Past question 2.", days=-5) + 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 = create_question(question_text="Future question.", days=5) + 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 = create_question(question_text="Past Question.", days=-5) + 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 new file mode 100644 index 0000000..6d2b663 --- /dev/null +++ b/polls/urls.py @@ -0,0 +1,12 @@ +from django.urls import path + +from . import views + +app_name = "polls" +urlpatterns = [ + path("", views.IndexView.as_view(), name="index"), + path("/", views.DetailView.as_view(), name="detail"), + 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 new file mode 100644 index 0000000..6180804 --- /dev/null +++ b/polls/views.py @@ -0,0 +1,90 @@ +from django.http import HttpResponseRedirect +from django.shortcuts import get_object_or_404, render +from django.urls import reverse +from django.views import generic +from django.utils import timezone +from django.views.generic import TemplateView + +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): + context = super().get_context_data(**kwargs) + context['latest_question_list'] = Question.objects.filter(pub_date__lte=timezone.now()).order_by("-pub_date")[:5] + context['total_polls'] = Question.objects.count() + return context + + +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" + + def get_queryset(self): + """ + Return the last five published questions (not including those set to be + published in the future). + """ + return Question.objects.filter(pub_date__lte=timezone.now()).order_by("-pub_date")[ + :5 + ] + + +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" + + def get_queryset(self): + """ + Excludes any questions that aren't published yet. + """ + 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"]) + except (KeyError, Choice.DoesNotExist): + return render( + request, + "polls/detail.html", + { + "question": question, + "error_message": "You didn't select a choice.", + }, + ) + else: + selected_choice.votes += 1 + selected_choice.save() + return HttpResponseRedirect(reverse("polls:results", args=(question.id,))) + \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 838e10a..63393f6 100644 Binary files a/requirements.txt and b/requirements.txt differ