From a5c8bbf79edc2e1934f094a7c03dad7a303715a9 Mon Sep 17 00:00:00 2001 From: jeremystretch Date: Fri, 18 Feb 2022 09:50:02 -0500 Subject: [PATCH] Closes #8684: Change custom link template context variable 'obj' to 'object' (backward-compatible) --- docs/models/extras/customlink.md | 15 ++++++++------- docs/release-notes/version-3.2.md | 1 + netbox/extras/forms/models.py | 4 ++-- netbox/extras/templatetags/custom_links.py | 3 ++- netbox/netbox/tables/columns.py | 10 ++++++++-- 5 files changed, 21 insertions(+), 12 deletions(-) diff --git a/docs/models/extras/customlink.md b/docs/models/extras/customlink.md index 96ff0bbf7..be914e99b 100644 --- a/docs/models/extras/customlink.md +++ b/docs/models/extras/customlink.md @@ -24,13 +24,14 @@ Custom links appear as buttons in the top right corner of the page. Numeric weig The following context data is available within the template when rendering a custom link's text or URL. -| Variable | Description | -|----------|-------------| -| `obj` | The NetBox object being displayed | -| `debug` | A boolean indicating whether debugging is enabled | -| `request` | The current WSGI request | -| `user` | The current user (if authenticated) | -| `perms` | The [permissions](https://docs.djangoproject.com/en/stable/topics/auth/default/#permissions) assigned to the user | +| Variable | Description | +|-----------|-------------------------------------------------------------------------------------------------------------------| +| `object` | The NetBox object being displayed | +| `obj` | Same as `object`; maintained for backward compatability until NetBox v3.5 | +| `debug` | A boolean indicating whether debugging is enabled | +| `request` | The current WSGI request | +| `user` | The current user (if authenticated) | +| `perms` | The [permissions](https://docs.djangoproject.com/en/stable/topics/auth/default/#permissions) assigned to the user | ## Conditional Rendering diff --git a/docs/release-notes/version-3.2.md b/docs/release-notes/version-3.2.md index 5f58eeb17..11f5b36a0 100644 --- a/docs/release-notes/version-3.2.md +++ b/docs/release-notes/version-3.2.md @@ -161,6 +161,7 @@ Where it is desired to limit the range of available VLANs within a group, users * [#8031](https://github.com/netbox-community/netbox/issues/8031) - Remove automatic redirection of legacy slug-based URLs * [#8195](https://github.com/netbox-community/netbox/issues/8195), [#8454](https://github.com/netbox-community/netbox/issues/8454) - Use 64-bit integers for all primary keys * [#8509](https://github.com/netbox-community/netbox/issues/8509) - `CSRF_TRUSTED_ORIGINS` is now a discrete configuration parameter (rather than being populated from `ALLOWED_HOSTS`) +* [#8684](https://github.com/netbox-community/netbox/issues/8684) - Change custom link template context variable `obj` to `object` (backward-compatible) ### REST API Changes diff --git a/netbox/extras/forms/models.py b/netbox/extras/forms/models.py index 4c4246be0..68b4bf9c6 100644 --- a/netbox/extras/forms/models.py +++ b/netbox/extras/forms/models.py @@ -72,9 +72,9 @@ class CustomLinkForm(BootstrapMixin, forms.ModelForm): 'link_url': forms.Textarea(attrs={'class': 'font-monospace'}), } help_texts = { - 'link_text': 'Jinja2 template code for the link text. Reference the object as {{ obj }}. ' + 'link_text': 'Jinja2 template code for the link text. Reference the object as {{ object }}. ' 'Links which render as empty text will not be displayed.', - 'link_url': 'Jinja2 template code for the link URL. Reference the object as {{ obj }}.', + 'link_url': 'Jinja2 template code for the link URL. Reference the object as {{ object }}.', } diff --git a/netbox/extras/templatetags/custom_links.py b/netbox/extras/templatetags/custom_links.py index dd5467338..d963bd25a 100644 --- a/netbox/extras/templatetags/custom_links.py +++ b/netbox/extras/templatetags/custom_links.py @@ -42,7 +42,8 @@ def custom_links(context, obj): # Pass select context data when rendering the CustomLink link_context = { - 'obj': obj, + 'object': obj, + 'obj': obj, # TODO: Remove in NetBox v3.5 'debug': context.get('debug', False), # django.template.context_processors.debug 'request': context['request'], # django.template.context_processors.request 'user': context['user'], # django.contrib.auth.context_processors.auth diff --git a/netbox/netbox/tables/columns.py b/netbox/netbox/tables/columns.py index ce3f0ecae..2e28a66e3 100644 --- a/netbox/netbox/tables/columns.py +++ b/netbox/netbox/tables/columns.py @@ -399,7 +399,10 @@ class CustomLinkColumn(tables.Column): def render(self, record): try: - rendered = self.customlink.render({'obj': record}) + rendered = self.customlink.render({ + 'object': record, + 'obj': record, # TODO: Remove in NetBox v3.5 + }) if rendered: return mark_safe(f'{rendered["text"]}') except Exception as e: @@ -408,7 +411,10 @@ class CustomLinkColumn(tables.Column): def value(self, record): try: - rendered = self.customlink.render({'obj': record}) + rendered = self.customlink.render({ + 'object': record, + 'obj': record, # TODO: Remove in NetBox v3.5 + }) if rendered: return rendered['link'] except Exception: