Parcourir la source

Closes #8684: Change custom link template context variable 'obj' to 'object' (backward-compatible)

jeremystretch il y a 4 ans
Parent
commit
a5c8bbf79e

+ 8 - 7
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
 

+ 1 - 0
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
 

+ 2 - 2
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 <code>{{ obj }}</code>. '
+            'link_text': 'Jinja2 template code for the link text. Reference the object as <code>{{ object }}</code>. '
                          'Links which render as empty text will not be displayed.',
-            'link_url': 'Jinja2 template code for the link URL. Reference the object as <code>{{ obj }}</code>.',
+            'link_url': 'Jinja2 template code for the link URL. Reference the object as <code>{{ object }}</code>.',
         }
 
 

+ 2 - 1
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

+ 8 - 2
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'<a href="{rendered["link"]}"{rendered["link_target"]}>{rendered["text"]}</a>')
         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: