Skip to content

Commit

Permalink
#7846: Show assigned component (if any) when creating inventory item
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremystretch committed Dec 28, 2021
1 parent e9910d1 commit 21356b4
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 7 deletions.
6 changes: 3 additions & 3 deletions netbox/dcim/forms/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1377,10 +1377,11 @@ class InventoryItemForm(CustomFieldModelForm):
queryset=ContentType.objects.all(),
limit_choices_to=MODULAR_COMPONENT_MODELS,
required=False,
widget=StaticSelect
widget=forms.HiddenInput
)
component_id = forms.IntegerField(
required=False
required=False,
widget=forms.HiddenInput
)
tags = DynamicModelMultipleChoiceField(
queryset=Tag.objects.all(),
Expand All @@ -1396,7 +1397,6 @@ class Meta:
fieldsets = (
('Inventory Item', ('device', 'parent', 'name', 'label', 'role', 'description', 'tags')),
('Hardware', ('manufacturer', 'part_id', 'serial', 'asset_tag')),
('Component', ('component_type', 'component_id')),
)
widgets = {
'device': forms.HiddenInput(),
Expand Down
6 changes: 4 additions & 2 deletions netbox/dcim/tables/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,8 @@ class InventoryItemTable(DeviceComponentTable):
linkify=True
)
component = tables.Column(
accessor=Accessor('component'),
orderable=False,
linkify=True
)
discovered = BooleanColumn()
Expand All @@ -792,8 +794,8 @@ class InventoryItemTable(DeviceComponentTable):
class Meta(BaseTable.Meta):
model = InventoryItem
fields = (
'pk', 'id', 'name', 'device', 'label', 'role', 'manufacturer', 'part_id', 'serial', 'asset_tag',
'component', 'description', 'discovered', 'tags',
'pk', 'id', 'name', 'device', 'component', 'label', 'role', 'manufacturer', 'part_id', 'serial',
'asset_tag', 'description', 'discovered', 'tags',
)
default_columns = (
'pk', 'name', 'device', 'label', 'role', 'manufacturer', 'part_id', 'serial', 'asset_tag',
Expand Down
12 changes: 12 additions & 0 deletions netbox/dcim/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2399,6 +2399,18 @@ class InventoryItemEditView(generic.ObjectEditView):
class InventoryItemCreateView(generic.ComponentCreateView):
queryset = InventoryItem.objects.all()
model_form = forms.InventoryItemForm
template_name = 'dcim/inventoryitem_create.html'

def alter_object(self, instance, request):
# Set component (if any)
component_type = request.GET.get('component_type')
component_id = request.GET.get('component_id')

if component_type and component_id:
content_type = get_object_or_404(ContentType, pk=component_type)
instance.component = get_object_or_404(content_type.model_class(), pk=component_id)

return instance


class InventoryItemDeleteView(generic.ObjectDeleteView):
Expand Down
9 changes: 7 additions & 2 deletions netbox/netbox/views/generic/object_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,9 @@ class ComponentCreateView(GetReturnURLMixin, ObjectPermissionRequiredMixin, View
def get_required_permission(self):
return get_permission_for_model(self.queryset.model, 'add')

def alter_object(self, instance, request):
return instance

def initialize_forms(self, request):
data = request.POST if request.method == 'POST' else None
initial_data = normalize_querydict(request.GET)
Expand All @@ -704,9 +707,10 @@ def initialize_forms(self, request):

def get(self, request):
form, model_form = self.initialize_forms(request)
instance = self.alter_object(self.queryset.model, request)

return render(request, self.template_name, {
'obj': self.queryset.model,
'obj': instance,
'obj_type': self.queryset.model._meta.verbose_name,
'replication_form': form,
'form': model_form,
Expand All @@ -715,6 +719,7 @@ def get(self, request):

def post(self, request):
form, model_form = self.initialize_forms(request)
instance = self.alter_object(self.queryset.model, request)

self.validate_form(request, form)

Expand All @@ -725,7 +730,7 @@ def post(self, request):
return redirect(self.get_return_url(request))

return render(request, self.template_name, {
'obj': self.queryset.model,
'obj': instance,
'obj_type': self.queryset.model._meta.verbose_name,
'replication_form': form,
'form': model_form,
Expand Down
18 changes: 18 additions & 0 deletions netbox/templates/dcim/inventoryitem_create.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{% extends 'dcim/component_create.html' %}
{% load helpers %}

{% block form %}
{% if obj.component %}
<div class="row mb-3">
<label class="col-sm-3 col-form-label text-lg-end required">
{{ obj.component|meta:"verbose_name"|bettertitle }}
</label>
<div class="col-sm-9">
<div class="form-control-plaintext">
<a href="{{ obj.component.get_absolute_url }}" class="">{{ obj.component }}</a>
</div>
</div>
</div>
{% endif %}
{{ block.super }}
{% endblock %}

0 comments on commit 21356b4

Please sign in to comment.