Skip to content

Commit

Permalink
#M01. Judging automation - calculate and display total scores (#120)
Browse files Browse the repository at this point in the history
* Update function check_projects_scores to calculate scores and sort teams

Create the final_score.html to show the score table, add css rules to style it.

Create an url path for final_score.

* #M01. Judging automation - Add  <thead> and  <tbody> for the table

Style the elements in the new format.
  • Loading branch information
natalijabujevic0708 authored Nov 30, 2020
1 parent 15ad19e commit af73575
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 10 deletions.
26 changes: 26 additions & 0 deletions hackathon/templates/hackathon/final_score.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{% extends "base.html" %}
{% load static %}
{% block content %}
<h1 class="table_titles"> {{ hackathon }} <br> Final Scores </h1>
{% if sorted_teams_scores %}
<table class="final_score_table">
<thead>
<tr class="table_titles">
<th> Project </th>
<th> Score </th>
</tr>
</thead>
<tbody>
{% for team in sorted_teams_scores %}
<tr>
<td>{{ team.0 }}</td>
<td>{{ team.1 }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% else %}
<strong>There are no final scores for this Hackathon.</strong>
{% endif %}
{% endblock %}

4 changes: 3 additions & 1 deletion hackathon/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
delete_hackathon,
HackathonDetailView,
enroll_toggle,
judging
judging,
check_projects_scores
)

urlpatterns = [
path('', HackathonListView.as_view(), name="hackathon-list"),
path("<int:hack_id>/team/<int:team_id>/judging/", judging, name="judging"),
path("<int:hack_id>/final_score", check_projects_scores, name="final_score"),
path("create_hackathon", create_hackathon, name='create_hackathon'),
path("<int:hackathon_id>/update_hackathon", update_hackathon, name="update_hackathon"),
path("<int:hackathon_id>/delete_hackathon", delete_hackathon, name="delete_hackathon"),
Expand Down
41 changes: 32 additions & 9 deletions hackathon/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from django.http import JsonResponse, HttpResponse
from django.utils import timezone

from .models import Hackathon, HackTeam, HackProject, HackProjectScore, HackProjectScoreCategory
from .models import Hackathon, HackTeam, HackProject, HackProjectScore, HackProjectScoreCategory, HackAwardCategory
from .forms import HackathonForm


Expand Down Expand Up @@ -84,7 +84,7 @@ def judging(request, hack_id, team_id):
hack_project_score_category=score_category,
)
team_score.save()
check_projects_scores(hackathon, score_categories)
return check_projects_scores(request, hack_id)

context = {
'hackathon': hackathon,
Expand All @@ -95,11 +95,20 @@ def judging(request, hack_id, team_id):
return render(request, 'hackathon/judging.html', context)


def check_projects_scores(hackathon, score_categories):
@login_required
def check_projects_scores(request, hack_id):
""" When a judge submits the score, check if all projects in the Hackathon
were scored by all the judges in all the categories by comparing the number of
objects in HackProjectScore for each projects to the required number of objects """
objects in HackProjectScore for each projects to the required number of objects.
If all projects weren't scored, render final_score.html without the score table.
If all the projects were scored, calculate the total score for each team, sort the teams by scores
and render final_score.html with the score table.
"""
score_categories = HackProjectScoreCategory.objects.all()
hackathon = get_object_or_404(Hackathon, pk=hack_id)
judges = hackathon.judges.count()
number_categories = score_categories.count()
projects = list(HackTeam.objects.filter(
Expand All @@ -109,12 +118,26 @@ def check_projects_scores(hackathon, score_categories):
number_of_objects = judges * number_categories

for project in projects:
if HackProjectScore.objects.filter(project=project).count() == number_of_objects:
continue
else:
return redirect("hackathon:hackathon-list")
if HackProjectScore.objects.filter(project=project).count() != number_of_objects:
return render(request, 'hackathon/final_score.html', {"hackathon": hackathon.display_name})

# Calculate total score for each team
scores_list = []
for project in projects:
total_score = sum(list(HackProjectScore.objects.filter(
project=project).values_list("score", flat=True)))
scores_list.append(total_score)

# Pair the teams with the total score
team_scores = {}
for i, score in enumerate(scores_list):
team_scores[str(HackProject.objects.get(id=projects[i]))] = score

# Sort the teams by score
sorted_teams_scores = sorted(
team_scores.items(), key=lambda x: x[1], reverse=True)

# Call a function to calculate the scores and redirect to a view that will show a list of all of the projects ranked by their score
return render(request, 'hackathon/final_score.html', {'sorted_teams_scores': sorted_teams_scores, "hackathon": hackathon.display_name})


def create_hackathon(request):
Expand Down
29 changes: 29 additions & 0 deletions static/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,35 @@ html .all-auth-container {
text-align: left;

}
/* Final Score page */

tbody >:first-child {
background-color: var(--p-orange);
color: var(--white);
}

.table_titles, .final_score_table {
text-align: center;
}

h1.table_titles{
margin-bottom: 5vh;
font-size: 6vh;
}

.final_score_table {
width: 75%;
margin: 5vh auto;
border: 1px solid var(--p-grey);
}

.final_score_table tr {
border-bottom: 1px solid var(--p-grey);
}

.final_score_table tr th {
padding: 2vh
}

.bg-p-blue {
background-color: var(--p-blue);
Expand Down

0 comments on commit af73575

Please sign in to comment.