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

Expose Inventory Items Parent/Child hierarchy in UI #1692

Closed
candlerb opened this issue Nov 7, 2017 · 3 comments
Closed

Expose Inventory Items Parent/Child hierarchy in UI #1692

candlerb opened this issue Nov 7, 2017 · 3 comments
Assignees
Labels
status: accepted This issue has been accepted for implementation type: feature Introduction of new functionality to the application
Milestone

Comments

@candlerb
Copy link
Contributor

candlerb commented Nov 7, 2017

Issue type

[X] Feature request
[ ] Bug report
[ ] Documentation

Environment

  • Python version: 3.5.2
  • NetBox version: 2.2.4

Description

groups thread

The dcim_inventoryitem table has a "parent_id" field which is self-referential:

    "dcim_inventoryitem_parent_id_7ebcd457_fk_dcim_inventoryitem_id" FOREIGN KEY (parent_id) REFERENCES dcim_inventoryitem(id) DEFERRABLE INITIALLY DEFERRED

and there is some support for rendering such items hierarchically:

{% for item in item.child_items.all %}
    {% with template_name='dcim/inc/inventoryitem.html' indent=indent|add:20 %}
        {% include template_name %}
    {% endwith %}
{% endfor %}

But there is currently no way to set the inventory item parent/child relationship in the GUI.

Implementing this could be as simple as a 'set parent' drop-down list (restricted to inventory items on the current device); or it could change the data model to MPTT, like Region.

@jeremystretch jeremystretch added status: accepted This issue has been accepted for implementation type: feature Introduction of new functionality to the application labels Jan 26, 2018
@jeremystretch
Copy link
Member

Poked at this a bit, but the list of potential parent InventoryItems doesn't make sense without proper hierarchical ordering. To achieve this we need to convert InventoryItem.parent to an MPTTField as suggested.

@jeremystretch jeremystretch added status: blocked Another issue or external requirement is preventing implementation and removed status: accepted This issue has been accepted for implementation labels Jan 30, 2018
@jeremystretch
Copy link
Member

Blocked by #1846

@candlerb
Copy link
Contributor Author

I don't think that MPTT (#1846) is necessary, or even desirable, in this case.

I think that parent_id is a simple, natural, and accurate representation of the reality: an inventory item may reside inside another inventory item. Since every inventory item already has a direct link to its overall enclosing device, processing at the device level is very efficient. In any case, I can imagine a device having 50 inventory items, but not 5000.

Processing0wise:

  • When you want to list all inventory items on a device, you select all inventory items with device_id=X (as is done today)
  • In the display, you can sort them so that parent comes before its children, and indent a child one more than its parent. That's straightforward.
  • When editing an InventoryItem in the GUI, to allow changing the parent_id, you would provide a drop-down list of all inventory items on the same device (except itself), and also offer Null as an option. That's a cheap, non-recursive query, and lets you move an item anywhere in the tree. If it has descendants, those naturally remain underneath it without touching any other database entries.

In the API, when changing the device_id and/or parent_id of an inventory item, you'd validate that the updated device_id is the same as the device_id of the updated parent_id (again: cheap and non-recursive) and reject if not. If the device_id has changed, you would also find any other inventory items whose parent_id is this inventory item, and set it to null. That's the cheap option anyway. A slightly more expensive approach is to recursively find all descendents of this inventory item, and set all their device_ids to the new device. "Recursively find" is pretty cheap though; you simply find all inventory items on the old device_id, and then filter them to only descendants of this inventory item. And remember this is only done in the very rare circumstance when you move an inventory item from one device to another.

Finally, if you want to allow moving of an inventory item from one device to another in the GUI as well (#2233), you would have logic similar to that I just described for the API.

Firstly, in the GUI you would offer a drop-down or set of drop-downs to select the new enclosing device (e.g. site + rack + device drill-down). If the user modifies the enclosing device, then:

  • If this item has a parent_id, you could set it to null; or better, the GUI could dynamically update the parent inventory item drop-down to offer all inventory items in the new device (defaulting to null)
  • For any other item which has a parent_id equal to this item you can just set its parent_id to null; OR better, you change the device_id on all the descendants of this inventory_item to be the new device.

@jeremystretch jeremystretch added status: accepted This issue has been accepted for implementation and removed status: blocked Another issue or external requirement is preventing implementation labels Sep 18, 2020
@jeremystretch jeremystretch added this to the v2.10 milestone Sep 18, 2020
@jeremystretch jeremystretch self-assigned this Sep 18, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 18, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
status: accepted This issue has been accepted for implementation type: feature Introduction of new functionality to the application
Projects
None yet
Development

No branches or pull requests

2 participants