Skip to content

Expose API_SELECT widget to custom scripts. #3766

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

Closed
pszulczewski opened this issue Dec 14, 2019 · 7 comments · Fixed by #4142
Closed

Expose API_SELECT widget to custom scripts. #3766

pszulczewski opened this issue Dec 14, 2019 · 7 comments · Fixed by #4142
Assignees
Labels
status: accepted This issue has been accepted for implementation type: feature Introduction of new functionality to the application

Comments

@pszulczewski
Copy link

pszulczewski commented Dec 14, 2019

Environment

  • Python version: 3.6.8
  • NetBox version: 2.6.6

Proposed Functionality

Add more advanced ObjectVar filtering mechanism in Custom Scripts.
Custom script to use widgets like API_SELECT to relate second ObjectVar on already selected first ObjectVar.

Use Case

Use Case 1
Two ObjectVars
1st Device
2nd Interfaces
Second ObjectVar is limited to interfaces only for selected device in first ObjectVar

Use Case 2
Two ObjectVars
1st Site
2nd Device
Second ObjectVar is limited to Devices only for selected site in first ObjectVar

Database Changes

None

External Dependencies

None

@stale
Copy link

stale bot commented Dec 28, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. NetBox is governed by a small group of core maintainers which means not all opened issues may receive direct feedback. Please see our contributing guide.

@stale stale bot added the pending closure Requires immediate attention to avoid being closed for inactivity label Dec 28, 2019
@lampwins
Copy link
Contributor

We need to do this but I am not sure our current widget API will work very well. We likely need to investigate modifying it or creating a new one for use with custom scripts.

@stale stale bot removed the pending closure Requires immediate attention to avoid being closed for inactivity label Dec 30, 2019
@lampwins lampwins added status: under review Further discussion is needed to determine this issue's scope and/or implementation type: feature Introduction of new functionality to the application labels Dec 30, 2019
@DanSheps
Copy link
Member

DanSheps commented Jan 2, 2020

I have managed to expose the widget API for my own custom scripts, I will have to see what changes I made. I believe I simply had it inherit something additional.

@DanSheps
Copy link
Member

DanSheps commented Jan 2, 2020

Yeah, it looks like I just created separate classes within the individual scripts:


class WidgetIntegerVar(IntegerVar):
    def __init__(self, widget, required=False, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.field_attrs['widget'] = widget
        self.field_attrs['required'] = required

    def as_field(self):
        """
        Render the variable as a Django form field.
        """
        form_field = self.form_field(**self.field_attrs)
        form_field.widget.attrs['class'] += ' form-control'

        return form_field


class WidgetObjectVar(ObjectVar):
    def __init__(self, queryset, widget, *args, **kwargs):
        super().__init__(queryset, *args, **kwargs)
        self.field_attrs['widget'] = widget

    def as_field(self):
        """
        Render the variable as a Django form field.
        """
        form_field = self.form_field(**self.field_attrs)
        form_field.widget.attrs['class'] += ' form-control'

        return form_field

I think this would be cleaner if it were incorporated into the main classes, as overriding certain things would not be required.

@pszulczewski
Copy link
Author

pszulczewski commented Jan 3, 2020

Hi Daniel,

Thanks for sharing the code.
I added the second class to the custom script and APISelect works fine.

    upstream_node = ObjectVar(
        description="Upstream Node",
        queryset=Device.objects.filter(
            status=1
        )
    )
    upstream_port = WidgetObjectVar(
        queryset=Interface.objects.all(),
        widget=APISelect(
            api_url="/api/dcim/interfaces/",
            disabled_indicator='cable',
            additional_query_params={
                'kind': 'physical',
                'device_id': '75',
            }
        )
    )

But the problem is that device_id above is static and I would like to dynamically pass device id depending on previously selected device in another ObjectVar.
All ObjectVars seems to be added to data upon running the script, but I don't see any method accessing them before the script is run to pass device_id to the widget.

@DanSheps
Copy link
Member

DanSheps commented Jan 3, 2020

@pszulczewski Please take any help requests to the discussion group.

@DanSheps
Copy link
Member

I am going to take this on (unless someone else wants to give it a shot).

@DanSheps DanSheps 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 Feb 11, 2020
@DanSheps DanSheps self-assigned this Feb 11, 2020
jeremystretch added a commit that referenced this issue Feb 12, 2020
…_script_fields

Fixes: #3766 - Enable widget usage in scripts
jeremystretch added a commit that referenced this issue Feb 12, 2020
@lock lock bot locked as resolved and limited conversation to collaborators May 20, 2020
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
Development

Successfully merging a pull request may close this issue.

3 participants