Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable support for polling #80

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions core/migrations/0016_auto_20210924_1351.py
Original file line number Diff line number Diff line change
@@ -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'),
),
]
19 changes: 15 additions & 4 deletions core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -345,10 +360,6 @@ class Audio(Content):
descriptor = "Audio"


class Poll(Content):
pass # STUB_POLL


class Story(Content):
"""The main story model."""

Expand Down
4 changes: 4 additions & 0 deletions home/templates/home/content.html
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ <h5>{{ comment.name }} <span class="text-muted">&mdash; {{ comment.date|naturalt
<br/>
{% endif %}

{% for poll in content.polls %}
{% include "content/poll.html" %}
{% endfor %}

<div class="card latest-stories">
{% if related_content.count > 0 %}
<div class="card-header mb-3">
Expand Down
12 changes: 11 additions & 1 deletion home/templates/home/content/poll.html
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
STUB_POLL
{% load home %}
{% with question=content.poll %}
<div class="poll-body">
<p>{{ question.text }}</p>
<form method="post" action="/vote/{{ choice.pk }}">
{% for choice in question.choices %}
<input type="radio" name="choice" value="{{ choice.pk }}"/>{{ choice.text }}
{% endfor %}
</form>
</div>
{% endwith %}
6 changes: 4 additions & 2 deletions home/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down