Skip to content

Commit

Permalink
feat(polls): add voter count
Browse files Browse the repository at this point in the history
closes #886
  • Loading branch information
Laur04 authored and anonymoose2 committed May 14, 2020
1 parent 1c0e278 commit 1b63a44
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
15 changes: 15 additions & 0 deletions intranet/apps/polls/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,21 @@ def get_users_voted(self):
users = list(q.get_users_voted())
return users

def get_num_eligible_voters(self):
if self.groups.exists():
return get_user_model().objects.exclude(user_type="service").filter(groups__poll=self).distinct().count()
else:
return get_user_model().objects.exclude(user_type="service").count()

def get_percentage_voted(self, voted, able):
return "{:.1%}".format(0 if able == 0 else voted / able)

def get_voted_string(self):
users_voted = len(self.get_users_voted())
users_able = self.get_num_eligible_voters()
percent = self.get_percentage_voted(users_voted, users_able)
return "{} out of {} ({}) eligible users voted in this poll.".format(users_voted, users_able, percent)

def has_user_voted(self, user):
return Answer.objects.filter(question__in=self.question_set.all(), user=user).count() == self.question_set.count()

Expand Down
15 changes: 15 additions & 0 deletions intranet/apps/polls/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ def test_poll(self):
self.assertEqual(Question.objects.filter(poll=poll[0]).count(), 1)
self.assertEqual(Choice.objects.filter(question__poll=poll[0]).count(), 3)

# Everyone can vote, no one has
self.assertEqual(poll[0].get_voted_string(), "{} out of {} ({}) eligible users voted in this poll.".format(0, 1, "0.0%"))

# Make sure that the user can vote in the poll
response = self.client.post(reverse("poll_vote", kwargs={"poll_id": poll.first().id}), data={"question-1": 3})

Expand All @@ -57,6 +60,9 @@ def test_poll(self):
user_choice = Answer.objects.get(question=Question.objects.get(poll=poll[0]), user=user).choice
self.assertEqual(user_choice, Choice.objects.get(question__poll=poll[0], info="Blue"))

# Everyone can vote, one person has
self.assertEqual(poll[0].get_voted_string(), "{} out of {} ({}) eligible users voted in this poll.".format(1, 1, "100.0%"))

# Test poll deletion
response = self.client.post(reverse("delete_poll", kwargs={"poll_id": poll.first().id}))

Expand All @@ -78,6 +84,9 @@ def test_can_vote(self):

poll = Poll.objects.create(title="Test", description="Test", start_time=ago_5, end_time=ago_5, visible=False)

# Everyone can vote, no one has
self.assertEqual(poll.get_voted_string(), "{} out of {} ({}) eligible users voted in this poll.".format(0, 1, "0.0%"))

# Happened in the past, not visible
self.assertFalse(poll.can_vote(user))

Expand Down Expand Up @@ -112,10 +121,16 @@ def test_can_vote(self):
poll.groups.add(temp_group)
self.assertFalse(poll.can_vote(user))

# temp_group can vote, no one in temp_group
self.assertEqual(poll.get_voted_string(), "{} out of {} ({}) eligible users voted in this poll.".format(0, 0, "0.0%"))

# Happening now, visible, in poll group
user.groups.add(temp_group)
self.assertTrue(poll.can_vote(user))

# temp_group can vote, one in temp_group
self.assertEqual(poll.get_voted_string(), "{} out of {} ({}) eligible users voted in this poll.".format(0, 1, "0.0%"))

# admin_all, happened in the past, not visible
poll.end_time = ago_5
poll.visible = False
Expand Down
7 changes: 6 additions & 1 deletion intranet/templates/polls/poll.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,15 @@ <h3>
for
{% for group in poll.groups.all %}
{{ group }}
{% if not forloop.last %},{% endif %}
{% if not forloop.last %},
{% else %}
{% endif %}
{% empty %}
everyone
{% endfor %}
{% if is_polls_admin and not poll.before_start_time %}
&bull; {{ poll.get_voted_string }}
{% endif %}
</div>

{% if poll.announcement and not announcement %}
Expand Down
1 change: 1 addition & 0 deletions intranet/templates/polls/results.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
&nbsp; <a href="{% url 'poll_csv_results' poll.id %}" class="button small-button print-hide">Download as CSV</a>
{% endif %}<br>
<h2>Results: {{ poll }}</h2>
<p>&nbsp; {{ poll.get_voted_string }}</p>

<ol class="questions">
{% for q in questions %}
Expand Down

0 comments on commit 1b63a44

Please sign in to comment.