Skip to content

Commit

Permalink
fix(polls): change how polls display gender
Browse files Browse the repository at this point in the history
  • Loading branch information
Laur04 committed Dec 25, 2020
1 parent d149a98 commit e2a37ad
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 34 deletions.
23 changes: 11 additions & 12 deletions intranet/apps/polls/views.py
Expand Up @@ -296,15 +296,15 @@ def handle_sap(q):
return {"question": q, "choices": choices, "user_scale": user_scale}


def generate_choice(name, votes, total_count, do_gender=True, show_answers=False):
def generate_choice(name, votes, total_count, show_answers=False):
choice = {
"choice": name,
"votes": {
"total": {
"all": votes.count(),
"all_percent": perc(votes.count(), total_count),
"male": votes.filter(user__gender=True).count() if do_gender else 0,
"female": votes.filter(user__gender__isnull=False, user__gender=False).count() if do_gender else 0,
"male": votes.filter(user__gender=True).count(),
"female": votes.filter(user__gender__isnull=False, user__gender=False).count(),
}
},
"users": [v.user for v in votes] if show_answers else None,
Expand All @@ -314,13 +314,13 @@ def generate_choice(name, votes, total_count, do_gender=True, show_answers=False
yr_votes = votes.filter(user__graduation_year=get_senior_graduation_year() + 12 - yr)
choice["votes"][yr] = {
"all": yr_votes.count(),
"male": yr_votes.filter(user__gender=True).count() if do_gender else 0,
"female": yr_votes.filter(user__gender__isnull=False, user__gender=False).count() if do_gender else 0,
"male": yr_votes.filter(user__gender=True).count(),
"female": yr_votes.filter(user__gender__isnull=False, user__gender=False).count(),
}
return choice


def handle_choice(q, do_gender=True, show_answers=False):
def handle_choice(q, show_answers=False):
question_votes = votes = Answer.objects.filter(question=q)
total_count = question_votes.count()
users = q.get_users_voted()
Expand All @@ -329,14 +329,14 @@ def handle_choice(q, do_gender=True, show_answers=False):
# Choices
for c in q.choice_set.all().order_by("num"):
votes = question_votes.filter(choice=c)
choices.append(generate_choice(c, votes, total_count, do_gender, show_answers))
choices.append(generate_choice(c, votes, total_count, show_answers))

# Clear vote
votes = question_votes.filter(clear_vote=True)
choices.append(generate_choice("Clear vote", votes, total_count, do_gender, show_answers))
choices.append(generate_choice("Clear vote", votes, total_count, show_answers))

# Total
total_choice = generate_choice("Total", question_votes, total_count, do_gender, show_answers)
total_choice = generate_choice("Total", question_votes, total_count, show_answers)
total_choice["votes"]["total"]["users_all"] = users.count()
choices.append(total_choice)

Expand All @@ -353,7 +353,6 @@ def poll_results_view(request, poll_id):
messages.error(request, "Poll results cannot be viewed while the poll is running.")
return redirect("polls")

do_gender = "no_gender" not in request.GET
show_answers = request.GET.get("show_answers", False)

if show_answers and poll.is_secret:
Expand All @@ -365,13 +364,13 @@ def poll_results_view(request, poll_id):
if q.type == "SAP": # Split-approval; each person splits their one vote
questions.append(handle_sap(q))
elif q.is_choice():
questions.append(handle_choice(q, do_gender, show_answers))
questions.append(handle_choice(q, show_answers))
elif q.is_writing():
answers = Answer.objects.filter(question=q)
question = {"question": q, "answers": answers}
questions.append(question)

context = {"poll": poll, "grades": range(9, 13), "questions": questions, "show_answers": show_answers, "do_gender": do_gender}
context = {"poll": poll, "grades": range(9, 13), "questions": questions, "show_answers": show_answers}
return render(request, "polls/results.html", context)


Expand Down
56 changes: 34 additions & 22 deletions intranet/templates/polls/results.html
Expand Up @@ -38,10 +38,22 @@
{{ block.super }}
<script>
$(function() {
$(".gender-results").hide();
$("#user-sels").click(function() {
$(".user-selections").toggle();
});
})
$('input[type="checkbox"]').click(function(){
if($(this).prop("checked") == true){
$(".gender-results").show();
$(".grade-header").attr('colSpan', 3);
$(".choice-header").attr('rowSpan', 2);
} else {
$(".gender-results").hide();
$(".grade-header").attr('colSpan', 1);
$(".choice-header").attr('rowSpan', 1);
}
});
});
</script>
{% endblock %}

Expand All @@ -68,8 +80,8 @@
&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>

<p>&nbsp; {{ poll.get_voted_string }}</p><br>
<p>&nbsp; Show results by gender?<input type="checkbox"></p><br>
<ol class="questions">
{% for q in questions %}
<li id="question-{{ q.question.num }}"><b>{{ q.question.question|safe }}</b>
Expand All @@ -83,18 +95,18 @@ <h2>Results: {{ poll }}</h2>
<table class="results-table choice-table fancy-table">
<thead>
<tr>
<th rowspan="2" class="choice-header">Choice</th>
<th colspan="3">Total Votes</th>
<th rowspan="1" class="choice-header">Choice</th>
<th class="grade-header" colspan="1">Total Votes</th>
{% for i in grades %}
<th colspan="3">{{ i }}</th>
<th class="grade-header" colspan="1">{{ i }}</th>
{% endfor %}
<th colspan="3">Staff</th>
<th class="grade-header" colspan="1">Staff</th>
</tr>
<tr>
<tr class="gender-results">
{% for i in "012345" %}
<th>T</th>
<th>M</th>
<th>F</th>
<th class="gender-results">M</th>
<th class="gender-results">F</th>
{% endfor %}
</tr>
</thead>
Expand All @@ -121,33 +133,33 @@ <h2>Results: {{ poll }}</h2>
</td>
{% with t=c.votes.total %}
<td>{{ t.all }} ({{ t.all_percent }}%)</td>
<td>{{ t.is_male }}</td>
<td>{{ t.is_female }}</td>
<td class="gender-results">{{ t.is_male }}</td>
<td class="gender-results">{{ t.is_female }}</td>
{% endwith %}
{% with t=c.votes.9 %}
<td>{{ t.all }}</td>
<td>{{ t.is_male }}</td>
<td>{{ t.is_female }}</td>
<td class="gender-results">{{ t.is_male }}</td>
<td class="gender-results">{{ t.is_female }}</td>
{% endwith %}
{% with t=c.votes.10 %}
<td>{{ t.all }}</td>
<td>{{ t.is_male }}</td>
<td>{{ t.is_female }}</td>
<td class="gender-results">{{ t.is_male }}</td>
<td class="gender-results">{{ t.is_female }}</td>
{% endwith %}
{% with t=c.votes.11 %}
<td>{{ t.all }}</td>
<td>{{ t.is_male }}</td>
<td>{{ t.is_female }}</td>
<td class="gender-results">{{ t.is_male }}</td>
<td class="gender-results">{{ t.is_female }}</td>
{% endwith %}
{% with t=c.votes.12 %}
<td>{{ t.all }}</td>
<td>{{ t.is_male }}</td>
<td>{{ t.is_female }}</td>
<td class="gender-results">{{ t.is_male }}</td>
<td class="gender-results">{{ t.is_female }}</td>
{% endwith %}
{% with t=c.votes.13 %}
<td>{{ t.all }}</td>
<td>{{ t.is_male }}</td>
<td>{{ t.is_female }}</td>
<td class="gender-results">{{ t.is_male }}</td>
<td class="gender-results">{{ t.is_female }}</td>
{% endwith %}
</tr>
{% endfor %}
Expand Down

0 comments on commit e2a37ad

Please sign in to comment.