Disallow renaming regtypes referenced in sponsorsihp benefits
authorMagnus Hagander <magnus@hagander.net>
Sun, 21 Jan 2024 13:47:05 +0000 (14:47 +0100)
committerMagnus Hagander <magnus@hagander.net>
Sun, 21 Jan 2024 13:51:56 +0000 (14:51 +0100)
We already had protection in place to prevent it from being deleted,
but it was possible to rename a regtype when it was referened in a
sponsorship benefit which would then cause a crash when trying to claim
it. This canont be enforced with a foreign key as it's set inside a json
object, so we need an explicit check for it.

postgresqleu/confreg/backendforms.py

index a414b9f966d0ecc873ca795b56469f9f4d804288..676ab5c55ab5b73aefd936d98dbcc3155aff7803 100644 (file)
@@ -31,6 +31,9 @@ from postgresqleu.util.time import datetime_string
 
 import postgresqleu.accounting.models
 
+from postgresqleu.confsponsor.models import SponsorshipBenefit
+from postgresqleu.confsponsor.benefitclasses import get_benefit_id
+
 from postgresqleu.confreg.models import Conference, ConferenceRegistration, ConferenceAdditionalOption
 from postgresqleu.confreg.models import RegistrationClass, RegistrationType, RegistrationDay
 from postgresqleu.confreg.models import ConferenceFeedbackQuestion, Speaker
@@ -480,6 +483,19 @@ class BackendRegistrationTypeForm(BackendForm):
             validate_special_reg_type_setup(self.cleaned_data['specialtype'], self.cleaned_data)
         return self.cleaned_data['specialtype']
 
+    def clean_regtype(self):
+        newtype = self.cleaned_data['regtype']
+        if self.instance and self.instance.regtype and self.instance.regtype != newtype:
+            # Disallow changing the regtype if there is a sponsorship benefit that uses it,
+            # since this link is done on name in a json object, and not a foreign key.
+            if SponsorshipBenefit.objects.filter(
+                    level__conference=self.instance.conference,
+                    benefit_class=get_benefit_id('entryvouchers.EntryVouchers'),
+                    class_parameters__type=self.instance.regtype,
+            ).exists():
+                raise ValidationError("A sponsorship benefit is using the registration type '{}', cannot rename.".format(self.instance.regtype))
+        return newtype
+
     @classmethod
     def copy_from_conference(self, targetconf, sourceconf, idlist):
         # Registration types are copied straight over, but we disallow duplicates. We also