Code development platform for open source projects from the European Union institutions :large_blue_circle: EU Login authentication by SMS has been phased out. To see alternatives please check here

Skip to content
Snippets Groups Projects

Test/vm assigned virtual machine type view

Merged Arkadiusz SZCZECINSKI requested to merge test/vm_assigned_virtual_machine_type_view into main
2 files
+ 104
12
Compare changes
  • Side-by-side
  • Inline

Files

+ 13
12
@@ -36,16 +36,6 @@ class VmAssignedVmTypeForm(NetBoxModelForm):
kwargs["initial"] = initial
super().__init__(*args, **kwargs)
if instance:
current_vm_id = instance.assigned_object.id if instance.assigned_object else None
else:
current_vm_id = None
assigned_vms = VmAssignedVirtualMachineType.objects.filter(
assigned_object_type=ContentType.objects.get_for_model(VirtualMachine)
).exclude(assigned_object_id=current_vm_id).values_list('assigned_object_id', flat=True)
self.fields['virtual_machine'].queryset = VirtualMachine.objects.exclude(id__in=assigned_vms)
class Meta:
"""Meta class"""
model = VmAssignedVirtualMachineType
@@ -55,14 +45,25 @@ class VmAssignedVmTypeForm(NetBoxModelForm):
"""Validates form inputs before submitting:"""
super().clean()
vm = self.cleaned_data.get("virtual_machine")
vm_ct = ContentType.objects.get_for_model(VirtualMachine)
#Check if Virtual Machine is assigned corretly
if not vm:
raise ValidationError(
{"__all__": "Can't assign more than one Type to the same Virtual Machine"},
{"__all__":"Virtual Machine can't be null"}
)
if VmAssignedVirtualMachineType.objects.filter(
assigned_object_type=vm_ct,
assigned_object_id=vm.id
).exclude(pk=self.instance.pk).exists():
raise ValidationError(
{"__all__":"Can't assign more than one Type to the same Virtual Machine"}
)
def save(self, *args, **kwargs):
"""Set assigned object and save"""
# Set assigned object
Loading