From 5db8759b4fb34fe5fba88f962a40ce0f92c8152a Mon Sep 17 00:00:00 2001 From: Paul Eggleton Date: Wed, 18 Apr 2018 09:43:41 +1200 Subject: rrs: validate that a layerbranch is only part of one plan The processing code can't currently handle if a layerbranch is part of more than one plan, so disallow that. Signed-off-by: Paul Eggleton --- rrs/admin.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/rrs/admin.py b/rrs/admin.py index 0831a9001d..682a5a6c04 100644 --- a/rrs/admin.py +++ b/rrs/admin.py @@ -9,6 +9,7 @@ from django.utils.functional import curry from django.contrib import admin from django.contrib.admin import DateFieldListFilter from django.forms.models import BaseInlineFormSet +from django.core.exceptions import ValidationError from rrs.models import Release, Milestone, Maintainer, RecipeMaintainerHistory, \ RecipeMaintainer, RecipeDistro, RecipeUpgrade, RecipeUpstream, \ @@ -41,6 +42,29 @@ class MaintenancePlanLayerBranchFormSet(BaseInlineFormSet): form.fields['python3_environment'].initial = py3env return form + def clean(self): + super(MaintenancePlanLayerBranchFormSet, self).clean() + total_checked = 0 + + for form in self.forms: + if not form.is_valid(): + return + if form.cleaned_data and not form.cleaned_data.get('DELETE'): + layerbranch = form.cleaned_data['layerbranch'] + if not layerbranch: + raise ValidationError('You must select a layerbranch') + # Only allow one plan per layer + # NOTE: This restriction is in place because we don't have enough safeguards in the + # processing code to avoid processing a layer multiple times if it's part of + # more than one plan, and there may be other challenges. For now, just keep it simple. + mplayerbranches = layerbranch.maintenanceplanlayerbranch_set.all() + if form.instance.pk is not None: + mplayerbranches = mplayerbranches.exclude(id=form.instance.id) + if mplayerbranches.exists(): + raise ValidationError('A layer branch can only be part of one maintenance plan - layer branch %s is already part of maintenance plan %s' % (layerbranch, mplayerbranches.first().plan.name)) + total_checked += 1 + + class MaintenancePlanLayerBranchInline(admin.StackedInline): model = MaintenancePlanLayerBranch formset = MaintenancePlanLayerBranchFormSet -- cgit 1.2.3-korg