From a5ce30aeebf3dd7609e84cdcfc5f5082a22d9c02 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Mon, 16 Sep 2024 06:56:04 -0400 Subject: [PATCH] Up through tutorial part 3, the first bit --- djoy/polls/admin.py | 4 ++- djoy/polls/apps.py | 4 +-- djoy/polls/migrations/0001_initial.py | 43 +++++++++++++++++++-------- djoy/polls/models.py | 12 ++++++++ djoy/polls/urls.py | 3 ++ djoy/polls/views.py | 24 +++++++++++++-- djoy/requirements.txt | 1 + 7 files changed, 74 insertions(+), 17 deletions(-) diff --git a/djoy/polls/admin.py b/djoy/polls/admin.py index 8c38f3f..6af8ff6 100644 --- a/djoy/polls/admin.py +++ b/djoy/polls/admin.py @@ -1,3 +1,5 @@ from django.contrib import admin -# Register your models here. +from .models import Question + +admin.site.register(Question) diff --git a/djoy/polls/apps.py b/djoy/polls/apps.py index 5a5f94c..5184937 100644 --- a/djoy/polls/apps.py +++ b/djoy/polls/apps.py @@ -2,5 +2,5 @@ from django.apps import AppConfig class PollsConfig(AppConfig): - default_auto_field = 'django.db.models.BigAutoField' - name = 'polls' + default_auto_field = "django.db.models.BigAutoField" + name = "polls" diff --git a/djoy/polls/migrations/0001_initial.py b/djoy/polls/migrations/0001_initial.py index 63290b2..a5c11ad 100644 --- a/djoy/polls/migrations/0001_initial.py +++ b/djoy/polls/migrations/0001_initial.py @@ -5,28 +5,47 @@ from django.db import migrations, models class Migration(migrations.Migration): - initial = True - dependencies = [ - ] + dependencies = [] operations = [ migrations.CreateModel( - name='Question', + 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')), + ( + "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', + 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')), + ( + "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/djoy/polls/models.py b/djoy/polls/models.py index 380716f..580fce7 100644 --- a/djoy/polls/models.py +++ b/djoy/polls/models.py @@ -1,12 +1,24 @@ +import datetime + from django.db import models +from django.utils import timezone class Question(models.Model): question_text = models.CharField(max_length=200) pub_date = models.DateTimeField("date published") + def __str__(self): + return self.question_text + + def was_published_recently(self): + return self.pub_date >= timezone.now() - datetime.timedelta(days=1) + class Choice(models.Model): question = models.ForeignKey(Question, on_delete=models.CASCADE) choice_text = models.CharField(max_length=200) votes = models.IntegerField(default=0) + + def __str__(self): + return self.choice_text diff --git a/djoy/polls/urls.py b/djoy/polls/urls.py index 0a44982..a0cb755 100644 --- a/djoy/polls/urls.py +++ b/djoy/polls/urls.py @@ -5,4 +5,7 @@ from . import views urlpatterns = [ path("", views.index, name="index"), + path("/", views.detail, name="detail"), + path("/results/", views.results, name="results"), + path("/vote/", views.vote, name="vote"), ] diff --git a/djoy/polls/views.py b/djoy/polls/views.py index b47b38f..6526de7 100644 --- a/djoy/polls/views.py +++ b/djoy/polls/views.py @@ -1,6 +1,26 @@ from django.http import HttpResponse +from .models import Question -def index(request): + +def index(request) -> HttpResponse: + latest_question_list = Question.objects.order_by("-pub_date")[:5] + output = ", ".join([q.question_text for q in latest_question_list]) + return HttpResponse(output.encode()) + + +def detail(request, question_id) -> HttpResponse: _ = request - return HttpResponse(b"Hello, world. You're at the polls index.") + return HttpResponse(f"You're looking at question {question_id}.".encode()) + + +def results(request, question_id) -> HttpResponse: + _ = request + return HttpResponse( + f"You're looking at the results of question {question_id}".encode() + ) + + +def vote(request, question_id) -> HttpResponse: + _ = request + return HttpResponse(f"You're voting on question {question_id}".encode()) diff --git a/djoy/requirements.txt b/djoy/requirements.txt index aac6287..ca63024 100644 --- a/djoy/requirements.txt +++ b/djoy/requirements.txt @@ -1,2 +1,3 @@ Django +django-stubs ruff