Skip to content

Commit

Permalink
Merge pull request #280 from barsch/poll_hide_results
Browse files Browse the repository at this point in the history
Adds "Hide results" option for polls
  • Loading branch information
ellmetha committed Oct 13, 2022
2 parents 42dfeea + 0dce4d4 commit 7521678
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 2 deletions.
7 changes: 6 additions & 1 deletion machina/apps/forum_conversation/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,11 @@ def __init__(self, *args, **kwargs):
help_text=_('If enabled users are able to change their vote.'),
initial=False,
)

self.fields['poll_hide_results'] = forms.BooleanField(
label=_('Hide results?'), required=False,
help_text=_('If enabled results are hidden to everyone except topic creator.'),
initial=False,
)
# Set the initial values
try:
if hasattr(self.instance, 'topic'):
Expand All @@ -187,6 +191,7 @@ def __init__(self, *args, **kwargs):
self.fields['poll_max_options'].initial = self.instance.topic.poll.max_options
self.fields['poll_duration'].initial = self.instance.topic.poll.duration
self.fields['poll_user_changes'].initial = self.instance.topic.poll.user_changes
self.fields['poll_hide_results'].initial = self.instance.topic.poll.hide_results
except ObjectDoesNotExist:
pass

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class AbstractTopicPoll(DatedModel):
# Are users allowed to change their votes ?
user_changes = models.BooleanField(verbose_name=_('Allow vote changes'), default=False)

# Hide results to everyone except topic creator ?
hide_results = models.BooleanField(verbose_name=_('Hide results'), default=False)

class Meta:
abstract = True
app_label = 'forum_polls'
Expand Down
2 changes: 1 addition & 1 deletion machina/apps/forum_conversation/forum_polls/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class TopicPollAdmin(admin.ModelAdmin):
""" The Topic Poll model admin. """

inlines = (TopicPollOptionInline,)
list_display = ('topic', 'duration', 'max_options', 'user_changes',)
list_display = ('topic', 'duration', 'max_options', 'user_changes', 'hide_results')
list_filter = ('created', 'updated',)
search_fields = ('topic__subject',)

Expand Down
2 changes: 2 additions & 0 deletions machina/apps/forum_conversation/forum_polls/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def save(self, commit=True, **kwargs):
poll_max_options = kwargs.pop('poll_max_options', None)
poll_duration = kwargs.pop('poll_duration', None)
poll_user_changes = kwargs.pop('poll_user_changes', False)
poll_hide_results = kwargs.pop('poll_hide_results', False)

if self.poll is None:
poll, _ = TopicPoll.objects.get_or_create(topic=self.topic)
Expand All @@ -89,6 +90,7 @@ def save(self, commit=True, **kwargs):
poll.duration = poll_duration
poll.max_options = poll_max_options
poll.user_changes = poll_user_changes
poll.hide_results = poll_hide_results
poll.save()

for form in self.forms:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.15 on 2022-09-11 20:25

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("forum_polls", "0002_auto_20151105_0029"),
]

operations = [
migrations.AddField(
model_name="topicpoll",
name="hide_results",
field=models.BooleanField(default=False, verbose_name="Hide results"),
),
]
1 change: 1 addition & 0 deletions machina/apps/forum_conversation/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,7 @@ def form_valid(self, post_form, attachment_formset, poll_option_formset, **kwarg
poll_max_options=post_form.cleaned_data.pop('poll_max_options', None),
poll_duration=post_form.cleaned_data.pop('poll_duration', None),
poll_user_changes=post_form.cleaned_data.pop('poll_user_changes', None),
poll_hide_results=post_form.cleaned_data.pop('poll_hide_results', None),
)

return valid
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
{% load i18n %}
{% load forum_permission_tags %}

{% get_permission 'can_edit_post' poll.topic.first_post request.user as user_can_edit_first_post %}

{% if poll.hide_results %}
{% trans "Results are currently hidden." %}<br><br>
{% endif %}

{% if not poll.hide_results or user_can_edit_first_post %}
{% for option in poll.options.all %}
{% if forloop.first or not forloop.counter|divisibleby:2 %}
<div class="mb-3 row">
Expand All @@ -16,4 +24,6 @@
</div>
{% endif %}
{% endfor %}
{% endif %}

{% trans "Total votes:" %}&nbsp;{{ poll.votes|length }}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@
</label>
</div>
</div>
<div{% if post_form.poll_hide_results.errors %} class="has-error"{% endif %}>
<div class="checkbox">
<label for="{{ post_form.poll_hide_results.auto_id }}">
{{ post_form.poll_hide_results }}
{{ post_form.poll_hide_results.label }}
</label>
</div>
</div>
</div>
<div id="poll_formset" class="col-md-6">
{% include "forum_conversation/forum_polls/poll_option_formset.html" %}
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/apps/forum_conversation/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,10 +402,12 @@ def test_can_append_poll_fields_if_the_user_is_allowed_to_create_polls(self):
assert 'poll_max_options' in form.fields
assert 'poll_duration' in form.fields
assert 'poll_user_changes' in form.fields
assert 'poll_hide_results' in form.fields
assert isinstance(form.fields['poll_question'], forms.CharField)
assert isinstance(form.fields['poll_max_options'], forms.IntegerField)
assert isinstance(form.fields['poll_duration'], forms.IntegerField)
assert isinstance(form.fields['poll_user_changes'], forms.BooleanField)
assert isinstance(form.fields['poll_hide_results'], forms.BooleanField)

def test_cannot_append_poll_fields_if_the_user_is_not_allowed_to_create_polls(self):
# Setup
Expand All @@ -426,6 +428,7 @@ def test_cannot_append_poll_fields_if_the_user_is_not_allowed_to_create_polls(se
assert 'poll_max_options' not in form.fields
assert 'poll_duration' not in form.fields
assert 'poll_user_changes' not in form.fields
assert 'poll_hide_results' not in form.fields

def test_can_initialize_poll_fields_from_topic_related_poll_object(self):
# Setup
Expand All @@ -448,10 +451,12 @@ def test_can_initialize_poll_fields_from_topic_related_poll_object(self):
assert 'poll_max_options' in form.fields
assert 'poll_duration' in form.fields
assert 'poll_user_changes' in form.fields
assert 'poll_hide_results' in form.fields
assert form.fields['poll_question'].initial == poll.question
assert form.fields['poll_max_options'].initial == poll.max_options
assert form.fields['poll_duration'].initial == poll.duration
assert form.fields['poll_user_changes'].initial == poll.user_changes
assert form.fields['poll_hide_results'].initial == poll.hide_results

def test_cannot_allow_users_to_create_polls_without_settings_the_maximum_options_number(self):
# Setup
Expand Down

0 comments on commit 7521678

Please sign in to comment.