netbox icon indicating copy to clipboard operation
netbox copied to clipboard

Allow Plugins to register a list of Django apps to be appended to INSTALLED_APPS

Open jsenecal opened this issue 3 years ago • 5 comments

NetBox version

v3.3-beta1

Feature type

New functionality

Proposed functionality

As discussed in https://github.com/netbox-community/netbox/issues/5086 (closed due to inactivity) it would be nice to allow for a plugin to append third-party Django Application into INSTALLED_APPS.

Borrowing from the issue above, the proposal is to extend the PluginConfig class to support a new django_apps attribute which would get appended to INSTALLED_APPS.

from extras.plugins import PluginConfig

class FooBarConfig(PluginConfig):
    name = 'foo_bar'
    verbose_name = 'Foo Bar'
    description = 'An example NetBox plugin'
    version = '0.1'
    author = 'Jeremy Stretch'
    author_email = '[email protected]'
    base_url = 'foo-bar'
    required_settings = []
    default_settings = {
        'baz': True
    }
    django_apps = ["foo", "bar", "baz"] ### <<< HERE

config = FooBarConfig

These new apps should be inserted before the plugin that requires it, but after existing "core" apps in order to avoid conflicts.

The documentation should point out that additional apps may cause more harm than good and could lead to make identifying problems within NetBox itself more difficult or even cause problems. (But isn't that the case for every python package?)

Use case

When working with plugins, reusing third party libraries is a must to promote code re-use and allow for better functionality and ease of use. Examples of that are apps that introduce new field types (with template tags), new templates, new models with migrations, new manage.py admin commands, etc. Most of the time, simply installing the python package for the additional app is simply not enough.

There are many existing Django apps that could be easily reused/extended within NetBox if we could seamlessly install them from a plugin.

Database changes

None within NetBox

External dependencies

None within NetBox

jsenecal avatar Jul 29 '22 13:07 jsenecal

This can be assigned to me and I can work on a PR if accepted.

jsenecal avatar Jul 29 '22 13:07 jsenecal

One thing to consider is the order in which apps are loaded. I don't know if it's something we need to address for plugins, but this has come up in core development. For example, we have to list django_rq after our extras app to ensure that we can override one of the management commands it provides.

jeremystretch avatar Aug 01 '22 14:08 jeremystretch

One thing to consider is the order in which apps are loaded. I don't know if it's something we need to address for plugins, but this has come up in core development. For example, we have to list django_rq after our extras app to ensure that we can override one of the management commands it provides.

As mentioned in the Proposed functionality section above:

These new apps should be inserted before the plugin that requires it, but after existing "core" apps in order to avoid conflicts.

Are you requesting for the ability to do the opposite ?

jsenecal avatar Aug 02 '22 18:08 jsenecal

I'm pointing out that it needs to be considered because there are scenarios where it may be required, such as the example I cited.

jeremystretch avatar Aug 02 '22 19:08 jeremystretch

In that case we may consider the order of the list django_apps as important when the plugin name itself is in the list. like so:

django_apps = ["foo", "bar", "my_netbox_plugin", "baz"]

If "my_netbox_plugin" is not in the list, we append it to the end automatically for convenience.

This would in turn define the import order for the apps and plugin.

jsenecal avatar Aug 02 '22 19:08 jsenecal

Thinking about this further, I can't come up with a scenario where you would need to load another Django app after a plugin which depends on it, so maybe it doesn't matter so long as the dependencies are loaded before all plugins.

jeremystretch avatar Aug 30 '22 20:08 jeremystretch