Skip to content

Commit

Permalink
Add panel to harness category
Browse files Browse the repository at this point in the history
  • Loading branch information
SchrodingersGat committed Feb 14, 2024
1 parent 89ce2eb commit e620a8b
Showing 1 changed file with 31 additions and 8 deletions.
39 changes: 31 additions & 8 deletions inventree_wireviz/wireviz.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from plugin.mixins import PanelMixin, ReportMixin, SettingsMixin, UrlsMixin

from build.views import BuildDetail
from part.models import Part
from part.models import Part, PartCategory
from part.views import PartDetail

from .version import PLUGIN_VERSION
Expand Down Expand Up @@ -52,6 +52,11 @@ class WirevizPlugin(PanelMixin, ReportMixin, SettingsMixin, UrlsMixin, InvenTree
HARNESS_BOM_KEY = "bom_data"

SETTINGS = {
"HARNESS_CATEGORY": {
'name': 'Wire Harness Category',
'description': 'Select the part category for wire harnesses',
'model': 'part.partcategory',
},
"WIREVIZ_PATH": {
'name': 'Wireviz Upload Path',
'description': 'Path to store uploaded wireviz template files (relative to media root)',
Expand Down Expand Up @@ -169,7 +174,9 @@ def get_custom_panels(self, view, request):

# A valid part object has been found
if part and isinstance(part, Part):


add_panel = False

# We are on the PartDetail or BuildDetail page
if isinstance(view, PartDetail) or isinstance(view, BuildDetail):

Expand All @@ -178,12 +185,28 @@ def get_custom_panels(self, view, request):
metadata = part.get_metadata('wireviz')

if metadata:
panels.append({
'title': 'Harness Diagram',
'icon': 'fas fa-project-diagram',
'content_template': 'wireviz/harness_panel.html',
'javascript_template': 'wireviz/harness_panel.js',
})
add_panel = True

if not add_panel and isinstance(view, PartDetail):
# Check if the Part belongs to the harness category
if harness_category := self.get_setting('HARNESS_CATEGORY'):
try:
category = PartCategory.objects.get(pk=harness_category)
children = category.get_descendants(include_self=True)

if part.category in children:
add_panel = True

except (PartCategory.DoesNotExist, ValueError):
pass

if add_panel:
panels.append({
'title': 'Harness Diagram',
'icon': 'fas fa-project-diagram',
'content_template': 'wireviz/harness_panel.html',
'javascript_template': 'wireviz/harness_panel.js',
})

return panels

Expand Down

0 comments on commit e620a8b

Please sign in to comment.