From 4ee2e1b9406ac71c82e12b79f82f879105cf4d6f Mon Sep 17 00:00:00 2001 From: cuom1999 Date: Thu, 2 May 2024 23:33:18 -0500 Subject: [PATCH] Fix inconsistency of contest is_organization_private --- judge/admin/contest.py | 4 ---- judge/models/contest.py | 17 +++++++++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/judge/admin/contest.py b/judge/admin/contest.py index b63e3da..4e58ec4 100644 --- a/judge/admin/contest.py +++ b/judge/admin/contest.py @@ -316,10 +316,6 @@ class ContestAdmin(CompareVersionAdmin): # Only rescored if we did not already do so in `save_model` if not self._rescored and any(formset.has_changed() for formset in formsets): self._rescore(form.cleaned_data["key"]) - obj = form.instance - obj.is_organization_private = obj.organizations.count() > 0 - obj.is_private = obj.private_contestants.count() > 0 - obj.save() def has_change_permission(self, request, obj=None): if not request.user.has_perm("judge.edit_own_contest"): diff --git a/judge/models/contest.py b/judge/models/contest.py index 948bcb5..a83af5c 100644 --- a/judge/models/contest.py +++ b/judge/models/contest.py @@ -2,11 +2,14 @@ from django.core.exceptions import ValidationError from django.core.validators import MaxValueValidator, MinValueValidator, RegexValidator from django.db import models, transaction from django.db.models import CASCADE, Q +from django.db.models.signals import m2m_changed from django.urls import reverse from django.utils import timezone from django.utils.functional import cached_property from django.utils.translation import gettext, gettext_lazy as _ from django.contrib.contenttypes.fields import GenericRelation +from django.dispatch import receiver + from jsonfield import JSONField from lupa import LuaRuntime from moss import ( @@ -658,6 +661,20 @@ class Contest(models.Model, PageVotable, Bookmarkable): verbose_name_plural = _("contests") +@receiver(m2m_changed, sender=Contest.organizations.through) +def update_organization_private(sender, instance, **kwargs): + if kwargs["action"] in ["post_add", "post_remove", "post_clear"]: + instance.is_organization_private = instance.organizations.exists() + instance.save(update_fields=["is_organization_private"]) + + +@receiver(m2m_changed, sender=Contest.private_contestants.through) +def update_private(sender, instance, **kwargs): + if kwargs["action"] in ["post_add", "post_remove", "post_clear"]: + instance.is_private = instance.private_contestants.exists() + instance.save(update_fields=["is_private"]) + + class ContestParticipation(models.Model): LIVE = 0 SPECTATE = -1