Example #1
0
    def get_object(self):
        """Lookup the plugin setting object, based on the URL.

        The URL provides the 'slug' of the plugin, and the 'key' of the setting.
        Both the 'slug' and 'key' must be valid, else a 404 error is raised
        """
        plugin_slug = self.kwargs['plugin']
        key = self.kwargs['key']

        # Check that the 'plugin' specified is valid!
        if not PluginConfig.objects.filter(key=plugin_slug).exists():
            raise NotFound(detail=f"Plugin '{plugin_slug}' not installed")

        # Get the list of settings available for the specified plugin
        plugin = registry.get_plugin(plugin_slug)

        if plugin is None:
            # This only occurs if the plugin mechanism broke
            raise NotFound(
                detail=f"Plugin '{plugin_slug}' not found")  # pragma: no cover

        settings = getattr(plugin, 'SETTINGS', {})

        if key not in settings:
            raise NotFound(
                detail=f"Plugin '{plugin_slug}' has no setting matching '{key}'"
            )

        return PluginSetting.get_setting_object(key, plugin=plugin)
Example #2
0
    def test_disabled(self):
        """Test that the panels *do not load* if the plugin is not enabled."""
        plugin = registry.get_plugin('samplepanel')

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

        # Ensure that the plugin is *not* enabled
        config = plugin.plugin_config()

        self.assertFalse(config.active)

        # Load some pages, ensure that the panel content is *not* loaded
        for url in [
                reverse('part-detail', kwargs={'pk': 1}),
                reverse('stock-item-detail', kwargs={'pk': 2}),
                reverse('stock-location-detail', kwargs={'pk': 1}),
        ]:
            response = self.client.get(url)

            self.assertEqual(response.status_code, 200)

            # Test that these panels have *not* been loaded
            self.assertNotIn('No Content', str(response.content))
            self.assertNotIn('Hello world', str(response.content))
            self.assertNotIn('Custom Part Panel', str(response.content))
Example #3
0
    def get_plugin(self, request):
        """
        Return the label printing plugin associated with this request.
        This is provided in the url, e.g. ?plugin=myprinter

        Requires:
        - settings.PLUGINS_ENABLED is True
        - matching plugin can be found
        - matching plugin implements the 'labels' mixin
        - matching plugin is enabled
        """

        if not settings.PLUGINS_ENABLED:
            return None  # pragma: no cover

        plugin_key = request.query_params.get('plugin', None)

        # No plugin provided, and that's OK
        if plugin_key is None:
            return None

        plugin = registry.get_plugin(plugin_key)

        if plugin:
            config = plugin.plugin_config()

            if config and config.active:
                # Only return the plugin if it is enabled!
                return plugin
            else:
                raise ValidationError(f"Plugin '{plugin_key}' is not enabled")
        else:
            raise NotFound(f"Plugin '{plugin_key}' not found")
Example #4
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))
Example #5
0
 def do_activate_plugin(self):
     """Activate the 'samplelabel' plugin."""
     config = registry.get_plugin('samplelabel').plugin_config()
     config.active = True
     config.save()