From e9aeca1f321b5a5cd6439fac77de04be1ebe56fd Mon Sep 17 00:00:00 2001 From: Sebastian Lohff Date: Fri, 31 Mar 2017 00:50:28 +0200 Subject: [PATCH] Allow only 5 maintainers, one needs to be your own + help text --- whoisdb/forms.py | 30 ++++++++++++++++-------------- whoisdb/models.py | 2 +- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/whoisdb/forms.py b/whoisdb/forms.py index 9dc7c8d..3d938d1 100644 --- a/whoisdb/forms.py +++ b/whoisdb/forms.py @@ -60,29 +60,31 @@ class MntFormMixin(object): if not hasattr(self, "_create"): self._create = not (instance and instance.pk) - mntWhens = [When(auth=self._user, then=2)] - if not self._create: - mntWhens.append(When(handle__in=instance.mnt_by.all().values_list("handle"), then=1)) - mntQs = Maintainer.objects.annotate(card=Case(*mntWhens, default=0, output_field=IntegerField())).order_by("-card") - # NOTE: We cannot use distinct on a field as some db backends don't support it - #self.fields["mnt_by"].queryset = mntQs.distinct("handle") + mntQs = orderQueryset(Maintainer.objects.all(), Q(auth=self._user), Q(pk__in=instance.mnt_by.all()) if not self._create else None) self.fields["mnt_by"].queryset = mntQs if "mnt_lower" in self.fields: self.fields["mnt_lower"].queryset = mntQs - def clean(self): cleaned_data = super(MntFormMixin, self).clean() - if not self.errors and self._create: - mnts = self._user.maintainer_set.all() + if not self.errors: + if self._create: + # at least one own mnt on creation + mnts = self._user.maintainer_set.all() + + for mnt in cleaned_data['mnt_by']: + if mnt in mnts: + break + else: + raise forms.ValidationError("On object creation at least one maintainer needs to be under your control") + + if cleaned_data['mnt_by'].count() > 5 or "mnt_lower" in self.fields and cleaned_data['mnt_lower'].count() > 5: + raise forms.ValidationError("Currently only 5 MNTs per object allowed. If you need more ask @ irc/rrequest and state your use case") + + return cleaned_data - for mnt in cleaned_data['mnt_by']: - if mnt in mnts: - break - else: - raise forms.ValidationError("On object creation at least one maintainer needs to be under your control") class MntForm(WhoisObjectFormMixin, forms.ModelForm): class Meta: diff --git a/whoisdb/models.py b/whoisdb/models.py index 91aa196..a8e06cd 100644 --- a/whoisdb/models.py +++ b/whoisdb/models.py @@ -116,7 +116,7 @@ class MntdObject(WhoisObject): class Meta: abstract = True - mnt_by = models.ManyToManyField(Maintainer) + mnt_by = models.ManyToManyField(Maintainer, help_text="You can select multiple maintainers here") def canEdit(self, user): if not hasattr(user, "maintainer_set"):