Skip to content

Added boolean sort priority fields, allowing for prioritisation of applicants. #8

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

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
12 changes: 11 additions & 1 deletion grants/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class QuestionForm(forms.ModelForm):

class Meta:
fields = ["question", "type", "required"]
fields = ["question", "type", "required", "sort_priority"]
model = Question

def clean_type(self):
Expand All @@ -14,6 +14,16 @@ def clean_type(self):
raise forms.ValidationError("Cannot change once this question has answers")
return type

def clean(self):
type = self.cleaned_data.get("type")
priority = self.cleaned_data.get("sort_priority")
if type == "boolean_sortpriority" and not priority:
error = "If you select the 'Yes/No with sort priority' type, you must provide a priority."
raise forms.ValidationError(error)
if type != "boolean_sortpriority" and priority:
raise forms.ValidationError("Sort priority can only be used with the 'Yes/No with sort priority' type.")
return self.cleaned_data


class BaseApplyForm(forms.Form):

Expand Down
29 changes: 29 additions & 0 deletions grants/migrations/0014_auto_20180103_1621.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models
import django.core.validators


class Migration(migrations.Migration):

dependencies = [
('grants', '0013_program_show_names_before_scoring'),
]

operations = [
migrations.AlterModelOptions(
name='answer',
options={'ordering': ['question']},
),
migrations.AddField(
model_name='question',
name='sort_priority',
field=models.IntegerField(blank=True, null=True, validators=[django.core.validators.MinValueValidator(1)]),
),
migrations.AlterField(
model_name='question',
name='type',
field=models.CharField(max_length=50, choices=[(b'boolean', b'Yes/No'), (b'boolean_sortpriority', b'Yes/No with sort priority'), (b'text', b'Short text'), (b'textarea', b'Long text'), (b'integer', b'Integer value')]),
),
]
11 changes: 11 additions & 0 deletions grants/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.core.validators import MinValueValidator
from django.db import models
from urlman import Urls

Expand Down Expand Up @@ -89,6 +90,7 @@ class Question(models.Model):

TYPE_CHOICES = [
("boolean", "Yes/No"),
("boolean_sortpriority", "Yes/No with sort priority"),
("text", "Short text"),
("textarea", "Long text"),
("integer", "Integer value"),
Expand All @@ -98,6 +100,7 @@ class Question(models.Model):
type = models.CharField(max_length=50, choices=TYPE_CHOICES)
question = models.TextField()
required = models.BooleanField(default=False)
sort_priority = models.IntegerField(null=True, blank=True, validators=[MinValueValidator(1)])
order = models.IntegerField(default=0)

class urls(Urls):
Expand Down Expand Up @@ -135,6 +138,13 @@ def average_score(self):
else:
return sum(scores) / float(len(scores))

def sort_priority(self):
priority = self.average_score()
priority_answers = self.answers.filter(question__type="boolean_sortpriority", answer='Yes')
answer_priority = sum([answer.question.sort_priority for answer in priority_answers])
priority += answer_priority * 1000
return priority

def variance(self):
data = [s.score for s in self.scores.all() if s.score]
n = len(data)
Expand Down Expand Up @@ -177,6 +187,7 @@ class Answer(models.Model):
answer = models.TextField()

class Meta:
ordering = ['question']
unique_together = [
("applicant", "question"),
]
Expand Down
9 changes: 6 additions & 3 deletions grants/views/program.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,18 @@ def get_queryset(self):
for applicant in applicants:
applicant.has_scored = applicant.scores.filter(user=self.request.user).exists()
if applicant.has_scored:
applicant.average_score = applicant.average_score()
applicant.sort_priority = applicant.sort_priority()
else:
applicant.average_score = -1
applicant.sort_priority = -1
# Enrich with additional fields
applicant.extra_answers = applicant.answers.filter(question__type="boolean_sortpriority")
if self.sort == "score":
applicants.sort(key=lambda a: a.average_score, reverse=True)
applicants.sort(key=lambda a: a.sort_priority, reverse=True)
return applicants

def get_context_data(self):
context = super(ProgramApplicants, self).get_context_data()
context['extra_fields'] = self.program.questions.filter(type="boolean_sortpriority")
context['sort'] = self.sort
return context

Expand Down
10 changes: 10 additions & 0 deletions templates/_sortpriority_explanation.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<p><em>
The 'Yes/No with sort priority' is a special type, and requires you to enter the sort priority.
When sorting your applicants on their score in the applicant overview, they will first be ordered on this
sort priority, then on their average score. This makes it easier to prioritise certain applicants over others
with higher scores, like underrepresented groups or speakers. Being able to set a priority allows you to
have multiple questions like this.
For example, if you'd like to prioritise underrepresented groups,
make this a Yes/No field in your CSV, load it into your sort priority question, and assign a priority of 1.
Fields with this type will also be included in the applicant overview table.
</em></p>
10 changes: 10 additions & 0 deletions templates/program-applicants.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ <h1>Applicants</h1>
<tr>
<th>Name</th>
<th><a href="?sort=applied">Applied {% if sort == "applied" %}<i class="fa fa-chevron-circle-down"></i>{% endif %}</a></th>
{% for field in extra_fields %}
<th>{{ field }}</th>
{% endfor %}
<th><a href="?sort=score">Score {% if sort == "score" %}<i class="fa fa-chevron-circle-down"></i>{% endif %}</a></th>
<th>Allocated</th>
<th></th>
Expand All @@ -18,6 +21,13 @@ <h1>Applicants</h1>
<tr>
<td>{{ applicant.name }}</td>
<td class="small">{{ applicant.applied|date:"j M Y P" }}</td>
{% for answer in applicant.extra_answers %}
{% if answer.answer == 'Yes' %}
<td><i class="fa fa-check green"></i></td>
{% else %}
<td><i class="fa fa-times red"></i></td>
{% endif %}
{% endfor %}
{% if applicant.has_scored %}
<td>
{{ applicant.average_score|floatformat:"1"|default:"-" }}
Expand Down
1 change: 1 addition & 0 deletions templates/program-question-edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<h3>Edit question</h3>
<form action="." method="POST">
{% include "_form.html" with include_delete=question.can_delete %}
{% include "_sortpriority_explanation.html" %}
</form>
</div>
</div>
Expand Down
10 changes: 10 additions & 0 deletions templates/program-questions.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,21 @@ <h1>Questions</h1>
<th>Question</th>
<th>Type</th>
<th>Required</th>
<th>Sort priority</th>
<th>Actions</th>
</tr>
<tr>
<td>Name</td>
<td>Text</td>
<td><i class="fa fa-check green"></i></td>
<td class="empty">(none)</td>
<td class="empty">Built in</td>
</tr>
<tr>
<td>Email</td>
<td>Email</td>
<td><i class="fa fa-check green"></i></td>
<td class="empty">(none)</td>
<td class="empty">Built in</td>
</tr>
{% for question in questions %}
Expand All @@ -37,6 +40,12 @@ <h1>Questions</h1>
<i class="fa fa-times red"></i>
{% endif %}
</td>
{% if question.sort_priority %}
<td>{{ question.sort_priority }}</td>
{% else %}
<td class="empty">(none)</td>
{% endif %}

<td>
<a href="{{ question.urls.edit }}" class="button">Edit</a>
</td>
Expand All @@ -46,6 +55,7 @@ <h1>Questions</h1>
<h3>Create a new question</h3>
<form action="." method="POST">
{% include "_form.html" %}
{% include "_sortpriority_explanation.html" %}
</form>
</div>
</div>
Expand Down