From 9c0d293af1697cb71302f3f1c8383d8e2efbf57b Mon Sep 17 00:00:00 2001 From: Aplet123 Date: Fri, 24 Sep 2021 13:51:18 -0400 Subject: [PATCH] preliminary setup for polling --- core/migrations/0016_auto_20210924_1351.py | 42 ++++++++++++++++++++++ core/models.py | 19 +++++++--- home/templates/home/content.html | 4 +++ home/templates/home/content/poll.html | 12 ++++++- home/views.py | 6 ++-- 5 files changed, 76 insertions(+), 7 deletions(-) create mode 100644 core/migrations/0016_auto_20210924_1351.py diff --git a/core/migrations/0016_auto_20210924_1351.py b/core/migrations/0016_auto_20210924_1351.py new file mode 100644 index 0000000..919bd9a --- /dev/null +++ b/core/migrations/0016_auto_20210924_1351.py @@ -0,0 +1,42 @@ +# Generated by Django 3.1.1 on 2021-09-24 17:51 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('core', '0015_profile_is_hidden'), + ] + + operations = [ + migrations.CreateModel( + name='PollChoice', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('text', models.CharField(max_length=300)), + ('votes', models.IntegerField(default=0)), + ], + ), + migrations.CreateModel( + name='PollQuestion', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('text', models.CharField(max_length=300)), + ], + ), + migrations.DeleteModel( + name='Poll', + ), + migrations.AddField( + model_name='pollchoice', + name='question', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='choices', to='core.pollquestion'), + ), + migrations.AddField( + model_name='content', + name='polls', + field=models.ManyToManyField(blank=True, related_name='stories', to='core.PollQuestion'), + ), + ] diff --git a/core/models.py b/core/models.py index dae59af..83ed4db 100755 --- a/core/models.py +++ b/core/models.py @@ -105,6 +105,18 @@ def __str__(self): return self.name +class PollQuestion(models.Model): + text = models.CharField(max_length=300) + + +class PollChoice(models.Model): + question = models.ForeignKey( + PollQuestion, on_delete=models.CASCADE, related_name="choices" + ) + text = models.CharField(max_length=300) + votes = models.IntegerField(default=0) + + class Content(PolymorphicModel): """A generic content model. @@ -142,6 +154,9 @@ class Content(PolymorphicModel): on_delete=models.SET_NULL, ) views = models.IntegerField(default=0) + polls = models.ManyToManyField( + PollQuestion, related_name="stories", blank=True + ) # Whether this content should show up by itself embed_only = models.BooleanField( @@ -345,10 +360,6 @@ class Audio(Content): descriptor = "Audio" -class Poll(Content): - pass # STUB_POLL - - class Story(Content): """The main story model.""" diff --git a/home/templates/home/content.html b/home/templates/home/content.html index 0e5eb81..d08c2f8 100644 --- a/home/templates/home/content.html +++ b/home/templates/home/content.html @@ -137,6 +137,10 @@
{{ comment.name }} — {{ comment.date|naturalt
{% endif %} + {% for poll in content.polls %} + {% include "content/poll.html" %} + {% endfor %} +
{% if related_content.count > 0 %}
diff --git a/home/templates/home/content/poll.html b/home/templates/home/content/poll.html index e48546f..efc5d76 100644 --- a/home/templates/home/content/poll.html +++ b/home/templates/home/content/poll.html @@ -1 +1,11 @@ -STUB_POLL +{% load home %} +{% with question=content.poll %} +
+

{{ question.text }}

+
+ {% for choice in question.choices %} + {{ choice.text }} + {% endfor %} +
+
+{% endwith %} diff --git a/home/views.py b/home/views.py index 6e98f3f..5e0adbb 100755 --- a/home/views.py +++ b/home/views.py @@ -308,9 +308,11 @@ def staff(request): # Content interaction views -def vote(request, pk): +def vote(request, pk, choice): """Vote in a poll.""" - pass + question = get_object_or_404(models.PollQuestion, pk=pk) + choice = get_object_or_404(models.PollChoice, pk=choice) + return HttpResponse("you voted") @check_recaptcha