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