Allow only 5 maintainers, one needs to be your own + help text

This commit is contained in:
Sebastian Lohff 2017-03-31 00:50:28 +02:00
parent f25d07ccf7
commit e9aeca1f32
2 changed files with 17 additions and 15 deletions

View File

@ -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:

View File

@ -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"):