offload custom panels to sample plugin

This commit is contained in:
Oliver Walters 2024-08-12 00:40:24 +00:00
parent 587ad8d09d
commit 250970e55d
3 changed files with 57 additions and 44 deletions

View File

@ -440,47 +440,11 @@ class PluginPanelList(APIView):
if plugin_panels and type(plugin_panels) is list:
for panel in plugin_panels:
panel['plugin'] = _plugin.slug
# TODO: Validate each panel before inserting
panels.append(panel)
if target_model == 'part' and target_id:
panels = [
*panels,
{
'plugin': 'myplugin',
'name': 'test-plugin',
'label': 'My Plugin',
'icon': 'part',
'content': '<div>hello world</div>',
},
{
'plugin': 'myplugin',
'name': 'test-plugin-2',
'label': 'My Plugin 2',
'icon': 'email',
'content': '<div>hello world 2</div>',
},
{
'plugin': 'myplugin',
'name': 'test-plugin-3',
'label': 'My Plugin 3',
'icon': 'website',
'content': '<div>hello world 3</div>',
},
]
if target_model == 'partcategory':
panels = [
*panels,
{
'plugin': 'cat',
'name': 'demo-cat',
'label': 'Custom Category',
'icon': 'customer',
'content': 'This should only appear for a category',
},
]
return Response(PluginSerializers.PluginPanelSerializer(panels, many=True).data)

View File

@ -29,3 +29,36 @@ class SampleUserInterfacePlugin(SettingsMixin, UserInterfaceMixin, InvenTreePlug
'validator': bool,
},
}
def get_custom_panels(self, instance_type: str, instance_id: int, request):
"""Return a list of custom panels to be injected into the UI."""
panels = []
# First, add a custom panel which will appear on every type of page
# This panel will contain a simple message
panels.append({
'name': 'sample_panel',
'label': 'Sample Panel',
'content': 'This is a <i>sample panel</i> which appears on every page. It renders a simple string of <b>HTML</b> content.',
})
# Next, add a custom panel which will appear on the 'part' page
if self.get_setting('ENABLE_PART_PANELS') and instance_type == 'part':
panels.append({
'name': 'part_panel',
'label': 'Part Panel',
'content': 'This is a custom panel which appears on the <b>Part</b> view page.',
})
# Next, add a custom panel which will appear on the 'purchaseorder' page
if (
self.get_setting('ENABLE_PURCHASE_ORDER_PANELS')
and instance_type == 'purchaseorder'
):
panels.append({
'name': 'purchase_order_panel',
'label': 'Purchase Order Panel',
'content': 'This is a custom panel which appears on the <b>Purchase Order</b> view page.',
})
return panels

View File

@ -311,10 +311,26 @@ class PluginPanelSerializer(serializers.Serializer):
class Meta:
"""Meta for serializer."""
fields = ['plugin', 'name', 'label', 'icon']
fields = ['plugin', 'name', 'label', 'icon', 'content', 'source']
plugin = serializers.CharField(label=_('Plugin Key'))
name = serializers.CharField(label=_('Panel Name'))
label = serializers.CharField(label=_('Panel Label'))
icon = serializers.CharField(label=_('Panel Icon'))
content = serializers.CharField(label=_('Panel Content'))
# Required fields
plugin = serializers.CharField(
label=_('Plugin Key'), required=True, allow_blank=False
)
name = serializers.CharField(
label=_('Panel Name'), required=True, allow_blank=False
)
label = serializers.CharField(
label=_('Panel Title'), required=True, allow_blank=False
)
# Optional fields
icon = serializers.CharField(
label=_('Panel Icon'), required=False, allow_blank=True
)
content = serializers.CharField(
label=_('Panel Content (HTML)'), required=False, allow_blank=True
)
source = serializers.CharField(
label=_('Panel Source (javascript)'), required=False, allow_blank=True
)