mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-08 16:48:16 -06:00
10500 token resolution
This commit is contained in:
parent
844fb1f7ec
commit
702c862a34
@ -70,6 +70,18 @@ class InterfaceCommonForm(forms.Form):
|
|||||||
|
|
||||||
class ModuleCommonForm(forms.Form):
|
class ModuleCommonForm(forms.Form):
|
||||||
|
|
||||||
|
def _get_module_bay_tree(self, module_bay):
|
||||||
|
module_bays = []
|
||||||
|
all_module_bays = module.device.modulebays.all().select_related('module')
|
||||||
|
while module_bay:
|
||||||
|
module_bays.append(module_bay)
|
||||||
|
if module_bay.module:
|
||||||
|
module_bay = module_bay.module.module_bay
|
||||||
|
else:
|
||||||
|
module_bay = None
|
||||||
|
|
||||||
|
return module_bays.reverse()
|
||||||
|
|
||||||
def clean(self):
|
def clean(self):
|
||||||
super().clean()
|
super().clean()
|
||||||
|
|
||||||
@ -104,13 +116,25 @@ class ModuleCommonForm(forms.Form):
|
|||||||
|
|
||||||
# Get the templates for the module type.
|
# Get the templates for the module type.
|
||||||
for template in getattr(module_type, templates).all():
|
for template in getattr(module_type, templates).all():
|
||||||
|
resolved_name = template.name
|
||||||
# Installing modules with placeholders require that the bay has a position value
|
# Installing modules with placeholders require that the bay has a position value
|
||||||
if MODULE_TOKEN in template.name and not module_bay.position:
|
if MODULE_TOKEN in template.name:
|
||||||
raise forms.ValidationError(
|
if not module_bay.position:
|
||||||
_("Cannot install module with placeholder values in a module bay with no position defined.")
|
raise forms.ValidationError(
|
||||||
)
|
_("Cannot install module with placeholder values in a module bay with no position defined.")
|
||||||
|
)
|
||||||
|
|
||||||
|
module_bays = self._get_module_bay_tree(template)
|
||||||
|
if len(module_bays) != template.name.count(MODULE_TOKEN):
|
||||||
|
raise forms.ValidationError(
|
||||||
|
_("Cannot install module with placeholder values in a module bay tree {level} in tree but {tokens} placeholders given.").format(
|
||||||
|
level=len(module_bays), tokens=template.name.count(MODULE_TOKEN)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
for module_bay in module_bays:
|
||||||
|
resolved_name = resolved_name.replace(MODULE_TOKEN, module_bay.position, 1)
|
||||||
|
|
||||||
resolved_name = template.name.replace(MODULE_TOKEN, module_bay.position)
|
|
||||||
existing_item = installed_components.get(resolved_name)
|
existing_item = installed_components.get(resolved_name)
|
||||||
|
|
||||||
# It is not possible to adopt components already belonging to a module
|
# It is not possible to adopt components already belonging to a module
|
||||||
|
@ -158,14 +158,34 @@ class ModularComponentTemplateModel(ComponentTemplateModel):
|
|||||||
_("A component template must be associated with either a device type or a module type.")
|
_("A component template must be associated with either a device type or a module type.")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def _get_module_tree(self, module):
|
||||||
|
modules = []
|
||||||
|
all_module_bays = module.device.modulebays.all().select_related('module')
|
||||||
|
while module:
|
||||||
|
modules.append(module)
|
||||||
|
if module.module_bay:
|
||||||
|
module = module.module_bay.module
|
||||||
|
else:
|
||||||
|
module = None
|
||||||
|
|
||||||
|
return modules.reverse()
|
||||||
|
|
||||||
def resolve_name(self, module):
|
def resolve_name(self, module):
|
||||||
if module:
|
if module:
|
||||||
return self.name.replace(MODULE_TOKEN, module.module_bay.position)
|
modules = self._get_module_tree(module)
|
||||||
|
name = self.name
|
||||||
|
for module in modules:
|
||||||
|
name = name.replace(MODULE_TOKEN, module.module_bay.position, 1)
|
||||||
|
return name
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
def resolve_label(self, module):
|
def resolve_label(self, module):
|
||||||
if module:
|
if module:
|
||||||
return self.label.replace(MODULE_TOKEN, module.module_bay.position)
|
modules = self._get_module_tree(module)
|
||||||
|
label = self.label
|
||||||
|
for module in modules:
|
||||||
|
label = label.replace(MODULE_TOKEN, module.module_bay.position, 1)
|
||||||
|
return label
|
||||||
return self.label
|
return self.label
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user