Skip to content

Commit

Permalink
(SIO-1832) Add button for invalidating ranking
Browse files Browse the repository at this point in the history
It lets admin manually control the ranking and invalidate it in case
something goes wrong during the constest.

Change-Id: I0b5ef6cffb70b0e66c4c17d81d7af7c83f0bef7d
  • Loading branch information
deutzia committed Jan 29, 2019
1 parent 55e5226 commit d7c145a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
4 changes: 4 additions & 0 deletions oioioi/rankings/templates/rankings/ranking_view.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
<span class="glyphicon glyphicon-download"></span>
{% trans "Export to CSV" %}
</a>
<a role="button" class="btn btn-sm btn-default"
href="#" data-post-url="{% url 'ranking_invalidate' contest_id=contest.id key=key %}">
{% trans "Regenerate ranking" %}
</a>
{% endif %}
{% if form and user.is_authenticated and not is_admin %}
<a role="button" class="btn btn-sm btn-default"
Expand Down
25 changes: 25 additions & 0 deletions oioioi/rankings/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ def test_ranking_view(self):
with fake_time(datetime(2015, 8, 5, tzinfo=utc)):
response = self.client.get(url)
self.assertContains(response, 'Export to CSV')
self.assertContains(response, 'Regenerate ranking')

# Check that Admin is filtered out.
self.client.login(username='test_user')
Expand All @@ -157,6 +158,7 @@ def test_ranking_view(self):
self.assertFalse(re.search(USER_CELL_PATTERN % ('Test Admin',),
response.content))
self.assertNotContains(response, 'Export to CSV')
self.assertNotContains(response, 'Regenerate ranking')

# Ok, so now we make test_admin a regular user.
admin = User.objects.get(username='test_admin')
Expand Down Expand Up @@ -244,6 +246,29 @@ def test_ranking_csv_view(self):
for task in ['zad2', 'zad3', 'zad3']:
self.assertNotContains(response, task)

def test_invalidate_view(self):
contest = Contest.objects.get()
url = reverse('ranking_invalidate', kwargs={'contest_id': contest.id,
'key': 'key'})

self.assertTrue(self.client.login(username='test_user'))
with fake_time(datetime(2019, 1, 27, tzinfo=utc)):
check_not_accessible(self, url)

self.assertTrue(self.client.login(username='test_admin'))
with fake_time(datetime(2019, 1, 27, tzinfo=utc)):
ranking, _ = Ranking.objects.get_or_create(contest=contest,
key='admin#key', needs_recalculation=False)
ranking.save()
self.assertTrue(ranking.is_up_to_date())
recalc = choose_for_recalculation()
self.assertIsNone(recalc)
response = self.client.post(url, key='key')
ranking.refresh_from_db()
self.assertFalse(ranking.is_up_to_date())
recalc = choose_for_recalculation()
self.assertIsNotNone(recalc)


class MockRankingController(DefaultRankingController):
recalculation_result = ('serialized', ['1st', '2nd', '3rd'])
Expand Down
2 changes: 2 additions & 0 deletions oioioi/rankings/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@
name='ranking'),
url(r'^ranking/(?P<key>[a-z0-9_-]+)/csv/$', views.ranking_csv_view,
name='ranking_csv'),
url(r'^ranking/(?P<key>[a-z0-9_-]+)/invalidate/$',
views.ranking_invalidate_view, name='ranking_invalidate'),
]
11 changes: 11 additions & 0 deletions oioioi/rankings/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.shortcuts import redirect
from django.template.response import TemplateResponse
from django.utils.translation import ugettext_lazy as _
from django.views.decorators.http import require_POST
from six.moves import zip

from oioioi.base.menu import menu_registry
Expand All @@ -14,6 +15,7 @@
from oioioi.contests.utils import (can_enter_contest, contest_exists,
is_contest_admin)
from oioioi.rankings.forms import FilterUsersInRankingForm
from oioioi.rankings.models import Ranking


@make_request_condition
Expand Down Expand Up @@ -119,3 +121,12 @@ def ranking_csv_view(request, key):
raise Http404

return rcontroller.render_ranking_to_csv(request, key)

@enforce_condition(contest_exists & is_contest_admin)
@require_POST
def ranking_invalidate_view(request, key):
rcontroller = request.contest.controller.ranking_controller()
full_key = rcontroller.get_full_key(request, key)
ranking = Ranking.objects.filter(key=full_key)
Ranking.invalidate_queryset(ranking)
return redirect('ranking', key=key)

0 comments on commit d7c145a

Please sign in to comment.