Skip to content

Commit

Permalink
Merge pull request #8473 from netbox-community/6221-pluginviews
Browse files Browse the repository at this point in the history
Closes #8472: Make view name resolution plugin-safe
  • Loading branch information
jeremystretch authored Jan 27, 2022
2 parents f1697c6 + 75aa1c7 commit 3a447d5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
4 changes: 2 additions & 2 deletions netbox/netbox/tables/columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from django_tables2.utils import Accessor

from extras.choices import CustomFieldTypeChoices
from utilities.utils import content_type_identifier, content_type_name
from utilities.utils import content_type_identifier, content_type_name, resolve_namespace

__all__ = (
'ActionsColumn',
Expand Down Expand Up @@ -134,7 +134,7 @@ def render(self, record, table, **kwargs):
return ''

model = table.Meta.model
viewname_base = f'{model._meta.app_label}:{model._meta.model_name}'
viewname_base = f'{resolve_namespace(model)}:{model._meta.model_name}'
request = getattr(table, 'context', {}).get('request')
url_appendix = f'?return_url={request.path}' if request else ''

Expand Down
9 changes: 6 additions & 3 deletions netbox/utilities/templatetags/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
from markdown import markdown

from netbox.config import get_config
from netbox.settings import PLUGINS
from utilities.forms import get_selected_values, TableConfigForm
from utilities.markdown import StrikethroughExtension
from utilities.utils import foreground_color
from utilities.utils import foreground_color, resolve_namespace

register = template.Library()

Expand Down Expand Up @@ -115,15 +116,17 @@ def viewname(model, action):
"""
Return the view name for the given model and action. Does not perform any validation.
"""
return f'{model._meta.app_label}:{model._meta.model_name}_{action}'
namespace = resolve_namespace(model)
return f'{namespace}:{model._meta.model_name}_{action}'


@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}'
namespace = resolve_namespace(model)
viewname = f'{namespace}:{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 %}.
Expand Down
10 changes: 10 additions & 0 deletions netbox/utilities/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,20 @@
from mptt.models import MPTTModel

from dcim.choices import CableLengthUnitChoices
from extras.plugins import PluginConfig
from extras.utils import is_taggable
from utilities.constants import HTTP_REQUEST_META_SAFE_COPY


def resolve_namespace(instance):
"""
Get the appropriate namepsace for the app based on whether it is a Plugin or base application
"""
if isinstance(instance._meta.app_config, PluginConfig):
return f'plugins:{instance._meta.app_label}'
return f'{instance._meta.app_label}'


def csv_format(data):
"""
Encapsulate any data which contains a comma within double quotes.
Expand Down

0 comments on commit 3a447d5

Please sign in to comment.