Skip to content

Commit

Permalink
SIO-1517 Sometimes package reupload creates second ProblemInstance
Browse files Browse the repository at this point in the history
Change-Id: I3669cb014b86e99da07e72a25e247f1e5efb4351
  • Loading branch information
Tommalla committed Jun 16, 2014
1 parent a027563 commit b09a43d
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 6 deletions.
17 changes: 14 additions & 3 deletions oioioi/problems/models.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import os.path

from django.core.validators import validate_slug
from django.db import models
from django.db.models.signals import post_save
from django.db.models.signals import post_save, pre_delete
from django.dispatch import receiver
from django.utils.translation import ugettext_lazy as _
from django.utils.text import get_valid_filename

from oioioi.base.fields import DottedNameField
from oioioi.base.utils import get_object_by_dotted_name
from oioioi.filetracker.fields import FileField

import os.path


def make_problem_filename(instance, filename):
if not isinstance(instance, Problem):
Expand Down Expand Up @@ -66,6 +67,16 @@ def _call_controller_adjust_problem(sender, instance, raw, **kwargs):
instance.controller.adjust_problem()


@receiver(pre_delete, sender=Problem)
def _check_problem_instance_integrity(sender, instance, **kwargs):
from oioioi.contests.models import ProblemInstance
pis = ProblemInstance.objects.filter(problem=instance)
if pis.count() > 1:
raise RuntimeError("Multiple ProblemInstance objects for a single "
"problem. Please reopen "
"https://jira.sio2project.mimuw.edu.pl/browse/SIO-1517")


class ProblemStatement(models.Model):
"""Represents a file containing problem statement.
Expand Down
50 changes: 49 additions & 1 deletion oioioi/problems/tests.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import os.path
import urllib

from django.contrib.auth.models import User, Permission
from django.contrib.contenttypes.models import ContentType
from django.test import TestCase
from django.core.urlresolvers import reverse
from nose.tools import nottest

from oioioi.base.tests import check_not_accessible
from oioioi.contests.models import Contest
from oioioi.contests.models import Contest, ProblemInstance
from oioioi.filetracker.tests import TestStreamingMixin
from oioioi.problems.controllers import ProblemController
from oioioi.problems.models import Problem, ProblemStatement, \
Expand All @@ -30,6 +35,13 @@ def test_make_problem_filename(self):
'problems/12/hej.txt')


@nottest
def get_test_filename(name):
import oioioi.sinolpack
return os.path.join(os.path.dirname(oioioi.sinolpack.__file__), 'files',
name)


class TestProblemViews(TestCase, TestStreamingMixin):
fixtures = ['test_users', 'test_contest', 'test_full_package']

Expand Down Expand Up @@ -103,3 +115,39 @@ def test_problem_permissions(self):
self._test_problem_permissions()
self.client.login(username='test_user')
self._test_problem_permissions()

def test_problem_submission_limit_changed(self):
ProblemInstance.objects.all().delete()
Problem.objects.all().delete()

contest = Contest.objects.get()
filename = get_test_filename('test_simple_package.zip')
self.client.login(username='test_admin')
url = reverse('oioioiadmin:problems_problem_add')
response = self.client.get(url, {'contest_id': contest.id},
follow=True)
url = response.redirect_chain[-1][0]
self.assertEqual(response.status_code, 200)

response = self.client.post(url,
{'package_file': open(filename, 'rb')}, follow=True)
self.assertEqual(response.status_code, 200)
self.assertEqual(Problem.objects.count(), 1)
self.assertEqual(ProblemInstance.objects.count(), 1)

problem = ProblemInstance.objects.get().problem
contest.default_submissions_limit += 100
contest.save()

url = reverse('add_or_update_contest_problem',
kwargs={'contest_id': contest.id}) + '?' + \
urllib.urlencode({'problem': problem.id})
response = self.client.get(url, follow=True)
url = response.redirect_chain[-1][0]
self.assertEqual(response.status_code, 200)
response = self.client.post(url,
{'package_file': open(filename, 'rb')}, follow=True)
self.assertEqual(response.status_code, 200)

pis = ProblemInstance.objects.filter(problem=problem)
self.assertEqual(pis.count(), 1)
7 changes: 5 additions & 2 deletions oioioi/problems/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,11 @@ def add_or_update_problem_view(request, contest_id=None):
problem.contest = contest
problem.save()
pi, created = ProblemInstance.objects.get_or_create(
problem=problem, contest=contest,
submissions_limit=contest.default_submissions_limit)
problem=problem, contest=contest)
if created:
pi.submissions_limit = contest.default_submissions_limit
pi.save()

contest.controller.process_uploaded_problem(problem, pi,
is_new=(not existing_problem))
if not pi.round:
Expand Down

0 comments on commit b09a43d

Please sign in to comment.