Skip to content

Create DecimalVar class for custom script input #19945

@kyerlasswell

Description

@kyerlasswell

NetBox version

v4.2.9

Feature type

Data model extension

Proposed functionality

Create a new class in netbox/extras/scripts.py called DecimalVar that enables custom scripts to take in decimal numbers as user input. This would be functionally identical to IntegerVar except it would allow decimal numbers.

class DecimalVar(ScriptVariable):
    """
    Decimal representation. Can enforce minimum/maximum values.
    """

    form_field = forms.DecimalField

    def __init__(self, min_value=None, max_value=None, *args, **kwargs):
        super().__init__(*args, **kwargs)

        # Optional minimum/maximum values
        if min_value:
            self.field_attrs["min_value"] = min_value
        if max_value:
            self.field_attrs["max_value"] = max_value

Use case

There is currently no built-in way to get decimal input from a user in a custom script (unless I'm just overlooking something obvious). IntegerVar exists for whole number input, but it cannot be used to get, for example, GPS coordinates from a user trying to create a site from a custom script.

Adding DecimalVar would enable the following:

# get data from user:

site_lat = DecimalVar(
    label="Latitude",
)

site_long = DecimalVar(
    label="Longitude",
)

# run:
site = Site(
    name="name",
    slug="slug",
    region="region",
    latitude=data["site_lat"],
    longitude=data["site_long"],
)

Database changes

None

External dependencies

None

Metadata

Metadata

Assignees

Labels

complexity: lowRequires minimal effort to implementstatus: acceptedThis issue has been accepted for implementationtype: featureIntroduction of new functionality to the application

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions