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

Allow Plugins to use utilities/templatetags/buttons.py #212

Closed
FragmentedPacket opened this issue Mar 30, 2021 · 2 comments · Fixed by #303
Closed

Allow Plugins to use utilities/templatetags/buttons.py #212

FragmentedPacket opened this issue Mar 30, 2021 · 2 comments · Fixed by #303
Assignees

Comments

@FragmentedPacket
Copy link
Contributor

FragmentedPacket commented Mar 30, 2021

Environment

  • Python version: 3.8.8
  • Nautobot version: 1.0.0b2

Proposed Functionality

Allow plugin authors to use the builtin buttons.

I spoke with @jathanism on this and he agreed with adding a plugin field to these buttons that would prepend on the _get_viewname function.

An example:

def _get_viewname(instance, action, plugin=False):
    """
    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 plugin:
        viewname = f"plugins:{viewname}"

    return viewname

@register.inclusion_tag("buttons/clone.html")
def clone_button(instance, plugin=False):
    url = reverse(_get_viewname(instance, "add", plugin=plugin))

    # Populate cloned field values
    param_string = prepare_cloned_fields(instance)
    if param_string:
        url = f"{url}?{param_string}"

    return {
        "url": url,
    }

This would allow plugin authors to reuse this code within HTML templates for views.

Also what may help is the following template snippet:

    <div class="pull-right">
        <div class="pull-right noprint">
            {% plugin_buttons object %}
            {% if perms.extras.add_status %}
                {% clone_button object plugin= True %}
            {% endif %}
            {% if perms.extras.change_status %}
                {% edit_button object plugin=True %}
            {% endif %}
            {% if perms.extras.delete_status %}
                {% delete_button object plugin=True %}
            {% endif %}
        </div>
    </div>

If this is accepted by the other maintainers, I can do the work to submit a PR to get this feature implemented.

Use Case

Developing a plugin and attempting to re-use as much code provided by Nautobot.

@glennmatthews
Copy link
Contributor

I'm in favor of this feature, but I'd recommend that instead of adding a separate plugin parameter to the API, we instead check whether the provided instance's app_label is a native app or a plugin app, and automatically derive the correct reversed viewname accordingly - see #293 for an example fix to a similar issue.

@FragmentedPacket
Copy link
Contributor Author

@glennmatthews Thanks for the suggestion. Implemented that and this helped me get rid of custom HTML for the list view and just use everything builtin.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants