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

Include request object in custom link renderer on tables #13415

Closed
cpmills1975 opened this issue Aug 8, 2023 · 3 comments · Fixed by #13551
Closed

Include request object in custom link renderer on tables #13415

cpmills1975 opened this issue Aug 8, 2023 · 3 comments · Fixed by #13551
Assignees
Labels
status: accepted This issue has been accepted for implementation type: feature Introduction of new functionality to the application

Comments

@cpmills1975
Copy link
Contributor

NetBox version

v3.5.7

Feature type

Change to existing functionality

Proposed functionality

The custom field renderer should have access to the request object.

Use case

I'd like to record a console server 'node reference' against a console port and create a custom link that deep links me to an SSH session on that console server.

The deep link needs to look something like ssh://{{ request.user }}:{{ object.cf['node_id'] }}@consoleserver.my.domain

If I create a custom link for console ports and then view the detail page for the console port, the custom link shows up and works fine, but if I add the custom link as a column to the console ports table view, the custom link returns 'Error' as the request object is not exposed to the custom link table column renderer.

Database changes

None

External dependencies

None

@cpmills1975 cpmills1975 added the type: feature Introduction of new functionality to the application label Aug 8, 2023
@jeremystretch
Copy link
Member

It seems we're missing several context variables when rendering the column. For reference, the template tag used to render custom links passes the following:

# Pass select context data when rendering the CustomLink
link_context = {
    'object': obj,
    '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
    'perms': context['perms'],  # django.contrib.auth.context_processors.auth
}

Whereas the column passes only object:

rendered = self.customlink.render({
    'object': record,
})

Unfortunately, I'm not sure it's feasible to access the current request from within the column.

@jeremystretch jeremystretch added the status: under review Further discussion is needed to determine this issue's scope and/or implementation label Aug 10, 2023
@kkthxbye-code
Copy link
Contributor

@jeremystretch

Unfortunately, I'm not sure it's feasible to access the current request from within the column.

We do it here:

def render(self, record, table, **kwargs):
# Skip dummy records (e.g. available VLANs) or those with no actions
if not getattr(record, 'pk', None) or not self.actions:
return ''
model = table.Meta.model
if request := getattr(table, 'context', {}).get('request'):
return_url = request.GET.get('return_url', request.get_full_path())
url_appendix = f'?return_url={quote(return_url)}'
else:
url_appendix = ''

Did a quick test and seems to work fine for the CustomLinkColumn, not sure if there's any downsides though.

@jeremystretch
Copy link
Member

Thank you! I wanted to say there was a way to do it but obviously dig far enough into it.

@jeremystretch jeremystretch self-assigned this Aug 24, 2023
@jeremystretch jeremystretch added status: accepted This issue has been accepted for implementation and removed status: under review Further discussion is needed to determine this issue's scope and/or implementation labels Aug 24, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 24, 2023
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 type: feature Introduction of new functionality to the application
Projects
None yet
3 participants