Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create new PluginViews to allow plugins to extend base view classes #6221

Closed
DanSheps opened this issue Apr 20, 2021 · 8 comments
Closed

Create new PluginViews to allow plugins to extend base view classes #6221

DanSheps opened this issue Apr 20, 2021 · 8 comments
Assignees
Labels
status: accepted This issue has been accepted for implementation topic: plugins Relates to the plugins framework type: feature Introduction of new functionality to the application
Milestone

Comments

@DanSheps
Copy link
Member

NetBox version

v2.11.0

Feature type

Change to existing functionality

Proposed functionality

Create separate view classes for plugins to extend and provide base functionality to plugins. This class would provide a consistent api for plugin developers to extend while maintaining compatibility with the existing base classes.

Use case

Currently there is no way for plugins to extend NetBox view classes

Database changes

None expected

External dependencies

No response

@DanSheps DanSheps added type: feature Introduction of new functionality to the application topic: plugins Relates to the plugins framework status: under review Further discussion is needed to determine this issue's scope and/or implementation labels Apr 20, 2021
@jeremystretch
Copy link
Member

This needs to be fleshed out before we can look at assigning a milestone.

  • What specific views are we introducing?
  • Where will they live?
  • How will they be documented?

@DanSheps DanSheps added status: blocked Another issue or external requirement is preventing implementation and removed status: under review Further discussion is needed to determine this issue's scope and/or implementation labels May 18, 2021
@DanSheps
Copy link
Member Author

Blocked by 2.12

@jeremystretch jeremystretch added status: accepted This issue has been accepted for implementation and removed status: blocked Another issue or external requirement is preventing implementation labels May 25, 2021
@jeremystretch jeremystretch added this to the v3.1 milestone May 25, 2021
@maximumG
Copy link
Contributor

When working on a netbox plugin, I also seek to re-use existings netbox UI components. I know it is not officially supported but I'd like to provide a consistent UI and UX while keeping code simple.

I was able to use the generic views that netbox provides to create the following plugins view.

  • ListView, parent class netbox.views.generic.ObjectListView
  • EditView, parent class netbox.views.generic.ObjectEditView
  • DeleteView, parent class netbox.views.generic.ObjectDeleteView

My conclusion on this work is that re-using existing Views is quite simple. The only issue that I ran mostly into was the top-right buttons in the ListView (add, edit, delete). Actually, these buttons are hyperlinks which are automatically generated and not "plugin aware".

To overcome this, I had to register a new django template filter named plugin_validated_viewname. This filter allows URL in the buttons to be "plugin-aware". I duplicated the django template netbox/template/generic/object_list.html and replace the validated_viewname filter with my own filter plugin_validated_viewname.

@register.filter()
def plugin_validated_viewname(model, action):
    """Return the view name for :
        * the given model and action if valid
        * or the given model and action if valid inside a plugin
        * or None if invalid.
    """
    core_viewname = f'{model._meta.app_label}:{model._meta.model_name}_{action}'
    plugin_viewname = f'plugins:{model._meta.app_label}:{model._meta.model_name}_{action}'
    try:
        # Validate and return the view name. We don't return the actual URL yet because many of the templates
        # are written to pass a name to {% url %}.
        reverse(core_viewname)
        return core_viewname
    except NoReverseMatch:
        try:
            reverse(plugin_viewname)
            return plugin_viewname
        except NoReverseMatch:
            return None

@hagbarddenstore
Copy link
Contributor

hagbarddenstore commented Sep 21, 2021

I've also done some modifications to make this issue work.

I've edited the following files:

# utilities/templatetags/helpers.py

@register.filter()
def validated_viewname(model, action):
    """
    Return the view name for the given model and action if valid, or None if invalid.
    """
    viewname = f'{model._meta.app_label}:{model._meta.model_name}_{action}'

    if model._meta.app_label in settings.PLUGINS:
        viewname = "plugins:{}".format(viewname)

    try:
        # Validate and return the view name. We don't return the actual URL yet because many of the templates
        # are written to pass a name to {% url %}.
        reverse(viewname)
        return viewname
    except NoReverseMatch:
        return None
# utilities/templatetags/buttons.py

def _get_viewname(instance, action):
    """
    Return the appropriate viewname for adding, editing, or deleting an instance.
    """

    # Validate action
    assert action in ('add', 'edit', 'delete')

    viewname = "{}:{}_{}".format(
        instance._meta.app_label, instance._meta.model_name, action
    )

    if instance._meta.app_label in settings.PLUGINS:
        viewname = "plugins:{}".format(viewname)

    return viewname

Not entirely sure this is the best solution, since it will allow plugins to override core Netbox URLs. This shouldn't be a large issue though, given that the application names needs to be unique without the plugins prefix.

@DanSheps
Copy link
Member Author

DanSheps commented Sep 21, 2021

While I applaud both efforts, for now, if you want to re-use, please use the netbox-plugin-extensions plugin. This is an unofficial plugin that allows reuse of the Netbox templates & components, but will prevent any namespace collisions with anything we develop down the road.

@maximumG
Copy link
Contributor

Your netbox-plugin-extensions is for me clearly the way to go in order to build easily 'Netbox like' WebUI. We had the same idea internally: to create some kind of templating plugin which would have been used as seed for all other internal plugins, but we are definitely looking forwarding with this public plugin.

@DanSheps
Copy link
Member Author

Feel free to suggest any improvements

@jeremystretch jeremystretch modified the milestones: v3.1, v3.2 Nov 2, 2021
DanSheps added a commit that referenced this issue Jan 5, 2022
…gins and update ButtonColumn to use viewname helper.
@jeremystretch
Copy link
Member

I spoke with @DanSheps and we agreed to replace this FR with #8334, which proposes that we expose NetBox's existing generic views rather than replicating them specifically for plugins. #8334 will be tagged as a v3.2 milestone.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 13, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
status: accepted This issue has been accepted for implementation topic: plugins Relates to the plugins framework type: feature Introduction of new functionality to the application
Projects
None yet
Development

No branches or pull requests

4 participants