From 88845aebd8b54567a917046f6a07b57f8a6d4ff4 Mon Sep 17 00:00:00 2001 From: cuom1999 Date: Tue, 3 Sep 2024 10:48:01 -0500 Subject: [PATCH] Delete contest in form before adding --- judge/models/contest.py | 9 ++++++++- judge/views/organization.py | 7 +++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/judge/models/contest.py b/judge/models/contest.py index e775d8d..914e79f 100644 --- a/judge/models/contest.py +++ b/judge/models/contest.py @@ -19,7 +19,7 @@ from moss import ( MOSS_LANG_PYTHON, MOSS_LANG_PASCAL, ) -from datetime import timedelta +from datetime import timedelta, datetime from judge import contest_format from judge.models.problem import Problem @@ -371,6 +371,13 @@ class Contest(models.Model, PageVotable, Bookmarkable): ) def save(self, *args, **kwargs): + earliest_start_time = datetime(2020, 1, 1).replace(tzinfo=timezone.utc) + if self.start_time < earliest_start_time: + self.start_time = earliest_start_time + + if self.end_time < self.start_time: + self.end_time = self.start_time + timedelta(hours=1) + one_year_later = self.start_time + timedelta(days=365) if self.end_time > one_year_later: self.end_time = one_year_later diff --git a/judge/views/organization.py b/judge/views/organization.py index 6dbcb83..672bf12 100644 --- a/judge/views/organization.py +++ b/judge/views/organization.py @@ -991,12 +991,15 @@ class EditOrganizationContest( return res problem_formset = self.get_problem_formset(True) if problem_formset.is_valid(): + for problem_form in problem_formset: + if problem_form.cleaned_data.get("DELETE") and problem_form.instance.pk: + problem_form.instance.delete() + for problem_form in problem_formset.save(commit=False): if problem_form: problem_form.contest = self.contest problem_form.save() - for problem_form in problem_formset.deleted_objects: - problem_form.delete() + super().post(request, *args, **kwargs) return HttpResponseRedirect( reverse(