Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Draft] Enhancement for status field for contracts #388

Draft
wants to merge 5 commits into
base: develop
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Removed Status field from forms/filter and utilized NautobotMixins
Campos committed Jan 23, 2025
commit 1dbc70f8e5a623631a445399b73c061a52733373
89 changes: 67 additions & 22 deletions nautobot_device_lifecycle_mgmt/forms.py
Original file line number Diff line number Diff line change
@@ -17,8 +17,20 @@
add_blank_choice,
)
from nautobot.core.forms.constants import BOOLEAN_WITH_BLANK_CHOICES
from nautobot.dcim.models import Device, DeviceType, InventoryItem, Location, Manufacturer, Platform
from nautobot.extras.forms import CustomFieldModelBulkEditFormMixin, NautobotFilterForm
from nautobot.dcim.models import (
Device,
DeviceType,
InventoryItem,
Location,
Manufacturer,
Platform,
)
from nautobot.extras.forms import (
CustomFieldModelBulkEditFormMixin,
NautobotFilterForm,
StatusModelBulkEditFormMixin,
StatusModelFilterFormMixin,
)
from nautobot.extras.models import Role, Status, Tag

from nautobot_device_lifecycle_mgmt.choices import (
@@ -75,7 +87,10 @@ class HardwareLCMForm(NautobotModelForm):
device_type = DynamicModelChoiceField(queryset=DeviceType.objects.all(), required=False)
inventory_item = HardwareLCMDynamicModelChoiceField(
queryset=InventoryItem.objects.without_tree_fields().order_by().distinct("part_id"),
query_params={"part_id__nre": "^$", "nautobot_device_lifecycle_mgmt_distinct_part_id": "true"},
query_params={
"part_id__nre": "^$",
"nautobot_device_lifecycle_mgmt_distinct_part_id": "true",
},
label="Inventory Part ID",
display_field="part_id",
to_field_name="part_id",
@@ -354,7 +369,9 @@ class ValidatedSoftwareLCMForm(NautobotModelForm):
devices = DynamicModelMultipleChoiceField(queryset=Device.objects.all(), required=False)
device_types = DynamicModelMultipleChoiceField(queryset=DeviceType.objects.all(), required=False)
device_roles = DynamicModelMultipleChoiceField(
queryset=Role.objects.all(), query_params={"content_types": "dcim.device"}, required=False
queryset=Role.objects.all(),
query_params={"content_types": "dcim.device"},
required=False,
)

inventory_items = DynamicModelMultipleChoiceField(queryset=InventoryItem.objects.all(), required=False)
@@ -383,7 +400,19 @@ def clean(self):
inventory_items = self.cleaned_data.get("inventory_items")
object_tags = self.cleaned_data.get("object_tags")

if sum(obj.count() for obj in (devices, device_types, device_roles, inventory_items, object_tags)) == 0:
if (
sum(
obj.count()
for obj in (
devices,
device_types,
device_roles,
inventory_items,
object_tags,
)
)
== 0
):
msg = "You need to assign to at least one object."
self.add_error(None, msg)

@@ -482,7 +511,10 @@ class DeviceSoftwareValidationResultFilterForm(NautobotFilterForm):
required=False,
)
device_role = DynamicModelMultipleChoiceField(
queryset=Role.objects.all(), query_params={"content_types": "dcim.device"}, to_field_name="name", required=False
queryset=Role.objects.all(),
query_params={"content_types": "dcim.device"},
to_field_name="name",
required=False,
)
exclude_sw_missing = forms.BooleanField(
required=False,
@@ -561,7 +593,10 @@ class InventoryItemSoftwareValidationResultFilterForm(NautobotFilterForm):
required=False,
)
device_role = DynamicModelMultipleChoiceField(
queryset=Role.objects.all(), query_params={"content_types": "dcim.device"}, to_field_name="name", required=False
queryset=Role.objects.all(),
query_params={"content_types": "dcim.device"},
to_field_name="name",
required=False,
)
exclude_sw_missing = forms.BooleanField(
required=False,
@@ -624,7 +659,7 @@ def get_form_kwargs(self):
return {"provider": self.request.GET.get("provider")} # pylint: disable=E1101


class ContractLCMBulkEditForm(NautobotBulkEditForm):
class ContractLCMBulkEditForm(NautobotBulkEditForm, StatusModelBulkEditFormMixin):
"""Device Lifecycle Contrcts bulk edit form."""

model = ContractLCM
@@ -636,17 +671,22 @@ class ContractLCMBulkEditForm(NautobotBulkEditForm):
currency = forms.ChoiceField(required=False, choices=CurrencyChoices.CHOICES)
contract_type = forms.ChoiceField(choices=ContractTypeChoices.CHOICES, required=False)
support_level = forms.CharField(required=False)
status = DynamicModelChoiceField(
queryset=Status.objects.all(), required=False, query_params={"content_types": model._meta.label_lower}
)

class Meta:
"""Meta attributes for the ContractLCMBulkEditForm class."""

nullable_fields = ["start", "end", "cost", "currency", "support_level", "contract_type", "status"]
nullable_fields = [
"start",
"end",
"cost",
"currency",
"support_level",
"contract_type",
"status",
]


class ContractLCMFilterForm(NautobotFilterForm):
class ContractLCMFilterForm(NautobotFilterForm, StatusModelFilterFormMixin):
"""Filter form to filter searches."""

model = ContractLCM
@@ -656,16 +696,12 @@ class ContractLCMFilterForm(NautobotFilterForm):
required=False, choices=CurrencyChoices.CHOICES, widget=StaticSelect2Multiple()
)
contract_type = forms.ChoiceField(
required=False, widget=StaticSelect2, choices=add_blank_choice(ContractTypeChoices.CHOICES)
required=False,
widget=StaticSelect2,
choices=add_blank_choice(ContractTypeChoices.CHOICES),
)
name = forms.CharField(required=False)
tags = TagFilterField(model)
status = DynamicModelMultipleChoiceField(
queryset=Status.objects.all(),
required=False,
query_params={"content_types": model._meta.label_lower},
to_field_name="name",
)

class Meta:
"""Meta attributes for the ContractLCMFilterForm class."""
@@ -793,7 +829,14 @@ class ContactLCMBulkEditForm(NautobotBulkEditForm):
class Meta:
"""Meta attributes for the ContactLCMBulkEditForm class."""

nullable_fields = ["address", "phone", "email", "comments", "priority", "contract"]
nullable_fields = [
"address",
"phone",
"email",
"comments",
"priority",
"contract",
]


class ContactLCMFilterForm(NautobotFilterForm):
@@ -850,7 +893,9 @@ class CVELCMBulkEditForm(NautobotBulkEditForm, CustomFieldModelBulkEditFormMixin
comments = forms.CharField(required=False)
tags = DynamicModelMultipleChoiceField(queryset=Tag.objects.all(), required=False)
status = DynamicModelChoiceField(
queryset=Status.objects.all(), required=False, query_params={"content_types": model._meta.label_lower}
queryset=Status.objects.all(),
required=False,
query_params={"content_types": model._meta.label_lower},
)

class Meta: