Skip to content

Commit

Permalink
Merge pull request #359 from nautobot/fix343-dlm1.6
Browse files Browse the repository at this point in the history
[LTM1.6] Fix logic populating inventory item drop-down in Hardware Notice form
  • Loading branch information
bradh11 authored Sep 6, 2024
2 parents c656575 + 62c15c8 commit 55fb938
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 10 deletions.
30 changes: 30 additions & 0 deletions nautobot_device_lifecycle_mgmt/filter_extensions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""Extensions to core filters."""

from django_filters import BooleanFilter

try:
from nautobot.apps.filters import FilterExtension
except ImportError:
from nautobot.extras.plugins import PluginFilterExtension as FilterExtension


def distinct_filter(queryset, _, value):
"""Returns distinct Inventory Items by part_id."""
if value:
return queryset.order_by().distinct("part_id")
return queryset


class InventoryItemFilterExtension(FilterExtension): # pylint: disable=too-few-public-methods
"""Extends Inventory Item Filters."""

model = "dcim.inventoryitem"

filterset_fields = {
"nautobot_device_lifecycle_mgmt_distinct_part_id": BooleanFilter(
method=distinct_filter, label="_dpid_dlm_app_internal_use_only"
)
}


filter_extensions = [InventoryItemFilterExtension]
32 changes: 23 additions & 9 deletions nautobot_device_lifecycle_mgmt/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
add_blank_choice,
StaticSelect2Multiple,
)
from nautobot.utilities.forms.fields import DynamicModelChoiceMixin

from nautobot_device_lifecycle_mgmt.choices import (
ContractTypeChoices,
Expand Down Expand Up @@ -65,16 +66,29 @@ def prepare_value(self, value):
return super().prepare_value(pk_list)


class HardwareLCMDynamicModelChoiceField(DynamicModelChoiceMixin, forms.ModelChoiceField):
"""DynamicModelChoiceField used for 'inventory_item' field in HardwareLCMForm."""

def to_python(self, value):
"""Overload 'to_python' in forms.ModelChoiceField to force returning 'part_id' as the field value."""
if value in self.empty_values:
return None
if self.to_field_name == "part_id":
return value
return super().to_python(value)


class HardwareLCMForm(BootstrapMixin, CustomFieldModelFormMixin, RelationshipModelFormMixin):
"""Hardware Device Lifecycle creation/edit form."""

inventory_item = forms.ModelChoiceField(
queryset=InventoryItem.objects.exclude(part_id__exact="")
.distinct()
.order_by("part_id")
.values_list("part_id", flat=True),
device_type = DynamicModelChoiceField(queryset=DeviceType.objects.all(), required=False)
inventory_item = HardwareLCMDynamicModelChoiceField(
queryset=InventoryItem.objects.order_by().distinct("part_id"),
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",
brief_mode=False,
required=False,
)

Expand Down Expand Up @@ -131,11 +145,11 @@ class HardwareLCMFilterForm(BootstrapMixin, forms.ModelForm):
required=False, queryset=DeviceType.objects.all(), to_field_name="slug"
)

inventory_item = forms.ModelMultipleChoiceField(
queryset=HardwareLCM.objects.exclude(inventory_item__isnull=True)
.exclude(inventory_item__exact="")
.values_list("inventory_item", flat=True),
inventory_item = DynamicModelMultipleChoiceField(
queryset=HardwareLCM.objects.exclude(inventory_item__isnull=True).exclude(inventory_item__exact=""),
label="Inventory Part ID",
display_field="inventory_item",
to_field_name="inventory_item",
required=False,
)

Expand Down
2 changes: 1 addition & 1 deletion tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def is_truthy(arg):
namespace.configure(
{
"nautobot_device_lifecycle_mgmt": {
"nautobot_ver": "1.5.4",
"nautobot_ver": "1.6.26",
"project_name": "nautobot_device_lifecycle_mgmt",
"python_ver": "3.8",
"local": False,
Expand Down

0 comments on commit 55fb938

Please sign in to comment.