From acdf94a8c977dc417169fade04004b8f75c6c435 Mon Sep 17 00:00:00 2001 From: cuom1999 Date: Sun, 17 Mar 2024 02:39:20 -0500 Subject: [PATCH] Fix problem admin error and give pdf problem more time to render --- judge/admin/problem.py | 17 +++++++++++++++-- templates/problem/problem.html | 4 +++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/judge/admin/problem.py b/judge/admin/problem.py index 550ced4..bf590db 100644 --- a/judge/admin/problem.py +++ b/judge/admin/problem.py @@ -1,8 +1,8 @@ from operator import attrgetter from django import forms -from django.contrib import admin -from django.db import transaction +from django.contrib import admin, messages +from django.db import transaction, IntegrityError from django.db.models import Q, Avg, Count from django.db.models.aggregates import StdDev from django.forms import ModelForm, TextInput @@ -11,6 +11,7 @@ from django.utils.html import format_html from django.utils.translation import gettext, gettext_lazy as _, ungettext from django_ace import AceWidget from django.utils import timezone +from django.core.exceptions import ValidationError from reversion.admin import VersionAdmin from reversion_compare.admin import CompareVersionAdmin @@ -56,6 +57,16 @@ class ProblemForm(ModelForm): } ) + def clean_code(self): + code = self.cleaned_data.get("code") + if self.instance.pk: + return code + + if Problem.objects.filter(code=code).exists(): + raise ValidationError(_("A problem with this code already exists.")) + + return code + def clean(self): memory_unit = self.cleaned_data.get("memory_unit", "KB") if memory_unit == "MB": @@ -131,6 +142,7 @@ class LanguageLimitInline(admin.TabularInline): model = LanguageLimit fields = ("language", "time_limit", "memory_limit", "memory_unit") form = LanguageLimitInlineForm + extra = 0 class LanguageTemplateInlineForm(ModelForm): @@ -145,6 +157,7 @@ class LanguageTemplateInline(admin.TabularInline): model = LanguageTemplate fields = ("language", "source") form = LanguageTemplateInlineForm + extra = 0 class ProblemSolutionForm(ModelForm): diff --git a/templates/problem/problem.html b/templates/problem/problem.html index 1bf4ab9..7487553 100644 --- a/templates/problem/problem.html +++ b/templates/problem/problem.html @@ -67,7 +67,9 @@ $('#raw_problem').attr('src', '{{problem.code}}/raw') $('#raw_problem').on('load', function() { renderKatex(frames['raw_problem'].document); - frames['raw_problem'].print(); + setTimeout(() => { + frames['raw_problem'].print(); + }, 500); }); } else {