Improvements for panel mixin sample

This commit is contained in:
Oliver Walters 2022-05-06 23:19:47 +10:00
parent 0797e9ebf0
commit 12c58b14d6
3 changed files with 19 additions and 6 deletions

View File

@ -75,7 +75,6 @@ class InvenTreePluginMixin:
panels = []
for plug in registry.with_mixin('panel'):
panels += plug.render_panels(self, self.request)
return panels

View File

@ -606,18 +606,18 @@ class PanelMixin:
for panel in self.get_custom_panels(view, request):
if 'content_template' in panel:
# TODO: Render the actual content
# TODO: Render the actual HTML content from a template file
...
if 'javascript_template' in panel:
# TODO: Render the actual content
# TODO: Render the actual javascript content from a template file
...
# Check for required keys
required_keys = ['title', 'content']
if any([key not in panel for key in required_keys]):
logger.warning(f"Custom panel for plugin '{__class__}' is missing a required key")
logger.warning(f"Custom panel for plugin {__class__} is missing a required parameter")
continue
# Add some information on this plugin

View File

@ -18,6 +18,19 @@ class CustomPanelSample(PanelMixin, IntegrationPluginBase):
PLUGIN_SLUG = "panel"
PLUGIN_TITLE = "Custom Panel Example"
def render_location_info(self, loc):
"""
Demonstrate that we can render information particular to a page
"""
return f"""
<h5>Location Information</h5>
<em>This location has no sublocations!</em>
<ul>
<li><b>Name</b>: {loc.name}</li>
<li><b>Path</b>: {loc.pathstring}</li>
</ul>
"""
def get_custom_panels(self, view, request):
panels = [
@ -46,14 +59,15 @@ class CustomPanelSample(PanelMixin, IntegrationPluginBase):
# This panel will *only* display on the StockLocation view,
# and *only* if the StockLocation has *no* child locations
if isinstance(view, StockLocationDetail):
try:
loc = view.get_object()
if not loc.get_descendants(include_self=False).exists():
panels.append({
'title': 'Childless',
'title': 'Childless Location',
'icon': 'fa-user',
'content': '<h4>I have no children!</h4>'
'content': self.render_location_info(loc),
})
except:
pass