Skip to content

Commit

Permalink
Merge branch 'master' into thuc/org-mixins
Browse files Browse the repository at this point in the history
  • Loading branch information
leduythuccs authored Oct 15, 2023
2 parents 3e2e352 + 9e97b9b commit 867c1fc
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 32 deletions.
55 changes: 34 additions & 21 deletions judge/management/commands/import_polygon_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,25 +636,29 @@ def input_choice(prompt, choices):


@transaction.atomic
def create_problem(problem_meta):
print('Creating problem in database')
problem = Problem(
code=problem_meta['code'],
name=problem_meta['name'],
time_limit=problem_meta['time_limit'],
memory_limit=problem_meta['memory_limit'],
description=problem_meta['description'],
partial=problem_meta['partial'],
group=ProblemGroup.objects.order_by('id').first(), # Uncategorized
points=0.0,
)
def update_or_create_problem(problem_meta, do_update):
if do_update:
print('Updating problem in database')
else:
print('Creating problem in database')
problem, _ = Problem.objects.update_or_create(code=problem_meta['code'], defaults={
'code': problem_meta['code'],
'name': problem_meta['name'],
'time_limit': problem_meta['time_limit'],
'memory_limit': problem_meta['memory_limit'],
'description': problem_meta['description'],
'partial': problem_meta['partial'],
'group': ProblemGroup.objects.order_by('id').first(), # Uncategorized
'points': 0.0,
})
problem.save()
problem.allowed_languages.set(Language.objects.filter(include_in_problem=True))
problem.authors.set(problem_meta['authors'])
problem.curators.set(problem_meta['curators'])
problem.types.set([ProblemType.objects.order_by('id').first()]) # Uncategorized
problem.save()

ProblemTranslation.objects.filter(problem=problem).delete()
for tran in problem_meta['translations']:
ProblemTranslation(
problem=problem,
Expand All @@ -663,6 +667,7 @@ def create_problem(problem_meta):
description=tran['description'],
).save()

Solution.objects.filter(problem=problem).delete()
if problem_meta['tutorial'] != '':
Solution(
problem=problem,
Expand All @@ -672,13 +677,13 @@ def create_problem(problem_meta):
).save()

with open(problem_meta['zipfile'], 'rb') as f:
problem_data = ProblemData(
problem=problem,
zipfile=File(f),
grader=problem_meta['grader'],
checker=problem_meta['checker'],
grader_args=json.dumps(problem_meta['grader_args']),
)
problem_data, _ = ProblemData.objects.update_or_create(problem=problem, defaults={
'problem': problem,
'zipfile': File(f),
'grader': problem_meta['grader'],
'checker': problem_meta['checker'],
'grader_args': json.dumps(problem_meta['grader_args']),
})
problem_data.save()

if problem_meta['checker'] == 'bridged':
Expand All @@ -695,6 +700,8 @@ def create_problem(problem_meta):
problem_data.custom_grader = File(f)
problem_data.save()

ProblemTestCase.objects.filter(dataset=problem).delete()

order = 0

for batch in problem_meta['batches'].values():
Expand Down Expand Up @@ -751,6 +758,7 @@ class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument('package', help='path to package in zip format')
parser.add_argument('code', help='problem code')
parser.add_argument('--update', help='update the problem if it exists', action='store_true')
parser.add_argument('--authors', help='username of problem author', nargs='*')
parser.add_argument('--curators', help='username of problem curator', nargs='*')

Expand All @@ -764,13 +772,18 @@ def handle(self, *args, **options):
if pandoc_get_version() < (3, 0, 0):
raise CommandError('pandoc version must be at least 3.0.0')

do_update = options['update']

# Let's validate the problem code right now.
# We don't want to have done everything and still fail because
# of invalid problem code.
problem_code = options['code']
Problem._meta.get_field('code').run_validators(problem_code)
if Problem.objects.filter(code=problem_code).exists():
raise CommandError(f'problem with code {problem_code} already exists')
if not do_update:
raise CommandError(f'problem with code {problem_code} already exists')
elif do_update:
raise CommandError(f'problem with code {problem_code} not found')

package = zipfile.ZipFile(options['package'], 'r')
if 'problem.xml' not in package.namelist():
Expand Down Expand Up @@ -810,7 +823,7 @@ def handle(self, *args, **options):
parse_assets(problem_meta, root, package)
parse_tests(problem_meta, root, package)
parse_statements(problem_meta, root, package)
create_problem(problem_meta)
update_or_create_problem(problem_meta, do_update)
except Exception:
# Remove imported images
for image_url in problem_meta['image_cache'].values():
Expand Down
11 changes: 0 additions & 11 deletions judge/models/problem_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,6 @@ class ProblemData(models.Model):
grader_args = models.TextField(verbose_name=_('grader arguments'), blank=True,
help_text=_('grader arguments as a JSON object'))

__original_zipfile = None

def __init__(self, *args, **kwargs):
super(ProblemData, self).__init__(*args, **kwargs)
self.__original_zipfile = self.zipfile

def save(self, *args, **kwargs):
if self.zipfile != self.__original_zipfile:
self.__original_zipfile.delete(save=False)
return super(ProblemData, self).save(*args, **kwargs)

def has_yml(self):
return problem_data_storage.exists('%s/init.yml' % self.problem.code)

Expand Down

0 comments on commit 867c1fc

Please sign in to comment.