Exemplo n.º 1
0
    def test_installed(self):
        """Test that the sample printing plugin is installed."""
        # Get all label plugins
        plugins = registry.with_mixin('labels')
        self.assertEqual(len(plugins), 1)

        # But, it is not 'active'
        plugins = registry.with_mixin('labels', active=True)
        self.assertEqual(len(plugins), 0)
Exemplo n.º 2
0
    def test_installed(self):
        """Test that the sample panel plugin is installed."""
        plugins = registry.with_mixin('panel')

        self.assertTrue(len(plugins) > 0)

        self.assertIn('samplepanel', [p.slug for p in plugins])

        plugins = registry.with_mixin('panel', active=True)

        self.assertEqual(len(plugins), 0)
Exemplo n.º 3
0
    def post(self, request, *args, **kwargs):
        """Check inputs and offload the task to the plugin."""
        # Which plugin to we wish to use?
        plugin = request.data.get('plugin', None)

        if not plugin:
            raise ParseError("'plugin' field must be supplied")

        # Check that the plugin exists, and supports the 'locate' mixin
        plugins = registry.with_mixin('locate')

        if plugin not in [p.slug for p in plugins]:
            raise ParseError(
                f"Plugin '{plugin}' is not installed, or does not support the location mixin"
            )

        # StockItem to identify
        item_pk = request.data.get('item', None)

        # StockLocation to identify
        location_pk = request.data.get('location', None)

        data = {
            "success": "Identification plugin activated",
            "plugin": plugin,
        }

        # StockItem takes priority
        if item_pk:
            try:
                StockItem.objects.get(pk=item_pk)

                offload_task(registry.call_plugin_function, plugin,
                             'locate_stock_item', item_pk)

                data['item'] = item_pk

                return Response(data)

            except (ValueError, StockItem.DoesNotExist):
                raise NotFound(f"StockItem matching PK '{item_pk}' not found")

        elif location_pk:
            try:
                StockLocation.objects.get(pk=location_pk)

                offload_task(registry.call_plugin_function, plugin,
                             'locate_stock_location', location_pk)

                data['location'] = location_pk

                return Response(data)

            except (ValueError, StockLocation.DoesNotExist):
                raise NotFound(
                    f"StockLocation matching PK '{location_pk}' not found")

        else:
            raise ParseError(
                "Must supply either 'item' or 'location' parameter")
Exemplo n.º 4
0
    def get_plugin_panels(self, ctx):
        """
        Return a list of extra 'plugin panels' associated with this view
        """

        panels = []

        for plug in registry.with_mixin('panel', active=True):

            try:
                panels += plug.render_panels(self, self.request, ctx)
            except Exception:
                # Prevent any plugin error from crashing the page render
                kind, info, data = sys.exc_info()

                # Log the error to the database
                Error.objects.create(
                    kind=kind.__name__,
                    info=info,
                    data='\n'.join(traceback.format_exception(kind, info, data)),
                    path=self.request.path,
                    html=ExceptionReporter(self.request, kind, info, data).get_traceback_html(),
                )

                logger.error(f"Plugin '{plug.slug}' could not render custom panels at '{self.request.path}'")

        return panels
Exemplo n.º 5
0
    def get_plugin_panels(self, ctx):
        """Return a list of extra 'plugin panels' associated with this view."""
        panels = []

        for plug in registry.with_mixin('panel', active=True):

            try:
                panels += plug.render_panels(self, self.request, ctx)
            except Exception:
                # Log the error to the database
                log_error(self.request.path)
                logger.error(f"Plugin '{plug.slug}' could not render custom panels at '{self.request.path}'")

        return panels
Exemplo n.º 6
0
    def test_enabled(self):
        """Test that the panels *do* load if the plugin is enabled."""
        plugin = registry.get_plugin('samplepanel')

        self.assertEqual(len(registry.with_mixin('panel', active=True)), 0)

        # Ensure that the plugin is enabled
        config = plugin.plugin_config()
        config.active = True
        config.save()

        self.assertTrue(config.active)
        self.assertEqual(len(registry.with_mixin('panel', active=True)), 1)

        # Load some pages, ensure that the panel content is *not* loaded
        urls = [
            reverse('part-detail', kwargs={'pk': 1}),
            reverse('stock-item-detail', kwargs={'pk': 2}),
            reverse('stock-location-detail', kwargs={'pk': 2}),
        ]

        plugin.set_setting('ENABLE_HELLO_WORLD', False)
        plugin.set_setting('ENABLE_BROKEN_PANEL', False)

        for url in urls:
            response = self.client.get(url)

            self.assertEqual(response.status_code, 200)

            self.assertIn('No Content', str(response.content))

            # This panel is disabled by plugin setting
            self.assertNotIn('Hello world!', str(response.content))

            # This panel is only active for the "Part" view
            if url == urls[0]:
                self.assertIn('Custom Part Panel', str(response.content))
            else:
                self.assertNotIn('Custom Part Panel', str(response.content))

        # Enable the 'Hello World' panel
        plugin.set_setting('ENABLE_HELLO_WORLD', True)

        for url in urls:
            response = self.client.get(url)

            self.assertEqual(response.status_code, 200)

            self.assertIn('Hello world!', str(response.content))

            # The 'Custom Part' panel should still be there, too
            if url == urls[0]:
                self.assertIn('Custom Part Panel', str(response.content))
            else:
                self.assertNotIn('Custom Part Panel', str(response.content))

        # Enable the 'broken panel' setting - this will cause all panels to not render
        plugin.set_setting('ENABLE_BROKEN_PANEL', True)

        n_errors = Error.objects.count()

        for url in urls:
            response = self.client.get(url)
            self.assertEqual(response.status_code, 200)

            # No custom panels should have been loaded
            self.assertNotIn('No Content', str(response.content))
            self.assertNotIn('Hello world!', str(response.content))
            self.assertNotIn('Broken Panel', str(response.content))
            self.assertNotIn('Custom Part Panel', str(response.content))

        # Assert that each request threw an error
        self.assertEqual(Error.objects.count(), n_errors + len(urls))