Validate position count on FrontPort & FrontPortTemplate

This commit is contained in:
Jeremy Stretch
2025-11-26 10:32:49 -05:00
parent 7c2193685e
commit ca880218d9
2 changed files with 27 additions and 1 deletions

View File

@@ -585,6 +585,19 @@ class FrontPortTemplate(ModularComponentTemplateModel):
verbose_name = _('front port template') verbose_name = _('front port template')
verbose_name_plural = _('front port templates') verbose_name_plural = _('front port templates')
def clean(self):
super().clean()
# Check that positions is greater than or equal to the number of associated RearPortTemplates
if not self._state.adding:
mapping_count = self.mappings.count()
if self.positions < mapping_count:
raise ValidationError({
"positions": _(
"The number of positions cannot be less than the number of mapped rear port templates ({count})"
).format(count=mapping_count)
})
def instantiate(self, **kwargs): def instantiate(self, **kwargs):
return self.component_model( return self.component_model(
name=self.resolve_name(kwargs.get('module')), name=self.resolve_name(kwargs.get('module')),
@@ -638,7 +651,7 @@ class RearPortTemplate(ModularComponentTemplateModel):
def clean(self): def clean(self):
super().clean() super().clean()
# Check that positions count is greater than or equal to the number of associated FrontPortTemplates # Check that positions is greater than or equal to the number of associated FrontPortTemplates
if not self._state.adding: if not self._state.adding:
mapping_count = self.mappings.count() mapping_count = self.mappings.count()
if self.positions < mapping_count: if self.positions < mapping_count:

View File

@@ -1132,6 +1132,19 @@ class FrontPort(ModularComponentModel, CabledObjectModel, TrackingModelMixin):
verbose_name = _('front port') verbose_name = _('front port')
verbose_name_plural = _('front ports') verbose_name_plural = _('front ports')
def clean(self):
super().clean()
# Check that positions is greater than or equal to the number of associated RearPorts
if not self._state.adding:
mapping_count = self.mappings.count()
if self.positions < mapping_count:
raise ValidationError({
"positions": _(
"The number of positions cannot be less than the number of mapped rear ports ({count})"
).format(count=mapping_count)
})
class RearPort(ModularComponentModel, CabledObjectModel, TrackingModelMixin): class RearPort(ModularComponentModel, CabledObjectModel, TrackingModelMixin):
""" """