diff --git a/.github/ISSUE_TEMPLATE/01-feature_request.yaml b/.github/ISSUE_TEMPLATE/01-feature_request.yaml index 24497f825..763733c09 100644 --- a/.github/ISSUE_TEMPLATE/01-feature_request.yaml +++ b/.github/ISSUE_TEMPLATE/01-feature_request.yaml @@ -15,7 +15,7 @@ body: attributes: label: NetBox version description: What version of NetBox are you currently running? - placeholder: v4.3.7 + placeholder: v4.4.0 validations: required: true - type: dropdown diff --git a/.github/ISSUE_TEMPLATE/02-bug_report.yaml b/.github/ISSUE_TEMPLATE/02-bug_report.yaml index 153109e31..f25b70b19 100644 --- a/.github/ISSUE_TEMPLATE/02-bug_report.yaml +++ b/.github/ISSUE_TEMPLATE/02-bug_report.yaml @@ -27,7 +27,7 @@ body: attributes: label: NetBox Version description: What version of NetBox are you currently running? - placeholder: v4.3.7 + placeholder: v4.4.0 validations: required: true - type: dropdown diff --git a/.gitignore b/.gitignore index e04e44a30..eb1eccbef 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ yarn-error.log* /netbox/netbox/configuration.py /netbox/netbox/ldap_config.py /netbox/local/* +/netbox/media /netbox/reports/* !/netbox/reports/__init__.py /netbox/scripts/* diff --git a/base_requirements.txt b/base_requirements.txt index d11eff972..fd20eae09 100644 --- a/base_requirements.txt +++ b/base_requirements.txt @@ -1,3 +1,7 @@ +# Shell text coloring +# https://github.com/tartley/colorama/blob/master/CHANGELOG.rst +colorama + # The Python web framework on which NetBox is built # https://docs.djangoproject.com/en/stable/releases/ Django==5.2.* @@ -102,7 +106,11 @@ mkdocs-material # Introspection for embedded code # https://github.com/mkdocstrings/mkdocstrings/blob/main/CHANGELOG.md -mkdocstrings[python] +mkdocstrings + +# Python handler for mkdocstrings +# https://github.com/mkdocstrings/python/blob/main/CHANGELOG.md +mkdocstrings-python # Library for manipulating IP prefixes and addresses # https://github.com/netaddr/netaddr/blob/master/CHANGELOG.rst @@ -131,7 +139,8 @@ requests # rq # https://github.com/rq/rq/blob/master/CHANGES.md -rq +# RQ v2.5 drops support for Redis < 5.0 +rq==2.4.1 # Django app for social-auth-core # https://github.com/python-social-auth/social-app-django/blob/master/CHANGELOG.md @@ -141,6 +150,10 @@ social-auth-app-django # https://github.com/python-social-auth/social-core/blob/master/CHANGELOG.md social-auth-core +# Image thumbnail generation +# https://github.com/jazzband/sorl-thumbnail/blob/master/CHANGES.rst +sorl-thumbnail + # Strawberry GraphQL # https://github.com/strawberry-graphql/strawberry/blob/main/CHANGELOG.md strawberry-graphql diff --git a/contrib/netbox-housekeeping.service b/contrib/netbox-housekeeping.service deleted file mode 100644 index 4b0361fcb..000000000 --- a/contrib/netbox-housekeeping.service +++ /dev/null @@ -1,17 +0,0 @@ -[Unit] -Description=NetBox Housekeeping Service -Documentation=https://docs.netbox.dev/ -After=network-online.target -Wants=network-online.target - -[Service] -Type=simple - -User=netbox -Group=netbox -WorkingDirectory=/opt/netbox - -ExecStart=/opt/netbox/venv/bin/python /opt/netbox/netbox/manage.py housekeeping - -[Install] -WantedBy=multi-user.target diff --git a/contrib/netbox-housekeeping.sh b/contrib/netbox-housekeeping.sh deleted file mode 100755 index 5b1c46c5e..000000000 --- a/contrib/netbox-housekeeping.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/sh -# This shell script invokes NetBox's housekeeping management command, which -# intended to be run nightly. This script can be copied into your system's -# daily cron directory (e.g. /etc/cron.daily), or referenced directly from -# within the cron configuration file. -# -# If NetBox has been installed into a nonstandard location, update the paths -# below. -/opt/netbox/venv/bin/python /opt/netbox/netbox/manage.py housekeeping diff --git a/contrib/netbox-housekeeping.timer b/contrib/netbox-housekeeping.timer deleted file mode 100644 index 16facb05c..000000000 --- a/contrib/netbox-housekeeping.timer +++ /dev/null @@ -1,13 +0,0 @@ -[Unit] -Description=NetBox Housekeeping Timer -Documentation=https://docs.netbox.dev/ -After=network-online.target -Wants=network-online.target - -[Timer] -OnCalendar=daily -AccuracySec=1h -Persistent=true - -[Install] -WantedBy=multi-user.target diff --git a/docs/administration/housekeeping.md b/docs/administration/housekeeping.md deleted file mode 100644 index 674ceb312..000000000 --- a/docs/administration/housekeeping.md +++ /dev/null @@ -1,49 +0,0 @@ -# Housekeeping - -NetBox includes a `housekeeping` management command that should be run nightly. This command handles: - -* Clearing expired authentication sessions from the database -* Deleting changelog records older than the configured [retention time](../configuration/miscellaneous.md#changelog_retention) -* Deleting job result records older than the configured [retention time](../configuration/miscellaneous.md#job_retention) -* Check for new NetBox releases (if [`RELEASE_CHECK_URL`](../configuration/miscellaneous.md#release_check_url) is set) - -This command can be invoked directly, or by using the shell script provided at `/opt/netbox/contrib/netbox-housekeeping.sh`. - -## Scheduling - -### Using Cron - -This script can be linked from your cron scheduler's daily jobs directory (e.g. `/etc/cron.daily`) or referenced directly within the cron configuration file. - -```shell -sudo ln -s /opt/netbox/contrib/netbox-housekeeping.sh /etc/cron.daily/netbox-housekeeping -``` - -!!! note - On Debian-based systems, be sure to omit the `.sh` file extension when linking to the script from within a cron directory. Otherwise, the task may not run. - -### Using Systemd - -First, create symbolic links for the systemd service and timer files. Link the existing service and timer files from the `/opt/netbox/contrib/` directory to the `/etc/systemd/system/` directory: - -```bash -sudo ln -s /opt/netbox/contrib/netbox-housekeeping.service /etc/systemd/system/netbox-housekeeping.service -sudo ln -s /opt/netbox/contrib/netbox-housekeeping.timer /etc/systemd/system/netbox-housekeeping.timer -``` - -Then, reload the systemd configuration and enable the timer to start automatically at boot: - -```bash -sudo systemctl daemon-reload -sudo systemctl enable --now netbox-housekeeping.timer -``` - -Check the status of your timer by running: - -```bash -sudo systemctl list-timers --all -``` - -This command will show a list of all timers, including your `netbox-housekeeping.timer`. Make sure the timer is active and properly scheduled. - -That's it! Your NetBox housekeeping service is now configured to run daily using systemd. diff --git a/docs/configuration/miscellaneous.md b/docs/configuration/miscellaneous.md index 1fbd0ee35..18de6458d 100644 --- a/docs/configuration/miscellaneous.md +++ b/docs/configuration/miscellaneous.md @@ -108,8 +108,6 @@ By default, NetBox will prevent the creation of duplicate prefixes and IP addres ## EVENTS_PIPELINE -!!! info "This parameter was introduced in NetBox v4.2." - Default: `['extras.events.process_event_queue',]` NetBox will call dotted paths to the functions listed here for events (create, update, delete) on models as well as when custom EventRules are fired. diff --git a/docs/configuration/required-parameters.md b/docs/configuration/required-parameters.md index 25aa7978a..19222740d 100644 --- a/docs/configuration/required-parameters.md +++ b/docs/configuration/required-parameters.md @@ -34,8 +34,6 @@ See the [`DATABASES`](#databases) configuration below for usage. ## DATABASES -!!! info "This parameter was introduced in NetBox v4.3." - NetBox requires access to a PostgreSQL 14 or later database service to store data. This service can run locally on the NetBox server or on a remote system. Databases are defined as named dictionaries: ```python diff --git a/docs/configuration/system.md b/docs/configuration/system.md index e294abb9c..89e7d8d8e 100644 --- a/docs/configuration/system.md +++ b/docs/configuration/system.md @@ -14,8 +14,6 @@ BASE_PATH = 'netbox/' ## DATABASE_ROUTERS -!!! info "This parameter was introduced in NetBox v4.3." - Default: `[]` (empty list) An iterable of [database routers](https://docs.djangoproject.com/en/stable/topics/db/multi-db/) to use for automatically selecting the appropriate database(s) for a query. This is useful only when [multiple databases](./required-parameters.md#databases) have been configured. @@ -72,6 +70,16 @@ Email is sent from NetBox only for critical events or if configured for [logging --- +## HOSTNAME + +!!! info "This parameter was introduced in NetBox v4.4." + +Default: System hostname + +The hostname displayed in the user interface identifying the system on which NetBox is running. If not defined, this defaults to the system hostname as reported by Python's `platform.node()`. + +--- + ## HTTP_PROXIES Default: `None` @@ -159,6 +167,7 @@ LOGGING = { * `netbox.auth.*` - Authentication events * `netbox.api.views.*` - Views which handle business logic for the REST API * `netbox.event_rules` - Event rules +* `netbox.jobs.*` - Background jobs * `netbox.reports.*` - Report execution (`module.name`) * `netbox.scripts.*` - Custom script execution (`module.name`) * `netbox.views.*` - Views which handle business logic for the web UI @@ -175,8 +184,6 @@ The file path to the location where media files (such as image attachments) are ## PROXY_ROUTERS -!!! info "This parameter was introduced in NetBox v4.3." - Default: `["utilities.proxy.DefaultProxyRouter"]` A list of Python classes responsible for determining which proxy server(s) to use for outbound HTTP requests. Each item in the list can be the class itself or the dotted path to the class. diff --git a/docs/customization/custom-scripts.md b/docs/customization/custom-scripts.md index e7536a654..df9437634 100644 --- a/docs/customization/custom-scripts.md +++ b/docs/customization/custom-scripts.md @@ -275,6 +275,15 @@ Stores a numeric integer. Options include: * `min_value` - Minimum value * `max_value` - Maximum value +### DecimalVar + +Stores a numeric decimal. Options include: + +* `min_value` - Minimum value +* `max_value` - Maximum value +* `max_digits` - Maximum number of digits, including decimal places +* `decimal_places` - Number of decimal places + ### BooleanVar A true/false flag. This field has no options beyond the defaults listed above. diff --git a/docs/development/application-registry.md b/docs/development/application-registry.md index fc96bfd76..8d36ccf96 100644 --- a/docs/development/application-registry.md +++ b/docs/development/application-registry.md @@ -22,24 +22,9 @@ Stores registration made using `netbox.denormalized.register()`. For each model, ### `model_features` -A dictionary of particular features (e.g. custom fields) mapped to the NetBox models which support them, arranged by app. For example: +A dictionary of model features (e.g. custom fields, tags, etc.) mapped to the functions used to qualify a model as supporting each feature. Model features are registered using the `register_model_feature()` function in `netbox.utils`. -```python -{ - 'custom_fields': { - 'circuits': ['provider', 'circuit'], - 'dcim': ['site', 'rack', 'devicetype', ...], - ... - }, - 'event_rules': { - 'extras': ['configcontext', 'tag', ...], - 'dcim': ['site', 'rack', 'devicetype', ...], - }, - ... -} -``` - -Supported model features are listed in the [features matrix](./models.md#features-matrix). +Core model features are listed in the [features matrix](./models.md#features-matrix). ### `models` diff --git a/docs/development/models.md b/docs/development/models.md index 1b91db515..5daa67742 100644 --- a/docs/development/models.md +++ b/docs/development/models.md @@ -10,19 +10,26 @@ The Django [content types](https://docs.djangoproject.com/en/stable/ref/contrib/ Depending on its classification, each NetBox model may support various features which enhance its operation. Each feature is enabled by inheriting from its designated mixin class, and some features also make use of the [application registry](./application-registry.md#model_features). -| Feature | Feature Mixin | Registry Key | Description | -|------------------------------------------------------------|-------------------------|--------------------|-----------------------------------------------------------------------------------------| -| [Change logging](../features/change-logging.md) | `ChangeLoggingMixin` | - | Changes to these objects are automatically recorded in the change log | -| Cloning | `CloningMixin` | - | Provides the `clone()` method to prepare a copy | -| [Custom fields](../customization/custom-fields.md) | `CustomFieldsMixin` | `custom_fields` | These models support the addition of user-defined fields | -| [Custom links](../customization/custom-links.md) | `CustomLinksMixin` | `custom_links` | These models support the assignment of custom links | -| [Custom validation](../customization/custom-validation.md) | `CustomValidationMixin` | - | Supports the enforcement of custom validation rules | -| [Export templates](../customization/export-templates.md) | `ExportTemplatesMixin` | `export_templates` | Users can create custom export templates for these models | -| [Job results](../features/background-jobs.md) | `JobsMixin` | `jobs` | Background jobs can be scheduled for these models | -| [Journaling](../features/journaling.md) | `JournalingMixin` | `journaling` | These models support persistent historical commentary | -| [Synchronized data](../integrations/synchronized-data.md) | `SyncedDataMixin` | `synced_data` | Certain model data can be automatically synchronized from a remote data source | -| [Tagging](../models/extras/tag.md) | `TagsMixin` | `tags` | The models can be tagged with user-defined tags | -| [Event rules](../features/event-rules.md) | `EventRulesMixin` | `event_rules` | Event rules can send webhooks or run custom scripts automatically in response to events | +| Feature | Feature Mixin | Registry Key | Description | +|------------------------------------------------------------|-------------------------|---------------------|-----------------------------------------------------------------------------------------| +| [Bookmarks](../features/customization.md#bookmarks) | `BookmarksMixin` | `bookmarks` | These models can be bookmarked natively in the user interface | +| [Change logging](../features/change-logging.md) | `ChangeLoggingMixin` | `change_logging` | Changes to these objects are automatically recorded in the change log | +| Cloning | `CloningMixin` | `cloning` | Provides the `clone()` method to prepare a copy | +| [Contacts](../features/contacts.md) | `ContactsMixin` | `contacts` | Contacts can be associated with these models | +| [Custom fields](../customization/custom-fields.md) | `CustomFieldsMixin` | `custom_fields` | These models support the addition of user-defined fields | +| [Custom links](../customization/custom-links.md) | `CustomLinksMixin` | `custom_links` | These models support the assignment of custom links | +| [Custom validation](../customization/custom-validation.md) | `CustomValidationMixin` | - | Supports the enforcement of custom validation rules | +| [Event rules](../features/event-rules.md) | `EventRulesMixin` | `event_rules` | Event rules can send webhooks or run custom scripts automatically in response to events | +| [Export templates](../customization/export-templates.md) | `ExportTemplatesMixin` | `export_templates` | Users can create custom export templates for these models | +| [Image attachments](../models/extras/imageattachment.md) | `ImageAttachmentsMixin` | `image_attachments` | Image uploads can be attached to these models | +| [Jobs](../features/background-jobs.md) | `JobsMixin` | `jobs` | Background jobs can be scheduled for these models | +| [Journaling](../features/journaling.md) | `JournalingMixin` | `journaling` | These models support persistent historical commentary | +| [Notifications](../features/notifications.md) | `NotificationsMixin` | `notifications` | These models support user notifications | +| [Synchronized data](../integrations/synchronized-data.md) | `SyncedDataMixin` | `synced_data` | Certain model data can be automatically synchronized from a remote data source | +| [Tagging](../models/extras/tag.md) | `TagsMixin` | `tags` | The models can be tagged with user-defined tags | + +!!! note + The above listed features are supported natively by NetBox. Beginning with NetBox v4.4.0, plugins can register their own model features as well. ## Models Index diff --git a/docs/development/release-checklist.md b/docs/development/release-checklist.md index 5478e37e9..3b097cc92 100644 --- a/docs/development/release-checklist.md +++ b/docs/development/release-checklist.md @@ -31,28 +31,14 @@ Close the [release milestone](https://github.com/netbox-community/netbox/milesto Check that a link to the release notes for the new version is present in the navigation menu (defined in `mkdocs.yml`), and that a summary of all major new features has been added to `docs/index.md`. -### Update the Dependency Requirements Matrix - -For every minor release, update the dependency requirements matrix in `docs/installation/upgrading.md` ("All versions") to reflect the supported versions of Python, PostgreSQL, and Redis: - -1. Add a new row with the supported dependency versions. -2. Include a documentation link using the release tag format: `https://github.com/netbox-community/netbox/blob/v4.2.0/docs/installation/index.md` -3. Bold any version changes for clarity. - -**Example Update:** - -```markdown -| NetBox Version | Python min | Python max | PostgreSQL min | Redis min | Documentation | -|:--------------:|:----------:|:----------:|:--------------:|:---------:|:-------------------------------------------------------------------------------------------------:| -| 4.2 | 3.10 | 3.12 | **13** | 4.0 | [Link](https://github.com/netbox-community/netbox/blob/v4.2.0/docs/installation/index.md) | -``` - ### Update System Requirements If a new Django release is adopted or other major dependencies (Python, PostgreSQL, Redis) change: * Update the installation guide (`docs/installation/index.md`) with the new minimum versions. -* Update the upgrade guide (`docs/installation/upgrading.md`) for the current version accordingly. +* Update the upgrade guide (`docs/installation/upgrading.md`) for the current version. + * Update the minimum versions for each dependency. + * Add a new row to the release history table. Bold any version changes for clarity. * Update the minimum PostgreSQL version in the programming error template (`netbox/templates/exceptions/programming_error.html`). * Update the minimum and supported Python versions in the project metadata file (`pyproject.toml`) diff --git a/docs/features/change-logging.md b/docs/features/change-logging.md index 919f59110..73e23709c 100644 --- a/docs/features/change-logging.md +++ b/docs/features/change-logging.md @@ -8,6 +8,12 @@ When a request is made, a UUID is generated and attached to any change records r Change records are exposed in the API via the read-only endpoint `/api/extras/object-changes/`. They may also be exported via the web UI in CSV format. +## User Messages + +!!! info "This feature was introduced in NetBox v4.4." + +When creating, modifying, or deleting an object in NetBox, a user has the option of recording an arbitrary message that will appear in the change record. This can be helpful to capture additional context, such as the reason for the change. + ## Correlating Changes by Request Every request made to NetBox is assigned a random unique ID that can be used to correlate change records. For example, if you change the status of three sites using the UI's bulk edit feature, you will see three new change records (one for each site) all referencing the same request ID. This shows that all three changes were made as part of the same request. diff --git a/docs/features/ipam.md b/docs/features/ipam.md index 3cbe4319d..5879a5fde 100644 --- a/docs/features/ipam.md +++ b/docs/features/ipam.md @@ -62,8 +62,8 @@ VRF modeling in NetBox very closely follows what you find in real-world network An often overlooked component of IPAM, NetBox also tracks autonomous system (AS) numbers and their assignment to sites. Both 16- and 32-bit AS numbers are supported, and like aggregates each ASN is assigned to an authoritative RIR. -## Service Mapping +## Application Service Mapping NetBox models network applications as discrete service objects associated with devices and/or virtual machines, and optionally with specific IP addresses attached to those parent objects. These can be used to catalog the applications running on your network for reference by other objects or integrated tools. -To model services in NetBox, begin by creating a service template defining the name, protocol, and port number(s) on which the service listens. This template can then be easily instantiated to "attach" new services to a device or virtual machine. It's also possible to create new services by hand, without a template, however this approach can be tedious. +To model application services in NetBox, begin by creating an application service template defining the name, protocol, and port number(s) on which the service listens. This template can then be easily instantiated to "attach" new services to a device or virtual machine. It's also possible to create new application services by hand, without a template, however this approach can be tedious. diff --git a/docs/installation/3-netbox.md b/docs/installation/3-netbox.md index a91ce5e9c..acf04dc2a 100644 --- a/docs/installation/3-netbox.md +++ b/docs/installation/3-netbox.md @@ -264,18 +264,6 @@ cd /opt/netbox/netbox python3 manage.py createsuperuser ``` -## Schedule the Housekeeping Task - -NetBox includes a `housekeeping` management command that handles some recurring cleanup tasks, such as clearing out old sessions and expired change records. Although this command may be run manually, it is recommended to configure a scheduled job using the system's `cron` daemon or a similar utility. - -A shell script which invokes this command is included at `contrib/netbox-housekeeping.sh`. It can be copied to or linked from your system's daily cron task directory, or included within the crontab directly. (If installing NetBox into a nonstandard path, be sure to update the system paths within this script first.) - -```shell -sudo ln -s /opt/netbox/contrib/netbox-housekeeping.sh /etc/cron.daily/netbox-housekeeping -``` - -See the [housekeeping documentation](../administration/housekeeping.md) for further details. - ## Test the Application At this point, we should be able to run NetBox's development server for testing. We can check by starting a development instance locally. diff --git a/docs/installation/upgrading.md b/docs/installation/upgrading.md index 0a02f7a04..cf0a16754 100644 --- a/docs/installation/upgrading.md +++ b/docs/installation/upgrading.md @@ -25,42 +25,21 @@ NetBox requires the following dependencies: ### Version History -| NetBox Version | Python min | Python max | PostgreSQL min | Redis min | Documentation | -|:--------------:|:----------:|:----------:|:--------------:|:---------:|:-------------------------------------------------------------------------------------------------:| -| 4.3 | 3.10 | 3.12 | 14 | 4.0 | [Link](https://github.com/netbox-community/netbox/blob/v4.3.0/docs/installation/index.md) | -| 4.2 | 3.10 | 3.12 | 13 | 4.0 | [Link](https://github.com/netbox-community/netbox/blob/v4.2.0/docs/installation/index.md) | -| 4.1 | 3.10 | 3.12 | 12 | 4.0 | [Link](https://github.com/netbox-community/netbox/blob/v4.1.0/docs/installation/index.md) | -| 4.0 | 3.10 | 3.12 | 12 | 4.0 | [Link](https://github.com/netbox-community/netbox/blob/v4.0.0/docs/installation/index.md) | -| 3.7 | 3.8 | 3.11 | 12 | 4.0 | [Link](https://github.com/netbox-community/netbox/blob/v3.7.0/docs/installation/index.md) | -| 3.6 | 3.8 | 3.11 | 12 | 4.0 | [Link](https://github.com/netbox-community/netbox/blob/v3.6.0/docs/installation/index.md) | -| 3.5 | 3.8 | 3.10 | 11 | 4.0 | [Link](https://github.com/netbox-community/netbox/blob/v3.5.0/docs/installation/index.md) | -| 3.4 | 3.8 | 3.10 | 11 | 4.0 | [Link](https://github.com/netbox-community/netbox/blob/v3.4.0/docs/installation/index.md) | -| 3.3 | 3.8 | 3.10 | 10 | 4.0 | [Link](https://github.com/netbox-community/netbox/blob/v3.3.0/docs/installation/index.md) | -| 3.2 | 3.8 | 3.10 | 10 | 4.0 | [Link](https://github.com/netbox-community/netbox/blob/v3.2.0/docs/installation/index.md) | -| 3.1 | 3.7 | 3.9 | 10 | 4.0 | [Link](https://github.com/netbox-community/netbox/blob/v3.1.0/docs/installation/index.md) | -| 3.0 | 3.7 | 3.9 | 9.6 | 4.0 | [Link](https://github.com/netbox-community/netbox/blob/v3.0.0/docs/installation/index.md) | -| 2.11 | 3.6 | 3.9 | 9.6 | 4.0 | [Link](https://github.com/netbox-community/netbox/blob/v2.11.0/docs/installation/index.md) | -| 2.10 | 3.6 | 3.8 | 9.6 | 4.0 | [Link](https://github.com/netbox-community/netbox/blob/v2.10.0/docs/installation/index.md) | -| 2.9 | 3.6 | 3.8 | 9.5 | 4.0 | [Link](https://github.com/netbox-community/netbox/blob/v2.9.0/docs/installation/index.md) | -| 2.8 | 3.6 | 3.8 | 9.5 | 3.4 | [Link](https://github.com/netbox-community/netbox/blob/v2.8.0/docs/installation/index.md) | -| 2.7 | 3.5 | 3.7 | 9.4 | - | [Link](https://github.com/netbox-community/netbox/blob/v2.7.0/docs/installation/index.md) | -| 2.6 | 3.5 | 3.7 | 9.4 | - | [Link](https://github.com/netbox-community/netbox/blob/v2.6.0/docs/installation/index.md) | -| 2.5 | 3.5 | 3.7 | 9.4 | - | [Link](https://github.com/netbox-community/netbox/blob/v2.5.0/docs/installation/index.md) | -| 2.4 | 3.4 | 3.7 | 9.4 | - | [Link](https://github.com/netbox-community/netbox/blob/v2.4.0/docs/installation/index.md) | -| 2.3 | 2.7 | 3.6 | 9.4 | - | [Link](https://github.com/netbox-community/netbox/blob/v2.3.0/docs/installation/postgresql.md) | -| 2.2 | 2.7 | 3.6 | 9.4 | - | [Link](https://github.com/netbox-community/netbox/blob/v2.2.0/docs/installation/postgresql.md) | -| 2.1 | 2.7 | 3.6 | 9.3 | - | [Link](https://github.com/netbox-community/netbox/blob/v2.1.0/docs/installation/postgresql.md) | -| 2.0 | 2.7 | 3.6 | 9.3 | - | [Link](https://github.com/netbox-community/netbox/blob/v2.0.0/docs/installation/postgresql.md) | -| 1.9 | 2.7 | 3.5 | 9.2 | - | [Link](https://github.com/netbox-community/netbox/blob/v1.9.0-r1/docs/installation/postgresql.md) | -| 1.8 | 2.7 | 3.5 | 9.2 | - | [Link](https://github.com/netbox-community/netbox/blob/v1.8.0/docs/installation/postgresql.md) | -| 1.7 | 2.7 | 3.5 | 9.2 | - | [Link](https://github.com/netbox-community/netbox/blob/v1.7.0/docs/installation/postgresql.md) | -| 1.6 | 2.7 | 3.5 | 9.2 | - | [Link](https://github.com/netbox-community/netbox/blob/v1.6.0/docs/installation/postgresql.md) | -| 1.5 | 2.7 | 3.5 | 9.2 | - | [Link](https://github.com/netbox-community/netbox/blob/v1.5.0/docs/installation/postgresql.md) | -| 1.4 | 2.7 | 3.5 | 9.1 | - | [Link](https://github.com/netbox-community/netbox/blob/v1.4.0/docs/installation/postgresql.md) | -| 1.3 | 2.7 | 3.5 | 9.1 | - | [Link](https://github.com/netbox-community/netbox/blob/v1.3.0/docs/installation/postgresql.md) | -| 1.2 | 2.7 | 3.5 | 9.1 | - | [Link](https://github.com/netbox-community/netbox/blob/v1.2.0/docs/installation/postgresql.md) | -| 1.1 | 2.7 | 3.5 | 9.1 | - | [Link](https://github.com/netbox-community/netbox/blob/v1.1.0/docs/getting-started.md) | -| 1.0 | 2.7 | 3.5 | 9.1 | - | [Link](https://github.com/netbox-community/netbox/blob/1.0.0/docs/getting-started.md) | +| NetBox Version | Python min | Python max | PostgreSQL min | Redis min | Documentation | +|:--------------:|:----------:|:----------:|:--------------:|:---------:|:-----------------------------------------------------------------------------------------:| +| 4.4 | 3.10 | 3.12 | 14 | 4.0 | [Link](https://github.com/netbox-community/netbox/blob/v4.4.0/docs/installation/index.md) | +| 4.3 | 3.10 | 3.12 | 14 | 4.0 | [Link](https://github.com/netbox-community/netbox/blob/v4.3.0/docs/installation/index.md) | +| 4.2 | 3.10 | 3.12 | 13 | 4.0 | [Link](https://github.com/netbox-community/netbox/blob/v4.2.0/docs/installation/index.md) | +| 4.1 | 3.10 | 3.12 | 12 | 4.0 | [Link](https://github.com/netbox-community/netbox/blob/v4.1.0/docs/installation/index.md) | +| 4.0 | 3.10 | 3.12 | 12 | 4.0 | [Link](https://github.com/netbox-community/netbox/blob/v4.0.0/docs/installation/index.md) | +| 3.7 | 3.8 | 3.11 | 12 | 4.0 | [Link](https://github.com/netbox-community/netbox/blob/v3.7.0/docs/installation/index.md) | +| 3.6 | 3.8 | 3.11 | 12 | 4.0 | [Link](https://github.com/netbox-community/netbox/blob/v3.6.0/docs/installation/index.md) | +| 3.5 | 3.8 | 3.10 | 11 | 4.0 | [Link](https://github.com/netbox-community/netbox/blob/v3.5.0/docs/installation/index.md) | +| 3.4 | 3.8 | 3.10 | 11 | 4.0 | [Link](https://github.com/netbox-community/netbox/blob/v3.4.0/docs/installation/index.md) | +| 3.3 | 3.8 | 3.10 | 10 | 4.0 | [Link](https://github.com/netbox-community/netbox/blob/v3.3.0/docs/installation/index.md) | +| 3.2 | 3.8 | 3.10 | 10 | 4.0 | [Link](https://github.com/netbox-community/netbox/blob/v3.2.0/docs/installation/index.md) | +| 3.1 | 3.7 | 3.9 | 10 | 4.0 | [Link](https://github.com/netbox-community/netbox/blob/v3.1.0/docs/installation/index.md) | +| 3.0 | 3.7 | 3.9 | 9.6 | 4.0 | [Link](https://github.com/netbox-community/netbox/blob/v3.0.0/docs/installation/index.md) | ## 3. Install the Latest Release @@ -183,13 +162,3 @@ Finally, restart the gunicorn and RQ services: ```no-highlight sudo systemctl restart netbox netbox-rq ``` - -## 6. Verify Housekeeping Scheduling - -If upgrading from a release prior to NetBox v3.0, check that a cron task (or similar scheduled process) has been configured to run NetBox's nightly housekeeping command. A shell script which invokes this command is included at `contrib/netbox-housekeeping.sh`. It can be linked from your system's daily cron task directory, or included within the crontab directly. (If NetBox has been installed in a nonstandard path, be sure to update the system paths within this script first.) - -```shell -sudo ln -s /opt/netbox/contrib/netbox-housekeeping.sh /etc/cron.daily/netbox-housekeeping -``` - -See the [housekeeping documentation](../administration/housekeeping.md) for further details. diff --git a/docs/integrations/prometheus-metrics.md b/docs/integrations/prometheus-metrics.md index 006ff16a4..3f0e0ebda 100644 --- a/docs/integrations/prometheus-metrics.md +++ b/docs/integrations/prometheus-metrics.md @@ -11,6 +11,8 @@ NetBox makes use of the [django-prometheus](https://github.com/korfuri/django-pr - Per model insert, update, and delete counters - Per view request counters - Per view request latency histograms +- REST API requests (by endpoint & method) +- GraphQL API requests - Request body size histograms - Response body size histograms - Response code counters diff --git a/docs/integrations/rest-api.md b/docs/integrations/rest-api.md index 7a0d3e176..47fb65494 100644 --- a/docs/integrations/rest-api.md +++ b/docs/integrations/rest-api.md @@ -608,6 +608,28 @@ http://netbox/api/dcim/sites/ \ !!! note The bulk deletion of objects is an all-or-none operation, meaning that if NetBox fails to delete any of the specified objects (e.g. due a dependency by a related object), the entire operation will be aborted and none of the objects will be deleted. +## Changelog Messages + +!!! info "This feature was introduced in NetBox v4.4." + +Most objects in NetBox support [change logging](../features/change-logging.md), which generates a detailed record each time an object is created, modified, or deleted. Beginning in NetBox v4.4, users can attach a message to the change record as well. This is accomplished via the REST API by including a `changelog_message` field in the object representation. + +For example, the following API request will create a new site and record a message in the resulting changelog entry: + +```no-highlight +curl -s -X POST \ +-H "Authorization: Token $TOKEN" \ +-H "Content-Type: application/json" \ +http://netbox/api/dcim/sites/ \ +--data '{ + "name": "Site A", + "slug": "site-a", + "changelog_message": "Adding a site for ticket #4137" +}' +``` + +This approach works when creating, modifying, or deleting objects, either individually or in bulk. + ## Uploading Files As JSON does not support the inclusion of binary data, files cannot be uploaded using JSON-formatted API requests. Instead, we can use form data encoding to attach a local file. diff --git a/docs/models/circuits/circuit.md b/docs/models/circuits/circuit.md index c75e20322..6e999f28d 100644 --- a/docs/models/circuits/circuit.md +++ b/docs/models/circuits/circuit.md @@ -38,8 +38,6 @@ The operational status of the circuit. By default, the following statuses are av ### Distance -!!! info "This field was introduced in NetBox v4.2." - The distance between the circuit's two endpoints, including a unit designation (e.g. 100 meters or 25 feet). ### Description diff --git a/docs/models/circuits/virtualcircuit.md b/docs/models/circuits/virtualcircuit.md index 17328b87a..51dfd1c20 100644 --- a/docs/models/circuits/virtualcircuit.md +++ b/docs/models/circuits/virtualcircuit.md @@ -1,7 +1,5 @@ # Virtual Circuits -!!! info "This feature was introduced in NetBox v4.2." - A virtual circuit can connect two or more interfaces atop a set of decoupled physical connections. For example, it's very common to form a virtual connection between two virtual interfaces, each of which is bound to a physical interface on its respective device and physically connected to a [provider network](./providernetwork.md) via an independent [physical circuit](./circuit.md). ## Fields diff --git a/docs/models/circuits/virtualcircuittermination.md b/docs/models/circuits/virtualcircuittermination.md index a7833e13c..82ea43eef 100644 --- a/docs/models/circuits/virtualcircuittermination.md +++ b/docs/models/circuits/virtualcircuittermination.md @@ -1,7 +1,5 @@ # Virtual Circuit Terminations -!!! info "This feature was introduced in NetBox v4.2." - This model represents the connection of a virtual [interface](../dcim/interface.md) to a [virtual circuit](./virtualcircuit.md). ## Fields diff --git a/docs/models/core/datasource.md b/docs/models/core/datasource.md index 527d93939..64a087cb6 100644 --- a/docs/models/core/datasource.md +++ b/docs/models/core/datasource.md @@ -46,8 +46,6 @@ A set of rules (one per line) identifying filenames to ignore during synchroniza ### Sync Interval -!!! info "This field was introduced in NetBox v4.3." - The interval at which the data source should automatically synchronize. If not set, the data source must be synchronized manually. ### Last Synced diff --git a/docs/models/dcim/devicerole.md b/docs/models/dcim/devicerole.md index abff149d6..e58373565 100644 --- a/docs/models/dcim/devicerole.md +++ b/docs/models/dcim/devicerole.md @@ -6,8 +6,6 @@ Devices can be organized by functional roles, which are fully customizable by th ### Parent -!!! info "This field was introduced in NetBox v4.3." - The parent role of which this role is a child (optional). ### Name diff --git a/docs/models/dcim/interface.md b/docs/models/dcim/interface.md index b7115050f..7f67e4e7a 100644 --- a/docs/models/dcim/interface.md +++ b/docs/models/dcim/interface.md @@ -126,8 +126,6 @@ The tagged VLANs which are configured to be carried by this interface. Valid onl ### Q-in-Q SVLAN -!!! info "This field was introduced in NetBox v4.2." - The assigned service VLAN (for Q-in-Q/802.1ad interfaces). ### Wireless Role @@ -155,6 +153,4 @@ The [wireless LANs](../wireless/wirelesslan.md) for which this interface carries ### VLAN Translation Policy -!!! info "This field was introduced in NetBox v4.2." - The [VLAN translation policy](../ipam/vlantranslationpolicy.md) that applies to this interface (optional). diff --git a/docs/models/dcim/inventoryitem.md b/docs/models/dcim/inventoryitem.md index 6aed0fc86..22be2c5aa 100644 --- a/docs/models/dcim/inventoryitem.md +++ b/docs/models/dcim/inventoryitem.md @@ -30,8 +30,6 @@ An alternative physical label identifying the inventory item. ### Status -!!! info "This field was introduced in NetBox v4.2." - The inventory item's operational status. ### Role diff --git a/docs/models/dcim/macaddress.md b/docs/models/dcim/macaddress.md index 5b1dd93be..fe3d1f0e3 100644 --- a/docs/models/dcim/macaddress.md +++ b/docs/models/dcim/macaddress.md @@ -1,7 +1,5 @@ # MAC Addresses -!!! info "This feature was introduced in NetBox v4.2." - A MAC address object in NetBox comprises a single Ethernet link layer address, and represents a MAC address as reported by or assigned to a network interface. MAC addresses can be assigned to [device](../dcim/device.md) and [virtual machine](../virtualization/virtualmachine.md) interfaces. A MAC address can be specified as the primary MAC address for a given device or VM interface. Most interfaces have only a single MAC address, hard-coded at the factory. However, on some devices (particularly virtual interfaces) it is possible to assign additional MAC addresses or change existing ones. For this reason NetBox allows multiple MACAddress objects to be assigned to a single interface. diff --git a/docs/models/dcim/moduletypeprofile.md b/docs/models/dcim/moduletypeprofile.md index 80345c82b..8e9879398 100644 --- a/docs/models/dcim/moduletypeprofile.md +++ b/docs/models/dcim/moduletypeprofile.md @@ -1,7 +1,5 @@ # Module Type Profiles -!!! info "This model was introduced in NetBox v4.3." - Each [module type](./moduletype.md) may optionally be assigned a profile according to its classification. A profile can extend module types with user-configured attributes. For example, you might want to specify the input current and voltage of a power supply, or the clock speed and number of cores for a processor. Module type attributes are managed via the configuration of a [JSON schema](https://json-schema.org/) on the profile. For example, the following schema introduces three module type attributes, two of which are designated as required attributes. diff --git a/docs/models/dcim/platform.md b/docs/models/dcim/platform.md index 0914d0aa6..3400294e6 100644 --- a/docs/models/dcim/platform.md +++ b/docs/models/dcim/platform.md @@ -2,19 +2,27 @@ A platform defines the type of software running on a [device](./device.md) or [virtual machine](../virtualization/virtualmachine.md). This can be helpful to model when it is necessary to distinguish between different versions or feature sets. Note that two devices of the same type may be assigned different platforms: For example, one Juniper MX240 might run Junos 14 while another runs Junos 15. +Platforms may be nested under parents to form a hierarchy. For example, platforms named "Debian" and "RHEL" might both be created under a generic "Linux" parent. + Platforms may optionally be limited by [manufacturer](./manufacturer.md): If a platform is assigned to a particular manufacturer, it can only be assigned to devices with a type belonging to that manufacturer. -The assignment of platforms to devices is an optional feature, and may be disregarded if not desired. +The assignment of platforms to devices and virtual machines is optional. ## Fields +## Parent + +!!! "This field was introduced in NetBox v4.4." + +The parent platform class to which this platform belongs (optional). + ### Name -A unique human-friendly name. +A human-friendly name for the platform. Must be unique per manufacturer. ### Slug -A unique URL-friendly identifier. (This value can be used for filtering.) +A URL-friendly identifier; must be unique per manufacturer. (This value can be used for filtering.) ### Manufacturer diff --git a/docs/models/dcim/poweroutlet.md b/docs/models/dcim/poweroutlet.md index 22a7ec63e..2aa95737a 100644 --- a/docs/models/dcim/poweroutlet.md +++ b/docs/models/dcim/poweroutlet.md @@ -40,12 +40,8 @@ The operational status of the power outlet. By default, the following statuses a !!! tip "Custom power outlet statuses" Additional power outlet statuses may be defined by setting `PowerOutlet.status` under the [`FIELD_CHOICES`](../../configuration/data-validation.md#field_choices) configuration parameter. -!!! info "This field was introduced in NetBox v4.3." - ### Color -!!! info "This field was introduced in NetBox v4.2." - The power outlet's color (optional). ### Power Port diff --git a/docs/models/dcim/rackreservation.md b/docs/models/dcim/rackreservation.md index 32d52c9d7..8eaa11af8 100644 --- a/docs/models/dcim/rackreservation.md +++ b/docs/models/dcim/rackreservation.md @@ -12,6 +12,13 @@ The [rack](./rack.md) being reserved. The rack unit or units being reserved. Multiple units can be expressed using commas and/or hyphens. For example, `1,3,5-7` specifies units 1, 3, 5, 6, and 7. +### Status + +The current status of the reservation. (This is for documentation only: The status of a reservation has no impact on the installation of devices within a reserved rack unit.) + +!!! tip + Additional statuses may be defined by setting `RackReservation.status` under the [`FIELD_CHOICES`](../../configuration/data-validation.md#field_choices) configuration parameter. + ### User The NetBox user account associated with the reservation. Note that users with sufficient permission can make rack reservations for other users. diff --git a/docs/models/dcim/racktype.md b/docs/models/dcim/racktype.md index 5298e8b26..ecaf539c9 100644 --- a/docs/models/dcim/racktype.md +++ b/docs/models/dcim/racktype.md @@ -42,8 +42,6 @@ The number of the numerically lowest unit in the rack. This value defaults to on The external width, height and depth of the rack can be tracked to aid in floorplan calculations. These measurements must be designated in either millimeters or inches. -!!! info "The `outer_height` field was introduced in NetBox v4.3." - ### Mounting Depth The maximum depth of a mounted device that the rack can accommodate, in millimeters. For four-post frames or cabinets, this is the horizontal distance between the front and rear vertical rails. (Note that this measurement does _not_ include space between the rails and the cabinet doors.) diff --git a/docs/models/extras/configcontext.md b/docs/models/extras/configcontext.md index 1e58b9e01..7c5c597a9 100644 --- a/docs/models/extras/configcontext.md +++ b/docs/models/extras/configcontext.md @@ -14,6 +14,10 @@ A unique human-friendly name. A numeric value which influences the order in which context data is merged. Contexts with a lower weight are merged before those with a higher weight. +### Profile + +The [profile](./configcontextprofile.md) to which the config context is assigned (optional). Profiles can be used to enforce structure in their data. + ### Data The context data expressed in JSON format. diff --git a/docs/models/extras/configcontextprofile.md b/docs/models/extras/configcontextprofile.md new file mode 100644 index 000000000..289cce20a --- /dev/null +++ b/docs/models/extras/configcontextprofile.md @@ -0,0 +1,33 @@ +# Config Context Profiles + +Profiles can be used to organize [configuration contexts](./configcontext.md) and to enforce a desired structure for their data. The later is achieved by defining a [JSON schema](https://json-schema.org/) to which all config context with this profile assigned must comply. + +For example, the following schema defines two keys, `size` and `priority`, of which the former is required: + +```json +{ + "properties": { + "size": { + "type": "integer" + }, + "priority": { + "type": "string", + "enum": ["high", "medium", "low"], + "default": "medium" + } + }, + "required": [ + "size" + ] +} +``` + +## Fields + +### Name + +A unique human-friendly name. + +### Schema + +The JSON schema to be enforced for all assigned config contexts (optional). diff --git a/docs/models/extras/configtemplate.md b/docs/models/extras/configtemplate.md index ac059bdc5..7fe3e865f 100644 --- a/docs/models/extras/configtemplate.md +++ b/docs/models/extras/configtemplate.md @@ -34,24 +34,15 @@ The `undefined` and `finalize` Jinja environment parameters, which must referenc ### MIME Type -!!! info "This field was introduced in NetBox v4.3." - The MIME type to indicate in the response when rendering the configuration template (optional). Defaults to `text/plain`. ### File Name -!!! info "This field was introduced in NetBox v4.3." - The file name to give to the rendered export file (optional). ### File Extension -!!! info "This field was introduced in NetBox v4.3." - The file extension to append to the file name in the response (optional). ### As Attachment - -!!! info "This field was introduced in NetBox v4.3." - If selected, the rendered content will be returned as a file attachment, rather than displayed directly in-browser (where supported). \ No newline at end of file diff --git a/docs/models/extras/exporttemplate.md b/docs/models/extras/exporttemplate.md index 32ee5eabc..3d80ffd69 100644 --- a/docs/models/extras/exporttemplate.md +++ b/docs/models/extras/exporttemplate.md @@ -22,8 +22,6 @@ Jinja2 template code for rendering the exported data. ### Environment Parameters -!!! info "This field was introduced in NetBox v4.3." - A dictionary of any additional parameters to pass when instantiating the [Jinja2 environment](https://jinja.palletsprojects.com/en/3.1.x/api/#jinja2.Environment). Jinja2 supports various optional parameters which can be used to modify its default behavior. The `undefined` and `finalize` Jinja environment parameters, which must reference a Python class or function, can define a dotted path to the desired resource. For example: diff --git a/docs/models/extras/tag.md b/docs/models/extras/tag.md index c4bc91b5a..b8a50b2eb 100644 --- a/docs/models/extras/tag.md +++ b/docs/models/extras/tag.md @@ -20,8 +20,6 @@ The color to use when displaying the tag in the NetBox UI. A numeric weight employed to influence the ordering of tags. Tags with a lower weight will be listed before those with higher weights. Values must be within the range **0** to **32767**. -!!! info "This field was introduced in NetBox v4.3." - ### Object Types The assignment of a tag may be limited to a prescribed set of objects. For example, it may be desirable to limit the application of a specific tag to only devices and virtual machines. diff --git a/docs/models/ipam/service.md b/docs/models/ipam/service.md index 0d5f12a17..fc6ab73d2 100644 --- a/docs/models/ipam/service.md +++ b/docs/models/ipam/service.md @@ -1,14 +1,18 @@ -# Services +# Application Services -A service represents a layer seven application available on a device or virtual machine. For example, a service might be created in NetBox to represent an HTTP server running on TCP/8000. Each service may optionally be further bound to one or more specific interfaces assigned to the selected device or virtual machine. +An application service represents a layer seven application available on a device or virtual machine. For example, a service might be created in NetBox to represent an HTTP server running on TCP/8000. Each service may optionally be further bound to one or more specific interfaces assigned to the selected device or virtual machine. -To aid in the efficient creation of services, users may opt to first create a [service template](./servicetemplate.md) from which service definitions can be quickly replicated. +To aid in the efficient creation of application services, users may opt to first create an [application service template](./servicetemplate.md) from which service definitions can be quickly replicated. + +!!! note "Changed in NetBox v4.4" + + Previously, application services were referred to simply as "services". The name has been changed in the UI to better reflect their intended use. There is no change to the name of the model or in any programmatic NetBox APIs. ## Fields ### Parent -The parent object to which the service is assigned. This must be one of [Device](../dcim/device.md), +The parent object to which the application service is assigned. This must be one of [Device](../dcim/device.md), [VirtualMachine](../virtualization/virtualmachine.md), or [FHRP Group](./fhrpgroup.md). !!! note "Changed in NetBox v4.3" diff --git a/docs/models/ipam/servicetemplate.md b/docs/models/ipam/servicetemplate.md index 28c66b648..9dd69b3c4 100644 --- a/docs/models/ipam/servicetemplate.md +++ b/docs/models/ipam/servicetemplate.md @@ -1,6 +1,10 @@ -# Service Templates +# Application Service Templates -Service templates can be used to instantiate [services](./service.md) on [devices](../dcim/device.md) and [virtual machines](../virtualization/virtualmachine.md). +Application service templates can be used to instantiate [application services](./service.md) on [devices](../dcim/device.md) and [virtual machines](../virtualization/virtualmachine.md). + +!!! note "Changed in NetBox v4.4" + + Previously, application service templates were referred to simply as "service templates". The name has been changed in the UI to better reflect their intended use. There is no change to the name of the model or in any programmatic NetBox APIs. ## Fields diff --git a/docs/models/ipam/vlan.md b/docs/models/ipam/vlan.md index 3c90d8cc9..58fc9f551 100644 --- a/docs/models/ipam/vlan.md +++ b/docs/models/ipam/vlan.md @@ -25,16 +25,15 @@ The user-defined functional [role](./role.md) assigned to the VLAN. ### VLAN Group or Site +!!! warning "Site assignment is deprecated" + The assignment of individual VLANs directly to a site has been deprecated. This ability will be removed in a future NetBox release. Users are strongly encouraged to utilize VLAN groups, which have the added benefit of supporting the assignment of a VLAN to multiple sites. + The [VLAN group](./vlangroup.md) or [site](../dcim/site.md) to which the VLAN is assigned. ### Q-in-Q Role -!!! info "This field was introduced in NetBox v4.2." - For VLANs which comprise a Q-in-Q/IEEE 802.1ad topology, this field indicates whether the VLAN is treated as a service or customer VLAN. ### Q-in-Q Service VLAN -!!! info "This field was introduced in NetBox v4.2." - The designated parent service VLAN for a Q-in-Q customer VLAN. This may be set only for Q-in-Q custom VLANs. diff --git a/docs/models/ipam/vlantranslationpolicy.md b/docs/models/ipam/vlantranslationpolicy.md index 9e3e8de98..59541931e 100644 --- a/docs/models/ipam/vlantranslationpolicy.md +++ b/docs/models/ipam/vlantranslationpolicy.md @@ -1,7 +1,5 @@ # VLAN Translation Policies -!!! info "This feature was introduced in NetBox v4.2." - VLAN translation is a feature that consists of VLAN translation policies and [VLAN translation rules](./vlantranslationrule.md). Many rules can belong to a policy, and each rule defines a mapping of a local to remote VLAN ID (VID). A policy can then be assigned to an [Interface](../dcim/interface.md) or [VMInterface](../virtualization/vminterface.md), and all VLAN translation rules associated with that policy will be visible in the interface details. There are uniqueness constraints on `(policy, local_vid)` and on `(policy, remote_vid)` in the `VLANTranslationRule` model. Thus, you cannot have multiple rules linked to the same policy that have the same local VID or the same remote VID. A set of policies and rules might look like this: diff --git a/docs/models/ipam/vlantranslationrule.md b/docs/models/ipam/vlantranslationrule.md index eb356d0d0..bffc030ed 100644 --- a/docs/models/ipam/vlantranslationrule.md +++ b/docs/models/ipam/vlantranslationrule.md @@ -1,7 +1,5 @@ # VLAN Translation Rules -!!! info "This feature was introduced in NetBox v4.2." - A VLAN translation rule represents a one-to-one mapping of a local VLAN ID (VID) to a remote VID. Many rules can belong to a single policy. See [VLAN translation policies](./vlantranslationpolicy.md) for an overview of the VLAN Translation feature. diff --git a/docs/models/virtualization/vminterface.md b/docs/models/virtualization/vminterface.md index ba0c68b15..726060c05 100644 --- a/docs/models/virtualization/vminterface.md +++ b/docs/models/virtualization/vminterface.md @@ -1,6 +1,6 @@ ## Interfaces -[Virtual machine](./virtualmachine.md) interfaces behave similarly to device [interfaces](../dcim/interface.md): They can be assigned to VRFs, may have IP addresses, VLANs, and services attached to them, and so on. However, given their virtual nature, they lack properties pertaining to physical attributes. For example, VM interfaces do not have a physical type and cannot have cables attached to them. +[Virtual machine](./virtualmachine.md) interfaces behave similarly to device [interfaces](../dcim/interface.md): They can be assigned to VRFs, may have IP addresses, VLANs, and so on. However, given their virtual nature, they lack properties pertaining to physical attributes. For example, VM interfaces do not have a physical type and cannot have cables attached to them. ## Fields @@ -59,8 +59,6 @@ The tagged VLANs which are configured to be carried by this interface. Valid onl ### Q-in-Q SVLAN -!!! info "This field was introduced in NetBox v4.2." - The assigned service VLAN (for Q-in-Q/802.1ad interfaces). ### VRF @@ -69,6 +67,4 @@ The [virtual routing and forwarding](../ipam/vrf.md) instance to which this inte ### VLAN Translation Policy -!!! info "This field was introduced in NetBox v4.2." - The [VLAN translation policy](../ipam/vlantranslationpolicy.md) that applies to this interface (optional). diff --git a/docs/models/vpn/l2vpn.md b/docs/models/vpn/l2vpn.md index 983095ef8..0bf17fa1b 100644 --- a/docs/models/vpn/l2vpn.md +++ b/docs/models/vpn/l2vpn.md @@ -44,8 +44,6 @@ The operational status of the L2VPN. By default, the following statuses are avai !!! tip "Custom L2VPN statuses" Additional L2VPN statuses may be defined by setting `L2VPN.status` under the [`FIELD_CHOICES`](../../configuration/data-validation.md#field_choices) configuration parameter. -!!! info "This field was introduced in NetBox v4.3." - ### Identifier An optional numeric identifier. This can be used to track a pseudowire ID, for example. diff --git a/docs/models/wireless/wirelesslan.md b/docs/models/wireless/wirelesslan.md index 2ce673086..a448c42a2 100644 --- a/docs/models/wireless/wirelesslan.md +++ b/docs/models/wireless/wirelesslan.md @@ -46,6 +46,4 @@ The security key configured on each client to grant access to the secured wirele ### Scope -!!! info "This field was introduced in NetBox v4.2." - The [region](../dcim/region.md), [site](../dcim/site.md), [site group](../dcim/sitegroup.md) or [location](../dcim/location.md) with which this wireless LAN is associated. diff --git a/docs/plugins/development/background-jobs.md b/docs/plugins/development/background-jobs.md index 6fc8c4e75..98229b78d 100644 --- a/docs/plugins/development/background-jobs.md +++ b/docs/plugins/development/background-jobs.md @@ -39,6 +39,27 @@ You can schedule the background job from within your code (e.g. from a model's ` This is the human-friendly names of your background job. If omitted, the class name will be used. +### Logging + +!!! info "This feature was introduced in NetBox v4.4." + +A Python logger is instantiated by the runner for each job. It can be utilized within a job's `run()` method as needed: + +```python +def run(self, *args, **kwargs): + obj = MyModel.objects.get(pk=kwargs.get('pk')) + self.logger.info("Retrieved object {obj}") +``` + +Four of the standard Python logging levels are supported: + +* `debug()` +* `info()` +* `warning()` +* `error()` + +Log entries recorded using the runner's logger will be saved in the job's log in the database in addition to being processed by other [system logging handlers](../../configuration/system.md#logging). + ### Scheduled Jobs As described above, jobs can be scheduled for immediate execution or at any later time using the `enqueue()` method. However, for management purposes, the `enqueue_once()` method allows a job to be scheduled exactly once avoiding duplicates. If a job is already scheduled for a particular instance, a second one won't be scheduled, respecting thread safety. An example use case would be to schedule a periodic task that is bound to an instance in general, but not to any event of that instance (such as updates). The parameters of the `enqueue_once()` method are identical to those of `enqueue()`. @@ -68,8 +89,6 @@ class MyModel(NetBoxModel): ### System Jobs -!!! info "This feature was introduced in NetBox v4.2." - Some plugins may implement background jobs that are decoupled from the request/response cycle. Typical use cases would be housekeeping tasks or synchronization jobs. These can be registered as _system jobs_ using the `system_job()` decorator. The job interval must be passed as an integer (in minutes) when registering a system job. System jobs are scheduled automatically when the RQ worker (`manage.py rqworker`) is run. #### Example diff --git a/docs/plugins/development/models.md b/docs/plugins/development/models.md index 508c4ce89..eb12204ff 100644 --- a/docs/plugins/development/models.md +++ b/docs/plugins/development/models.md @@ -24,20 +24,7 @@ Every model includes by default a numeric primary key. This value is generated a ## Enabling NetBox Features -Plugin models can leverage certain NetBox features by inheriting from NetBox's `NetBoxModel` class. This class extends the plugin model to enable features unique to NetBox, including: - -* Bookmarks -* Change logging -* Cloning -* Custom fields -* Custom links -* Custom validation -* Export templates -* Journaling -* Tags -* Webhooks - -This class performs two crucial functions: +Plugin models can leverage certain [model features](../../development/models.md#features-matrix) (such as tags, custom fields, event rules, etc.) by inheriting from NetBox's `NetBoxModel` class. This class performs two crucial functions: 1. Apply any fields, methods, and/or attributes necessary to the operation of these features 2. Register the model with NetBox as utilizing these features @@ -119,8 +106,6 @@ For more information about database migrations, see the [Django documentation](h ::: netbox.models.features.ContactsMixin -!!! info "Plugin support for ContactsMixin was introduced in NetBox v4.3." - ::: netbox.models.features.CustomLinksMixin ::: netbox.models.features.CustomFieldsMixin @@ -137,6 +122,27 @@ For more information about database migrations, see the [Django documentation](h ::: netbox.models.features.TagsMixin +## Custom Model Features + +In addition to utilizing the model features provided natively by NetBox (listed above), plugins can register their own model features. This is done using the `register_model_feature()` function from `netbox.utils`. This function takes two arguments: a feature name, and a callable which accepts a model class. The callable must return a boolean value indicting whether the given model supports the named feature. + +This function can be used as a decorator: + +```python +@register_model_feature('foo') +def supports_foo(model): + # Your logic here +``` + +Or it can be called directly: + +```python +register_model_feature('foo', supports_foo) +``` + +!!! tip + Consider performing feature registration inside your PluginConfig's `ready()` method. + ## Choice Sets For model fields which support the selection of one or more values from a predefined list of choices, NetBox provides the `ChoiceSet` utility class. This can be used in place of a regular choices tuple to provide enhanced functionality, namely dynamic configuration and colorization. (See [Django's documentation](https://docs.djangoproject.com/en/stable/ref/models/fields/#choices) on the `choices` parameter for supported model fields.) diff --git a/docs/plugins/development/tables.md b/docs/plugins/development/tables.md index 6fe8357b3..c51158849 100644 --- a/docs/plugins/development/tables.md +++ b/docs/plugins/development/tables.md @@ -51,6 +51,10 @@ This will automatically apply any user-specific preferences for the table. (If u The table column classes listed below are supported for use in plugins. These classes can be imported from `netbox.tables.columns`. +::: netbox.tables.ArrayColumn + options: + members: false + ::: netbox.tables.BooleanColumn options: members: false diff --git a/docs/plugins/development/user-interface.md b/docs/plugins/development/user-interface.md new file mode 100644 index 000000000..a918eb185 --- /dev/null +++ b/docs/plugins/development/user-interface.md @@ -0,0 +1,14 @@ +# User Interface + +## Light & Dark Mode + +The NetBox user interface supports toggling between light and dark versions of the theme. If needed, a plugin can determine the currently active color theme by inspecting `window.localStorage['netbox-color-mode']`, which will indicate either `light` or `dark`. + +Additionally, when the color scheme is toggled by the user, a custom event `netbox.colorModeChanged` indicating the new scheme is dispatched. A plugin can listen for this event if needed to react to the change: + +```typescript +window.addEventListener('netbox.colorModeChanged', e => { + const customEvent = e as CustomEvent; + console.log('New color mode:', customEvent.detail.netboxColorMode); +}); +``` diff --git a/docs/plugins/development/views.md b/docs/plugins/development/views.md index d5d376db8..01c7737ba 100644 --- a/docs/plugins/development/views.md +++ b/docs/plugins/development/views.md @@ -64,6 +64,7 @@ Generic view classes (documented below) facilitate common operations, such as cr | `ObjectListView` | View a list of objects | | `BulkImportView` | Import a set of new objects | | `BulkEditView` | Edit multiple objects | +| `BulkRenameView` | Rename multiple objects | | `BulkDeleteView` | Delete multiple objects | !!! warning @@ -171,6 +172,10 @@ Below are the class definitions for NetBox's multi-object views. These views han options: members: false +::: netbox.views.generic.BulkRenameView + options: + members: false + ::: netbox.views.generic.BulkDeleteView options: members: diff --git a/docs/plugins/development/webhooks.md b/docs/plugins/development/webhooks.md new file mode 100644 index 000000000..755d19d42 --- /dev/null +++ b/docs/plugins/development/webhooks.md @@ -0,0 +1,75 @@ +# Webhooks + +NetBox supports the configuration of outbound [webhooks](../../integrations/webhooks.md) which can be triggered by custom [event rules](../../features/event-rules.md). By default, a webhook's payload will contain a serialized representation of the object, before & after snapshots (if applicable), and some metadata. + +## Callback Registration + +Plugins can register callback functions to supplement a webhook's payload with their own data. For example, it might be desirable for a plugin to attach information about the status of some objects at the time a change was made. + +This can be accomplished by defining a function which accepts a defined set of keyword arguments and registering it as a webhook callback. Whenever a new webhook is generated, the function will be called, and any data it returns will be attached to the webhook's payload under the `context` key. + +### Example + +```python +from extras.webhooks import register_webhook_callback +from my_plugin.utilities import get_foo_status + +@register_webhook_callback +def set_foo_status(object_type, event_type, data, request): + if status := get_foo_status(): + return { + 'foo': status + } +``` + +The resulting webhook payload will look like the following: + +```json +{ + "event": "updated", + "timestamp": "2025-08-07T14:24:30.627321+00:00", + "object_type": "dcim.site", + "username": "admin", + "request_id": "49e3e39e-7333-4b9c-a9af-19f0dc1e7dc9", + "data": { + "id": 2, + "url": "/api/dcim/sites/2/", + ... + }, + "snapshots": {...}, + "context": { + "foo": 123 + } +} +``` + +!!! note "Consider namespacing webhook data" + The data returned from all webhook callbacks will be compiled into a single `context` dictionary. Any existing keys within this dictionary will be overwritten by subsequent callbacks which include those keys. To avoid collisions with webhook data provided by other plugins, consider namespacing your plugin's data within a nested dictionary as such: + + ```python + return { + 'my_plugin': { + 'foo': 123, + 'bar': 456, + } + } + ``` + +### Callback Function Arguments + +| Name | Type | Description | +|---------------|-------------------|-------------------------------------------------------------------| +| `object_type` | ObjectType | The ObjectType which represents the triggering object | +| `event_type` | String | The type of event which triggered the webhook (see `core.events`) | +| `data` | Dictionary | The serialized representation of the object | +| `request` | NetBoxFakeRequest | A copy of the request (if any) which resulted in the change | + +## Where to Define Callbacks + +Webhook callbacks can be defined anywhere within a plugin, but must be imported during plugin initialization. If you wish to keep them in a separate module, you can import that module under the PluginConfig's `ready()` method: + +```python +def ready(self): + super().ready() + from my_plugin import webhook_callbacks +``` diff --git a/docs/release-notes/index.md b/docs/release-notes/index.md index 0d0b10092..b86ac3d02 100644 --- a/docs/release-notes/index.md +++ b/docs/release-notes/index.md @@ -10,6 +10,13 @@ Minor releases are published in April, August, and December of each calendar yea This page contains a history of all major and minor releases since NetBox v2.0. For more detail on a specific patch release, please see the release notes page for that specific minor release. +#### [Version 4.4](./version-4.4.md) (September 2025) + +* Background Jobs for Bulk Operations ([#19589](https://github.com/netbox-community/netbox/issues/19589), [#19891](https://github.com/netbox-community/netbox/issues/19891)) +* Logging Mechanism for Background Jobs ([#19816](https://github.com/netbox-community/netbox/issues/19816)) +* Changelog Comments ([#19713](https://github.com/netbox-community/netbox/issues/19713)) +* Config Context Data Validation ([#19377](https://github.com/netbox-community/netbox/issues/19377)) + #### [Version 4.3](./version-4.3.md) (May 2025) * Module Type Profiles & Custom Attributes ([#19002](https://github.com/netbox-community/netbox/issues/19002)) diff --git a/docs/release-notes/version-3.0.md b/docs/release-notes/version-3.0.md index 06b889c22..a7003eedf 100644 --- a/docs/release-notes/version-3.0.md +++ b/docs/release-notes/version-3.0.md @@ -434,7 +434,7 @@ A new management command has been added: `manage.py housekeeping`. This command * Delete change log records which have surpassed the configured retention period (if configured) * Check for new NetBox releases (if enabled) -A convenience script for calling this command via an automated scheduler has been included at `/contrib/netbox-housekeeping.sh`. Please see the [housekeeping documentation](../administration/housekeeping.md) for further details. +A convenience script for calling this command via an automated scheduler has been included at `/contrib/netbox-housekeeping.sh`. Please see the housekeeping documentation for further details. #### Custom Queue Support for Plugins ([#6651](https://github.com/netbox-community/netbox/issues/6651)) diff --git a/docs/release-notes/version-4.4.md b/docs/release-notes/version-4.4.md new file mode 100644 index 000000000..7138c277c --- /dev/null +++ b/docs/release-notes/version-4.4.md @@ -0,0 +1,87 @@ +# NetBox v4.4 + +## v4.4.0 (2025-09-02) + +### New Features + +#### Background Jobs for Bulk Operations ([#19589](https://github.com/netbox-community/netbox/issues/19589), [#19891](https://github.com/netbox-community/netbox/issues/19891)) + +Most bulk operations, such as the import, modification, or deletion of objects can now be executed as a background job. This frees the user to continue working in NetBox while the bulk operation is processed. Once completed, the user will be notified of the job's result. + +#### Logging Mechanism for Background Jobs ([#19816](https://github.com/netbox-community/netbox/issues/19816)) + +A dedicated logging mechanism has been implemented for background jobs. Jobs can now easily record log messages by calling e.g. `self.logger.info("Log message")` under the `run()` method. These messages are displayed along with the job's resulting data. Supported log levels include `DEBUG`, `INFO`, `WARNING`, and `ERROR`. + +#### Changelog Comments ([#19713](https://github.com/netbox-community/netbox/issues/19713)) + +When creating, editing, or deleting objects in NetBox, users now have the option of providing a short message explaining the change. This message will be recorded on the resulting changelog records for all affected objects. + +#### Config Context Data Validation ([#19377](https://github.com/netbox-community/netbox/issues/19377)) + +A new ConfigContextProfile model has been introduced to support JSON schema validation for config context data. If a validation schema has been defined for a profile, all config contexts assigned to it will have their data validated against the schema whenever a change is made. (The assignment of a config context to a profile is optional.) + +### Enhancements + +* [#17413](https://github.com/netbox-community/netbox/issues/17413) - Platforms belonging to different manufacturers may now have identical names +* [#18204](https://github.com/netbox-community/netbox/issues/18204) - Improved layout of the image attachments view & tables +* [#18528](https://github.com/netbox-community/netbox/issues/18528) - Introduced the `HOSTNAME` configuration parameter to override the system hostname reported by NetBox +* [#18984](https://github.com/netbox-community/netbox/issues/18984) - Added a `status` field for rack reservations +* [#18990](https://github.com/netbox-community/netbox/issues/18990) - Image attachments now include an optional description field +* [#19134](https://github.com/netbox-community/netbox/issues/19134) - Interface transmit power now accepts negative values +* [#19231](https://github.com/netbox-community/netbox/issues/19231) - Bulk renaming support has been implemented in the UI for most object types +* [#19591](https://github.com/netbox-community/netbox/issues/19591) - Thumbnails for all images attached to an object are now displayed under a dedicated tab +* [#19722](https://github.com/netbox-community/netbox/issues/19722) - The REST API endpoint for object types has been extended to include additional details +* [#19739](https://github.com/netbox-community/netbox/issues/19739) - Introduced a user preference for CSV delimiter +* [#19740](https://github.com/netbox-community/netbox/issues/19740) - Enable nesting of platforms within a hierarchy for improved organization +* [#19773](https://github.com/netbox-community/netbox/issues/19773) - Extend the system UI view with additional information +* [#19893](https://github.com/netbox-community/netbox/issues/19893) - The `/api/status/` REST API endpoint now includes the system hostname +* [#19920](https://github.com/netbox-community/netbox/issues/19920) - Contacts can now be assigned to ASNs +* [#19945](https://github.com/netbox-community/netbox/issues/19945) - Introduce a new custom script variable to represent decimal values +* [#19965](https://github.com/netbox-community/netbox/issues/19965) - Add REST & GraphQL API request counters to the Prometheus metrics exporter +* [#20029](https://github.com/netbox-community/netbox/issues/20029) - Include complete representation of object type in webhook payload data + +### Plugins + +* [#18006](https://github.com/netbox-community/netbox/issues/18006) - A Javascript is now triggered when UI is toggled between light and dark mode +* [#19735](https://github.com/netbox-community/netbox/issues/19735) - Custom individual and bulk operations can now be registered under individual views using `ObjectAction` +* [#20003](https://github.com/netbox-community/netbox/issues/20003) - Enable registration of callbacks to provide supplementary webhook payload data +* [#20115](https://github.com/netbox-community/netbox/issues/20115) - Support the use of ArrayColumn for plugin tables +* [#20129](https://github.com/netbox-community/netbox/issues/20129) - Enable plugins to register custom model features + +### Deprecations + +* [#19738](https://github.com/netbox-community/netbox/issues/19738) - The direct assignment of VLANs to sites is now discouraged in favor of VLAN groups + +### Other Changes + +* [#18349](https://github.com/netbox-community/netbox/issues/18349) - The housekeeping script has been replaced with a system job +* [#18588](https://github.com/netbox-community/netbox/issues/18588) - The "Service" model has been renamed to "Application Service" for clarity (UI change only) +* [#19829](https://github.com/netbox-community/netbox/issues/19829) - The REST API endpoint for object types is now available under `/api/core/` +* [#19924](https://github.com/netbox-community/netbox/issues/19924) - ObjectTypes are now tracked as concrete objects in the database (alongside ContentTypes) +* [#19973](https://github.com/netbox-community/netbox/issues/19973) - Miscellaneous improvements to the `nbshell` management command + +### REST API Changes + +* All object types which support change logging now support the inclusion of a `changelog_message` for write operations. If provided, this message will be attached to the changelog record resulting from the change (if successful). +* The `/api/status/` endpoint now includes the system hostname. +* The `/api/extras/object-types/` endpoint is now available at `/api/core/object-types/`. (The original endpoint will be removed in NetBox v4.5.) +* The `/api/core/object-types/` endpoint has been expanded to include the following read-only fields: + * `app_name` + * `model_name` + * `model_name_plural` + * `is_plugin_model` + * `rest_api_endpoint` + * `description` +* Introduced the `/api/extras/config-context-profiles/` endpoint +* core.Job + * Added the read-only `log_entries` array field +* dcim.Interface + * The `tx_power` field now accepts negative values +* dcim.RackReservation + * Added the `status` choice field +* dcim.Platform + * Add an optional `parent` foreign key field to support nesting +* extras.ConfigContext + * Added the optional `profile` foreign key field +* extras.ImageAttachment + * Added an optional `description` field diff --git a/mkdocs.yml b/mkdocs.yml index 27526bd26..d8524e593 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -30,6 +30,8 @@ plugins: python: paths: ["netbox"] options: + docstring_options: + warn_missing_types: false heading_level: 3 members_order: source show_root_heading: true @@ -144,6 +146,8 @@ nav: - Search: 'plugins/development/search.md' - Event Types: 'plugins/development/event-types.md' - Data Backends: 'plugins/development/data-backends.md' + - Webhooks: 'plugins/development/webhooks.md' + - User Interface: 'plugins/development/user-interface.md' - REST API: 'plugins/development/rest-api.md' - GraphQL API: 'plugins/development/graphql-api.md' - Background Jobs: 'plugins/development/background-jobs.md' @@ -158,7 +162,6 @@ nav: - Okta: 'administration/authentication/okta.md' - Permissions: 'administration/permissions.md' - Error Reporting: 'administration/error-reporting.md' - - Housekeeping: 'administration/housekeeping.md' - Replicating NetBox: 'administration/replicating-netbox.md' - NetBox Shell: 'administration/netbox-shell.md' - Data Model: @@ -225,6 +228,7 @@ nav: - Extras: - Bookmark: 'models/extras/bookmark.md' - ConfigContext: 'models/extras/configcontext.md' + - ConfigContextProfile: 'models/extras/configcontextprofile.md' - ConfigTemplate: 'models/extras/configtemplate.md' - CustomField: 'models/extras/customfield.md' - CustomFieldChoiceSet: 'models/extras/customfieldchoiceset.md' @@ -309,6 +313,7 @@ nav: - git Cheat Sheet: 'development/git-cheat-sheet.md' - Release Notes: - Summary: 'release-notes/index.md' + - Version 4.4: 'release-notes/version-4.4.md' - Version 4.3: 'release-notes/version-4.3.md' - Version 4.2: 'release-notes/version-4.2.md' - Version 4.1: 'release-notes/version-4.1.md' diff --git a/netbox/circuits/urls.py b/netbox/circuits/urls.py index 90e9e511f..594570638 100644 --- a/netbox/circuits/urls.py +++ b/netbox/circuits/urls.py @@ -35,11 +35,7 @@ urlpatterns = [ path('circuit-group-assignments//', include(get_model_urls('circuits', 'circuitgroupassignment'))), # Virtual circuits - path('virtual-circuits/', views.VirtualCircuitListView.as_view(), name='virtualcircuit_list'), - path('virtual-circuits/add/', views.VirtualCircuitEditView.as_view(), name='virtualcircuit_add'), - path('virtual-circuits/import/', views.VirtualCircuitBulkImportView.as_view(), name='virtualcircuit_bulk_import'), - path('virtual-circuits/edit/', views.VirtualCircuitBulkEditView.as_view(), name='virtualcircuit_bulk_edit'), - path('virtual-circuits/delete/', views.VirtualCircuitBulkDeleteView.as_view(), name='virtualcircuit_bulk_delete'), + path('virtual-circuits/', include(get_model_urls('circuits', 'virtualcircuit', detail=False))), path('virtual-circuits//', include(get_model_urls('circuits', 'virtualcircuit'))), path('virtual-circuit-types/', include(get_model_urls('circuits', 'virtualcircuittype', detail=False))), diff --git a/netbox/circuits/views.py b/netbox/circuits/views.py index 0b4439857..89ec03831 100644 --- a/netbox/circuits/views.py +++ b/netbox/circuits/views.py @@ -5,6 +5,7 @@ from django.utils.translation import gettext_lazy as _ from dcim.views import PathTraceView from ipam.models import ASN +from netbox.object_actions import AddObject, BulkDelete, BulkEdit, BulkExport, BulkImport from netbox.views import generic from utilities.forms import ConfirmationForm from utilities.query import count_related @@ -79,6 +80,11 @@ class ProviderBulkEditView(generic.BulkEditView): form = forms.ProviderBulkEditForm +@register_model_view(Provider, 'bulk_rename', path='rename', detail=False) +class ProviderBulkRenameView(generic.BulkRenameView): + queryset = Provider.objects.all() + + @register_model_view(Provider, 'bulk_delete', path='delete', detail=False) class ProviderBulkDeleteView(generic.BulkDeleteView): queryset = Provider.objects.annotate( @@ -141,6 +147,11 @@ class ProviderAccountBulkEditView(generic.BulkEditView): form = forms.ProviderAccountBulkEditForm +@register_model_view(ProviderAccount, 'bulk_rename', path='rename', detail=False) +class ProviderAccountBulkRenameView(generic.BulkRenameView): + queryset = ProviderAccount.objects.all() + + @register_model_view(ProviderAccount, 'bulk_delete', path='delete', detail=False) class ProviderAccountBulkDeleteView(generic.BulkDeleteView): queryset = ProviderAccount.objects.annotate( @@ -212,6 +223,11 @@ class ProviderNetworkBulkEditView(generic.BulkEditView): form = forms.ProviderNetworkBulkEditForm +@register_model_view(ProviderNetwork, 'bulk_rename', path='rename', detail=False) +class ProviderNetworkBulkRenameView(generic.BulkRenameView): + queryset = ProviderNetwork.objects.all() + + @register_model_view(ProviderNetwork, 'bulk_delete', path='delete', detail=False) class ProviderNetworkBulkDeleteView(generic.BulkDeleteView): queryset = ProviderNetwork.objects.all() @@ -271,6 +287,11 @@ class CircuitTypeBulkEditView(generic.BulkEditView): form = forms.CircuitTypeBulkEditForm +@register_model_view(CircuitType, 'bulk_rename', path='rename', detail=False) +class CircuitTypeBulkRenameView(generic.BulkRenameView): + queryset = CircuitType.objects.all() + + @register_model_view(CircuitType, 'bulk_delete', path='delete', detail=False) class CircuitTypeBulkDeleteView(generic.BulkDeleteView): queryset = CircuitType.objects.annotate( @@ -337,6 +358,12 @@ class CircuitBulkEditView(generic.BulkEditView): form = forms.CircuitBulkEditForm +@register_model_view(Circuit, 'bulk_rename', path='rename', detail=False) +class CircuitBulkRenameView(generic.BulkRenameView): + queryset = Circuit.objects.all() + field_name = 'cid' + + @register_model_view(Circuit, 'bulk_delete', path='delete', detail=False) class CircuitBulkDeleteView(generic.BulkDeleteView): queryset = Circuit.objects.prefetch_related( @@ -432,6 +459,7 @@ class CircuitTerminationListView(generic.ObjectListView): filterset = filtersets.CircuitTerminationFilterSet filterset_form = forms.CircuitTerminationFilterForm table = tables.CircuitTerminationTable + actions = (AddObject, BulkImport, BulkExport, BulkEdit, BulkDelete) @register_model_view(CircuitTermination) @@ -526,6 +554,11 @@ class CircuitGroupBulkEditView(generic.BulkEditView): form = forms.CircuitGroupBulkEditForm +@register_model_view(CircuitGroup, 'bulk_rename', path='rename', detail=False) +class CircuitGroupBulkRenameView(generic.BulkRenameView): + queryset = CircuitGroup.objects.all() + + @register_model_view(CircuitGroup, 'bulk_delete', path='delete', detail=False) class CircuitGroupBulkDeleteView(generic.BulkDeleteView): queryset = CircuitGroup.objects.all() @@ -543,6 +576,7 @@ class CircuitGroupAssignmentListView(generic.ObjectListView): filterset = filtersets.CircuitGroupAssignmentFilterSet filterset_form = forms.CircuitGroupAssignmentFilterForm table = tables.CircuitGroupAssignmentTable + actions = (AddObject, BulkImport, BulkExport, BulkEdit, BulkDelete) @register_model_view(CircuitGroupAssignment) @@ -635,6 +669,11 @@ class VirtualCircuitTypeBulkEditView(generic.BulkEditView): form = forms.VirtualCircuitTypeBulkEditForm +@register_model_view(VirtualCircuitType, 'bulk_rename', path='rename', detail=False) +class VirtualCircuitTypeBulkRenameView(generic.BulkRenameView): + queryset = VirtualCircuitType.objects.all() + + @register_model_view(VirtualCircuitType, 'bulk_delete', path='delete', detail=False) class VirtualCircuitTypeBulkDeleteView(generic.BulkDeleteView): queryset = VirtualCircuitType.objects.annotate( @@ -648,6 +687,7 @@ class VirtualCircuitTypeBulkDeleteView(generic.BulkDeleteView): # Virtual circuits # +@register_model_view(VirtualCircuit, 'list', path='', detail=False) class VirtualCircuitListView(generic.ObjectListView): queryset = VirtualCircuit.objects.annotate( termination_count=count_related(VirtualCircuitTermination, 'virtual_circuit') @@ -662,6 +702,7 @@ class VirtualCircuitView(generic.ObjectView): queryset = VirtualCircuit.objects.all() +@register_model_view(VirtualCircuit, 'add', detail=False) @register_model_view(VirtualCircuit, 'edit') class VirtualCircuitEditView(generic.ObjectEditView): queryset = VirtualCircuit.objects.all() @@ -673,6 +714,7 @@ class VirtualCircuitDeleteView(generic.ObjectDeleteView): queryset = VirtualCircuit.objects.all() +@register_model_view(VirtualCircuit, 'bulk_import', path='import', detail=False) class VirtualCircuitBulkImportView(generic.BulkImportView): queryset = VirtualCircuit.objects.all() model_form = forms.VirtualCircuitImportForm @@ -688,6 +730,7 @@ class VirtualCircuitBulkImportView(generic.BulkImportView): return data +@register_model_view(VirtualCircuit, 'bulk_edit', path='edit', detail=False) class VirtualCircuitBulkEditView(generic.BulkEditView): queryset = VirtualCircuit.objects.annotate( termination_count=count_related(VirtualCircuitTermination, 'virtual_circuit') @@ -697,6 +740,13 @@ class VirtualCircuitBulkEditView(generic.BulkEditView): form = forms.VirtualCircuitBulkEditForm +@register_model_view(VirtualCircuit, 'bulk_rename', path='rename', detail=False) +class VirtualCircuitBulkRenameView(generic.BulkRenameView): + queryset = VirtualCircuit.objects.all() + field_name = 'cid' + + +@register_model_view(VirtualCircuit, 'bulk_delete', path='delete', detail=False) class VirtualCircuitBulkDeleteView(generic.BulkDeleteView): queryset = VirtualCircuit.objects.annotate( termination_count=count_related(VirtualCircuitTermination, 'virtual_circuit') @@ -714,6 +764,7 @@ class VirtualCircuitTerminationListView(generic.ObjectListView): filterset = filtersets.VirtualCircuitTerminationFilterSet filterset_form = forms.VirtualCircuitTerminationFilterForm table = tables.VirtualCircuitTerminationTable + actions = (AddObject, BulkImport, BulkExport, BulkEdit, BulkDelete) @register_model_view(VirtualCircuitTermination) diff --git a/netbox/core/api/serializers.py b/netbox/core/api/serializers.py index 9a6d4d726..d1c778f65 100644 --- a/netbox/core/api/serializers.py +++ b/netbox/core/api/serializers.py @@ -1,4 +1,5 @@ from .serializers_.change_logging import * from .serializers_.data import * from .serializers_.jobs import * +from .serializers_.object_types import * from .serializers_.tasks import * diff --git a/netbox/core/api/serializers_/change_logging.py b/netbox/core/api/serializers_/change_logging.py index e8af31ae8..575a849d5 100644 --- a/netbox/core/api/serializers_/change_logging.py +++ b/netbox/core/api/serializers_/change_logging.py @@ -44,7 +44,8 @@ class ObjectChangeSerializer(BaseModelSerializer): model = ObjectChange fields = [ 'id', 'url', 'display_url', 'display', 'time', 'user', 'user_name', 'request_id', 'action', - 'changed_object_type', 'changed_object_id', 'changed_object', 'prechange_data', 'postchange_data', + 'changed_object_type', 'changed_object_id', 'changed_object', 'message', 'prechange_data', + 'postchange_data', ] @extend_schema_field(serializers.JSONField(allow_null=True)) diff --git a/netbox/core/api/serializers_/jobs.py b/netbox/core/api/serializers_/jobs.py index 306287e88..dd0dd1245 100644 --- a/netbox/core/api/serializers_/jobs.py +++ b/netbox/core/api/serializers_/jobs.py @@ -23,6 +23,6 @@ class JobSerializer(BaseModelSerializer): model = Job fields = [ 'id', 'url', 'display_url', 'display', 'object_type', 'object_id', 'name', 'status', 'created', 'scheduled', - 'interval', 'started', 'completed', 'user', 'data', 'error', 'job_id', + 'interval', 'started', 'completed', 'user', 'data', 'error', 'job_id', 'log_entries', ] brief_fields = ('url', 'created', 'completed', 'user', 'status') diff --git a/netbox/core/api/serializers_/object_types.py b/netbox/core/api/serializers_/object_types.py new file mode 100644 index 000000000..c36796b5f --- /dev/null +++ b/netbox/core/api/serializers_/object_types.py @@ -0,0 +1,47 @@ +import inspect + +from django.urls import NoReverseMatch +from drf_spectacular.types import OpenApiTypes +from drf_spectacular.utils import extend_schema_field +from rest_framework import serializers + +from core.models import ObjectType +from netbox.api.serializers import BaseModelSerializer +from utilities.views import get_action_url + +__all__ = ( + 'ObjectTypeSerializer', +) + + +class ObjectTypeSerializer(BaseModelSerializer): + url = serializers.HyperlinkedIdentityField(view_name='core-api:objecttype-detail') + app_name = serializers.CharField(source='app_verbose_name', read_only=True) + model_name = serializers.CharField(source='model_verbose_name', read_only=True) + model_name_plural = serializers.CharField(source='model_verbose_name_plural', read_only=True) + is_plugin_model = serializers.BooleanField(read_only=True) + rest_api_endpoint = serializers.SerializerMethodField() + description = serializers.SerializerMethodField() + + class Meta: + model = ObjectType + fields = [ + 'id', 'url', 'display', 'app_label', 'app_name', 'model', 'model_name', 'model_name_plural', 'public', + 'features', 'is_plugin_model', 'rest_api_endpoint', 'description', + ] + read_only_fields = ['public', 'features'] + + @extend_schema_field(OpenApiTypes.STR) + def get_rest_api_endpoint(self, obj): + if not (model := obj.model_class()): + return + try: + return get_action_url(model, action='list', rest_api=True) + except NoReverseMatch: + return + + @extend_schema_field(OpenApiTypes.STR) + def get_description(self, obj): + if not (model := obj.model_class()): + return + return inspect.getdoc(model) diff --git a/netbox/core/api/urls.py b/netbox/core/api/urls.py index 3c22f1cf4..5f2fcad10 100644 --- a/netbox/core/api/urls.py +++ b/netbox/core/api/urls.py @@ -9,7 +9,8 @@ router.APIRootView = views.CoreRootView router.register('data-sources', views.DataSourceViewSet) router.register('data-files', views.DataFileViewSet) router.register('jobs', views.JobViewSet) -router.register('object-changes', views.ObjectChangeViewSet) +router.register('object-changes', views.ObjectChangeViewSet, basename='objectchange') +router.register('object-types', views.ObjectTypeViewSet) router.register('background-queues', views.BackgroundQueueViewSet, basename='rqqueue') router.register('background-workers', views.BackgroundWorkerViewSet, basename='rqworker') router.register('background-tasks', views.BackgroundTaskViewSet, basename='rqtask') diff --git a/netbox/core/api/views.py b/netbox/core/api/views.py index fc4b85e61..e9569a717 100644 --- a/netbox/core/api/views.py +++ b/netbox/core/api/views.py @@ -20,6 +20,7 @@ from core import filtersets from core.jobs import SyncDataSourceJob from core.models import * from core.utils import delete_rq_job, enqueue_rq_job, get_rq_jobs, requeue_rq_job, stop_rq_job +from netbox.api.authentication import IsAuthenticatedOrLoginNotRequired from netbox.api.metadata import ContentTypeMetadata from netbox.api.pagination import LimitOffsetListPagination from netbox.api.viewsets import NetBoxModelViewSet, NetBoxReadOnlyModelViewSet @@ -77,10 +78,22 @@ class ObjectChangeViewSet(ReadOnlyModelViewSet): Retrieve a list of recent changes. """ metadata_class = ContentTypeMetadata - queryset = ObjectChange.objects.valid_models() serializer_class = serializers.ObjectChangeSerializer filterset_class = filtersets.ObjectChangeFilterSet + def get_queryset(self): + return ObjectChange.objects.valid_models() + + +class ObjectTypeViewSet(ReadOnlyModelViewSet): + """ + Read-only list of ObjectTypes. + """ + permission_classes = [IsAuthenticatedOrLoginNotRequired] + queryset = ObjectType.objects.order_by('app_label', 'model') + serializer_class = serializers.ObjectTypeSerializer + filterset_class = filtersets.ObjectTypeFilterSet + class BaseRQViewSet(viewsets.ViewSet): """ diff --git a/netbox/core/constants.py b/netbox/core/constants.py index 3c3382dcc..582768186 100644 --- a/netbox/core/constants.py +++ b/netbox/core/constants.py @@ -4,23 +4,31 @@ from django.utils.translation import gettext_lazy as _ from rq.job import JobStatus __all__ = ( + 'JOB_LOG_ENTRY_LEVELS', 'RQ_TASK_STATUSES', ) @dataclass -class Status: +class Badge: label: str color: str RQ_TASK_STATUSES = { - JobStatus.QUEUED: Status(_('Queued'), 'cyan'), - JobStatus.FINISHED: Status(_('Finished'), 'green'), - JobStatus.FAILED: Status(_('Failed'), 'red'), - JobStatus.STARTED: Status(_('Started'), 'blue'), - JobStatus.DEFERRED: Status(_('Deferred'), 'gray'), - JobStatus.SCHEDULED: Status(_('Scheduled'), 'purple'), - JobStatus.STOPPED: Status(_('Stopped'), 'orange'), - JobStatus.CANCELED: Status(_('Cancelled'), 'yellow'), + JobStatus.QUEUED: Badge(_('Queued'), 'cyan'), + JobStatus.FINISHED: Badge(_('Finished'), 'green'), + JobStatus.FAILED: Badge(_('Failed'), 'red'), + JobStatus.STARTED: Badge(_('Started'), 'blue'), + JobStatus.DEFERRED: Badge(_('Deferred'), 'gray'), + JobStatus.SCHEDULED: Badge(_('Scheduled'), 'purple'), + JobStatus.STOPPED: Badge(_('Stopped'), 'orange'), + JobStatus.CANCELED: Badge(_('Cancelled'), 'yellow'), +} + +JOB_LOG_ENTRY_LEVELS = { + 'debug': Badge(_('Debug'), 'gray'), + 'info': Badge(_('Info'), 'blue'), + 'warning': Badge(_('Warning'), 'orange'), + 'error': Badge(_('Error'), 'red'), } diff --git a/netbox/core/dataclasses.py b/netbox/core/dataclasses.py new file mode 100644 index 000000000..21f97d01d --- /dev/null +++ b/netbox/core/dataclasses.py @@ -0,0 +1,21 @@ +import logging + +from dataclasses import dataclass, field +from datetime import datetime + +from django.utils import timezone + +__all__ = ( + 'JobLogEntry', +) + + +@dataclass +class JobLogEntry: + level: str + message: str + timestamp: datetime = field(default_factory=timezone.now) + + @classmethod + def from_logrecord(cls, record: logging.LogRecord): + return cls(record.levelname.lower(), record.msg) diff --git a/netbox/core/filtersets.py b/netbox/core/filtersets.py index 42ec22350..215745e7d 100644 --- a/netbox/core/filtersets.py +++ b/netbox/core/filtersets.py @@ -1,9 +1,8 @@ +import django_filters from django.contrib.contenttypes.models import ContentType from django.db.models import Q from django.utils.translation import gettext as _ -import django_filters - from netbox.filtersets import BaseFilterSet, ChangeLoggedModelFilterSet, NetBoxModelFilterSet from netbox.utils import get_data_backend_choices from users.models import User @@ -17,6 +16,7 @@ __all__ = ( 'DataSourceFilterSet', 'JobFilterSet', 'ObjectChangeFilterSet', + 'ObjectTypeFilterSet', ) @@ -134,6 +134,31 @@ class JobFilterSet(BaseFilterSet): ) +class ObjectTypeFilterSet(BaseFilterSet): + q = django_filters.CharFilter( + method='search', + label=_('Search'), + ) + features = django_filters.CharFilter( + method='filter_features' + ) + + class Meta: + model = ObjectType + fields = ('id', 'app_label', 'model', 'public') + + def search(self, queryset, name, value): + if not value.strip(): + return queryset + return queryset.filter( + Q(app_label__icontains=value) | + Q(model__icontains=value) + ) + + def filter_features(self, queryset, name, value): + return queryset.filter(features__icontains=value) + + class ObjectChangeFilterSet(BaseFilterSet): q = django_filters.CharFilter( method='search', @@ -167,7 +192,8 @@ class ObjectChangeFilterSet(BaseFilterSet): return queryset return queryset.filter( Q(user_name__icontains=value) | - Q(object_repr__icontains=value) + Q(object_repr__icontains=value) | + Q(message__icontains=value) ) diff --git a/netbox/core/graphql/mixins.py b/netbox/core/graphql/mixins.py index 72191e6fd..6c9042313 100644 --- a/netbox/core/graphql/mixins.py +++ b/netbox/core/graphql/mixins.py @@ -7,10 +7,12 @@ from django.contrib.contenttypes.models import ContentType from core.models import ObjectChange if TYPE_CHECKING: + from core.graphql.types import DataFileType, DataSourceType from netbox.core.graphql.types import ObjectChangeType __all__ = ( 'ChangelogMixin', + 'SyncedDataMixin', ) @@ -25,3 +27,9 @@ class ChangelogMixin: changed_object_id=self.pk ) return object_changes.restrict(info.context.request.user, 'view') + + +@strawberry.type +class SyncedDataMixin: + data_source: Annotated["DataSourceType", strawberry.lazy('core.graphql.types')] | None + data_file: Annotated["DataFileType", strawberry.lazy('core.graphql.types')] | None diff --git a/netbox/core/jobs.py b/netbox/core/jobs.py index 5806d7d42..8b516e660 100644 --- a/netbox/core/jobs.py +++ b/netbox/core/jobs.py @@ -1,17 +1,21 @@ -import logging -import requests import sys +from datetime import timedelta +from importlib import import_module +import requests from django.conf import settings +from django.core.cache import cache +from django.utils import timezone +from packaging import version + +from core.models import Job, ObjectChange +from netbox.config import Config from netbox.jobs import JobRunner, system_job from netbox.search.backends import search_backend from utilities.proxy import resolve_proxies from .choices import DataSourceStatusChoices, JobIntervalChoices -from .exceptions import SyncError from .models import DataSource -logger = logging.getLogger(__name__) - class SyncDataSourceJob(JobRunner): """ @@ -34,19 +38,23 @@ class SyncDataSourceJob(JobRunner): def run(self, *args, **kwargs): datasource = DataSource.objects.get(pk=self.job.object_id) + self.logger.debug(f"Found DataSource ID {datasource.pk}") try: + self.logger.info(f"Syncing data source {datasource}") datasource.sync() # Update the search cache for DataFiles belonging to this source + self.logger.debug("Updating search cache for data files") search_backend.cache(datasource.datafiles.iterator()) except Exception as e: + self.logger.error(f"Error syncing data source: {e}") DataSource.objects.filter(pk=datasource.pk).update(status=DataSourceStatusChoices.FAILED) - if type(e) is SyncError: - logging.error(e) raise e + self.logger.info("Syncing completed successfully") + @system_job(interval=JobIntervalChoices.INTERVAL_DAILY) class SystemHousekeepingJob(JobRunner): @@ -58,19 +66,29 @@ class SystemHousekeepingJob(JobRunner): def run(self, *args, **kwargs): # Skip if running in development or test mode - if settings.DEBUG or 'test' in sys.argv: + if settings.DEBUG: + self.logger.warning("Aborting execution: Debug is enabled") + return + if 'test' in sys.argv: + self.logger.warning("Aborting execution: Tests are running") return - # TODO: Migrate other housekeeping functions from the `housekeeping` management command. self.send_census_report() + self.clear_expired_sessions() + self.prune_changelog() + self.delete_expired_jobs() + self.check_for_new_releases() - @staticmethod - def send_census_report(): + def send_census_report(self): """ Send a census report (if enabled). """ - # Skip if census reporting is disabled - if settings.ISOLATED_DEPLOYMENT or not settings.CENSUS_REPORTING_ENABLED: + self.logger.info("Reporting census data...") + if settings.ISOLATED_DEPLOYMENT: + self.logger.info("ISOLATED_DEPLOYMENT is enabled; skipping") + return + if not settings.CENSUS_REPORTING_ENABLED: + self.logger.info("CENSUS_REPORTING_ENABLED is disabled; skipping") return census_data = { @@ -87,3 +105,92 @@ class SystemHousekeepingJob(JobRunner): ) except requests.exceptions.RequestException: pass + + def clear_expired_sessions(self): + """ + Clear any expired sessions from the database. + """ + self.logger.info("Clearing expired sessions...") + engine = import_module(settings.SESSION_ENGINE) + try: + engine.SessionStore.clear_expired() + self.logger.info("Sessions cleared.") + except NotImplementedError: + self.logger.warning( + f"The configured session engine ({settings.SESSION_ENGINE}) does not support " + f"clearing sessions; skipping." + ) + + def prune_changelog(self): + """ + Delete any ObjectChange records older than the configured changelog retention time (if any). + """ + self.logger.info("Pruning old changelog entries...") + config = Config() + if not config.CHANGELOG_RETENTION: + self.logger.info("No retention period specified; skipping.") + return + + cutoff = timezone.now() - timedelta(days=config.CHANGELOG_RETENTION) + self.logger.debug( + f"Changelog retention period: {config.CHANGELOG_RETENTION} days ({cutoff:%Y-%m-%d %H:%M:%S})" + ) + + count = ObjectChange.objects.filter(time__lt=cutoff).delete()[0] + self.logger.info(f"Deleted {count} expired changelog records") + + def delete_expired_jobs(self): + """ + Delete any jobs older than the configured retention period (if any). + """ + self.logger.info("Deleting expired jobs...") + config = Config() + if not config.JOB_RETENTION: + self.logger.info("No retention period specified; skipping.") + return + + cutoff = timezone.now() - timedelta(days=config.JOB_RETENTION) + self.logger.debug( + f"Job retention period: {config.JOB_RETENTION} days ({cutoff:%Y-%m-%d %H:%M:%S})" + ) + + count = Job.objects.filter(created__lt=cutoff).delete()[0] + self.logger.info(f"Deleted {count} expired jobs") + + def check_for_new_releases(self): + """ + Check for new releases and cache the latest release. + """ + self.logger.info("Checking for new releases...") + if settings.ISOLATED_DEPLOYMENT: + self.logger.info("ISOLATED_DEPLOYMENT is enabled; skipping") + return + if not settings.RELEASE_CHECK_URL: + self.logger.info("RELEASE_CHECK_URL is not set; skipping") + return + + # Fetch the latest releases + self.logger.debug(f"Release check URL: {settings.RELEASE_CHECK_URL}") + try: + response = requests.get( + url=settings.RELEASE_CHECK_URL, + headers={'Accept': 'application/vnd.github.v3+json'}, + proxies=resolve_proxies(url=settings.RELEASE_CHECK_URL) + ) + response.raise_for_status() + except requests.exceptions.RequestException as exc: + self.logger.error(f"Error fetching release: {exc}") + return + + # Determine the most recent stable release + releases = [] + for release in response.json(): + if 'tag_name' not in release or release.get('devrelease') or release.get('prerelease'): + continue + releases.append((version.parse(release['tag_name']), release.get('html_url'))) + latest_release = max(releases) + self.logger.debug(f"Found {len(response.json())} releases; {len(releases)} usable") + self.logger.info(f"Latest release: {latest_release[0]}") + + # Cache the most recent release + cache.set('latest_release', latest_release, None) diff --git a/netbox/core/management/commands/nbshell.py b/netbox/core/management/commands/nbshell.py index 8f729d10a..7b7cee0bc 100644 --- a/netbox/core/management/commands/nbshell.py +++ b/netbox/core/management/commands/nbshell.py @@ -1,29 +1,48 @@ import code import platform -import sys +from collections import defaultdict +from types import SimpleNamespace +from colorama import Fore, Style from django import get_version from django.apps import apps from django.conf import settings from django.core.management.base import BaseCommand +from django.utils.module_loading import import_string -from core.models import ObjectType -from users.models import User +from netbox.constants import CORE_APPS +from netbox.plugins.utils import get_installed_plugins -APPS = ('circuits', 'core', 'dcim', 'extras', 'ipam', 'tenancy', 'users', 'virtualization', 'vpn', 'wireless') -EXCLUDE_MODELS = ( - 'extras.branch', - 'extras.stagedchange', -) -BANNER_TEXT = """### NetBox interactive shell ({node}) -### Python {python} | Django {django} | NetBox {netbox} -### lsmodels() will show available models. Use help() for more info.""".format( - node=platform.node(), - python=platform.python_version(), - django=get_version(), - netbox=settings.RELEASE.name -) +def color(color: str, text: str): + return getattr(Fore, color.upper()) + text + Style.RESET_ALL + + +def bright(text: str): + return Style.BRIGHT + text + Style.RESET_ALL + + +def get_models(app_config): + """ + Return a list of all non-private models within an app. + """ + return [ + model for model in app_config.get_models() + if not getattr(model, '_netbox_private', False) + ] + + +def get_constants(app_config): + """ + Return a dictionary mapping of all constants defined within an app. + """ + try: + constants = import_string(f'{app_config.name}.constants') + except ImportError: + return {} + return { + name: value for name, value in vars(constants).items() + } class Command(BaseCommand): @@ -36,47 +55,88 @@ class Command(BaseCommand): help='Python code to execute (instead of starting an interactive shell)', ) - def _lsmodels(self): - for app, models in self.django_models.items(): - app_name = apps.get_app_config(app).verbose_name + def _lsapps(self): + for app_label in self.django_models.keys(): + app_name = apps.get_app_config(app_label).verbose_name + print(f'{app_label} - {app_name}') + + def _lsmodels(self, app_label=None): + """ + Return a list of all models within each app. + + Args: + app_label: The name of a specific app + """ + if app_label: + if app_label not in self.django_models: + print(f"No models listed for {app_label}") + return + app_labels = [app_label] + else: + app_labels = self.django_models.keys() # All apps + + for app_label in app_labels: + app_name = apps.get_app_config(app_label).verbose_name print(f'{app_name}:') - for m in models: - print(f' {m}') + for model in self.django_models[app_label]: + print(f' {app_label}.{model}') def get_namespace(self): - namespace = {} + namespace = defaultdict(SimpleNamespace) - # Gather Django models and constants from each app - for app in APPS: - models = [] + # Iterate through all core apps & plugins to compile namespace of models and constants + for app_name in [*CORE_APPS, *get_installed_plugins().keys()]: + app_config = apps.get_app_config(app_name) - # Load models from each app - for model in apps.get_app_config(app).get_models(): - app_label = model._meta.app_label - model_name = model._meta.model_name - if f'{app_label}.{model_name}' not in EXCLUDE_MODELS: - namespace[model.__name__] = model - models.append(model.__name__) - self.django_models[app] = sorted(models) + # Populate models + if models := get_models(app_config): + for model in models: + setattr(namespace[app_name], model.__name__, model) + self.django_models[app_name] = sorted([ + model.__name__ for model in models + ]) - # Constants - try: - app_constants = sys.modules[f'{app}.constants'] - for name in dir(app_constants): - namespace[name] = getattr(app_constants, name) - except KeyError: - pass + # Populate constants + for const_name, const_value in get_constants(app_config).items(): + setattr(namespace[app_name], const_name, const_value) - # Additional objects to include - namespace['ObjectType'] = ObjectType - namespace['User'] = User - - # Load convenience commands - namespace.update({ + return { + **namespace, + 'lsapps': self._lsapps, 'lsmodels': self._lsmodels, - }) + } - return namespace + @staticmethod + def get_banner_text(): + lines = [ + '{title} ({hostname})'.format( + title=bright('NetBox interactive shell'), + hostname=platform.node(), + ), + '{python} | {django} | {netbox}'.format( + python=color('green', f'Python v{platform.python_version()}'), + django=color('green', f'Django v{get_version()}'), + netbox=color('green', settings.RELEASE.name), + ), + ] + + if installed_plugins := get_installed_plugins(): + plugin_list = ', '.join([ + color('cyan', f'{name} v{version}') for name, version in installed_plugins.items() + ]) + lines.append( + 'Plugins: {plugin_list}'.format( + plugin_list=plugin_list + ) + ) + + lines.append( + 'lsapps() & lsmodels() will show available models. Use help() for more info.' + ) + + return '\n'.join([ + f'### {line}' for line in lines + ]) def handle(self, **options): namespace = self.get_namespace() @@ -97,5 +157,4 @@ class Command(BaseCommand): readline.parse_and_bind('tab: complete') # Run interactive shell - shell = code.interact(banner=BANNER_TEXT, local=namespace) - return shell + return code.interact(banner=self.get_banner_text(), local=namespace) diff --git a/netbox/core/migrations/0008_contenttype_proxy.py b/netbox/core/migrations/0008_contenttype_proxy.py index 9acaf3ad7..a0af81813 100644 --- a/netbox/core/migrations/0008_contenttype_proxy.py +++ b/netbox/core/migrations/0008_contenttype_proxy.py @@ -1,4 +1,4 @@ -import core.models.contenttypes +import core.models.object_types from django.db import migrations @@ -19,7 +19,7 @@ class Migration(migrations.Migration): }, bases=('contenttypes.contenttype',), managers=[ - ('objects', core.models.contenttypes.ObjectTypeManager()), + ('objects', core.models.object_types.ObjectTypeManager()), ], ), ] diff --git a/netbox/core/migrations/0016_job_log_entries.py b/netbox/core/migrations/0016_job_log_entries.py new file mode 100644 index 000000000..030bd4e38 --- /dev/null +++ b/netbox/core/migrations/0016_job_log_entries.py @@ -0,0 +1,28 @@ +import django.contrib.postgres.fields +import django.core.serializers.json +from django.db import migrations, models + +import utilities.json + + +class Migration(migrations.Migration): + + dependencies = [ + ('core', '0015_remove_redundant_indexes'), + ] + + operations = [ + migrations.AddField( + model_name='job', + name='log_entries', + field=django.contrib.postgres.fields.ArrayField( + base_field=models.JSONField( + decoder=utilities.json.JobLogDecoder, + encoder=django.core.serializers.json.DjangoJSONEncoder + ), + blank=True, + default=list, + size=None + ), + ), + ] diff --git a/netbox/core/migrations/0017_objectchange_message.py b/netbox/core/migrations/0017_objectchange_message.py new file mode 100644 index 000000000..c669513a0 --- /dev/null +++ b/netbox/core/migrations/0017_objectchange_message.py @@ -0,0 +1,16 @@ +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('core', '0016_job_log_entries'), + ] + + operations = [ + migrations.AddField( + model_name='objectchange', + name='message', + field=models.CharField(blank=True, editable=False, max_length=200), + ), + ] diff --git a/netbox/core/migrations/0018_concrete_objecttype.py b/netbox/core/migrations/0018_concrete_objecttype.py new file mode 100644 index 000000000..4e227fe7a --- /dev/null +++ b/netbox/core/migrations/0018_concrete_objecttype.py @@ -0,0 +1,63 @@ +import django.contrib.postgres.fields +import django.contrib.postgres.indexes +import django.db.models.deletion +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('contenttypes', '0002_remove_content_type_name'), + ('core', '0017_objectchange_message'), + ] + + operations = [ + # Delete the proxy model from the migration state + migrations.DeleteModel( + name='ObjectType', + ), + # Create the new concrete model + migrations.CreateModel( + name='ObjectType', + fields=[ + ( + 'contenttype_ptr', + models.OneToOneField( + on_delete=django.db.models.deletion.CASCADE, + parent_link=True, + primary_key=True, + serialize=False, + to='contenttypes.contenttype', + related_name='object_type' + ) + ), + ( + 'public', + models.BooleanField( + default=False + ) + ), + ( + 'features', + django.contrib.postgres.fields.ArrayField( + base_field=models.CharField(max_length=50), + default=list, + size=None + ) + ), + ], + options={ + 'verbose_name': 'object type', + 'verbose_name_plural': 'object types', + 'ordering': ('app_label', 'model'), + 'indexes': [ + django.contrib.postgres.indexes.GinIndex( + fields=['features'], + name='core_object_feature_aec4de_gin' + ), + ] + }, + bases=('contenttypes.contenttype',), + managers=[], + ), + ] diff --git a/netbox/core/models/__init__.py b/netbox/core/models/__init__.py index db00e67aa..27eb3a92b 100644 --- a/netbox/core/models/__init__.py +++ b/netbox/core/models/__init__.py @@ -1,4 +1,4 @@ -from .contenttypes import * +from .object_types import * from .change_logging import * from .config import * from .data import * diff --git a/netbox/core/models/change_logging.py b/netbox/core/models/change_logging.py index 1d1bbc07c..a011c457f 100644 --- a/netbox/core/models/change_logging.py +++ b/netbox/core/models/change_logging.py @@ -11,8 +11,8 @@ from mptt.models import MPTTModel from core.choices import ObjectChangeActionChoices from core.querysets import ObjectChangeQuerySet from netbox.models.features import ChangeLoggingMixin +from netbox.models.features import has_feature from utilities.data import shallow_compare_dict -from .contenttypes import ObjectType __all__ = ( 'ObjectChange', @@ -82,6 +82,12 @@ class ObjectChange(models.Model): max_length=200, editable=False ) + message = models.CharField( + verbose_name=_('message'), + max_length=200, + editable=False, + blank=True + ) prechange_data = models.JSONField( verbose_name=_('pre-change data'), editable=False, @@ -118,7 +124,7 @@ class ObjectChange(models.Model): super().clean() # Validate the assigned object type - if self.changed_object_type not in ObjectType.objects.with_feature('change_logging'): + if not has_feature(self.changed_object_type, 'change_logging'): raise ValidationError( _("Change logging is not supported for this object type ({type}).").format( type=self.changed_object_type diff --git a/netbox/core/models/contenttypes.py b/netbox/core/models/contenttypes.py index b0301848f..3d5c5e8b2 100644 --- a/netbox/core/models/contenttypes.py +++ b/netbox/core/models/contenttypes.py @@ -1,50 +1,3 @@ -from django.contrib.contenttypes.models import ContentType, ContentTypeManager -from django.db.models import Q - -from netbox.registry import registry - -__all__ = ( - 'ObjectType', - 'ObjectTypeManager', -) - - -class ObjectTypeManager(ContentTypeManager): - - def public(self): - """ - Filter the base queryset to return only ContentTypes corresponding to "public" models; those which are listed - in registry['models'] and intended for reference by other objects. - """ - q = Q() - for app_label, models in registry['models'].items(): - q |= Q(app_label=app_label, model__in=models) - return self.get_queryset().filter(q) - - def with_feature(self, feature): - """ - Return the ContentTypes only for models which are registered as supporting the specified feature. For example, - we can find all ContentTypes for models which support webhooks with - - ContentType.objects.with_feature('event_rules') - """ - if feature not in registry['model_features']: - raise KeyError( - f"{feature} is not a registered model feature! Valid features are: {registry['model_features'].keys()}" - ) - - q = Q() - for app_label, models in registry['model_features'][feature].items(): - q |= Q(app_label=app_label, model__in=models) - - return self.get_queryset().filter(q) - - -class ObjectType(ContentType): - """ - Wrap Django's native ContentType model to use our custom manager. - """ - objects = ObjectTypeManager() - - class Meta: - proxy = True +# TODO: Remove this module in NetBox v4.5 +# Provided for backward compatibility +from .object_types import * diff --git a/netbox/core/models/jobs.py b/netbox/core/models/jobs.py index 941f5fe67..8a6bf6a1d 100644 --- a/netbox/core/models/jobs.py +++ b/netbox/core/models/jobs.py @@ -1,9 +1,12 @@ +import logging import uuid +from dataclasses import asdict from functools import partial import django_rq from django.conf import settings from django.contrib.contenttypes.fields import GenericForeignKey +from django.contrib.postgres.fields import ArrayField from django.core.exceptions import ValidationError from django.core.serializers.json import DjangoJSONEncoder from django.core.validators import MinValueValidator @@ -14,8 +17,13 @@ from django.utils.translation import gettext as _ from rq.exceptions import InvalidJobOperation from core.choices import JobStatusChoices +from core.dataclasses import JobLogEntry +from core.events import JOB_COMPLETED, JOB_ERRORED, JOB_FAILED from core.models import ObjectType from core.signals import job_end, job_start +from extras.models import Notification +from netbox.models.features import has_feature +from utilities.json import JobLogDecoder from utilities.querysets import RestrictedQuerySet from utilities.rqworker import get_queue_for_model @@ -104,6 +112,15 @@ class Job(models.Model): verbose_name=_('job ID'), unique=True ) + log_entries = ArrayField( + verbose_name=_('log entries'), + base_field=models.JSONField( + encoder=DjangoJSONEncoder, + decoder=JobLogDecoder, + ), + blank=True, + default=list, + ) objects = RestrictedQuerySet.as_manager() @@ -116,7 +133,7 @@ class Job(models.Model): verbose_name_plural = _('jobs') def __str__(self): - return str(self.job_id) + return self.name def get_absolute_url(self): # TODO: Employ dynamic registration @@ -130,11 +147,18 @@ class Job(models.Model): def get_status_color(self): return JobStatusChoices.colors.get(self.status) + def get_event_type(self): + return { + JobStatusChoices.STATUS_COMPLETED: JOB_COMPLETED, + JobStatusChoices.STATUS_FAILED: JOB_FAILED, + JobStatusChoices.STATUS_ERRORED: JOB_ERRORED, + }.get(self.status) + def clean(self): super().clean() # Validate the assigned object type - if self.object_type and self.object_type not in ObjectType.objects.with_feature('jobs'): + if self.object_type and not has_feature(self.object_type, 'jobs'): raise ValidationError( _("Jobs cannot be assigned to this object type ({type}).").format(type=self.object_type) ) @@ -201,9 +225,24 @@ class Job(models.Model): self.completed = timezone.now() self.save() + # Notify the user (if any) of completion + if self.user: + Notification( + user=self.user, + object=self, + event_type=self.get_event_type(), + ).save() + # Send signal job_end.send(self) + def log(self, record: logging.LogRecord): + """ + Record a LogRecord from Python's native logging in the job's log. + """ + entry = JobLogEntry.from_logrecord(record) + self.log_entries.append(asdict(entry)) + @classmethod def enqueue( cls, diff --git a/netbox/core/models/object_types.py b/netbox/core/models/object_types.py new file mode 100644 index 000000000..bb031b4eb --- /dev/null +++ b/netbox/core/models/object_types.py @@ -0,0 +1,211 @@ +import inspect +from collections import defaultdict + +from django.contrib.contenttypes.models import ContentType +from django.contrib.postgres.fields import ArrayField +from django.contrib.postgres.indexes import GinIndex +from django.core.exceptions import ObjectDoesNotExist +from django.db import models +from django.db.models import Q +from django.utils.translation import gettext as _ + +from netbox.plugins import PluginConfig +from netbox.registry import registry +from utilities.string import title + +__all__ = ( + 'ObjectType', + 'ObjectTypeManager', + 'ObjectTypeQuerySet', +) + + +class ObjectTypeQuerySet(models.QuerySet): + + def create(self, **kwargs): + # If attempting to create a new ObjectType for a given app_label & model, replace those kwargs + # with a reference to the ContentType (if one exists). + if (app_label := kwargs.get('app_label')) and (model := kwargs.get('model')): + try: + kwargs['contenttype_ptr'] = ContentType.objects.get(app_label=app_label, model=model) + except ObjectDoesNotExist: + pass + return super().create(**kwargs) + + +class ObjectTypeManager(models.Manager): + + def get_queryset(self): + return ObjectTypeQuerySet(self.model, using=self._db) + + def get_by_natural_key(self, app_label, model): + """ + Retrieve an ObjectType by its application label & model name. + + This method exists to provide parity with ContentTypeManager. + """ + return self.get(app_label=app_label, model=model) + + # TODO: Remove in NetBox v4.5 + def get_for_id(self, id): + """ + Retrieve an ObjectType by its primary key (numeric ID). + + This method exists to provide parity with ContentTypeManager. + """ + return self.get(pk=id) + + def _get_opts(self, model, for_concrete_model): + if for_concrete_model: + model = model._meta.concrete_model + return model._meta + + def get_for_model(self, model, for_concrete_model=True): + """ + Retrieve or create and return the ObjectType for a model. + """ + from netbox.models.features import get_model_features, model_is_public + + if not inspect.isclass(model): + model = model.__class__ + opts = self._get_opts(model, for_concrete_model) + + try: + # Use .get() instead of .get_or_create() initially to ensure db_for_read is honored (Django bug #20401). + ot = self.get(app_label=opts.app_label, model=opts.model_name) + except self.model.DoesNotExist: + # If the ObjectType doesn't exist, create it. (Use .get_or_create() to avoid race conditions.) + ot = self.get_or_create( + app_label=opts.app_label, + model=opts.model_name, + public=model_is_public(model), + features=get_model_features(model), + )[0] + + return ot + + def get_for_models(self, *models, for_concrete_models=True): + """ + Retrieve or create the ObjectTypes for multiple models, returning a mapping {model: ObjectType}. + + This method exists to provide parity with ContentTypeManager. + """ + from netbox.models.features import get_model_features, model_is_public + results = {} + + # Compile the model and options mappings + needed_models = defaultdict(set) + needed_opts = defaultdict(list) + for model in models: + if not inspect.isclass(model): + model = model.__class__ + opts = self._get_opts(model, for_concrete_models) + needed_models[opts.app_label].add(opts.model_name) + needed_opts[(opts.app_label, opts.model_name)].append(model) + + # Fetch existing ObjectType from the database + condition = Q( + *( + Q(('app_label', app_label), ('model__in', model_names)) + for app_label, model_names in needed_models.items() + ), + _connector=Q.OR, + ) + for ot in self.filter(condition): + opts_models = needed_opts.pop((ot.app_label, ot.model), []) + for model in opts_models: + results[model] = ot + + # Create any missing ObjectTypes + for (app_label, model_name), opts_models in needed_opts.items(): + for model in opts_models: + results[model] = self.create( + app_label=app_label, + model=model_name, + public=model_is_public(model), + features=get_model_features(model), + ) + + return results + + def public(self): + """ + Includes only ObjectTypes for "public" models. + + Filter the base queryset to return only ObjectTypes corresponding to public models; those which are intended + for reference by other objects within the application. + """ + return self.get_queryset().filter(public=True) + + def with_feature(self, feature): + """ + Return ObjectTypes only for models which support the given feature. + + Only ObjectTypes which list the specified feature will be included. Supported features are declared in the + application registry under `registry["model_features"]`. For example, we can find all ObjectTypes for models + which support event rules with: + + ObjectType.objects.with_feature('event_rules') + """ + if feature not in registry['model_features']: + raise KeyError( + f"{feature} is not a registered model feature! Valid features are: {registry['model_features'].keys()}" + ) + return self.get_queryset().filter(features__contains=[feature]) + + +class ObjectType(ContentType): + """ + Wrap Django's native ContentType model to use our custom manager. + """ + contenttype_ptr = models.OneToOneField( + on_delete=models.CASCADE, + to='contenttypes.ContentType', + parent_link=True, + primary_key=True, + serialize=False, + related_name='object_type', + ) + public = models.BooleanField( + default=False, + ) + features = ArrayField( + base_field=models.CharField(max_length=50), + default=list, + ) + + objects = ObjectTypeManager() + + class Meta: + verbose_name = _('object type') + verbose_name_plural = _('object types') + ordering = ('app_label', 'model') + indexes = [ + GinIndex(fields=['features']), + ] + + @property + def app_labeled_name(self): + # Override ContentType's "app | model" representation style. + return f"{self.app_verbose_name} > {title(self.model_verbose_name)}" + + @property + def app_verbose_name(self): + if model := self.model_class(): + return model._meta.app_config.verbose_name + + @property + def model_verbose_name(self): + if model := self.model_class(): + return model._meta.verbose_name + + @property + def model_verbose_name_plural(self): + if model := self.model_class(): + return model._meta.verbose_name_plural + + @property + def is_plugin_model(self): + if not (model := self.model_class()): + return # Return null if model class is invalid + return isinstance(model._meta.app_config, PluginConfig) diff --git a/netbox/core/object_actions.py b/netbox/core/object_actions.py new file mode 100644 index 000000000..81b5fb2c8 --- /dev/null +++ b/netbox/core/object_actions.py @@ -0,0 +1,18 @@ +from django.utils.translation import gettext as _ + +from netbox.object_actions import ObjectAction + +__all__ = ( + 'BulkSync', +) + + +class BulkSync(ObjectAction): + """ + Synchronize multiple objects at once. + """ + name = 'bulk_sync' + label = _('Sync Data') + multi = True + permissions_required = {'sync'} + template_name = 'core/buttons/bulk_sync.html' diff --git a/netbox/core/signals.py b/netbox/core/signals.py index e042255a7..46a0fe0fd 100644 --- a/netbox/core/signals.py +++ b/netbox/core/signals.py @@ -2,9 +2,9 @@ import logging from threading import local from django.contrib.contenttypes.models import ContentType -from django.core.exceptions import ValidationError +from django.core.exceptions import ObjectDoesNotExist, ValidationError from django.db.models.fields.reverse_related import ManyToManyRel, ManyToOneRel -from django.db.models.signals import m2m_changed, post_save, pre_delete +from django.db.models.signals import m2m_changed, post_migrate, post_save, pre_delete from django.dispatch import receiver, Signal from django.core.signals import request_finished from django.utils.translation import gettext_lazy as _ @@ -12,12 +12,13 @@ from django_prometheus.models import model_deletes, model_inserts, model_updates from core.choices import JobStatusChoices, ObjectChangeActionChoices from core.events import * +from core.models import ObjectType from extras.events import enqueue_event from extras.models import Tag from extras.utils import run_validators from netbox.config import get_config from netbox.context import current_request, events_queue -from netbox.models.features import ChangeLoggingMixin +from netbox.models.features import ChangeLoggingMixin, get_model_features, model_is_public from utilities.exceptions import AbortRequest from .models import ConfigRevision, DataSource, ObjectChange @@ -41,6 +42,37 @@ post_sync = Signal() clear_events = Signal() +# +# Object types +# + +@receiver(post_migrate) +def update_object_types(sender, **kwargs): + """ + Create or update the corresponding ObjectType for each model within the migrated app. + """ + for model in sender.get_models(): + app_label, model_name = model._meta.label_lower.split('.') + + # Determine whether model is public and its supported features + is_public = model_is_public(model) + features = get_model_features(model) + + # Create/update the ObjectType for the model + try: + ot = ObjectType.objects.get_by_natural_key(app_label=app_label, model=model_name) + ot.public = is_public + ot.features = features + ot.save() + except ObjectDoesNotExist: + ObjectType.objects.create( + app_label=app_label, + model=model_name, + public=is_public, + features=features, + ) + + # # Change logging & event handling # @@ -116,7 +148,7 @@ def handle_changed_object(sender, instance, **kwargs): # Enqueue the object for event processing queue = events_queue.get() - enqueue_event(queue, instance, request.user, request.id, event_type) + enqueue_event(queue, instance, request, event_type) events_queue.set(queue) # Increment metric counters @@ -200,7 +232,7 @@ def handle_deleted_object(sender, instance, **kwargs): # Enqueue the object for event processing queue = events_queue.get() - enqueue_event(queue, instance, request.user, request.id, OBJECT_DELETED) + enqueue_event(queue, instance, request, OBJECT_DELETED) events_queue.set(queue) # Increment metric counters diff --git a/netbox/core/tables/change_logging.py b/netbox/core/tables/change_logging.py index aced0e8a6..b35b711bb 100644 --- a/netbox/core/tables/change_logging.py +++ b/netbox/core/tables/change_logging.py @@ -41,6 +41,9 @@ class ObjectChangeTable(NetBoxTable): template_code=OBJECTCHANGE_REQUEST_ID, verbose_name=_('Request ID') ) + message = tables.Column( + verbose_name=_('Message'), + ) actions = columns.ActionsColumn( actions=() ) @@ -49,5 +52,8 @@ class ObjectChangeTable(NetBoxTable): model = ObjectChange fields = ( 'pk', 'time', 'user_name', 'full_name', 'action', 'changed_object_type', 'object_repr', 'request_id', - 'actions', + 'message', 'actions', + ) + default_columns = ( + 'pk', 'time', 'user_name', 'action', 'changed_object_type', 'object_repr', 'message', 'actions', ) diff --git a/netbox/core/tables/columns.py b/netbox/core/tables/columns.py index f3d985bc3..84187b0fa 100644 --- a/netbox/core/tables/columns.py +++ b/netbox/core/tables/columns.py @@ -1,12 +1,11 @@ import django_tables2 as tables from django.utils.safestring import mark_safe -from core.constants import RQ_TASK_STATUSES from netbox.registry import registry __all__ = ( 'BackendTypeColumn', - 'RQJobStatusColumn', + 'BadgeColumn', ) @@ -23,14 +22,21 @@ class BackendTypeColumn(tables.Column): return value -class RQJobStatusColumn(tables.Column): +class BadgeColumn(tables.Column): """ - Render a colored label for the status of an RQ job. + Render a colored badge for a value. + + Args: + badges: A dictionary mapping of values to core.constants.Badge instances. """ + def __init__(self, badges, *args, **kwargs): + super().__init__(*args, **kwargs) + self.badges = badges + def render(self, value): - status = RQ_TASK_STATUSES.get(value) - return mark_safe(f'{status.label}') + badge = self.badges.get(value) + return mark_safe(f'{badge.label}') def value(self, value): - status = RQ_TASK_STATUSES.get(value) - return status.label + badge = self.badges.get(value) + return badge.label diff --git a/netbox/core/tables/jobs.py b/netbox/core/tables/jobs.py index ac27224b3..00032057f 100644 --- a/netbox/core/tables/jobs.py +++ b/netbox/core/tables/jobs.py @@ -1,8 +1,10 @@ import django_tables2 as tables from django.utils.translation import gettext_lazy as _ -from netbox.tables import NetBoxTable, columns -from ..models import Job +from netbox.tables import BaseTable, NetBoxTable, columns +from core.constants import JOB_LOG_ENTRY_LEVELS +from core.models import Job +from core.tables.columns import BadgeColumn class JobTable(NetBoxTable): @@ -40,6 +42,9 @@ class JobTable(NetBoxTable): completed = columns.DateTimeColumn( verbose_name=_('Completed'), ) + log_entries = tables.Column( + verbose_name=_('Log Entries'), + ) actions = columns.ActionsColumn( actions=('delete',) ) @@ -53,3 +58,24 @@ class JobTable(NetBoxTable): default_columns = ( 'pk', 'id', 'object_type', 'object', 'name', 'status', 'created', 'started', 'completed', 'user', ) + + def render_log_entries(self, value): + return len(value) + + +class JobLogEntryTable(BaseTable): + timestamp = columns.DateTimeColumn( + timespec='milliseconds', + verbose_name=_('Time'), + ) + level = BadgeColumn( + badges=JOB_LOG_ENTRY_LEVELS, + verbose_name=_('Level'), + ) + message = tables.Column( + verbose_name=_('Message'), + ) + + class Meta(BaseTable.Meta): + empty_text = _('No log entries') + fields = ('timestamp', 'level', 'message') diff --git a/netbox/core/tables/tasks.py b/netbox/core/tables/tasks.py index f53e598b5..64641b282 100644 --- a/netbox/core/tables/tasks.py +++ b/netbox/core/tables/tasks.py @@ -2,7 +2,8 @@ import django_tables2 as tables from django.utils.translation import gettext_lazy as _ from django_tables2.utils import A -from core.tables.columns import RQJobStatusColumn +from core.constants import RQ_TASK_STATUSES +from core.tables.columns import BadgeColumn from netbox.tables import BaseTable, columns @@ -84,7 +85,8 @@ class BackgroundTaskTable(BaseTable): ended_at = columns.DateTimeColumn( verbose_name=_("Ended") ) - status = RQJobStatusColumn( + status = BadgeColumn( + badges=RQ_TASK_STATUSES, verbose_name=_("Status"), accessor='get_status' ) diff --git a/netbox/core/tests/test_api.py b/netbox/core/tests/test_api.py index e9e77f252..4a285bdb4 100644 --- a/netbox/core/tests/test_api.py +++ b/netbox/core/tests/test_api.py @@ -7,6 +7,7 @@ from django.utils import timezone from rq.job import Job as RQ_Job, JobStatus from rq.registry import FailedJobRegistry, StartedJobRegistry +from rest_framework import status from users.models import Token, User from utilities.testing import APITestCase, APIViewTestCases, TestCase from utilities.testing.utils import disable_logging @@ -101,6 +102,22 @@ class DataFileTest( DataFile.objects.bulk_create(data_files) +class ObjectTypeTest(APITestCase): + + def test_list_objects(self): + object_type_count = ObjectType.objects.count() + + response = self.client.get(reverse('extras-api:objecttype-list'), **self.header) + self.assertHttpStatus(response, status.HTTP_200_OK) + self.assertEqual(response.data['count'], object_type_count) + + def test_get_object(self): + object_type = ObjectType.objects.first() + + url = reverse('extras-api:objecttype-detail', kwargs={'pk': object_type.pk}) + self.assertHttpStatus(self.client.get(url, **self.header), status.HTTP_200_OK) + + class BackgroundTaskTestCase(TestCase): user_permissions = () diff --git a/netbox/core/tests/test_filtersets.py b/netbox/core/tests/test_filtersets.py index b7dfd516e..50441cf62 100644 --- a/netbox/core/tests/test_filtersets.py +++ b/netbox/core/tests/test_filtersets.py @@ -150,7 +150,7 @@ class DataFileTestCase(TestCase, ChangeLoggedFilterSetTests): class ObjectChangeTestCase(TestCase, BaseFilterSetTests): queryset = ObjectChange.objects.all() filterset = ObjectChangeFilterSet - ignore_fields = ('prechange_data', 'postchange_data') + ignore_fields = ('message', 'prechange_data', 'postchange_data') @classmethod def setUpTestData(cls): @@ -241,3 +241,48 @@ class ObjectChangeTestCase(TestCase, BaseFilterSetTests): self.assertEqual(self.filterset(params, self.queryset).qs.count(), 3) params = {'changed_object_type_id': [ContentType.objects.get(app_label='dcim', model='site').pk]} self.assertEqual(self.filterset(params, self.queryset).qs.count(), 3) + + +class ObjectTypeTestCase(TestCase, BaseFilterSetTests): + queryset = ObjectType.objects.all() + filterset = ObjectTypeFilterSet + ignore_fields = ( + 'custom_fields', + 'custom_links', + 'event_rules', + 'export_templates', + 'object_permissions', + 'saved_filters', + ) + + def test_q(self): + params = {'q': 'vrf'} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1) + + def test_app_label(self): + self.assertEqual( + self.filterset({'app_label': ['dcim']}, self.queryset).qs.count(), + ObjectType.objects.filter(app_label='dcim').count(), + ) + + def test_model(self): + self.assertEqual( + self.filterset({'model': ['site']}, self.queryset).qs.count(), + ObjectType.objects.filter(model='site').count(), + ) + + def test_public(self): + self.assertEqual( + self.filterset({'public': True}, self.queryset).qs.count(), + ObjectType.objects.filter(public=True).count(), + ) + self.assertEqual( + self.filterset({'public': False}, self.queryset).qs.count(), + ObjectType.objects.filter(public=False).count(), + ) + + def test_feature(self): + self.assertEqual( + self.filterset({'features': 'tags'}, self.queryset).qs.count(), + ObjectType.objects.filter(features__contains=['tags']).count(), + ) diff --git a/netbox/core/tests/test_models.py b/netbox/core/tests/test_models.py index ff71c2e88..28225c7a6 100644 --- a/netbox/core/tests/test_models.py +++ b/netbox/core/tests/test_models.py @@ -1,7 +1,10 @@ +from django.contrib.contenttypes.models import ContentType +from django.core.exceptions import ObjectDoesNotExist from django.test import TestCase -from core.models import DataSource +from core.models import DataSource, ObjectType from core.choices import ObjectChangeActionChoices +from dcim.models import Site, Location, Device from netbox.constants import CENSOR_TOKEN, CENSOR_TOKEN_CHANGED @@ -120,3 +123,80 @@ class DataSourceChangeLoggingTestCase(TestCase): self.assertEqual(objectchange.prechange_data['parameters']['password'], CENSOR_TOKEN) self.assertEqual(objectchange.postchange_data['parameters']['username'], 'username2') self.assertEqual(objectchange.postchange_data['parameters']['password'], CENSOR_TOKEN) + + +class ObjectTypeTest(TestCase): + + def test_create(self): + """ + Test that an ObjectType created for a given app_label & model name will be automatically assigned to + the appropriate ContentType. + """ + kwargs = { + 'app_label': 'foo', + 'model': 'bar', + } + ct = ContentType.objects.create(**kwargs) + ot = ObjectType.objects.create(**kwargs) + self.assertEqual(ot.contenttype_ptr, ct) + + def test_get_by_natural_key(self): + """ + Test that get_by_natural_key() returns the appropriate ObjectType. + """ + self.assertEqual( + ObjectType.objects.get_by_natural_key('dcim', 'site'), + ObjectType.objects.get(app_label='dcim', model='site') + ) + with self.assertRaises(ObjectDoesNotExist): + ObjectType.objects.get_by_natural_key('foo', 'bar') + + def test_get_for_id(self): + """ + Test that get_by_id() returns the appropriate ObjectType. + """ + ot = ObjectType.objects.get_by_natural_key('dcim', 'site') + self.assertEqual( + ObjectType.objects.get_for_id(ot.pk), + ObjectType.objects.get(pk=ot.pk) + ) + with self.assertRaises(ObjectDoesNotExist): + ObjectType.objects.get_for_id(0) + + def test_get_for_model(self): + """ + Test that get_by_model() returns the appropriate ObjectType. + """ + self.assertEqual( + ObjectType.objects.get_for_model(Site), + ObjectType.objects.get_by_natural_key('dcim', 'site') + ) + + def test_get_for_models(self): + """ + Test that get_by_models() returns the appropriate ObjectType mapping. + """ + self.assertEqual( + ObjectType.objects.get_for_models(Site, Location, Device), + { + Site: ObjectType.objects.get_by_natural_key('dcim', 'site'), + Location: ObjectType.objects.get_by_natural_key('dcim', 'location'), + Device: ObjectType.objects.get_by_natural_key('dcim', 'device'), + } + ) + + def test_public(self): + """ + Test that public() returns only ObjectTypes for public models. + """ + public_ots = ObjectType.objects.public() + self.assertIn(ObjectType.objects.get_by_natural_key('dcim', 'site'), public_ots) + self.assertNotIn(ObjectType.objects.get_by_natural_key('extras', 'taggeditem'), public_ots) + + def test_with_feature(self): + """ + Test that with_feature() returns only ObjectTypes for models which support the specified feature. + """ + bookmarks_ots = ObjectType.objects.with_feature('bookmarks') + self.assertIn(ObjectType.objects.get_by_natural_key('dcim', 'site'), bookmarks_ots) + self.assertNotIn(ObjectType.objects.get_by_natural_key('dcim', 'cabletermination'), bookmarks_ots) diff --git a/netbox/core/tests/test_views.py b/netbox/core/tests/test_views.py index 96a4292df..1001243eb 100644 --- a/netbox/core/tests/test_views.py +++ b/netbox/core/tests/test_views.py @@ -1,3 +1,4 @@ +import json import urllib.parse import uuid from datetime import datetime @@ -366,6 +367,11 @@ class SystemTestCase(TestCase): # Test export response = self.client.get(f"{reverse('core:system')}?export=true") self.assertEqual(response.status_code, 200) + data = json.loads(response.content) + self.assertIn('netbox_release', data) + self.assertIn('plugins', data) + self.assertIn('config', data) + self.assertIn('objects', data) def test_system_view_with_config_revision(self): ConfigRevision.objects.create() diff --git a/netbox/core/views.py b/netbox/core/views.py index 8ae8e82d8..b18937308 100644 --- a/netbox/core/views.py +++ b/netbox/core/views.py @@ -1,7 +1,7 @@ import json import platform -from django import __version__ as DJANGO_VERSION +from django import __version__ as django_version from django.conf import settings from django.contrib import messages from django.contrib.auth.mixins import UserPassesTestMixin @@ -22,21 +22,23 @@ from rq.worker_registration import clean_worker_registry from core.utils import delete_rq_job, enqueue_rq_job, get_rq_jobs_from_status, requeue_rq_job, stop_rq_job from netbox.config import get_config, PARAMS -from netbox.registry import registry +from netbox.object_actions import AddObject, BulkDelete, BulkExport, DeleteObject +from netbox.plugins.utils import get_installed_plugins from netbox.views import generic from netbox.views.generic.base import BaseObjectView from netbox.views.generic.mixins import TableMixin +from utilities.apps import get_installed_apps from utilities.data import shallow_compare_dict from utilities.forms import ConfirmationForm from utilities.htmx import htmx_partial from utilities.json import ConfigJSONEncoder from utilities.query import count_related -from utilities.views import ContentTypePermissionRequiredMixin, GetRelatedModelsMixin, register_model_view +from utilities.views import ContentTypePermissionRequiredMixin, GetRelatedModelsMixin, ViewTab, register_model_view from . import filtersets, forms, tables from .jobs import SyncDataSourceJob from .models import * from .plugins import get_catalog_plugins, get_local_plugins -from .tables import CatalogPluginTable, PluginVersionTable +from .tables import CatalogPluginTable, JobLogEntryTable, PluginVersionTable # @@ -114,6 +116,11 @@ class DataSourceBulkEditView(generic.BulkEditView): form = forms.DataSourceBulkEditForm +@register_model_view(DataSource, 'bulk_rename', path='rename', detail=False) +class DataSourceBulkRenameView(generic.BulkRenameView): + queryset = DataSource.objects.all() + + @register_model_view(DataSource, 'bulk_delete', path='delete', detail=False) class DataSourceBulkDeleteView(generic.BulkDeleteView): queryset = DataSource.objects.annotate( @@ -133,14 +140,13 @@ class DataFileListView(generic.ObjectListView): filterset = filtersets.DataFileFilterSet filterset_form = forms.DataFileFilterForm table = tables.DataFileTable - actions = { - 'bulk_delete': {'delete'}, - } + actions = (BulkDelete,) @register_model_view(DataFile) class DataFileView(generic.ObjectView): queryset = DataFile.objects.all() + actions = (DeleteObject,) @register_model_view(DataFile, 'delete') @@ -165,15 +171,32 @@ class JobListView(generic.ObjectListView): filterset = filtersets.JobFilterSet filterset_form = forms.JobFilterForm table = tables.JobTable - actions = { - 'export': {'view'}, - 'bulk_delete': {'delete'}, - } + actions = (BulkExport, BulkDelete) @register_model_view(Job) class JobView(generic.ObjectView): queryset = Job.objects.all() + actions = (DeleteObject,) + + +@register_model_view(Job, 'log') +class JobLogView(generic.ObjectView): + queryset = Job.objects.all() + actions = (DeleteObject,) + template_name = 'core/job/log.html' + tab = ViewTab( + label=_('Log'), + badge=lambda obj: len(obj.log_entries), + weight=500, + ) + + def get_extra_context(self, request, instance): + table = JobLogEntryTable(instance.log_entries) + table.configure(request) + return { + 'table': table, + } @register_model_view(Job, 'delete') @@ -194,19 +217,23 @@ class JobBulkDeleteView(generic.BulkDeleteView): @register_model_view(ObjectChange, 'list', path='', detail=False) class ObjectChangeListView(generic.ObjectListView): - queryset = ObjectChange.objects.valid_models() + queryset = None filterset = filtersets.ObjectChangeFilterSet filterset_form = forms.ObjectChangeFilterForm table = tables.ObjectChangeTable template_name = 'core/objectchange_list.html' - actions = { - 'export': {'view'}, - } + actions = (BulkExport,) + + def get_queryset(self, request): + return ObjectChange.objects.valid_models() @register_model_view(ObjectChange) class ObjectChangeView(generic.ObjectView): - queryset = ObjectChange.objects.valid_models() + queryset = None + + def get_queryset(self, request): + return ObjectChange.objects.valid_models() def get_extra_context(self, request, instance): related_changes = ObjectChange.objects.valid_models().restrict(request.user, 'view').filter( @@ -269,6 +296,7 @@ class ConfigRevisionListView(generic.ObjectListView): filterset = filtersets.ConfigRevisionFilterSet filterset_form = forms.ConfigRevisionFilterForm table = tables.ConfigRevisionTable + actions = (AddObject, BulkExport) @register_model_view(ConfigRevision) @@ -525,7 +553,7 @@ class SystemView(UserPassesTestMixin, View): def get(self, request): - # System stats + # System status psql_version = db_name = db_size = None try: with connection.cursor() as cursor: @@ -540,7 +568,7 @@ class SystemView(UserPassesTestMixin, View): pass stats = { 'netbox_release': settings.RELEASE, - 'django_version': DJANGO_VERSION, + 'django_version': django_version, 'python_version': platform.python_version(), 'postgresql_version': psql_version, 'database_name': db_name, @@ -548,19 +576,35 @@ class SystemView(UserPassesTestMixin, View): 'rq_worker_count': Worker.count(get_connection('default')), } + # Django apps + django_apps = get_installed_apps() + # Configuration config = get_config() + # Plugins + plugins = get_installed_plugins() + + # Object counts + objects = {} + for ot in ObjectType.objects.public().order_by('app_label', 'model'): + if model := ot.model_class(): + objects[ot] = model.objects.count() + # Raw data export if 'export' in request.GET: stats['netbox_release'] = stats['netbox_release'].asdict() params = [param.name for param in PARAMS] data = { **stats, - 'plugins': registry['plugins']['installed'], + 'django_apps': django_apps, + 'plugins': plugins, 'config': { k: getattr(config, k) for k in sorted(params) }, + 'objects': { + f'{ot.app_label}.{ot.model}': count for ot, count in objects.items() + }, } response = HttpResponse(json.dumps(data, cls=ConfigJSONEncoder, indent=4), content_type='text/json') response['Content-Disposition'] = 'attachment; filename="netbox.json"' @@ -573,7 +617,10 @@ class SystemView(UserPassesTestMixin, View): return render(request, 'core/system.html', { 'stats': stats, + 'django_apps': django_apps, 'config': config, + 'plugins': plugins, + 'objects': objects, }) diff --git a/netbox/dcim/api/serializers_/devicetype_components.py b/netbox/dcim/api/serializers_/devicetype_components.py index 04f6395a6..8d4403d2d 100644 --- a/netbox/dcim/api/serializers_/devicetype_components.py +++ b/netbox/dcim/api/serializers_/devicetype_components.py @@ -9,7 +9,7 @@ from dcim.models import ( InventoryItemTemplate, ModuleBayTemplate, PowerOutletTemplate, PowerPortTemplate, RearPortTemplate, ) from netbox.api.fields import ChoiceField, ContentTypeField -from netbox.api.serializers import ValidatedModelSerializer +from netbox.api.serializers import ChangeLogMessageSerializer, ValidatedModelSerializer from utilities.api import get_serializer_for_model from wireless.choices import * from .devicetypes import DeviceTypeSerializer, ModuleTypeSerializer @@ -31,7 +31,11 @@ __all__ = ( ) -class ConsolePortTemplateSerializer(ValidatedModelSerializer): +class ComponentTemplateSerializer(ChangeLogMessageSerializer, ValidatedModelSerializer): + pass + + +class ConsolePortTemplateSerializer(ComponentTemplateSerializer): device_type = DeviceTypeSerializer( nested=True, required=False, @@ -59,7 +63,7 @@ class ConsolePortTemplateSerializer(ValidatedModelSerializer): brief_fields = ('id', 'url', 'display', 'name', 'description') -class ConsoleServerPortTemplateSerializer(ValidatedModelSerializer): +class ConsoleServerPortTemplateSerializer(ComponentTemplateSerializer): device_type = DeviceTypeSerializer( nested=True, required=False, @@ -87,7 +91,7 @@ class ConsoleServerPortTemplateSerializer(ValidatedModelSerializer): brief_fields = ('id', 'url', 'display', 'name', 'description') -class PowerPortTemplateSerializer(ValidatedModelSerializer): +class PowerPortTemplateSerializer(ComponentTemplateSerializer): device_type = DeviceTypeSerializer( nested=True, required=False, @@ -116,7 +120,7 @@ class PowerPortTemplateSerializer(ValidatedModelSerializer): brief_fields = ('id', 'url', 'display', 'name', 'description') -class PowerOutletTemplateSerializer(ValidatedModelSerializer): +class PowerOutletTemplateSerializer(ComponentTemplateSerializer): device_type = DeviceTypeSerializer( nested=True, required=False, @@ -156,7 +160,7 @@ class PowerOutletTemplateSerializer(ValidatedModelSerializer): brief_fields = ('id', 'url', 'display', 'name', 'description') -class InterfaceTemplateSerializer(ValidatedModelSerializer): +class InterfaceTemplateSerializer(ComponentTemplateSerializer): device_type = DeviceTypeSerializer( nested=True, required=False, @@ -202,7 +206,7 @@ class InterfaceTemplateSerializer(ValidatedModelSerializer): brief_fields = ('id', 'url', 'display', 'name', 'description') -class RearPortTemplateSerializer(ValidatedModelSerializer): +class RearPortTemplateSerializer(ComponentTemplateSerializer): device_type = DeviceTypeSerializer( required=False, nested=True, @@ -226,7 +230,7 @@ class RearPortTemplateSerializer(ValidatedModelSerializer): brief_fields = ('id', 'url', 'display', 'name', 'description') -class FrontPortTemplateSerializer(ValidatedModelSerializer): +class FrontPortTemplateSerializer(ComponentTemplateSerializer): device_type = DeviceTypeSerializer( nested=True, required=False, @@ -251,7 +255,7 @@ class FrontPortTemplateSerializer(ValidatedModelSerializer): brief_fields = ('id', 'url', 'display', 'name', 'description') -class ModuleBayTemplateSerializer(ValidatedModelSerializer): +class ModuleBayTemplateSerializer(ComponentTemplateSerializer): device_type = DeviceTypeSerializer( nested=True, required=False, @@ -274,7 +278,7 @@ class ModuleBayTemplateSerializer(ValidatedModelSerializer): brief_fields = ('id', 'url', 'display', 'name', 'description') -class DeviceBayTemplateSerializer(ValidatedModelSerializer): +class DeviceBayTemplateSerializer(ComponentTemplateSerializer): device_type = DeviceTypeSerializer( nested=True ) @@ -288,7 +292,7 @@ class DeviceBayTemplateSerializer(ValidatedModelSerializer): brief_fields = ('id', 'url', 'display', 'name', 'description') -class InventoryItemTemplateSerializer(ValidatedModelSerializer): +class InventoryItemTemplateSerializer(ComponentTemplateSerializer): device_type = DeviceTypeSerializer( nested=True ) diff --git a/netbox/dcim/api/serializers_/nested.py b/netbox/dcim/api/serializers_/nested.py index 0e9eaa52f..5b1be4d98 100644 --- a/netbox/dcim/api/serializers_/nested.py +++ b/netbox/dcim/api/serializers_/nested.py @@ -6,11 +6,13 @@ from dcim import models __all__ = ( 'NestedDeviceBaySerializer', + 'NestedDeviceRoleSerializer', 'NestedDeviceSerializer', 'NestedInterfaceSerializer', 'NestedInterfaceTemplateSerializer', 'NestedLocationSerializer', 'NestedModuleBaySerializer', + 'NestedPlatformSerializer', 'NestedRegionSerializer', 'NestedSiteGroupSerializer', ) @@ -102,3 +104,10 @@ class NestedModuleBaySerializer(WritableNestedSerializer): class Meta: model = models.ModuleBay fields = ['id', 'url', 'display_url', 'display', 'name'] + + +class NestedPlatformSerializer(WritableNestedSerializer): + + class Meta: + model = models.Platform + fields = ['id', 'url', 'display_url', 'display', 'name'] diff --git a/netbox/dcim/api/serializers_/platforms.py b/netbox/dcim/api/serializers_/platforms.py index 2f4745701..08f8a64a8 100644 --- a/netbox/dcim/api/serializers_/platforms.py +++ b/netbox/dcim/api/serializers_/platforms.py @@ -1,26 +1,32 @@ +from rest_framework import serializers + from dcim.models import Platform from extras.api.serializers_.configtemplates import ConfigTemplateSerializer -from netbox.api.fields import RelatedObjectCountField -from netbox.api.serializers import NetBoxModelSerializer +from netbox.api.serializers import NestedGroupModelSerializer from .manufacturers import ManufacturerSerializer +from .nested import NestedPlatformSerializer __all__ = ( 'PlatformSerializer', ) -class PlatformSerializer(NetBoxModelSerializer): +class PlatformSerializer(NestedGroupModelSerializer): + parent = NestedPlatformSerializer(required=False, allow_null=True, default=None) manufacturer = ManufacturerSerializer(nested=True, required=False, allow_null=True) config_template = ConfigTemplateSerializer(nested=True, required=False, allow_null=True, default=None) # Related object counts - device_count = RelatedObjectCountField('devices') - virtualmachine_count = RelatedObjectCountField('virtual_machines') + device_count = serializers.IntegerField(read_only=True, default=0) + virtualmachine_count = serializers.IntegerField(read_only=True, default=0) class Meta: model = Platform fields = [ - 'id', 'url', 'display_url', 'display', 'name', 'slug', 'manufacturer', 'config_template', 'description', - 'tags', 'custom_fields', 'created', 'last_updated', 'device_count', 'virtualmachine_count', + 'id', 'url', 'display_url', 'display', 'parent', 'name', 'slug', 'manufacturer', 'config_template', + 'description', 'comments', 'tags', 'custom_fields', 'created', 'last_updated', 'device_count', + 'virtualmachine_count', '_depth', ] - brief_fields = ('id', 'url', 'display', 'name', 'slug', 'description', 'device_count', 'virtualmachine_count') + brief_fields = ( + 'id', 'url', 'display', 'name', 'slug', 'description', 'device_count', 'virtualmachine_count', '_depth', + ) diff --git a/netbox/dcim/api/serializers_/racks.py b/netbox/dcim/api/serializers_/racks.py index 4bc2900dc..9c2c739fe 100644 --- a/netbox/dcim/api/serializers_/racks.py +++ b/netbox/dcim/api/serializers_/racks.py @@ -137,17 +137,29 @@ class RackSerializer(RackBaseSerializer): class RackReservationSerializer(NetBoxModelSerializer): - rack = RackSerializer(nested=True) - user = UserSerializer(nested=True) - tenant = TenantSerializer(nested=True, required=False, allow_null=True) + rack = RackSerializer( + nested=True, + ) + status = ChoiceField( + choices=RackReservationStatusChoices, + required=False, + ) + user = UserSerializer( + nested=True, + ) + tenant = TenantSerializer( + nested=True, + required=False, + allow_null=True, + ) class Meta: model = RackReservation fields = [ - 'id', 'url', 'display_url', 'display', 'rack', 'units', 'created', 'last_updated', 'user', 'tenant', - 'description', 'comments', 'tags', 'custom_fields', + 'id', 'url', 'display_url', 'display', 'rack', 'units', 'status', 'created', 'last_updated', 'user', + 'tenant', 'description', 'comments', 'tags', 'custom_fields', ] - brief_fields = ('id', 'url', 'display', 'user', 'description', 'units') + brief_fields = ('id', 'url', 'display', 'status', 'user', 'description', 'units') class RackElevationDetailFilterSerializer(serializers.Serializer): diff --git a/netbox/dcim/api/views.py b/netbox/dcim/api/views.py index eafe813a7..ffc0ca4d6 100644 --- a/netbox/dcim/api/views.py +++ b/netbox/dcim/api/views.py @@ -373,8 +373,20 @@ class DeviceRoleViewSet(NetBoxModelViewSet): # Platforms # -class PlatformViewSet(NetBoxModelViewSet): - queryset = Platform.objects.all() +class PlatformViewSet(MPTTLockedMixin, NetBoxModelViewSet): + queryset = Platform.objects.add_related_count( + Platform.objects.add_related_count( + Platform.objects.all(), + VirtualMachine, + 'platform', + 'virtualmachine_count', + cumulative=True + ), + Device, + 'platform', + 'device_count', + cumulative=True + ) serializer_class = serializers.PlatformSerializer filterset_class = filtersets.PlatformFilterSet diff --git a/netbox/dcim/choices.py b/netbox/dcim/choices.py index 846c2ec99..d44048d58 100644 --- a/netbox/dcim/choices.py +++ b/netbox/dcim/choices.py @@ -139,6 +139,24 @@ class RackAirflowChoices(ChoiceSet): ] +# +# Rack reservations +# + +class RackReservationStatusChoices(ChoiceSet): + key = 'RackReservation.status' + + STATUS_PENDING = 'pending' + STATUS_ACTIVE = 'active' + STATUS_STALE = 'stale' + + CHOICES = [ + (STATUS_PENDING, _('Pending'), 'cyan'), + (STATUS_ACTIVE, _('Active'), 'green'), + (STATUS_STALE, _('Stale'), 'orange'), + ] + + # # DeviceTypes # diff --git a/netbox/dcim/filtersets.py b/netbox/dcim/filtersets.py index 2a200ba3e..37a0d99a2 100644 --- a/netbox/dcim/filtersets.py +++ b/netbox/dcim/filtersets.py @@ -499,6 +499,10 @@ class RackReservationFilterSet(NetBoxModelFilterSet, TenancyFilterSet): to_field_name='slug', label=_('Location (slug)'), ) + status = django_filters.MultipleChoiceFilter( + choices=RackReservationStatusChoices, + null_value=None + ) user_id = django_filters.ModelMultipleChoiceFilter( queryset=User.objects.all(), label=_('User (ID)'), @@ -547,14 +551,17 @@ class DeviceTypeFilterSet(NetBoxModelFilterSet): to_field_name='slug', label=_('Manufacturer (slug)'), ) - default_platform_id = django_filters.ModelMultipleChoiceFilter( + default_platform_id = TreeNodeMultipleChoiceFilter( queryset=Platform.objects.all(), + field_name='default_platform', + lookup_expr='in', label=_('Default platform (ID)'), ) - default_platform = django_filters.ModelMultipleChoiceFilter( - field_name='default_platform__slug', + default_platform = TreeNodeMultipleChoiceFilter( queryset=Platform.objects.all(), + field_name='default_platform', to_field_name='slug', + lookup_expr='in', label=_('Default platform (slug)'), ) has_front_image = django_filters.BooleanFilter( @@ -979,6 +986,29 @@ class DeviceRoleFilterSet(OrganizationalModelFilterSet): class PlatformFilterSet(OrganizationalModelFilterSet): + parent_id = django_filters.ModelMultipleChoiceFilter( + queryset=Platform.objects.all(), + label=_('Immediate parent platform (ID)'), + ) + parent = django_filters.ModelMultipleChoiceFilter( + field_name='parent__slug', + queryset=Platform.objects.all(), + to_field_name='slug', + label=_('Immediate parent platform (slug)'), + ) + ancestor_id = TreeNodeMultipleChoiceFilter( + queryset=Platform.objects.all(), + field_name='parent', + lookup_expr='in', + label=_('Parent platform (ID)'), + ) + ancestor = TreeNodeMultipleChoiceFilter( + queryset=Platform.objects.all(), + field_name='parent', + lookup_expr='in', + to_field_name='slug', + label=_('Parent platform (slug)'), + ) manufacturer_id = django_filters.ModelMultipleChoiceFilter( field_name='manufacturer', queryset=Manufacturer.objects.all(), @@ -1058,14 +1088,17 @@ class DeviceFilterSet( queryset=Device.objects.all(), label=_('Parent Device (ID)'), ) - platform_id = django_filters.ModelMultipleChoiceFilter( + platform_id = TreeNodeMultipleChoiceFilter( queryset=Platform.objects.all(), + field_name='platform', + lookup_expr='in', label=_('Platform (ID)'), ) - platform = django_filters.ModelMultipleChoiceFilter( - field_name='platform__slug', + platform = TreeNodeMultipleChoiceFilter( + field_name='platform', queryset=Platform.objects.all(), to_field_name='slug', + lookup_expr='in', label=_('Platform (slug)'), ) region_id = TreeNodeMultipleChoiceFilter( diff --git a/netbox/dcim/forms/bulk_edit.py b/netbox/dcim/forms/bulk_edit.py index 9db7c250e..b7d9bcdb7 100644 --- a/netbox/dcim/forms/bulk_edit.py +++ b/netbox/dcim/forms/bulk_edit.py @@ -11,6 +11,7 @@ from ipam.choices import VLANQinQRoleChoices from ipam.models import ASN, VLAN, VLANGroup, VRF from netbox.choices import * from netbox.forms import NetBoxModelBulkEditForm +from netbox.forms.mixins import ChangelogMessageMixin from tenancy.models import Tenant from users.models import User from utilities.forms import BulkEditForm, add_blank_choice, form_from_model @@ -475,6 +476,12 @@ class RackBulkEditForm(NetBoxModelBulkEditForm): class RackReservationBulkEditForm(NetBoxModelBulkEditForm): + status = forms.ChoiceField( + label=_('Status'), + choices=add_blank_choice(RackReservationStatusChoices), + required=False, + initial='' + ) user = forms.ModelChoiceField( label=_('User'), queryset=User.objects.order_by('username'), @@ -494,7 +501,7 @@ class RackReservationBulkEditForm(NetBoxModelBulkEditForm): model = RackReservation fieldsets = ( - FieldSet('user', 'tenant', 'description'), + FieldSet('status', 'user', 'tenant', 'description'), ) nullable_fields = ('comments',) @@ -681,6 +688,11 @@ class DeviceRoleBulkEditForm(NetBoxModelBulkEditForm): class PlatformBulkEditForm(NetBoxModelBulkEditForm): + parent = DynamicModelChoiceField( + label=_('Parent'), + queryset=Platform.objects.all(), + required=False, + ) manufacturer = DynamicModelChoiceField( label=_('Manufacturer'), queryset=Manufacturer.objects.all(), @@ -696,12 +708,13 @@ class PlatformBulkEditForm(NetBoxModelBulkEditForm): max_length=200, required=False ) + comments = CommentField() model = Platform fieldsets = ( - FieldSet('manufacturer', 'config_template', 'description'), + FieldSet('parent', 'manufacturer', 'config_template', 'description'), ) - nullable_fields = ('manufacturer', 'config_template', 'description') + nullable_fields = ('parent', 'manufacturer', 'config_template', 'description', 'comments') class DeviceBulkEditForm(NetBoxModelBulkEditForm): @@ -1037,7 +1050,11 @@ class PowerFeedBulkEditForm(NetBoxModelBulkEditForm): # Device component templates # -class ConsolePortTemplateBulkEditForm(BulkEditForm): +class ComponentTemplateBulkEditForm(ChangelogMessageMixin, BulkEditForm): + pass + + +class ConsolePortTemplateBulkEditForm(ComponentTemplateBulkEditForm): pk = forms.ModelMultipleChoiceField( queryset=ConsolePortTemplate.objects.all(), widget=forms.MultipleHiddenInput() @@ -1056,7 +1073,7 @@ class ConsolePortTemplateBulkEditForm(BulkEditForm): nullable_fields = ('label', 'type', 'description') -class ConsoleServerPortTemplateBulkEditForm(BulkEditForm): +class ConsoleServerPortTemplateBulkEditForm(ComponentTemplateBulkEditForm): pk = forms.ModelMultipleChoiceField( queryset=ConsoleServerPortTemplate.objects.all(), widget=forms.MultipleHiddenInput() @@ -1079,7 +1096,7 @@ class ConsoleServerPortTemplateBulkEditForm(BulkEditForm): nullable_fields = ('label', 'type', 'description') -class PowerPortTemplateBulkEditForm(BulkEditForm): +class PowerPortTemplateBulkEditForm(ComponentTemplateBulkEditForm): pk = forms.ModelMultipleChoiceField( queryset=PowerPortTemplate.objects.all(), widget=forms.MultipleHiddenInput() @@ -1114,7 +1131,7 @@ class PowerPortTemplateBulkEditForm(BulkEditForm): nullable_fields = ('label', 'type', 'maximum_draw', 'allocated_draw', 'description') -class PowerOutletTemplateBulkEditForm(BulkEditForm): +class PowerOutletTemplateBulkEditForm(ComponentTemplateBulkEditForm): pk = forms.ModelMultipleChoiceField( queryset=PowerOutletTemplate.objects.all(), widget=forms.MultipleHiddenInput() @@ -1165,7 +1182,7 @@ class PowerOutletTemplateBulkEditForm(BulkEditForm): self.fields['power_port'].widget.attrs['disabled'] = True -class InterfaceTemplateBulkEditForm(BulkEditForm): +class InterfaceTemplateBulkEditForm(ComponentTemplateBulkEditForm): pk = forms.ModelMultipleChoiceField( queryset=InterfaceTemplate.objects.all(), widget=forms.MultipleHiddenInput() @@ -1216,7 +1233,7 @@ class InterfaceTemplateBulkEditForm(BulkEditForm): nullable_fields = ('label', 'description', 'poe_mode', 'poe_type', 'rf_role') -class FrontPortTemplateBulkEditForm(BulkEditForm): +class FrontPortTemplateBulkEditForm(ComponentTemplateBulkEditForm): pk = forms.ModelMultipleChoiceField( queryset=FrontPortTemplate.objects.all(), widget=forms.MultipleHiddenInput() @@ -1243,7 +1260,7 @@ class FrontPortTemplateBulkEditForm(BulkEditForm): nullable_fields = ('description',) -class RearPortTemplateBulkEditForm(BulkEditForm): +class RearPortTemplateBulkEditForm(ComponentTemplateBulkEditForm): pk = forms.ModelMultipleChoiceField( queryset=RearPortTemplate.objects.all(), widget=forms.MultipleHiddenInput() @@ -1270,7 +1287,7 @@ class RearPortTemplateBulkEditForm(BulkEditForm): nullable_fields = ('description',) -class ModuleBayTemplateBulkEditForm(BulkEditForm): +class ModuleBayTemplateBulkEditForm(ComponentTemplateBulkEditForm): pk = forms.ModelMultipleChoiceField( queryset=ModuleBayTemplate.objects.all(), widget=forms.MultipleHiddenInput() @@ -1288,7 +1305,7 @@ class ModuleBayTemplateBulkEditForm(BulkEditForm): nullable_fields = ('label', 'position', 'description') -class DeviceBayTemplateBulkEditForm(BulkEditForm): +class DeviceBayTemplateBulkEditForm(ComponentTemplateBulkEditForm): pk = forms.ModelMultipleChoiceField( queryset=DeviceBayTemplate.objects.all(), widget=forms.MultipleHiddenInput() @@ -1306,7 +1323,7 @@ class DeviceBayTemplateBulkEditForm(BulkEditForm): nullable_fields = ('label', 'description') -class InventoryItemTemplateBulkEditForm(BulkEditForm): +class InventoryItemTemplateBulkEditForm(ComponentTemplateBulkEditForm): pk = forms.ModelMultipleChoiceField( queryset=InventoryItemTemplate.objects.all(), widget=forms.MultipleHiddenInput() diff --git a/netbox/dcim/forms/bulk_import.py b/netbox/dcim/forms/bulk_import.py index fc33c2162..ce234130a 100644 --- a/netbox/dcim/forms/bulk_import.py +++ b/netbox/dcim/forms/bulk_import.py @@ -358,6 +358,11 @@ class RackReservationImportForm(NetBoxModelImportForm): required=True, help_text=_('Comma-separated list of individual unit numbers') ) + status = CSVChoiceField( + label=_('Status'), + choices=RackReservationStatusChoices, + help_text=_('Operational status') + ) tenant = CSVModelChoiceField( label=_('Tenant'), queryset=Tenant.objects.all(), @@ -368,7 +373,7 @@ class RackReservationImportForm(NetBoxModelImportForm): class Meta: model = RackReservation - fields = ('site', 'location', 'rack', 'units', 'tenant', 'description', 'comments', 'tags') + fields = ('site', 'location', 'rack', 'units', 'status', 'tenant', 'description', 'comments', 'tags') def __init__(self, data=None, *args, **kwargs): super().__init__(data, *args, **kwargs) @@ -504,6 +509,16 @@ class DeviceRoleImportForm(NetBoxModelImportForm): class PlatformImportForm(NetBoxModelImportForm): slug = SlugField() + parent = CSVModelChoiceField( + label=_('Parent'), + queryset=Platform.objects.all(), + required=False, + to_field_name='name', + help_text=_('Parent platform'), + error_messages={ + 'invalid_choice': _('Platform not found.'), + } + ) manufacturer = CSVModelChoiceField( label=_('Manufacturer'), queryset=Manufacturer.objects.all(), @@ -522,7 +537,7 @@ class PlatformImportForm(NetBoxModelImportForm): class Meta: model = Platform fields = ( - 'name', 'slug', 'manufacturer', 'config_template', 'description', 'tags', + 'name', 'slug', 'parent', 'manufacturer', 'config_template', 'description', 'tags', ) @@ -676,6 +691,12 @@ class DeviceImportForm(BaseDeviceImportForm): }) self.fields['rack'].queryset = self.fields['rack'].queryset.filter(**params) + # Limit platform queryset by manufacturer + params = {f"manufacturer__{self.fields['manufacturer'].to_field_name}": data.get('manufacturer')} + self.fields['platform'].queryset = self.fields['platform'].queryset.filter( + Q(**params) | Q(manufacturer=None) + ) + # Limit device bay queryset by parent device if parent := data.get('parent'): params = {f"device__{self.fields['parent'].to_field_name}": parent} diff --git a/netbox/dcim/forms/filtersets.py b/netbox/dcim/forms/filtersets.py index 813a578d6..daa3eef65 100644 --- a/netbox/dcim/forms/filtersets.py +++ b/netbox/dcim/forms/filtersets.py @@ -417,7 +417,7 @@ class RackReservationFilterForm(TenancyFilterForm, NetBoxModelFilterSetForm): model = RackReservation fieldsets = ( FieldSet('q', 'filter_id', 'tag'), - FieldSet('user_id', name=_('User')), + FieldSet('status', 'user_id', name=_('Reservation')), FieldSet('region_id', 'site_group_id', 'site_id', 'location_id', 'rack_id', name=_('Rack')), FieldSet('tenant_group_id', 'tenant_id', name=_('Tenant')), ) @@ -458,6 +458,11 @@ class RackReservationFilterForm(TenancyFilterForm, NetBoxModelFilterSetForm): }, label=_('Rack') ) + status = forms.MultipleChoiceField( + label=_('Status'), + choices=RackReservationStatusChoices, + required=False + ) user_id = DynamicModelMultipleChoiceField( queryset=User.objects.all(), required=False, @@ -714,6 +719,11 @@ class DeviceRoleFilterForm(NetBoxModelFilterSetForm): class PlatformFilterForm(NetBoxModelFilterSetForm): model = Platform selector_fields = ('filter_id', 'q', 'manufacturer_id') + parent_id = DynamicModelMultipleChoiceField( + queryset=Platform.objects.all(), + required=False, + label=_('Parent') + ) manufacturer_id = DynamicModelMultipleChoiceField( queryset=Manufacturer.objects.all(), required=False, @@ -1507,7 +1517,7 @@ class InterfaceFilterForm(PathEndpointFilterForm, DeviceComponentFilterForm): tx_power = forms.IntegerField( required=False, label=_('Transmit power (dBm)'), - min_value=0, + min_value=-40, max_value=127 ) vrf_id = DynamicModelMultipleChoiceField( diff --git a/netbox/dcim/forms/model_forms.py b/netbox/dcim/forms/model_forms.py index 00b7733f1..32ea2d263 100644 --- a/netbox/dcim/forms/model_forms.py +++ b/netbox/dcim/forms/model_forms.py @@ -11,6 +11,7 @@ from extras.models import ConfigTemplate from ipam.choices import VLANQinQRoleChoices from ipam.models import ASN, IPAddress, VLAN, VLANGroup, VLANTranslationPolicy, VRF from netbox.forms import NetBoxModelForm +from netbox.forms.mixins import ChangelogMessageMixin from tenancy.forms import TenancyForm from users.models import User from utilities.forms import add_blank_choice, get_field_value @@ -335,14 +336,14 @@ class RackReservationForm(TenancyForm, NetBoxModelForm): comments = CommentField() fieldsets = ( - FieldSet('rack', 'units', 'user', 'description', 'tags', name=_('Reservation')), + FieldSet('rack', 'units', 'status', 'user', 'description', 'tags', name=_('Reservation')), FieldSet('tenant_group', 'tenant', name=_('Tenancy')), ) class Meta: model = RackReservation fields = [ - 'rack', 'units', 'user', 'tenant_group', 'tenant', 'description', 'comments', 'tags', + 'rack', 'units', 'status', 'user', 'tenant_group', 'tenant', 'description', 'comments', 'tags', ] @@ -535,6 +536,11 @@ class DeviceRoleForm(NetBoxModelForm): class PlatformForm(NetBoxModelForm): + parent = DynamicModelChoiceField( + label=_('Parent'), + queryset=Platform.objects.all(), + required=False, + ) manufacturer = DynamicModelChoiceField( label=_('Manufacturer'), queryset=Manufacturer.objects.all(), @@ -550,15 +556,18 @@ class PlatformForm(NetBoxModelForm): label=_('Slug'), max_length=64 ) + comments = CommentField() fieldsets = ( - FieldSet('name', 'slug', 'manufacturer', 'config_template', 'description', 'tags', name=_('Platform')), + FieldSet( + 'name', 'slug', 'parent', 'manufacturer', 'config_template', 'description', 'tags', name=_('Platform'), + ), ) class Meta: model = Platform fields = [ - 'name', 'slug', 'manufacturer', 'config_template', 'description', 'tags', + 'name', 'slug', 'parent', 'manufacturer', 'config_template', 'description', 'comments', 'tags', ] @@ -973,7 +982,7 @@ class VCMemberSelectForm(forms.Form): # Device component templates # -class ComponentTemplateForm(forms.ModelForm): +class ComponentTemplateForm(ChangelogMessageMixin, forms.ModelForm): device_type = DynamicModelChoiceField( label=_('Device type'), queryset=DeviceType.objects.all(), diff --git a/netbox/dcim/forms/object_create.py b/netbox/dcim/forms/object_create.py index bcf91c547..5c9599eeb 100644 --- a/netbox/dcim/forms/object_create.py +++ b/netbox/dcim/forms/object_create.py @@ -426,6 +426,11 @@ class VirtualChassisCreateForm(NetBoxModelForm): help_text=_('Position of the first member device. Increases by one for each additional member.') ) + fieldsets = ( + FieldSet('name', 'domain', 'description', 'tags', name=_('Virtual Chassis')), + FieldSet('region', 'site_group', 'site', 'rack', 'members', 'initial_position', name=_('Member Devices')), + ) + class Meta: model = VirtualChassis fields = [ diff --git a/netbox/dcim/graphql/types.py b/netbox/dcim/graphql/types.py index 8b1755e35..0cd5e8fd1 100644 --- a/netbox/dcim/graphql/types.py +++ b/netbox/dcim/graphql/types.py @@ -633,6 +633,8 @@ class ModuleTypeType(NetBoxObjectType): pagination=True ) class PlatformType(OrganizationalObjectType): + parent: Annotated['PlatformType', strawberry.lazy('dcim.graphql.types')] | None + children: List[Annotated['PlatformType', strawberry.lazy('dcim.graphql.types')]] manufacturer: Annotated["ManufacturerType", strawberry.lazy('dcim.graphql.types')] | None config_template: Annotated["ConfigTemplateType", strawberry.lazy('extras.graphql.types')] | None diff --git a/netbox/dcim/migrations/0205_moduletypeprofile.py b/netbox/dcim/migrations/0205_moduletypeprofile.py index 25ab3415b..8e3ca9f74 100644 --- a/netbox/dcim/migrations/0205_moduletypeprofile.py +++ b/netbox/dcim/migrations/0205_moduletypeprofile.py @@ -3,6 +3,7 @@ import taggit.managers from django.db import migrations, models import utilities.json +import utilities.jsonschema class Migration(migrations.Migration): @@ -25,7 +26,7 @@ class Migration(migrations.Migration): ('description', models.CharField(blank=True, max_length=200)), ('comments', models.TextField(blank=True)), ('name', models.CharField(max_length=100, unique=True)), - ('schema', models.JSONField(blank=True, null=True)), + ('schema', models.JSONField(blank=True, null=True, validators=[utilities.jsonschema.validate_schema])), ('tags', taggit.managers.TaggableManager(through='extras.TaggedItem', to='extras.Tag')), ], options={ diff --git a/netbox/dcim/migrations/0211_platform_manufacturer_uniqueness.py b/netbox/dcim/migrations/0211_platform_manufacturer_uniqueness.py new file mode 100644 index 000000000..9a03c0b84 --- /dev/null +++ b/netbox/dcim/migrations/0211_platform_manufacturer_uniqueness.py @@ -0,0 +1,54 @@ +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('dcim', '0210_macaddress_ordering'), + ('extras', '0129_fix_script_paths'), + ] + + operations = [ + migrations.AlterField( + model_name='platform', + name='name', + field=models.CharField(max_length=100), + ), + migrations.AlterField( + model_name='platform', + name='slug', + field=models.SlugField(max_length=100), + ), + migrations.AddConstraint( + model_name='platform', + constraint=models.UniqueConstraint( + fields=('manufacturer', 'name'), + name='dcim_platform_manufacturer_name' + ), + ), + migrations.AddConstraint( + model_name='platform', + constraint=models.UniqueConstraint( + condition=models.Q(('manufacturer__isnull', True)), + fields=('name',), + name='dcim_platform_name', + violation_error_message='Platform name must be unique.' + ), + ), + migrations.AddConstraint( + model_name='platform', + constraint=models.UniqueConstraint( + fields=('manufacturer', 'slug'), + name='dcim_platform_manufacturer_slug' + ), + ), + migrations.AddConstraint( + model_name='platform', + constraint=models.UniqueConstraint( + condition=models.Q(('manufacturer__isnull', True)), + fields=('slug',), + name='dcim_platform_slug', + violation_error_message='Platform slug must be unique.' + ), + ), + ] diff --git a/netbox/dcim/migrations/0212_interface_tx_power_negative.py b/netbox/dcim/migrations/0212_interface_tx_power_negative.py new file mode 100644 index 000000000..4e49ad83c --- /dev/null +++ b/netbox/dcim/migrations/0212_interface_tx_power_negative.py @@ -0,0 +1,24 @@ +import django.core.validators +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('dcim', '0211_platform_manufacturer_uniqueness'), + ] + + operations = [ + migrations.AlterField( + model_name='interface', + name='tx_power', + field=models.SmallIntegerField( + blank=True, + null=True, + validators=[ + django.core.validators.MinValueValidator(-40), + django.core.validators.MaxValueValidator(127) + ] + ), + ), + ] diff --git a/netbox/dcim/migrations/0213_platform_parent.py b/netbox/dcim/migrations/0213_platform_parent.py new file mode 100644 index 000000000..1a1e0f228 --- /dev/null +++ b/netbox/dcim/migrations/0213_platform_parent.py @@ -0,0 +1,55 @@ +import django.db.models.deletion +import mptt.fields +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('dcim', '0212_interface_tx_power_negative'), + ] + + operations = [ + # Add parent & MPTT fields + migrations.AddField( + model_name='platform', + name='parent', + field=mptt.fields.TreeForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.CASCADE, + related_name='children', + to='dcim.platform' + ), + ), + migrations.AddField( + model_name='platform', + name='level', + field=models.PositiveIntegerField(default=0, editable=False), + preserve_default=False, + ), + migrations.AddField( + model_name='platform', + name='lft', + field=models.PositiveIntegerField(default=0, editable=False), + preserve_default=False, + ), + migrations.AddField( + model_name='platform', + name='rght', + field=models.PositiveIntegerField(default=0, editable=False), + preserve_default=False, + ), + migrations.AddField( + model_name='platform', + name='tree_id', + field=models.PositiveIntegerField(db_index=True, default=0, editable=False), + preserve_default=False, + ), + # Add comments field + migrations.AddField( + model_name='platform', + name='comments', + field=models.TextField(blank=True), + ), + ] diff --git a/netbox/dcim/migrations/0214_platform_rebuild.py b/netbox/dcim/migrations/0214_platform_rebuild.py new file mode 100644 index 000000000..786703c62 --- /dev/null +++ b/netbox/dcim/migrations/0214_platform_rebuild.py @@ -0,0 +1,29 @@ +from django.db import migrations +import mptt +import mptt.managers + + +def rebuild_mptt(apps, schema_editor): + """ + Construct the MPTT hierarchy. + """ + Platform = apps.get_model('dcim', 'Platform') + manager = mptt.managers.TreeManager() + manager.model = Platform + mptt.register(Platform) + manager.contribute_to_class(Platform, 'objects') + manager.rebuild() + + +class Migration(migrations.Migration): + + dependencies = [ + ('dcim', '0213_platform_parent'), + ] + + operations = [ + migrations.RunPython( + code=rebuild_mptt, + reverse_code=migrations.RunPython.noop + ), + ] diff --git a/netbox/dcim/migrations/0215_rackreservation_status.py b/netbox/dcim/migrations/0215_rackreservation_status.py new file mode 100644 index 000000000..6a762c4d0 --- /dev/null +++ b/netbox/dcim/migrations/0215_rackreservation_status.py @@ -0,0 +1,16 @@ +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('dcim', '0214_platform_rebuild'), + ] + + operations = [ + migrations.AddField( + model_name='rackreservation', + name='status', + field=models.CharField(default='active', max_length=50), + ), + ] diff --git a/netbox/dcim/models/cables.py b/netbox/dcim/models/cables.py index 26bb0fed7..69e07ed94 100644 --- a/netbox/dcim/models/cables.py +++ b/netbox/dcim/models/cables.py @@ -1,6 +1,7 @@ import itertools from django.contrib.contenttypes.fields import GenericForeignKey +from django.contrib.contenttypes.models import ContentType from django.core.exceptions import ValidationError from django.db import models from django.dispatch import Signal @@ -489,13 +490,13 @@ class CablePath(models.Model): def origin_type(self): if self.path: ct_id, _ = decompile_path_node(self.path[0][0]) - return ObjectType.objects.get_for_id(ct_id) + return ContentType.objects.get_for_id(ct_id) @property def destination_type(self): if self.is_complete: ct_id, _ = decompile_path_node(self.path[-1][0]) - return ObjectType.objects.get_for_id(ct_id) + return ContentType.objects.get_for_id(ct_id) @property def _path_decompiled(self): diff --git a/netbox/dcim/models/device_components.py b/netbox/dcim/models/device_components.py index 87680dd98..f1e460d77 100644 --- a/netbox/dcim/models/device_components.py +++ b/netbox/dcim/models/device_components.py @@ -750,10 +750,13 @@ class Interface(ModularComponentModel, BaseInterface, CabledObjectModel, PathEnd verbose_name=('channel width (MHz)'), help_text=_("Populated by selected channel (if set)") ) - tx_power = models.PositiveSmallIntegerField( + tx_power = models.SmallIntegerField( blank=True, null=True, - validators=(MaxValueValidator(127),), + validators=( + MinValueValidator(-40), + MaxValueValidator(127), + ), verbose_name=_('transmit power (dBm)') ) poe_mode = models.CharField( diff --git a/netbox/dcim/models/devices.py b/netbox/dcim/models/devices.py index e1ad9c15c..be93f33b9 100644 --- a/netbox/dcim/models/devices.py +++ b/netbox/dcim/models/devices.py @@ -4,6 +4,7 @@ import yaml from functools import cached_property from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation +from django.contrib.contenttypes.models import ContentType from django.core.exceptions import ValidationError from django.core.files.storage import default_storage from django.core.validators import MaxValueValidator, MinValueValidator @@ -15,7 +16,6 @@ from django.urls import reverse from django.utils.safestring import mark_safe from django.utils.translation import gettext_lazy as _ -from core.models import ObjectType from dcim.choices import * from dcim.constants import * from dcim.fields import MACAddressField @@ -425,7 +425,7 @@ class DeviceRole(NestedGroupModel): verbose_name_plural = _('device roles') -class Platform(OrganizationalModel): +class Platform(NestedGroupModel): """ Platform refers to the software or firmware running on a Device. For example, "Cisco IOS-XR" or "Juniper Junos". A Platform may optionally be associated with a particular Manufacturer. @@ -446,10 +446,34 @@ class Platform(OrganizationalModel): null=True ) + clone_fields = ('parent', 'description') + class Meta: ordering = ('name',) verbose_name = _('platform') verbose_name_plural = _('platforms') + constraints = ( + models.UniqueConstraint( + fields=('manufacturer', 'name'), + name='%(app_label)s_%(class)s_manufacturer_name', + ), + models.UniqueConstraint( + fields=('name',), + name='%(app_label)s_%(class)s_name', + condition=Q(manufacturer__isnull=True), + violation_error_message=_("Platform name must be unique.") + ), + models.UniqueConstraint( + fields=('manufacturer', 'slug'), + name='%(app_label)s_%(class)s_manufacturer_slug', + ), + models.UniqueConstraint( + fields=('slug',), + name='%(app_label)s_%(class)s_slug', + condition=Q(manufacturer__isnull=True), + violation_error_message=_("Platform slug must be unique.") + ), + ) class Device( @@ -1301,7 +1325,7 @@ class MACAddress(PrimaryModel): super().clean() if self._original_assigned_object_id and self._original_assigned_object_type_id: assigned_object = self.assigned_object - ct = ObjectType.objects.get_for_id(self._original_assigned_object_type_id) + ct = ContentType.objects.get_for_id(self._original_assigned_object_type_id) original_assigned_object = ct.get_object_for_this_type(pk=self._original_assigned_object_id) if ( diff --git a/netbox/dcim/models/modules.py b/netbox/dcim/models/modules.py index f60162fe9..4376f40aa 100644 --- a/netbox/dcim/models/modules.py +++ b/netbox/dcim/models/modules.py @@ -36,7 +36,8 @@ class ModuleTypeProfile(PrimaryModel): schema = models.JSONField( blank=True, null=True, - verbose_name=_('schema') + validators=[validate_schema], + verbose_name=_('schema'), ) clone_fields = ('schema',) @@ -49,18 +50,6 @@ class ModuleTypeProfile(PrimaryModel): def __str__(self): return self.name - def clean(self): - super().clean() - - # Validate the schema definition - if self.schema is not None: - try: - validate_schema(self.schema) - except ValidationError as e: - raise ValidationError({ - 'schema': e.message, - }) - class ModuleType(ImageAttachmentsMixin, PrimaryModel, WeightMixin): """ diff --git a/netbox/dcim/models/racks.py b/netbox/dcim/models/racks.py index b15cd8b34..02bce2019 100644 --- a/netbox/dcim/models/racks.py +++ b/netbox/dcim/models/racks.py @@ -673,6 +673,12 @@ class RackReservation(PrimaryModel): verbose_name=_('units'), base_field=models.PositiveSmallIntegerField() ) + status = models.CharField( + verbose_name=_('status'), + max_length=50, + choices=RackReservationStatusChoices, + default=RackReservationStatusChoices.STATUS_ACTIVE + ) tenant = models.ForeignKey( to='tenancy.Tenant', on_delete=models.PROTECT, @@ -733,6 +739,9 @@ class RackReservation(PrimaryModel): def unit_list(self): return array_to_string(self.units) + def get_status_color(self): + return RackReservationStatusChoices.colors.get(self.status) + def to_objectchange(self, action): objectchange = super().to_objectchange(action) objectchange.related_object = self.rack diff --git a/netbox/dcim/object_actions.py b/netbox/dcim/object_actions.py new file mode 100644 index 000000000..67cb188e8 --- /dev/null +++ b/netbox/dcim/object_actions.py @@ -0,0 +1,35 @@ +from django.utils.translation import gettext as _ + +from netbox.object_actions import ObjectAction + +__all__ = ( + 'BulkAddComponents', + 'BulkDisconnect', +) + + +class BulkAddComponents(ObjectAction): + """ + Add components to the selected devices. + """ + label = _('Add Components') + multi = True + permissions_required = {'change'} + template_name = 'dcim/buttons/bulk_add_components.html' + + @classmethod + def get_context(cls, context, obj): + return { + 'formaction': context.get('formaction'), + } + + +class BulkDisconnect(ObjectAction): + """ + Disconnect each of a set of objects to which a cable is connected. + """ + name = 'bulk_disconnect' + label = _('Disconnect Selected') + multi = True + permissions_required = {'change'} + template_name = 'dcim/buttons/bulk_disconnect.html' diff --git a/netbox/dcim/tables/devices.py b/netbox/dcim/tables/devices.py index f0465a1b5..8287e3666 100644 --- a/netbox/dcim/tables/devices.py +++ b/netbox/dcim/tables/devices.py @@ -103,10 +103,14 @@ class DeviceRoleTable(NetBoxTable): # class PlatformTable(NetBoxTable): - name = tables.Column( + name = columns.MPTTColumn( verbose_name=_('Name'), linkify=True ) + parent = tables.Column( + verbose_name=_('Parent'), + linkify=True, + ) manufacturer = tables.Column( verbose_name=_('Manufacturer'), linkify=True @@ -132,8 +136,8 @@ class PlatformTable(NetBoxTable): class Meta(NetBoxTable.Meta): model = models.Platform fields = ( - 'pk', 'id', 'name', 'manufacturer', 'device_count', 'vm_count', 'slug', 'config_template', 'description', - 'tags', 'actions', 'created', 'last_updated', + 'pk', 'id', 'name', 'parent', 'manufacturer', 'device_count', 'vm_count', 'slug', 'config_template', + 'description', 'tags', 'actions', 'created', 'last_updated', ) default_columns = ( 'pk', 'name', 'manufacturer', 'device_count', 'vm_count', 'description', diff --git a/netbox/dcim/tables/racks.py b/netbox/dcim/tables/racks.py index ee40056de..afb2c44c8 100644 --- a/netbox/dcim/tables/racks.py +++ b/netbox/dcim/tables/racks.py @@ -229,6 +229,9 @@ class RackReservationTable(TenancyColumnsMixin, NetBoxTable): orderable=False, verbose_name=_('Units') ) + status = columns.ChoiceFieldColumn( + verbose_name=_('Status'), + ) comments = columns.MarkdownColumn( verbose_name=_('Comments'), ) @@ -239,7 +242,7 @@ class RackReservationTable(TenancyColumnsMixin, NetBoxTable): class Meta(NetBoxTable.Meta): model = RackReservation fields = ( - 'pk', 'id', 'reservation', 'site', 'location', 'rack', 'unit_list', 'user', 'created', 'tenant', + 'pk', 'id', 'reservation', 'site', 'location', 'rack', 'unit_list', 'status', 'user', 'created', 'tenant', 'tenant_group', 'description', 'comments', 'tags', 'actions', 'created', 'last_updated', ) - default_columns = ('pk', 'reservation', 'site', 'rack', 'unit_list', 'user', 'description') + default_columns = ('pk', 'reservation', 'site', 'rack', 'unit_list', 'status', 'user', 'description') diff --git a/netbox/dcim/tests/test_api.py b/netbox/dcim/tests/test_api.py index 8af539b04..6a819a3c0 100644 --- a/netbox/dcim/tests/test_api.py +++ b/netbox/dcim/tests/test_api.py @@ -465,7 +465,7 @@ class RackTest(APIViewTestCases.APIViewTestCase): class RackReservationTest(APIViewTestCases.APIViewTestCase): model = RackReservation - brief_fields = ['description', 'display', 'id', 'units', 'url', 'user'] + brief_fields = ['description', 'display', 'id', 'status', 'units', 'url', 'user'] bulk_update_data = { 'description': 'New description', } @@ -483,9 +483,24 @@ class RackReservationTest(APIViewTestCases.APIViewTestCase): Rack.objects.bulk_create(racks) rack_reservations = ( - RackReservation(rack=racks[0], units=[1, 2, 3], user=user, description='Reservation #1'), - RackReservation(rack=racks[0], units=[4, 5, 6], user=user, description='Reservation #2'), - RackReservation(rack=racks[0], units=[7, 8, 9], user=user, description='Reservation #3'), + RackReservation( + rack=racks[0], + units=[1, 2, 3], + user=user, + description='Reservation #1', + ), + RackReservation( + rack=racks[0], + units=[4, 5, 6], + user=user, + description='Reservation #2' + ), + RackReservation( + rack=racks[0], + units=[7, 8, 9], + user=user, + description='Reservation #3', + ), ) RackReservation.objects.bulk_create(rack_reservations) @@ -493,18 +508,21 @@ class RackReservationTest(APIViewTestCases.APIViewTestCase): { 'rack': racks[1].pk, 'units': [10, 11, 12], + 'status': RackReservationStatusChoices.STATUS_ACTIVE, 'user': user.pk, 'description': 'Reservation #4', }, { 'rack': racks[1].pk, 'units': [13, 14, 15], + 'status': RackReservationStatusChoices.STATUS_PENDING, 'user': user.pk, 'description': 'Reservation #5', }, { 'rack': racks[1].pk, 'units': [16, 17, 18], + 'status': RackReservationStatusChoices.STATUS_STALE, 'user': user.pk, 'description': 'Reservation #6', }, @@ -1247,7 +1265,9 @@ class DeviceRoleTest(APIViewTestCases.APIViewTestCase): class PlatformTest(APIViewTestCases.APIViewTestCase): model = Platform - brief_fields = ['description', 'device_count', 'display', 'id', 'name', 'slug', 'url', 'virtualmachine_count'] + brief_fields = [ + '_depth', 'description', 'device_count', 'display', 'id', 'name', 'slug', 'url', 'virtualmachine_count', + ] create_data = [ { 'name': 'Platform 4', @@ -1274,7 +1294,8 @@ class PlatformTest(APIViewTestCases.APIViewTestCase): Platform(name='Platform 2', slug='platform-2'), Platform(name='Platform 3', slug='platform-3'), ) - Platform.objects.bulk_create(platforms) + for platform in platforms: + platform.save() class DeviceTest(APIViewTestCases.APIViewTestCase): diff --git a/netbox/dcim/tests/test_filtersets.py b/netbox/dcim/tests/test_filtersets.py index 0331de56c..c05d07ab0 100644 --- a/netbox/dcim/tests/test_filtersets.py +++ b/netbox/dcim/tests/test_filtersets.py @@ -1141,9 +1141,30 @@ class RackReservationTestCase(TestCase, ChangeLoggedFilterSetTests): Tenant.objects.bulk_create(tenants) reservations = ( - RackReservation(rack=racks[0], units=[1, 2, 3], user=users[0], tenant=tenants[0], description='foobar1'), - RackReservation(rack=racks[1], units=[4, 5, 6], user=users[1], tenant=tenants[1], description='foobar2'), - RackReservation(rack=racks[2], units=[7, 8, 9], user=users[2], tenant=tenants[2], description='foobar3'), + RackReservation( + rack=racks[0], + units=[1, 2, 3], + status=RackReservationStatusChoices.STATUS_ACTIVE, + user=users[0], + tenant=tenants[0], + description='foobar1', + ), + RackReservation( + rack=racks[1], + units=[4, 5, 6], + status=RackReservationStatusChoices.STATUS_PENDING, + user=users[1], + tenant=tenants[1], + description='foobar2', + ), + RackReservation( + rack=racks[2], + units=[7, 8, 9], + status=RackReservationStatusChoices.STATUS_STALE, + user=users[2], + tenant=tenants[2], + description='foobar3', + ), ) RackReservation.objects.bulk_create(reservations) @@ -1179,6 +1200,10 @@ class RackReservationTestCase(TestCase, ChangeLoggedFilterSetTests): params = {'location': [locations[0].slug, locations[1].slug]} self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) + def test_status(self): + params = {'status': [RackReservationStatusChoices.STATUS_ACTIVE, RackReservationStatusChoices.STATUS_PENDING]} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) + def test_user(self): users = User.objects.all()[:2] params = {'user_id': [users[0].pk, users[1].pk]} @@ -1256,7 +1281,8 @@ class DeviceTypeTestCase(TestCase, ChangeLoggedFilterSetTests): Platform(name='Platform 2', slug='platform-2', manufacturer=manufacturers[1]), Platform(name='Platform 3', slug='platform-3', manufacturer=manufacturers[2]), ) - Platform.objects.bulk_create(platforms) + for platform in platforms: + platform.save() device_types = ( DeviceType( @@ -2435,7 +2461,37 @@ class PlatformTestCase(TestCase, ChangeLoggedFilterSetTests): Platform(name='Platform 3', slug='platform-3', manufacturer=manufacturers[2], description='foobar3'), Platform(name='Platform 4', slug='platform-4'), ) - Platform.objects.bulk_create(platforms) + for platform in platforms: + platform.save() + child_platforms = ( + Platform(parent=platforms[0], name='Platform 1A', slug='platform-1a', manufacturer=manufacturers[0]), + Platform(parent=platforms[1], name='Platform 2A', slug='platform-2a', manufacturer=manufacturers[1]), + Platform(parent=platforms[2], name='Platform 3A', slug='platform-3a', manufacturer=manufacturers[2]), + ) + for platform in child_platforms: + platform.save() + grandchild_platforms = ( + Platform( + parent=child_platforms[0], + name='Platform 1A1', + slug='platform-1a1', + manufacturer=manufacturers[0], + ), + Platform( + parent=child_platforms[1], + name='Platform 2A1', + slug='platform-2a1', + manufacturer=manufacturers[1], + ), + Platform( + parent=child_platforms[2], + name='Platform 3A1', + slug='platform-3a1', + manufacturer=manufacturers[2], + ), + ) + for platform in grandchild_platforms: + platform.save() def test_q(self): params = {'q': 'foobar1'} @@ -2453,12 +2509,26 @@ class PlatformTestCase(TestCase, ChangeLoggedFilterSetTests): params = {'description': ['foobar1', 'foobar2']} self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) + def test_parent(self): + platforms = Platform.objects.filter(parent__isnull=True)[:2] + params = {'parent_id': [platforms[0].pk, platforms[1].pk]} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) + params = {'parent': [platforms[0].slug, platforms[1].slug]} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) + + def test_ancestor(self): + platforms = Platform.objects.filter(parent__isnull=True)[:2] + params = {'ancestor_id': [platforms[0].pk, platforms[1].pk]} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 4) + params = {'ancestor': [platforms[0].slug, platforms[1].slug]} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 4) + def test_manufacturer(self): manufacturers = Manufacturer.objects.all()[:2] params = {'manufacturer_id': [manufacturers[0].pk, manufacturers[1].pk]} - self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 6) params = {'manufacturer': [manufacturers[0].slug, manufacturers[1].slug]} - self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 6) def test_available_for_device_type(self): manufacturers = Manufacturer.objects.all()[:2] @@ -2469,7 +2539,7 @@ class PlatformTestCase(TestCase, ChangeLoggedFilterSetTests): u_height=1 ) params = {'available_for_device_type': device_type.pk} - self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 4) class DeviceTestCase(TestCase, ChangeLoggedFilterSetTests): @@ -2507,7 +2577,8 @@ class DeviceTestCase(TestCase, ChangeLoggedFilterSetTests): Platform(name='Platform 2', slug='platform-2'), Platform(name='Platform 3', slug='platform-3'), ) - Platform.objects.bulk_create(platforms) + for platform in platforms: + platform.save() regions = ( Region(name='Region 1', slug='region-1'), @@ -2763,7 +2834,7 @@ class DeviceTestCase(TestCase, ChangeLoggedFilterSetTests): params = {'device_type': [device_types[0].slug, device_types[1].slug]} self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) - def test_devicerole(self): + def test_role(self): roles = DeviceRole.objects.all()[:2] params = {'role_id': [roles[0].pk, roles[1].pk]} self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) diff --git a/netbox/dcim/tests/test_views.py b/netbox/dcim/tests/test_views.py index 5e41b37f7..b23f7e16d 100644 --- a/netbox/dcim/tests/test_views.py +++ b/netbox/dcim/tests/test_views.py @@ -337,6 +337,7 @@ class RackReservationTestCase(ViewTestCases.PrimaryObjectViewTestCase): cls.form_data = { 'rack': rack.pk, 'units': "10,11,12", + 'status': RackReservationStatusChoices.STATUS_PENDING, 'user': user3.pk, 'tenant': None, 'description': 'Rack reservation', @@ -344,10 +345,10 @@ class RackReservationTestCase(ViewTestCases.PrimaryObjectViewTestCase): } cls.csv_data = ( - 'site,location,rack,units,description', - 'Site 1,Location 1,Rack 1,"10,11,12",Reservation 1', - 'Site 1,Location 1,Rack 1,"13,14,15",Reservation 2', - 'Site 1,Location 1,Rack 1,"16,17,18",Reservation 3', + 'site,location,rack,units,status,description', + 'Site 1,Location 1,Rack 1,"10,11,12",active,Reservation 1', + 'Site 1,Location 1,Rack 1,"13,14,15",pending,Reservation 2', + 'Site 1,Location 1,Rack 1,"16,17,18",stale,Reservation 3', ) cls.csv_update_data = ( @@ -358,6 +359,7 @@ class RackReservationTestCase(ViewTestCases.PrimaryObjectViewTestCase): ) cls.bulk_edit_data = { + 'status': RackReservationStatusChoices.STATUS_STALE, 'user': user3.pk, 'tenant': None, 'description': 'New description', @@ -619,7 +621,8 @@ class DeviceTypeTestCase( Platform(name='Platform 1', slug='platform-1', manufacturer=manufacturers[0]), Platform(name='Platform 2', slug='platform-3', manufacturer=manufacturers[1]), ) - Platform.objects.bulk_create(platforms) + for platform in platforms: + platform.save() DeviceType.objects.bulk_create([ DeviceType(model='Device Type 1', slug='device-type-1', manufacturer=manufacturers[0]), @@ -1891,7 +1894,8 @@ class PlatformTestCase(ViewTestCases.OrganizationalObjectViewTestCase): Platform(name='Platform 2', slug='platform-2', manufacturer=manufacturer), Platform(name='Platform 3', slug='platform-3', manufacturer=manufacturer), ) - Platform.objects.bulk_create(platforms) + for platform in platforms: + platform.save() tags = create_tags('Alpha', 'Bravo', 'Charlie') @@ -1912,9 +1916,9 @@ class PlatformTestCase(ViewTestCases.OrganizationalObjectViewTestCase): cls.csv_update_data = ( "id,name,description", - f"{platforms[0].pk},Platform 7,Fourth platform7", - f"{platforms[1].pk},Platform 8,Fifth platform8", - f"{platforms[2].pk},Platform 9,Sixth platform9", + f"{platforms[0].pk},Foo,New description", + f"{platforms[1].pk},Bar,New description", + f"{platforms[2].pk},Baz,New description", ) cls.bulk_edit_data = { @@ -1962,7 +1966,8 @@ class DeviceTestCase(ViewTestCases.PrimaryObjectViewTestCase): Platform(name='Platform 1', slug='platform-1'), Platform(name='Platform 2', slug='platform-2'), ) - Platform.objects.bulk_create(platforms) + for platform in platforms: + platform.save() devices = ( Device( diff --git a/netbox/dcim/views.py b/netbox/dcim/views.py index c53426f1a..97ca99874 100644 --- a/netbox/dcim/views.py +++ b/netbox/dcim/views.py @@ -15,7 +15,7 @@ from circuits.models import Circuit, CircuitTermination from extras.views import ObjectConfigContextView, ObjectRenderConfigView from ipam.models import ASN, IPAddress, Prefix, VLANGroup, VLAN from ipam.tables import InterfaceVLANTable, VLANTranslationRuleTable -from netbox.constants import DEFAULT_ACTION_PERMISSIONS +from netbox.object_actions import * from netbox.views import generic from utilities.forms import ConfirmationForm from utilities.paginator import EnhancedPaginator, get_paginate_count @@ -34,6 +34,7 @@ from wireless.models import WirelessLAN from . import filtersets, forms, tables from .choices import DeviceFaceChoices, InterfaceModeChoices from .models import * +from .object_actions import BulkAddComponents, BulkDisconnect CABLE_TERMINATION_TYPES = { 'dcim.consoleport': ConsolePort, @@ -49,11 +50,6 @@ CABLE_TERMINATION_TYPES = { class DeviceComponentsView(generic.ObjectChildrenView): - actions = { - **DEFAULT_ACTION_PERMISSIONS, - 'bulk_rename': {'change'}, - 'bulk_disconnect': {'change'}, - } queryset = Device.objects.all() def get_children(self, request, parent): @@ -61,12 +57,8 @@ class DeviceComponentsView(generic.ObjectChildrenView): class DeviceTypeComponentsView(generic.ObjectChildrenView): - actions = { - **DEFAULT_ACTION_PERMISSIONS, - 'bulk_rename': {'change'}, - } + actions = (EditObject, DeleteObject, BulkEdit, BulkRename, BulkDelete) queryset = DeviceType.objects.all() - template_name = 'dcim/devicetype/component_templates.html' viewname = None # Used for return_url resolution def get_children(self, request, parent): @@ -78,9 +70,9 @@ class DeviceTypeComponentsView(generic.ObjectChildrenView): } -class ModuleTypeComponentsView(DeviceComponentsView): +class ModuleTypeComponentsView(generic.ObjectChildrenView): queryset = ModuleType.objects.all() - template_name = 'dcim/moduletype/component_templates.html' + actions = (EditObject, DeleteObject, BulkEdit, BulkRename, BulkDelete) viewname = None # Used for return_url resolution def get_children(self, request, parent): @@ -300,6 +292,11 @@ class RegionBulkEditView(generic.BulkEditView): form = forms.RegionBulkEditForm +@register_model_view(Region, 'bulk_rename', path='rename', detail=False) +class RegionBulkRenameView(generic.BulkRenameView): + queryset = Region.objects.all() + + @register_model_view(Region, 'bulk_delete', path='delete', detail=False) class RegionBulkDeleteView(generic.BulkDeleteView): queryset = Region.objects.add_related_count( @@ -426,6 +423,11 @@ class SiteGroupBulkEditView(generic.BulkEditView): form = forms.SiteGroupBulkEditForm +@register_model_view(SiteGroup, 'bulk_rename', path='rename', detail=False) +class SiteGroupBulkRenameView(generic.BulkRenameView): + queryset = SiteGroup.objects.all() + + @register_model_view(SiteGroup, 'bulk_delete', path='delete', detail=False) class SiteGroupBulkDeleteView(generic.BulkDeleteView): queryset = SiteGroup.objects.add_related_count( @@ -511,6 +513,11 @@ class SiteBulkEditView(generic.BulkEditView): form = forms.SiteBulkEditForm +@register_model_view(Site, 'bulk_rename', path='rename', detail=False) +class SiteBulkRenameView(generic.BulkRenameView): + queryset = Site.objects.all() + + @register_model_view(Site, 'bulk_delete', path='delete', detail=False) class SiteBulkDeleteView(generic.BulkDeleteView): queryset = Site.objects.all() @@ -615,6 +622,11 @@ class LocationBulkEditView(generic.BulkEditView): form = forms.LocationBulkEditForm +@register_model_view(Location, 'bulk_rename', path='rename', detail=False) +class LocationBulkRenameView(generic.BulkRenameView): + queryset = Location.objects.all() + + @register_model_view(Location, 'bulk_delete', path='delete', detail=False) class LocationBulkDeleteView(generic.BulkDeleteView): queryset = Location.objects.add_related_count( @@ -680,6 +692,11 @@ class RackRoleBulkEditView(generic.BulkEditView): form = forms.RackRoleBulkEditForm +@register_model_view(RackRole, 'bulk_rename', path='rename', detail=False) +class RackRoleBulkRenameView(generic.BulkRenameView): + queryset = RackRole.objects.all() + + @register_model_view(RackRole, 'bulk_delete', path='delete', detail=False) class RackRoleBulkDeleteView(generic.BulkDeleteView): queryset = RackRole.objects.annotate( @@ -739,6 +756,12 @@ class RackTypeBulkEditView(generic.BulkEditView): form = forms.RackTypeBulkEditForm +@register_model_view(RackType, 'bulk_rename', path='rename', detail=False) +class RackTypeBulkRenameView(generic.BulkRenameView): + queryset = RackType.objects.all() + field_name = 'model' + + @register_model_view(RackType, 'bulk_delete', path='delete', detail=False) class RackTypeBulkDeleteView(generic.BulkDeleteView): queryset = RackType.objects.all() @@ -918,6 +941,11 @@ class RackBulkEditView(generic.BulkEditView): form = forms.RackBulkEditForm +@register_model_view(Rack, 'bulk_rename', path='rename', detail=False) +class RackBulkRenameView(generic.BulkRenameView): + queryset = Rack.objects.all() + + @register_model_view(Rack, 'bulk_delete', path='delete', detail=False) class RackBulkDeleteView(generic.BulkDeleteView): queryset = Rack.objects.all() @@ -935,6 +963,7 @@ class RackReservationListView(generic.ObjectListView): filterset = filtersets.RackReservationFilterSet filterset_form = forms.RackReservationFilterForm table = tables.RackReservationTable + actions = (AddObject, BulkImport, BulkExport, BulkEdit, BulkDelete) @register_model_view(RackReservation) @@ -1051,6 +1080,11 @@ class ManufacturerBulkEditView(generic.BulkEditView): form = forms.ManufacturerBulkEditForm +@register_model_view(Manufacturer, 'bulk_rename', path='rename', detail=False) +class ManufacturerBulkRenameView(generic.BulkRenameView): + queryset = Manufacturer.objects.all() + + @register_model_view(Manufacturer, 'bulk_delete', path='delete', detail=False) class ManufacturerBulkDeleteView(generic.BulkDeleteView): queryset = Manufacturer.objects.annotate( @@ -1298,6 +1332,12 @@ class DeviceTypeBulkEditView(generic.BulkEditView): form = forms.DeviceTypeBulkEditForm +@register_model_view(DeviceType, 'bulk_rename', path='rename', detail=False) +class DeviceTypeBulkRenameView(generic.BulkRenameView): + queryset = DeviceType.objects.all() + field_name = 'model' + + @register_model_view(DeviceType, 'bulk_delete', path='delete', detail=False) class DeviceTypeBulkDeleteView(generic.BulkDeleteView): queryset = DeviceType.objects.annotate( @@ -1354,6 +1394,11 @@ class ModuleTypeProfileBulkEditView(generic.BulkEditView): form = forms.ModuleTypeProfileBulkEditForm +@register_model_view(ModuleTypeProfile, 'bulk_rename', path='rename', detail=False) +class ModuleTypeProfileBulkRenameView(generic.BulkRenameView): + queryset = ModuleTypeProfile.objects.all() + + @register_model_view(ModuleTypeProfile, 'bulk_delete', path='delete', detail=False) class ModuleTypeProfileBulkDeleteView(generic.BulkDeleteView): queryset = ModuleTypeProfile.objects.annotate( @@ -1564,6 +1609,11 @@ class ModuleTypeBulkEditView(generic.BulkEditView): form = forms.ModuleTypeBulkEditForm +@register_model_view(ModuleType, 'bulk_rename', path='rename', detail=False) +class ModuleTypeBulkRenameView(generic.BulkRenameView): + queryset = ModuleType.objects.all() + + @register_model_view(ModuleType, 'bulk_delete', path='delete', detail=False) class ModuleTypeBulkDeleteView(generic.BulkDeleteView): queryset = ModuleType.objects.annotate( @@ -2047,6 +2097,11 @@ class DeviceRoleBulkEditView(generic.BulkEditView): form = forms.DeviceRoleBulkEditForm +@register_model_view(DeviceRole, 'bulk_rename', path='rename', detail=False) +class DeviceRoleBulkRenameView(generic.BulkRenameView): + queryset = DeviceRole.objects.all() + + @register_model_view(DeviceRole, 'bulk_delete', path='delete', detail=False) class DeviceRoleBulkDeleteView(generic.BulkDeleteView): queryset = DeviceRole.objects.annotate( @@ -2063,9 +2118,18 @@ class DeviceRoleBulkDeleteView(generic.BulkDeleteView): @register_model_view(Platform, 'list', path='', detail=False) class PlatformListView(generic.ObjectListView): - queryset = Platform.objects.annotate( - device_count=count_related(Device, 'platform'), - vm_count=count_related(VirtualMachine, 'platform') + queryset = Platform.objects.add_related_count( + Platform.objects.add_related_count( + Platform.objects.all(), + VirtualMachine, + 'platform', + 'vm_count', + cumulative=True + ), + Device, + 'platform', + 'device_count', + cumulative=True ) table = tables.PlatformTable filterset = filtersets.PlatformFilterSet @@ -2108,6 +2172,11 @@ class PlatformBulkEditView(generic.BulkEditView): form = forms.PlatformBulkEditForm +@register_model_view(Platform, 'bulk_rename', path='rename', detail=False) +class PlatformBulkRenameView(generic.BulkRenameView): + queryset = Platform.objects.all() + + @register_model_view(Platform, 'bulk_delete', path='delete', detail=False) class PlatformBulkDeleteView(generic.BulkDeleteView): queryset = Platform.objects.all() @@ -2125,7 +2194,7 @@ class DeviceListView(generic.ObjectListView): filterset = filtersets.DeviceFilterSet filterset_form = forms.DeviceFilterForm table = tables.DeviceTable - template_name = 'dcim/device_list.html' + actions = (AddObject, BulkImport, BulkExport, BulkAddComponents, BulkEdit, BulkRename, BulkDelete) @register_model_view(Device) @@ -2166,7 +2235,7 @@ class DeviceConsolePortsView(DeviceComponentsView): table = tables.DeviceConsolePortTable filterset = filtersets.ConsolePortFilterSet filterset_form = forms.ConsolePortFilterForm - template_name = 'dcim/device/consoleports.html', + actions = (EditObject, DeleteObject, BulkEdit, BulkRename, BulkDisconnect, BulkDelete) tab = ViewTab( label=_('Console Ports'), badge=lambda obj: obj.console_port_count, @@ -2182,7 +2251,7 @@ class DeviceConsoleServerPortsView(DeviceComponentsView): table = tables.DeviceConsoleServerPortTable filterset = filtersets.ConsoleServerPortFilterSet filterset_form = forms.ConsoleServerPortFilterForm - template_name = 'dcim/device/consoleserverports.html' + actions = (EditObject, DeleteObject, BulkEdit, BulkRename, BulkDisconnect, BulkDelete) tab = ViewTab( label=_('Console Server Ports'), badge=lambda obj: obj.console_server_port_count, @@ -2198,7 +2267,7 @@ class DevicePowerPortsView(DeviceComponentsView): table = tables.DevicePowerPortTable filterset = filtersets.PowerPortFilterSet filterset_form = forms.PowerPortFilterForm - template_name = 'dcim/device/powerports.html' + actions = (EditObject, DeleteObject, BulkEdit, BulkRename, BulkDisconnect, BulkDelete) tab = ViewTab( label=_('Power Ports'), badge=lambda obj: obj.power_port_count, @@ -2214,7 +2283,7 @@ class DevicePowerOutletsView(DeviceComponentsView): table = tables.DevicePowerOutletTable filterset = filtersets.PowerOutletFilterSet filterset_form = forms.PowerOutletFilterForm - template_name = 'dcim/device/poweroutlets.html' + actions = (EditObject, DeleteObject, BulkEdit, BulkRename, BulkDisconnect, BulkDelete) tab = ViewTab( label=_('Power Outlets'), badge=lambda obj: obj.power_outlet_count, @@ -2230,6 +2299,7 @@ class DeviceInterfacesView(DeviceComponentsView): table = tables.DeviceInterfaceTable filterset = filtersets.InterfaceFilterSet filterset_form = forms.InterfaceFilterForm + actions = (EditObject, DeleteObject, BulkEdit, BulkRename, BulkDisconnect, BulkDelete) template_name = 'dcim/device/interfaces.html' tab = ViewTab( label=_('Interfaces'), @@ -2252,7 +2322,7 @@ class DeviceFrontPortsView(DeviceComponentsView): table = tables.DeviceFrontPortTable filterset = filtersets.FrontPortFilterSet filterset_form = forms.FrontPortFilterForm - template_name = 'dcim/device/frontports.html' + actions = (EditObject, DeleteObject, BulkEdit, BulkRename, BulkDisconnect, BulkDelete) tab = ViewTab( label=_('Front Ports'), badge=lambda obj: obj.front_port_count, @@ -2268,7 +2338,7 @@ class DeviceRearPortsView(DeviceComponentsView): table = tables.DeviceRearPortTable filterset = filtersets.RearPortFilterSet filterset_form = forms.RearPortFilterForm - template_name = 'dcim/device/rearports.html' + actions = (EditObject, DeleteObject, BulkEdit, BulkRename, BulkDisconnect, BulkDelete) tab = ViewTab( label=_('Rear Ports'), badge=lambda obj: obj.rear_port_count, @@ -2284,11 +2354,7 @@ class DeviceModuleBaysView(DeviceComponentsView): table = tables.DeviceModuleBayTable filterset = filtersets.ModuleBayFilterSet filterset_form = forms.ModuleBayFilterForm - template_name = 'dcim/device/modulebays.html' - actions = { - **DEFAULT_ACTION_PERMISSIONS, - 'bulk_rename': {'change'}, - } + actions = (EditObject, DeleteObject, BulkEdit, BulkRename, BulkDelete) tab = ViewTab( label=_('Module Bays'), badge=lambda obj: obj.module_bay_count, @@ -2304,11 +2370,7 @@ class DeviceDeviceBaysView(DeviceComponentsView): table = tables.DeviceDeviceBayTable filterset = filtersets.DeviceBayFilterSet filterset_form = forms.DeviceBayFilterForm - template_name = 'dcim/device/devicebays.html' - actions = { - **DEFAULT_ACTION_PERMISSIONS, - 'bulk_rename': {'change'}, - } + actions = (EditObject, DeleteObject, BulkEdit, BulkRename, BulkDelete) tab = ViewTab( label=_('Device Bays'), badge=lambda obj: obj.device_bay_count, @@ -2324,11 +2386,7 @@ class DeviceInventoryView(DeviceComponentsView): table = tables.DeviceInventoryItemTable filterset = filtersets.InventoryItemFilterSet filterset_form = forms.InventoryItemFilterForm - template_name = 'dcim/device/inventory.html' - actions = { - **DEFAULT_ACTION_PERMISSIONS, - 'bulk_rename': {'change'}, - } + actions = (EditObject, DeleteObject, BulkEdit, BulkRename, BulkDelete) tab = ViewTab( label=_('Inventory Items'), badge=lambda obj: obj.inventory_item_count, @@ -2402,16 +2460,16 @@ class DeviceBulkEditView(generic.BulkEditView): form = forms.DeviceBulkEditForm -@register_model_view(Device, 'bulk_delete', path='delete', detail=False) -class DeviceBulkDeleteView(generic.BulkDeleteView): - queryset = Device.objects.prefetch_related('device_type__manufacturer') +@register_model_view(Device, 'bulk_rename', path='rename', detail=False) +class DeviceBulkRenameView(generic.BulkRenameView): + queryset = Device.objects.all() filterset = filtersets.DeviceFilterSet table = tables.DeviceTable -@register_model_view(Device, 'bulk_rename', path='rename', detail=False) -class DeviceBulkRenameView(generic.BulkRenameView): - queryset = Device.objects.all() +@register_model_view(Device, 'bulk_delete', path='delete', detail=False) +class DeviceBulkDeleteView(generic.BulkDeleteView): + queryset = Device.objects.prefetch_related('device_type__manufacturer') filterset = filtersets.DeviceFilterSet table = tables.DeviceTable @@ -2426,6 +2484,7 @@ class ModuleListView(generic.ObjectListView): filterset = filtersets.ModuleFilterSet filterset_form = forms.ModuleFilterForm table = tables.ModuleTable + actions = (AddObject, BulkImport, BulkExport, BulkEdit, BulkDelete) @register_model_view(Module) @@ -2481,11 +2540,6 @@ class ConsolePortListView(generic.ObjectListView): filterset = filtersets.ConsolePortFilterSet filterset_form = forms.ConsolePortFilterForm table = tables.ConsolePortTable - template_name = 'dcim/component_list.html' - actions = { - **DEFAULT_ACTION_PERMISSIONS, - 'bulk_rename': {'change'}, - } @register_model_view(ConsolePort) @@ -2556,11 +2610,6 @@ class ConsoleServerPortListView(generic.ObjectListView): filterset = filtersets.ConsoleServerPortFilterSet filterset_form = forms.ConsoleServerPortFilterForm table = tables.ConsoleServerPortTable - template_name = 'dcim/component_list.html' - actions = { - **DEFAULT_ACTION_PERMISSIONS, - 'bulk_rename': {'change'}, - } @register_model_view(ConsoleServerPort) @@ -2631,11 +2680,6 @@ class PowerPortListView(generic.ObjectListView): filterset = filtersets.PowerPortFilterSet filterset_form = forms.PowerPortFilterForm table = tables.PowerPortTable - template_name = 'dcim/component_list.html' - actions = { - **DEFAULT_ACTION_PERMISSIONS, - 'bulk_rename': {'change'}, - } @register_model_view(PowerPort) @@ -2706,11 +2750,6 @@ class PowerOutletListView(generic.ObjectListView): filterset = filtersets.PowerOutletFilterSet filterset_form = forms.PowerOutletFilterForm table = tables.PowerOutletTable - template_name = 'dcim/component_list.html' - actions = { - **DEFAULT_ACTION_PERMISSIONS, - 'bulk_rename': {'change'}, - } @register_model_view(PowerOutlet) @@ -2781,11 +2820,6 @@ class InterfaceListView(generic.ObjectListView): filterset = filtersets.InterfaceFilterSet filterset_form = forms.InterfaceFilterForm table = tables.InterfaceTable - template_name = 'dcim/component_list.html' - actions = { - **DEFAULT_ACTION_PERMISSIONS, - 'bulk_rename': {'change'}, - } @register_model_view(Interface) @@ -2929,11 +2963,6 @@ class FrontPortListView(generic.ObjectListView): filterset = filtersets.FrontPortFilterSet filterset_form = forms.FrontPortFilterForm table = tables.FrontPortTable - template_name = 'dcim/component_list.html' - actions = { - **DEFAULT_ACTION_PERMISSIONS, - 'bulk_rename': {'change'}, - } @register_model_view(FrontPort) @@ -3004,11 +3033,6 @@ class RearPortListView(generic.ObjectListView): filterset = filtersets.RearPortFilterSet filterset_form = forms.RearPortFilterForm table = tables.RearPortTable - template_name = 'dcim/component_list.html' - actions = { - **DEFAULT_ACTION_PERMISSIONS, - 'bulk_rename': {'change'}, - } @register_model_view(RearPort) @@ -3079,11 +3103,6 @@ class ModuleBayListView(generic.ObjectListView): filterset = filtersets.ModuleBayFilterSet filterset_form = forms.ModuleBayFilterForm table = tables.ModuleBayTable - template_name = 'dcim/component_list.html' - actions = { - **DEFAULT_ACTION_PERMISSIONS, - 'bulk_rename': {'change'}, - } @register_model_view(ModuleBay) @@ -3145,11 +3164,6 @@ class DeviceBayListView(generic.ObjectListView): filterset = filtersets.DeviceBayFilterSet filterset_form = forms.DeviceBayFilterForm table = tables.DeviceBayTable - template_name = 'dcim/component_list.html' - actions = { - **DEFAULT_ACTION_PERMISSIONS, - 'bulk_rename': {'change'}, - } @register_model_view(DeviceBay) @@ -3292,11 +3306,6 @@ class InventoryItemListView(generic.ObjectListView): filterset = filtersets.InventoryItemFilterSet filterset_form = forms.InventoryItemFilterForm table = tables.InventoryItemTable - template_name = 'dcim/component_list.html' - actions = { - **DEFAULT_ACTION_PERMISSIONS, - 'bulk_rename': {'change'}, - } @register_model_view(InventoryItem) @@ -3419,6 +3428,11 @@ class InventoryItemRoleBulkEditView(generic.BulkEditView): form = forms.InventoryItemRoleBulkEditForm +@register_model_view(InventoryItemRole, 'bulk_rename', path='rename', detail=False) +class InventoryItemRoleBulkRenameView(generic.BulkRenameView): + queryset = InventoryItemRole.objects.all() + + @register_model_view(InventoryItemRole, 'bulk_delete', path='delete', detail=False) class InventoryItemRoleBulkDeleteView(generic.BulkDeleteView): queryset = InventoryItemRole.objects.annotate( @@ -3616,6 +3630,12 @@ class CableBulkEditView(generic.BulkEditView): form = forms.CableBulkEditForm +@register_model_view(Cable, 'bulk_rename', path='rename', detail=False) +class CableBulkRenameView(generic.BulkRenameView): + queryset = Cable.objects.all() + field_name = 'label' + + @register_model_view(Cable, 'bulk_delete', path='delete', detail=False) class CableBulkDeleteView(generic.BulkDeleteView): queryset = Cable.objects.prefetch_related( @@ -3636,9 +3656,7 @@ class ConsoleConnectionsListView(generic.ObjectListView): filterset_form = forms.ConsoleConnectionFilterForm table = tables.ConsoleConnectionTable template_name = 'dcim/connections_list.html' - actions = { - 'export': {'view'}, - } + actions = (BulkExport,) def get_extra_context(self, request): return { @@ -3652,9 +3670,7 @@ class PowerConnectionsListView(generic.ObjectListView): filterset_form = forms.PowerConnectionFilterForm table = tables.PowerConnectionTable template_name = 'dcim/connections_list.html' - actions = { - 'export': {'view'}, - } + actions = (BulkExport,) def get_extra_context(self, request): return { @@ -3668,9 +3684,7 @@ class InterfaceConnectionsListView(generic.ObjectListView): filterset_form = forms.InterfaceConnectionFilterForm table = tables.InterfaceConnectionTable template_name = 'dcim/connections_list.html' - actions = { - 'export': {'view'}, - } + actions = (BulkExport,) def get_extra_context(self, request): return { @@ -3706,7 +3720,6 @@ class VirtualChassisView(generic.ObjectView): class VirtualChassisCreateView(generic.ObjectEditView): queryset = VirtualChassis.objects.all() form = forms.VirtualChassisCreateForm - template_name = 'dcim/virtualchassis_add.html' @register_model_view(VirtualChassis, 'edit') @@ -3754,6 +3767,7 @@ class VirtualChassisEditView(ObjectPermissionRequiredMixin, GetReturnURLMixin, V formset = VCMemberFormSet(request.POST, queryset=members_queryset) if vc_form.is_valid() and formset.is_valid(): + virtual_chassis._changelog_message = vc_form.cleaned_data.pop('changelog_message', '') with transaction.atomic(using=router.db_for_write(Device)): @@ -3914,6 +3928,11 @@ class VirtualChassisBulkEditView(generic.BulkEditView): form = forms.VirtualChassisBulkEditForm +@register_model_view(VirtualChassis, 'bulk_rename', path='rename', detail=False) +class VirtualChassisBulkRenameView(generic.BulkRenameView): + queryset = VirtualChassis.objects.all() + + @register_model_view(VirtualChassis, 'bulk_delete', path='delete', detail=False) class VirtualChassisBulkDeleteView(generic.BulkDeleteView): queryset = VirtualChassis.objects.all() @@ -3971,6 +3990,11 @@ class PowerPanelBulkEditView(generic.BulkEditView): form = forms.PowerPanelBulkEditForm +@register_model_view(PowerPanel, 'bulk_rename', path='rename', detail=False) +class PowerPanelBulkRenameView(generic.BulkRenameView): + queryset = PowerPanel.objects.all() + + @register_model_view(PowerPanel, 'bulk_delete', path='delete', detail=False) class PowerPanelBulkDeleteView(generic.BulkDeleteView): queryset = PowerPanel.objects.annotate( @@ -4023,6 +4047,11 @@ class PowerFeedBulkEditView(generic.BulkEditView): form = forms.PowerFeedBulkEditForm +@register_model_view(PowerFeed, 'bulk_rename', path='rename', detail=False) +class PowerFeedBulkRenameView(generic.BulkRenameView): + queryset = PowerFeed.objects.all() + + @register_model_view(PowerFeed, 'bulk_disconnect', path='disconnect', detail=False) class PowerFeedBulkDisconnectView(BulkDisconnectView): queryset = PowerFeed.objects.all() @@ -4051,6 +4080,7 @@ class VirtualDeviceContextListView(generic.ObjectListView): filterset = filtersets.VirtualDeviceContextFilterSet filterset_form = forms.VirtualDeviceContextFilterForm table = tables.VirtualDeviceContextTable + actions = (AddObject, BulkImport, BulkEdit, BulkRename, BulkExport, BulkDelete) @register_model_view(VirtualDeviceContext) @@ -4095,6 +4125,11 @@ class VirtualDeviceContextBulkEditView(generic.BulkEditView): form = forms.VirtualDeviceContextBulkEditForm +@register_model_view(VirtualDeviceContext, 'bulk_rename', path='rename', detail=False) +class VirtualDeviceContextBulkRenameView(generic.BulkRenameView): + queryset = VirtualDeviceContext.objects.all() + + @register_model_view(VirtualDeviceContext, 'bulk_delete', path='delete', detail=False) class VirtualDeviceContextBulkDeleteView(generic.BulkDeleteView): queryset = VirtualDeviceContext.objects.all() @@ -4112,6 +4147,7 @@ class MACAddressListView(generic.ObjectListView): filterset = filtersets.MACAddressFilterSet filterset_form = forms.MACAddressFilterForm table = tables.MACAddressTable + actions = (AddObject, BulkImport, BulkExport, BulkEdit, BulkDelete) @register_model_view(MACAddress) diff --git a/netbox/extras/api/serializers.py b/netbox/extras/api/serializers.py index 07540c50d..eb8d050cd 100644 --- a/netbox/extras/api/serializers.py +++ b/netbox/extras/api/serializers.py @@ -1,4 +1,3 @@ -from .serializers_.objecttypes import * from .serializers_.attachments import * from .serializers_.bookmarks import * from .serializers_.customfields import * diff --git a/netbox/extras/api/serializers_/attachments.py b/netbox/extras/api/serializers_/attachments.py index fe0964eae..6507a12be 100644 --- a/netbox/extras/api/serializers_/attachments.py +++ b/netbox/extras/api/serializers_/attachments.py @@ -24,10 +24,10 @@ class ImageAttachmentSerializer(ValidatedModelSerializer): class Meta: model = ImageAttachment fields = [ - 'id', 'url', 'display', 'object_type', 'object_id', 'parent', 'name', 'image', + 'id', 'url', 'display', 'object_type', 'object_id', 'parent', 'name', 'image', 'description', 'image_height', 'image_width', 'created', 'last_updated', ] - brief_fields = ('id', 'url', 'display', 'name', 'image') + brief_fields = ('id', 'url', 'display', 'name', 'image', 'description') def validate(self, data): diff --git a/netbox/extras/api/serializers_/configcontexts.py b/netbox/extras/api/serializers_/configcontexts.py index 42a11ffcd..ff85f0fc6 100644 --- a/netbox/extras/api/serializers_/configcontexts.py +++ b/netbox/extras/api/serializers_/configcontexts.py @@ -6,20 +6,52 @@ from dcim.api.serializers_.platforms import PlatformSerializer from dcim.api.serializers_.roles import DeviceRoleSerializer from dcim.api.serializers_.sites import LocationSerializer, RegionSerializer, SiteSerializer, SiteGroupSerializer from dcim.models import DeviceRole, DeviceType, Location, Platform, Region, Site, SiteGroup -from extras.models import ConfigContext, Tag +from extras.models import ConfigContext, ConfigContextProfile, Tag from netbox.api.fields import SerializedPKRelatedField -from netbox.api.serializers import ValidatedModelSerializer +from netbox.api.serializers import ChangeLogMessageSerializer, ValidatedModelSerializer from tenancy.api.serializers_.tenants import TenantSerializer, TenantGroupSerializer from tenancy.models import Tenant, TenantGroup from virtualization.api.serializers_.clusters import ClusterSerializer, ClusterGroupSerializer, ClusterTypeSerializer from virtualization.models import Cluster, ClusterGroup, ClusterType __all__ = ( + 'ConfigContextProfileSerializer', 'ConfigContextSerializer', ) -class ConfigContextSerializer(ValidatedModelSerializer): +class ConfigContextProfileSerializer(ChangeLogMessageSerializer, ValidatedModelSerializer): + tags = serializers.SlugRelatedField( + queryset=Tag.objects.all(), + slug_field='slug', + required=False, + many=True + ) + data_source = DataSourceSerializer( + nested=True, + required=False + ) + data_file = DataFileSerializer( + nested=True, + read_only=True + ) + + class Meta: + model = ConfigContextProfile + fields = [ + 'id', 'url', 'display_url', 'display', 'name', 'description', 'schema', 'tags', 'comments', 'data_source', + 'data_path', 'data_file', 'data_synced', 'created', 'last_updated', + ] + brief_fields = ('id', 'url', 'display', 'name', 'description') + + +class ConfigContextSerializer(ChangeLogMessageSerializer, ValidatedModelSerializer): + profile = ConfigContextProfileSerializer( + nested=True, + required=False, + allow_null=True, + default=None, + ) regions = SerializedPKRelatedField( queryset=Region.objects.all(), serializer=RegionSerializer, @@ -122,9 +154,9 @@ class ConfigContextSerializer(ValidatedModelSerializer): class Meta: model = ConfigContext fields = [ - 'id', 'url', 'display_url', 'display', 'name', 'weight', 'description', 'is_active', 'regions', + 'id', 'url', 'display_url', 'display', 'name', 'weight', 'profile', 'description', 'is_active', 'regions', 'site_groups', 'sites', 'locations', 'device_types', 'roles', 'platforms', 'cluster_types', - 'cluster_groups', 'clusters', 'tenant_groups', 'tenants', 'tags', 'data_source', 'data_path', - 'data_file', 'data_synced', 'data', 'created', 'last_updated', + 'cluster_groups', 'clusters', 'tenant_groups', 'tenants', 'tags', 'data_source', 'data_path', 'data_file', + 'data_synced', 'data', 'created', 'last_updated', ] brief_fields = ('id', 'url', 'display', 'name', 'description') diff --git a/netbox/extras/api/serializers_/configtemplates.py b/netbox/extras/api/serializers_/configtemplates.py index 69652907e..244308535 100644 --- a/netbox/extras/api/serializers_/configtemplates.py +++ b/netbox/extras/api/serializers_/configtemplates.py @@ -1,6 +1,6 @@ from core.api.serializers_.data import DataFileSerializer, DataSourceSerializer from extras.models import ConfigTemplate -from netbox.api.serializers import ValidatedModelSerializer +from netbox.api.serializers import ChangeLogMessageSerializer, ValidatedModelSerializer from netbox.api.serializers.features import TaggableModelSerializer __all__ = ( @@ -8,7 +8,7 @@ __all__ = ( ) -class ConfigTemplateSerializer(TaggableModelSerializer, ValidatedModelSerializer): +class ConfigTemplateSerializer(ChangeLogMessageSerializer, TaggableModelSerializer, ValidatedModelSerializer): data_source = DataSourceSerializer( nested=True, required=False diff --git a/netbox/extras/api/serializers_/customfields.py b/netbox/extras/api/serializers_/customfields.py index a65fafc4e..f50f7a829 100644 --- a/netbox/extras/api/serializers_/customfields.py +++ b/netbox/extras/api/serializers_/customfields.py @@ -7,7 +7,7 @@ from core.models import ObjectType from extras.choices import * from extras.models import CustomField, CustomFieldChoiceSet from netbox.api.fields import ChoiceField, ContentTypeField -from netbox.api.serializers import ValidatedModelSerializer +from netbox.api.serializers import ChangeLogMessageSerializer, ValidatedModelSerializer __all__ = ( 'CustomFieldChoiceSetSerializer', @@ -15,7 +15,7 @@ __all__ = ( ) -class CustomFieldChoiceSetSerializer(ValidatedModelSerializer): +class CustomFieldChoiceSetSerializer(ChangeLogMessageSerializer, ValidatedModelSerializer): base_choices = ChoiceField( choices=CustomFieldChoiceSetBaseChoices, required=False @@ -36,7 +36,7 @@ class CustomFieldChoiceSetSerializer(ValidatedModelSerializer): brief_fields = ('id', 'url', 'display', 'name', 'description', 'choices_count') -class CustomFieldSerializer(ValidatedModelSerializer): +class CustomFieldSerializer(ChangeLogMessageSerializer, ValidatedModelSerializer): object_types = ContentTypeField( queryset=ObjectType.objects.with_feature('custom_fields'), many=True diff --git a/netbox/extras/api/serializers_/customlinks.py b/netbox/extras/api/serializers_/customlinks.py index 8cc4f5f77..951c3aded 100644 --- a/netbox/extras/api/serializers_/customlinks.py +++ b/netbox/extras/api/serializers_/customlinks.py @@ -1,14 +1,14 @@ from core.models import ObjectType from extras.models import CustomLink from netbox.api.fields import ContentTypeField -from netbox.api.serializers import ValidatedModelSerializer +from netbox.api.serializers import ChangeLogMessageSerializer, ValidatedModelSerializer __all__ = ( 'CustomLinkSerializer', ) -class CustomLinkSerializer(ValidatedModelSerializer): +class CustomLinkSerializer(ChangeLogMessageSerializer, ValidatedModelSerializer): object_types = ContentTypeField( queryset=ObjectType.objects.with_feature('custom_links'), many=True diff --git a/netbox/extras/api/serializers_/exporttemplates.py b/netbox/extras/api/serializers_/exporttemplates.py index 0d19d642c..0d3eed442 100644 --- a/netbox/extras/api/serializers_/exporttemplates.py +++ b/netbox/extras/api/serializers_/exporttemplates.py @@ -2,14 +2,14 @@ from core.api.serializers_.data import DataFileSerializer, DataSourceSerializer from core.models import ObjectType from extras.models import ExportTemplate from netbox.api.fields import ContentTypeField -from netbox.api.serializers import ValidatedModelSerializer +from netbox.api.serializers import ChangeLogMessageSerializer, ValidatedModelSerializer __all__ = ( 'ExportTemplateSerializer', ) -class ExportTemplateSerializer(ValidatedModelSerializer): +class ExportTemplateSerializer(ChangeLogMessageSerializer, ValidatedModelSerializer): object_types = ContentTypeField( queryset=ObjectType.objects.with_feature('export_templates'), many=True diff --git a/netbox/extras/api/serializers_/notifications.py b/netbox/extras/api/serializers_/notifications.py index 62e1a8d63..9f0c7cff3 100644 --- a/netbox/extras/api/serializers_/notifications.py +++ b/netbox/extras/api/serializers_/notifications.py @@ -4,7 +4,7 @@ from rest_framework import serializers from core.models import ObjectType from extras.models import Notification, NotificationGroup, Subscription from netbox.api.fields import ContentTypeField, SerializedPKRelatedField -from netbox.api.serializers import ValidatedModelSerializer +from netbox.api.serializers import ChangeLogMessageSerializer, ValidatedModelSerializer from users.api.serializers_.users import GroupSerializer, UserSerializer from users.models import Group, User from utilities.api import get_serializer_for_model @@ -37,7 +37,7 @@ class NotificationSerializer(ValidatedModelSerializer): return serializer(instance.object, nested=True, context=context).data -class NotificationGroupSerializer(ValidatedModelSerializer): +class NotificationGroupSerializer(ChangeLogMessageSerializer, ValidatedModelSerializer): groups = SerializedPKRelatedField( queryset=Group.objects.all(), serializer=GroupSerializer, diff --git a/netbox/extras/api/serializers_/objecttypes.py b/netbox/extras/api/serializers_/objecttypes.py deleted file mode 100644 index 8e4806652..000000000 --- a/netbox/extras/api/serializers_/objecttypes.py +++ /dev/null @@ -1,16 +0,0 @@ -from rest_framework import serializers - -from core.models import ObjectType -from netbox.api.serializers import BaseModelSerializer - -__all__ = ( - 'ObjectTypeSerializer', -) - - -class ObjectTypeSerializer(BaseModelSerializer): - url = serializers.HyperlinkedIdentityField(view_name='extras-api:objecttype-detail') - - class Meta: - model = ObjectType - fields = ['id', 'url', 'display', 'app_label', 'model'] diff --git a/netbox/extras/api/serializers_/savedfilters.py b/netbox/extras/api/serializers_/savedfilters.py index fb0744e59..e7128389c 100644 --- a/netbox/extras/api/serializers_/savedfilters.py +++ b/netbox/extras/api/serializers_/savedfilters.py @@ -1,14 +1,14 @@ from core.models import ObjectType from extras.models import SavedFilter from netbox.api.fields import ContentTypeField -from netbox.api.serializers import ValidatedModelSerializer +from netbox.api.serializers import ChangeLogMessageSerializer, ValidatedModelSerializer __all__ = ( 'SavedFilterSerializer', ) -class SavedFilterSerializer(ValidatedModelSerializer): +class SavedFilterSerializer(ChangeLogMessageSerializer, ValidatedModelSerializer): object_types = ContentTypeField( queryset=ObjectType.objects.all(), many=True diff --git a/netbox/extras/api/serializers_/tags.py b/netbox/extras/api/serializers_/tags.py index 5dc39584f..7567a4543 100644 --- a/netbox/extras/api/serializers_/tags.py +++ b/netbox/extras/api/serializers_/tags.py @@ -5,7 +5,7 @@ from core.models import ObjectType from extras.models import Tag, TaggedItem from netbox.api.exceptions import SerializerNotFound from netbox.api.fields import ContentTypeField, RelatedObjectCountField -from netbox.api.serializers import BaseModelSerializer, ValidatedModelSerializer +from netbox.api.serializers import BaseModelSerializer, ChangeLogMessageSerializer, ValidatedModelSerializer from utilities.api import get_serializer_for_model __all__ = ( @@ -14,7 +14,7 @@ __all__ = ( ) -class TagSerializer(ValidatedModelSerializer): +class TagSerializer(ChangeLogMessageSerializer, ValidatedModelSerializer): object_types = ContentTypeField( queryset=ObjectType.objects.with_feature('tags'), many=True, diff --git a/netbox/extras/api/urls.py b/netbox/extras/api/urls.py index 101808753..3757157b4 100644 --- a/netbox/extras/api/urls.py +++ b/netbox/extras/api/urls.py @@ -1,5 +1,6 @@ from django.urls import include, path +from core.api.views import ObjectTypeViewSet from netbox.api.routers import NetBoxRouter from . import views @@ -24,9 +25,12 @@ router.register('tagged-objects', views.TaggedItemViewSet) router.register('image-attachments', views.ImageAttachmentViewSet) router.register('journal-entries', views.JournalEntryViewSet) router.register('config-contexts', views.ConfigContextViewSet) +router.register('config-context-profiles', views.ConfigContextProfileViewSet) router.register('config-templates', views.ConfigTemplateViewSet) router.register('scripts', views.ScriptViewSet, basename='script') -router.register('object-types', views.ObjectTypeViewSet) + +# TODO: Remove in NetBox v4.5 +router.register('object-types', ObjectTypeViewSet) app_name = 'extras-api' urlpatterns = [ diff --git a/netbox/extras/api/views.py b/netbox/extras/api/views.py index facb1b17a..f333d5dbf 100644 --- a/netbox/extras/api/views.py +++ b/netbox/extras/api/views.py @@ -10,10 +10,9 @@ from rest_framework.mixins import ListModelMixin, RetrieveModelMixin from rest_framework.renderers import JSONRenderer from rest_framework.response import Response from rest_framework.routers import APIRootView -from rest_framework.viewsets import ModelViewSet, ReadOnlyModelViewSet +from rest_framework.viewsets import ModelViewSet from rq import Worker -from core.models import ObjectType from extras import filtersets from extras.jobs import ScriptJob from extras.models import * @@ -218,6 +217,12 @@ class JournalEntryViewSet(NetBoxModelViewSet): # Config contexts # +class ConfigContextProfileViewSet(SyncedDataMixin, NetBoxModelViewSet): + queryset = ConfigContextProfile.objects.all() + serializer_class = serializers.ConfigContextProfileSerializer + filterset_class = filtersets.ConfigContextProfileFilterSet + + class ConfigContextViewSet(SyncedDataMixin, NetBoxModelViewSet): queryset = ConfigContext.objects.all() serializer_class = serializers.ConfigContextSerializer @@ -316,20 +321,6 @@ class ScriptViewSet(ModelViewSet): return Response(input_serializer.errors, status=status.HTTP_400_BAD_REQUEST) -# -# Object types -# - -class ObjectTypeViewSet(ReadOnlyModelViewSet): - """ - Read-only list of ObjectTypes. - """ - permission_classes = [IsAuthenticatedOrLoginNotRequired] - queryset = ObjectType.objects.order_by('app_label', 'model') - serializer_class = serializers.ObjectTypeSerializer - filterset_class = filtersets.ObjectTypeFilterSet - - # # User dashboard # diff --git a/netbox/extras/dashboard/widgets.py b/netbox/extras/dashboard/widgets.py index c8805bd57..f88490ad2 100644 --- a/netbox/extras/dashboard/widgets.py +++ b/netbox/extras/dashboard/widgets.py @@ -11,7 +11,7 @@ from django.conf import settings from django.core.cache import cache from django.db.models import Model from django.template.loader import render_to_string -from django.urls import NoReverseMatch, resolve, reverse +from django.urls import NoReverseMatch, resolve from django.utils.translation import gettext as _ from core.models import ObjectType @@ -21,7 +21,7 @@ from utilities.permissions import get_permission_for_model from utilities.proxy import resolve_proxies from utilities.querydict import dict_to_querydict from utilities.templatetags.builtins.filters import render_markdown -from utilities.views import get_viewname +from utilities.views import get_action_url from .utils import register_widget __all__ = ( @@ -53,9 +53,9 @@ def object_list_widget_supports_model(model: Model) -> bool: """ def can_resolve_model_list_view(model: Model) -> bool: try: - reverse(get_viewname(model, action='list')) + get_action_url(model, action='list') return True - except Exception: + except NoReverseMatch: return False tests = [ @@ -206,7 +206,7 @@ class ObjectCountsWidget(DashboardWidget): permission = get_permission_for_model(model, 'view') if request.user.has_perm(permission): try: - url = reverse(get_viewname(model, 'list')) + url = get_action_url(model, action='list') except NoReverseMatch: url = None qs = model.objects.restrict(request.user, 'view') @@ -275,15 +275,13 @@ class ObjectListWidget(DashboardWidget): logger.debug(f"Dashboard Widget model_class not found: {app_label}:{model_name}") return - viewname = get_viewname(model, action='list') - # Evaluate user's permission. Note that this controls only whether the HTMX element is # embedded on the page: The view itself will also evaluate permissions separately. permission = get_permission_for_model(model, 'view') has_permission = request.user.has_perm(permission) try: - htmx_url = reverse(viewname) + htmx_url = get_action_url(model, action='list') except NoReverseMatch: htmx_url = None parameters = self.config.get('url_params') or {} @@ -297,7 +295,7 @@ class ObjectListWidget(DashboardWidget): except ValueError: pass return render_to_string(self.template_name, { - 'viewname': viewname, + 'model_name': model_name, 'has_permission': has_permission, 'htmx_url': htmx_url, }) diff --git a/netbox/extras/events.py b/netbox/extras/events.py index d7c642c4e..9dac4ce45 100644 --- a/netbox/extras/events.py +++ b/netbox/extras/events.py @@ -2,18 +2,19 @@ import logging from collections import defaultdict from django.conf import settings -from django.contrib.contenttypes.models import ContentType from django.utils import timezone from django.utils.module_loading import import_string from django.utils.translation import gettext as _ from django_rq import get_queue from core.events import * +from core.models import ObjectType from netbox.config import get_config from netbox.constants import RQ_QUEUE_DEFAULT -from netbox.registry import registry +from netbox.models.features import has_feature from users.models import User from utilities.api import get_serializer_for_model +from utilities.request import copy_safe_request from utilities.rqworker import get_rq_retry from utilities.serialization import serialize_object from .choices import EventRuleActionChoices @@ -50,16 +51,17 @@ def get_snapshots(instance, event_type): return snapshots -def enqueue_event(queue, instance, user, request_id, event_type): +def enqueue_event(queue, instance, request, event_type): """ Enqueue a serialized representation of a created/updated/deleted object for the processing of events once the request has completed. """ - # Determine whether this type of object supports event rules + # Bail if this type of object does not support event rules + if not has_feature(instance, 'event_rules'): + return + app_label = instance._meta.app_label model_name = instance._meta.model_name - if model_name not in registry['model_features']['event_rules'].get(app_label, []): - return assert instance.pk is not None key = f'{app_label}.{model_name}:{instance.pk}' @@ -71,17 +73,19 @@ def enqueue_event(queue, instance, user, request_id, event_type): queue[key]['event_type'] = event_type else: queue[key] = { - 'object_type': ContentType.objects.get_for_model(instance), + 'object_type': ObjectType.objects.get_for_model(instance), 'object_id': instance.pk, 'event_type': event_type, 'data': serialize_for_event(instance), 'snapshots': get_snapshots(instance, event_type), - 'username': user.username, - 'request_id': request_id + 'request': request, + # Legacy request attributes for backward compatibility + 'username': request.user.username, + 'request_id': request.id, } -def process_event_rules(event_rules, object_type, event_type, data, username=None, snapshots=None, request_id=None): +def process_event_rules(event_rules, object_type, event_type, data, username=None, snapshots=None, request=None): user = User.objects.get(username=username) if username else None for event_rule in event_rules: @@ -104,7 +108,7 @@ def process_event_rules(event_rules, object_type, event_type, data, username=Non # Compile the task parameters params = { "event_rule": event_rule, - "model_name": object_type.model, + "object_type": object_type, "event_type": event_type, "data": event_data, "snapshots": snapshots, @@ -114,8 +118,8 @@ def process_event_rules(event_rules, object_type, event_type, data, username=Non } if snapshots: params["snapshots"] = snapshots - if request_id: - params["request_id"] = request_id + if request: + params["request"] = copy_safe_request(request) # Enqueue the task rq_queue.enqueue( @@ -179,7 +183,7 @@ def process_event_queue(events): data=event['data'], username=event['username'], snapshots=event['snapshots'], - request_id=event['request_id'] + request=event['request'], ) diff --git a/netbox/extras/filtersets.py b/netbox/extras/filtersets.py index 6adad110d..f34b21370 100644 --- a/netbox/extras/filtersets.py +++ b/netbox/extras/filtersets.py @@ -19,6 +19,7 @@ from .models import * __all__ = ( 'BookmarkFilterSet', 'ConfigContextFilterSet', + 'ConfigContextProfileFilterSet', 'ConfigTemplateFilterSet', 'CustomFieldChoiceSetFilterSet', 'CustomFieldFilterSet', @@ -29,7 +30,6 @@ __all__ = ( 'JournalEntryFilterSet', 'LocalConfigContextFilterSet', 'NotificationGroupFilterSet', - 'ObjectTypeFilterSet', 'SavedFilterFilterSet', 'ScriptFilterSet', 'TableConfigFilterSet', @@ -452,12 +452,16 @@ class ImageAttachmentFilterSet(ChangeLoggedModelFilterSet): class Meta: model = ImageAttachment - fields = ('id', 'object_type_id', 'object_id', 'name', 'image_width', 'image_height') + fields = ('id', 'object_type_id', 'object_id', 'name', 'description', 'image_width', 'image_height') def search(self, queryset, name, value): if not value.strip(): return queryset - return queryset.filter(name__icontains=value) + return queryset.filter( + Q(name__icontains=value) | + Q(image__icontains=value) | + Q(description__icontains=value) + ) class JournalEntryFilterSet(NetBoxModelFilterSet): @@ -585,11 +589,51 @@ class TaggedItemFilterSet(BaseFilterSet): ) +class ConfigContextProfileFilterSet(NetBoxModelFilterSet): + q = django_filters.CharFilter( + method='search', + label=_('Search'), + ) + data_source_id = django_filters.ModelMultipleChoiceFilter( + queryset=DataSource.objects.all(), + label=_('Data source (ID)'), + ) + data_file_id = django_filters.ModelMultipleChoiceFilter( + queryset=DataSource.objects.all(), + label=_('Data file (ID)'), + ) + + class Meta: + model = ConfigContextProfile + fields = ( + 'id', 'name', 'description', 'auto_sync_enabled', 'data_synced', + ) + + def search(self, queryset, name, value): + if not value.strip(): + return queryset + return queryset.filter( + Q(name__icontains=value) | + Q(description__icontains=value) | + Q(comments__icontains=value) + ) + + class ConfigContextFilterSet(ChangeLoggedModelFilterSet): q = django_filters.CharFilter( method='search', label=_('Search'), ) + profile_id = django_filters.ModelMultipleChoiceFilter( + queryset=ConfigContextProfile.objects.all(), + label=_('Profile (ID)'), + ) + profile = django_filters.ModelMultipleChoiceFilter( + field_name='profile__name', + queryset=ConfigContextProfile.objects.all(), + to_field_name='name', + label=_('Profile (name)'), + ) region_id = django_filters.ModelMultipleChoiceFilter( field_name='regions', queryset=Region.objects.all(), @@ -788,26 +832,3 @@ class LocalConfigContextFilterSet(django_filters.FilterSet): def _local_context_data(self, queryset, name, value): return queryset.exclude(local_context_data__isnull=value) - - -# -# ContentTypes -# - -class ObjectTypeFilterSet(django_filters.FilterSet): - q = django_filters.CharFilter( - method='search', - label=_('Search'), - ) - - class Meta: - model = ObjectType - fields = ('id', 'app_label', 'model') - - def search(self, queryset, name, value): - if not value.strip(): - return queryset - return queryset.filter( - Q(app_label__icontains=value) | - Q(model__icontains=value) - ) diff --git a/netbox/extras/forms/bulk_edit.py b/netbox/extras/forms/bulk_edit.py index c854a6c81..c0a210e42 100644 --- a/netbox/extras/forms/bulk_edit.py +++ b/netbox/extras/forms/bulk_edit.py @@ -5,6 +5,7 @@ from extras.choices import * from extras.models import * from netbox.events import get_event_type_choices from netbox.forms import NetBoxModelBulkEditForm +from netbox.forms.mixins import ChangelogMessageMixin from utilities.forms import BulkEditForm, add_blank_choice from utilities.forms.fields import ColorField, CommentField, DynamicModelChoiceField from utilities.forms.rendering import FieldSet @@ -12,12 +13,14 @@ from utilities.forms.widgets import BulkEditNullBooleanSelect __all__ = ( 'ConfigContextBulkEditForm', + 'ConfigContextProfileBulkEditForm', 'ConfigTemplateBulkEditForm', 'CustomFieldBulkEditForm', 'CustomFieldChoiceSetBulkEditForm', 'CustomLinkBulkEditForm', 'EventRuleBulkEditForm', 'ExportTemplateBulkEditForm', + 'ImageAttachmentBulkEditForm', 'JournalEntryBulkEditForm', 'NotificationGroupBulkEditForm', 'SavedFilterBulkEditForm', @@ -27,7 +30,7 @@ __all__ = ( ) -class CustomFieldBulkEditForm(BulkEditForm): +class CustomFieldBulkEditForm(ChangelogMessageMixin, BulkEditForm): pk = forms.ModelMultipleChoiceField( queryset=CustomField.objects.all(), widget=forms.MultipleHiddenInput @@ -95,7 +98,7 @@ class CustomFieldBulkEditForm(BulkEditForm): nullable_fields = ('group_name', 'description', 'choice_set') -class CustomFieldChoiceSetBulkEditForm(BulkEditForm): +class CustomFieldChoiceSetBulkEditForm(ChangelogMessageMixin, BulkEditForm): pk = forms.ModelMultipleChoiceField( queryset=CustomFieldChoiceSet.objects.all(), widget=forms.MultipleHiddenInput @@ -115,7 +118,7 @@ class CustomFieldChoiceSetBulkEditForm(BulkEditForm): nullable_fields = ('base_choices', 'description') -class CustomLinkBulkEditForm(BulkEditForm): +class CustomLinkBulkEditForm(ChangelogMessageMixin, BulkEditForm): pk = forms.ModelMultipleChoiceField( queryset=CustomLink.objects.all(), widget=forms.MultipleHiddenInput @@ -141,7 +144,7 @@ class CustomLinkBulkEditForm(BulkEditForm): ) -class ExportTemplateBulkEditForm(BulkEditForm): +class ExportTemplateBulkEditForm(ChangelogMessageMixin, BulkEditForm): pk = forms.ModelMultipleChoiceField( queryset=ExportTemplate.objects.all(), widget=forms.MultipleHiddenInput @@ -174,7 +177,7 @@ class ExportTemplateBulkEditForm(BulkEditForm): nullable_fields = ('description', 'mime_type', 'file_name', 'file_extension') -class SavedFilterBulkEditForm(BulkEditForm): +class SavedFilterBulkEditForm(ChangelogMessageMixin, BulkEditForm): pk = forms.ModelMultipleChoiceField( queryset=SavedFilter.objects.all(), widget=forms.MultipleHiddenInput @@ -294,7 +297,7 @@ class EventRuleBulkEditForm(NetBoxModelBulkEditForm): nullable_fields = ('description', 'conditions') -class TagBulkEditForm(BulkEditForm): +class TagBulkEditForm(ChangelogMessageMixin, BulkEditForm): pk = forms.ModelMultipleChoiceField( queryset=Tag.objects.all(), widget=forms.MultipleHiddenInput @@ -316,7 +319,26 @@ class TagBulkEditForm(BulkEditForm): nullable_fields = ('description',) -class ConfigContextBulkEditForm(BulkEditForm): +class ConfigContextProfileBulkEditForm(NetBoxModelBulkEditForm): + pk = forms.ModelMultipleChoiceField( + queryset=ConfigContextProfile.objects.all(), + widget=forms.MultipleHiddenInput + ) + description = forms.CharField( + label=_('Description'), + required=False, + max_length=100 + ) + comments = CommentField() + + model = ConfigContextProfile + fieldsets = ( + FieldSet('description',), + ) + nullable_fields = ('description',) + + +class ConfigContextBulkEditForm(ChangelogMessageMixin, BulkEditForm): pk = forms.ModelMultipleChoiceField( queryset=ConfigContext.objects.all(), widget=forms.MultipleHiddenInput @@ -326,6 +348,10 @@ class ConfigContextBulkEditForm(BulkEditForm): required=False, min_value=0 ) + profile = DynamicModelChoiceField( + queryset=ConfigContextProfile.objects.all(), + required=False + ) is_active = forms.NullBooleanField( label=_('Is active'), required=False, @@ -337,10 +363,13 @@ class ConfigContextBulkEditForm(BulkEditForm): max_length=100 ) - nullable_fields = ('description',) + fieldsets = ( + FieldSet('weight', 'profile', 'is_active', 'description'), + ) + nullable_fields = ('profile', 'description') -class ConfigTemplateBulkEditForm(BulkEditForm): +class ConfigTemplateBulkEditForm(ChangelogMessageMixin, BulkEditForm): pk = forms.ModelMultipleChoiceField( queryset=ConfigTemplate.objects.all(), widget=forms.MultipleHiddenInput @@ -373,7 +402,19 @@ class ConfigTemplateBulkEditForm(BulkEditForm): nullable_fields = ('description', 'mime_type', 'file_name', 'file_extension') -class JournalEntryBulkEditForm(BulkEditForm): +class ImageAttachmentBulkEditForm(ChangelogMessageMixin, BulkEditForm): + pk = forms.ModelMultipleChoiceField( + queryset=ImageAttachment.objects.all(), + widget=forms.MultipleHiddenInput + ) + description = forms.CharField( + label=_('Description'), + max_length=200, + required=False + ) + + +class JournalEntryBulkEditForm(ChangelogMessageMixin, BulkEditForm): pk = forms.ModelMultipleChoiceField( queryset=JournalEntry.objects.all(), widget=forms.MultipleHiddenInput @@ -386,7 +427,7 @@ class JournalEntryBulkEditForm(BulkEditForm): comments = CommentField() -class NotificationGroupBulkEditForm(BulkEditForm): +class NotificationGroupBulkEditForm(ChangelogMessageMixin, BulkEditForm): pk = forms.ModelMultipleChoiceField( queryset=NotificationGroup.objects.all(), widget=forms.MultipleHiddenInput diff --git a/netbox/extras/forms/bulk_import.py b/netbox/extras/forms/bulk_import.py index cf15495ca..4f7c85e85 100644 --- a/netbox/extras/forms/bulk_import.py +++ b/netbox/extras/forms/bulk_import.py @@ -18,6 +18,7 @@ from utilities.forms.fields import ( ) __all__ = ( + 'ConfigContextProfileImportForm', 'ConfigTemplateImportForm', 'CustomFieldChoiceSetImportForm', 'CustomFieldImportForm', @@ -149,6 +150,15 @@ class ExportTemplateImportForm(CSVModelForm): ) +class ConfigContextProfileImportForm(NetBoxModelImportForm): + + class Meta: + model = ConfigContextProfile + fields = [ + 'name', 'description', 'schema', 'comments', 'tags', + ] + + class ConfigTemplateImportForm(CSVModelForm): class Meta: diff --git a/netbox/extras/forms/filtersets.py b/netbox/extras/forms/filtersets.py index 27881f17a..675315bed 100644 --- a/netbox/extras/forms/filtersets.py +++ b/netbox/extras/forms/filtersets.py @@ -20,6 +20,7 @@ from virtualization.models import Cluster, ClusterGroup, ClusterType __all__ = ( 'ConfigContextFilterForm', + 'ConfigContextProfileFilterForm', 'ConfigTemplateFilterForm', 'CustomFieldChoiceSetFilterForm', 'CustomFieldFilterForm', @@ -354,16 +355,43 @@ class TagFilterForm(SavedFiltersMixin, FilterForm): ) +class ConfigContextProfileFilterForm(SavedFiltersMixin, FilterForm): + model = ConfigContextProfile + fieldsets = ( + FieldSet('q', 'filter_id'), + FieldSet('data_source_id', 'data_file_id', name=_('Data')), + ) + data_source_id = DynamicModelMultipleChoiceField( + queryset=DataSource.objects.all(), + required=False, + label=_('Data source') + ) + data_file_id = DynamicModelMultipleChoiceField( + queryset=DataFile.objects.all(), + required=False, + label=_('Data file'), + query_params={ + 'source_id': '$data_source_id' + } + ) + + class ConfigContextFilterForm(SavedFiltersMixin, FilterForm): model = ConfigContext fieldsets = ( FieldSet('q', 'filter_id', 'tag_id'), + FieldSet('profile', name=_('Config Context')), FieldSet('data_source_id', 'data_file_id', name=_('Data')), FieldSet('region_id', 'site_group_id', 'site_id', 'location_id', name=_('Location')), FieldSet('device_type_id', 'platform_id', 'device_role_id', name=_('Device')), FieldSet('cluster_type_id', 'cluster_group_id', 'cluster_id', name=_('Cluster')), FieldSet('tenant_group_id', 'tenant_id', name=_('Tenant')) ) + profile_id = DynamicModelMultipleChoiceField( + queryset=ConfigContextProfile.objects.all(), + required=False, + label=_('Profile') + ) data_source_id = DynamicModelMultipleChoiceField( queryset=DataSource.objects.all(), required=False, diff --git a/netbox/extras/forms/model_forms.py b/netbox/extras/forms/model_forms.py index 5590dfa1a..37ee10604 100644 --- a/netbox/extras/forms/model_forms.py +++ b/netbox/extras/forms/model_forms.py @@ -13,6 +13,7 @@ from extras.choices import * from extras.models import * from netbox.events import get_event_type_choices from netbox.forms import NetBoxModelForm +from netbox.forms.mixins import ChangelogMessageMixin from tenancy.models import Tenant, TenantGroup from users.models import Group, User from utilities.forms import get_field_value @@ -28,6 +29,7 @@ from virtualization.models import Cluster, ClusterGroup, ClusterType __all__ = ( 'BookmarkForm', 'ConfigContextForm', + 'ConfigContextProfileForm', 'ConfigTemplateForm', 'CustomFieldChoiceSetForm', 'CustomFieldForm', @@ -45,7 +47,7 @@ __all__ = ( ) -class CustomFieldForm(forms.ModelForm): +class CustomFieldForm(ChangelogMessageMixin, forms.ModelForm): object_types = ContentTypeMultipleChoiceField( label=_('Object types'), queryset=ObjectType.objects.with_feature('custom_fields'), @@ -164,7 +166,7 @@ class CustomFieldForm(forms.ModelForm): del self.fields['choice_set'] -class CustomFieldChoiceSetForm(forms.ModelForm): +class CustomFieldChoiceSetForm(ChangelogMessageMixin, forms.ModelForm): # TODO: The extra_choices field definition diverge from the CustomFieldChoiceSet model extra_choices = forms.CharField( widget=ChoicesWidget(), @@ -217,7 +219,7 @@ class CustomFieldChoiceSetForm(forms.ModelForm): return data -class CustomLinkForm(forms.ModelForm): +class CustomLinkForm(ChangelogMessageMixin, forms.ModelForm): object_types = ContentTypeMultipleChoiceField( label=_('Object types'), queryset=ObjectType.objects.with_feature('custom_links') @@ -249,7 +251,7 @@ class CustomLinkForm(forms.ModelForm): } -class ExportTemplateForm(SyncedDataMixin, forms.ModelForm): +class ExportTemplateForm(ChangelogMessageMixin, SyncedDataMixin, forms.ModelForm): object_types = ContentTypeMultipleChoiceField( label=_('Object types'), queryset=ObjectType.objects.with_feature('export_templates') @@ -291,7 +293,7 @@ class ExportTemplateForm(SyncedDataMixin, forms.ModelForm): return self.cleaned_data -class SavedFilterForm(forms.ModelForm): +class SavedFilterForm(ChangelogMessageMixin, forms.ModelForm): slug = SlugField() object_types = ContentTypeMultipleChoiceField( label=_('Object types'), @@ -388,7 +390,7 @@ class BookmarkForm(forms.ModelForm): fields = ('object_type', 'object_id') -class NotificationGroupForm(forms.ModelForm): +class NotificationGroupForm(ChangelogMessageMixin, forms.ModelForm): groups = DynamicModelMultipleChoiceField( label=_('Groups'), required=False, @@ -561,7 +563,7 @@ class EventRuleForm(NetBoxModelForm): return self.cleaned_data -class TagForm(forms.ModelForm): +class TagForm(ChangelogMessageMixin, forms.ModelForm): slug = SlugField() object_types = ContentTypeMultipleChoiceField( label=_('Object types'), @@ -584,7 +586,36 @@ class TagForm(forms.ModelForm): ] -class ConfigContextForm(SyncedDataMixin, forms.ModelForm): +class ConfigContextProfileForm(SyncedDataMixin, NetBoxModelForm): + schema = JSONField( + label=_('Schema'), + required=False, + help_text=_("Enter a valid JSON schema to define supported attributes.") + ) + tags = DynamicModelMultipleChoiceField( + label=_('Tags'), + queryset=Tag.objects.all(), + required=False + ) + + fieldsets = ( + FieldSet('name', 'description', 'schema', 'tags', name=_('Config Context Profile')), + FieldSet('data_source', 'data_file', 'auto_sync_enabled', name=_('Data Source')), + ) + + class Meta: + model = ConfigContextProfile + fields = ( + 'name', 'description', 'schema', 'data_source', 'data_file', 'auto_sync_enabled', 'comments', 'tags', + ) + + +class ConfigContextForm(ChangelogMessageMixin, SyncedDataMixin, forms.ModelForm): + profile = DynamicModelChoiceField( + label=_('Profile'), + queryset=ConfigContextProfile.objects.all(), + required=False + ) regions = DynamicModelMultipleChoiceField( label=_('Regions'), queryset=Region.objects.all(), @@ -656,7 +687,7 @@ class ConfigContextForm(SyncedDataMixin, forms.ModelForm): ) fieldsets = ( - FieldSet('name', 'weight', 'description', 'data', 'is_active', name=_('Config Context')), + FieldSet('name', 'weight', 'profile', 'description', 'data', 'is_active', name=_('Config Context')), FieldSet('data_source', 'data_file', 'auto_sync_enabled', name=_('Data Source')), FieldSet( 'regions', 'site_groups', 'sites', 'locations', 'device_types', 'roles', 'platforms', 'cluster_types', @@ -668,9 +699,9 @@ class ConfigContextForm(SyncedDataMixin, forms.ModelForm): class Meta: model = ConfigContext fields = ( - 'name', 'weight', 'description', 'data', 'is_active', 'regions', 'site_groups', 'sites', 'locations', - 'roles', 'device_types', 'platforms', 'cluster_types', 'cluster_groups', 'clusters', 'tenant_groups', - 'tenants', 'tags', 'data_source', 'data_file', 'auto_sync_enabled', + 'name', 'weight', 'profile', 'description', 'data', 'is_active', 'regions', 'site_groups', 'sites', + 'locations', 'roles', 'device_types', 'platforms', 'cluster_types', 'cluster_groups', 'clusters', + 'tenant_groups', 'tenants', 'tags', 'data_source', 'data_file', 'auto_sync_enabled', ) def __init__(self, *args, initial=None, **kwargs): @@ -696,7 +727,7 @@ class ConfigContextForm(SyncedDataMixin, forms.ModelForm): return self.cleaned_data -class ConfigTemplateForm(SyncedDataMixin, forms.ModelForm): +class ConfigTemplateForm(ChangelogMessageMixin, SyncedDataMixin, forms.ModelForm): tags = DynamicModelMultipleChoiceField( label=_('Tags'), queryset=Tag.objects.all(), @@ -744,14 +775,17 @@ class ConfigTemplateForm(SyncedDataMixin, forms.ModelForm): class ImageAttachmentForm(forms.ModelForm): fieldsets = ( - FieldSet(ObjectAttribute('parent'), 'name', 'image'), + FieldSet(ObjectAttribute('parent'), 'image', 'name', 'description'), ) class Meta: model = ImageAttachment fields = [ - 'name', 'image', + 'image', 'name', 'description', ] + help_texts = { + 'name': _("If no name is specified, the file name will be used.") + } class JournalEntryForm(NetBoxModelForm): diff --git a/netbox/extras/graphql/filters.py b/netbox/extras/graphql/filters.py index 1712b7056..dda9d947b 100644 --- a/netbox/extras/graphql/filters.py +++ b/netbox/extras/graphql/filters.py @@ -8,7 +8,7 @@ from strawberry_django import FilterLookup from core.graphql.filter_mixins import BaseObjectTypeFilterMixin, ChangeLogFilterMixin from extras import models from extras.graphql.filter_mixins import TagBaseFilterMixin, CustomFieldsFilterMixin, TagsFilterMixin -from netbox.graphql.filter_mixins import SyncedDataFilterMixin +from netbox.graphql.filter_mixins import PrimaryModelFilterMixin, SyncedDataFilterMixin if TYPE_CHECKING: from core.graphql.filters import ContentTypeFilter @@ -24,6 +24,7 @@ if TYPE_CHECKING: __all__ = ( 'ConfigContextFilter', + 'ConfigContextProfileFilter', 'ConfigTemplateFilter', 'CustomFieldFilter', 'CustomFieldChoiceSetFilter', @@ -97,6 +98,13 @@ class ConfigContextFilter(BaseObjectTypeFilterMixin, SyncedDataFilterMixin, Chan ) +@strawberry_django.filter_type(models.ConfigContextProfile, lookups=True) +class ConfigContextProfileFilter(SyncedDataFilterMixin, PrimaryModelFilterMixin): + name: FilterLookup[str] = strawberry_django.filter_field() + description: FilterLookup[str] = strawberry_django.filter_field() + tags: Annotated['TagFilter', strawberry.lazy('extras.graphql.filters')] | None = strawberry_django.filter_field() + + @strawberry_django.filter_type(models.ConfigTemplate, lookups=True) class ConfigTemplateFilter(BaseObjectTypeFilterMixin, SyncedDataFilterMixin, ChangeLogFilterMixin): name: FilterLookup[str] | None = strawberry_django.filter_field() diff --git a/netbox/extras/graphql/schema.py b/netbox/extras/graphql/schema.py index 947ff0b00..60d596f01 100644 --- a/netbox/extras/graphql/schema.py +++ b/netbox/extras/graphql/schema.py @@ -11,6 +11,9 @@ class ExtrasQuery: config_context: ConfigContextType = strawberry_django.field() config_context_list: List[ConfigContextType] = strawberry_django.field() + config_context_profile: ConfigContextProfileType = strawberry_django.field() + config_context_profile_list: List[ConfigContextProfileType] = strawberry_django.field() + config_template: ConfigTemplateType = strawberry_django.field() config_template_list: List[ConfigTemplateType] = strawberry_django.field() diff --git a/netbox/extras/graphql/types.py b/netbox/extras/graphql/types.py index 4bd836f6b..97637684e 100644 --- a/netbox/extras/graphql/types.py +++ b/netbox/extras/graphql/types.py @@ -3,13 +3,13 @@ from typing import Annotated, List, TYPE_CHECKING import strawberry import strawberry_django +from core.graphql.mixins import SyncedDataMixin from extras import models from extras.graphql.mixins import CustomFieldsMixin, TagsMixin -from netbox.graphql.types import BaseObjectType, ContentTypeType, ObjectType, OrganizationalObjectType +from netbox.graphql.types import BaseObjectType, ContentTypeType, NetBoxObjectType, ObjectType, OrganizationalObjectType from .filters import * if TYPE_CHECKING: - from core.graphql.types import DataFileType, DataSourceType from dcim.graphql.types import ( DeviceRoleType, DeviceType, @@ -25,6 +25,7 @@ if TYPE_CHECKING: from virtualization.graphql.types import ClusterGroupType, ClusterType, ClusterTypeType, VirtualMachineType __all__ = ( + 'ConfigContextProfileType', 'ConfigContextType', 'ConfigTemplateType', 'CustomFieldChoiceSetType', @@ -44,15 +45,24 @@ __all__ = ( ) +@strawberry_django.type( + models.ConfigContextProfile, + fields='__all__', + filters=ConfigContextProfileFilter, + pagination=True +) +class ConfigContextProfileType(SyncedDataMixin, NetBoxObjectType): + pass + + @strawberry_django.type( models.ConfigContext, fields='__all__', filters=ConfigContextFilter, pagination=True ) -class ConfigContextType(ObjectType): - data_source: Annotated["DataSourceType", strawberry.lazy('core.graphql.types')] | None - data_file: Annotated["DataFileType", strawberry.lazy('core.graphql.types')] | None +class ConfigContextType(SyncedDataMixin, ObjectType): + profile: ConfigContextProfileType | None roles: List[Annotated["DeviceRoleType", strawberry.lazy('dcim.graphql.types')]] device_types: List[Annotated["DeviceTypeType", strawberry.lazy('dcim.graphql.types')]] tags: List[Annotated["TagType", strawberry.lazy('extras.graphql.types')]] @@ -74,10 +84,7 @@ class ConfigContextType(ObjectType): filters=ConfigTemplateFilter, pagination=True ) -class ConfigTemplateType(TagsMixin, ObjectType): - data_source: Annotated["DataSourceType", strawberry.lazy('core.graphql.types')] | None - data_file: Annotated["DataFileType", strawberry.lazy('core.graphql.types')] | None - +class ConfigTemplateType(SyncedDataMixin, TagsMixin, ObjectType): virtualmachines: List[Annotated["VirtualMachineType", strawberry.lazy('virtualization.graphql.types')]] devices: List[Annotated["DeviceType", strawberry.lazy('dcim.graphql.types')]] platforms: List[Annotated["PlatformType", strawberry.lazy('dcim.graphql.types')]] @@ -123,9 +130,8 @@ class CustomLinkType(ObjectType): filters=ExportTemplateFilter, pagination=True ) -class ExportTemplateType(ObjectType): - data_source: Annotated["DataSourceType", strawberry.lazy('core.graphql.types')] | None - data_file: Annotated["DataFileType", strawberry.lazy('core.graphql.types')] | None +class ExportTemplateType(SyncedDataMixin, ObjectType): + pass @strawberry_django.type( diff --git a/netbox/extras/jobs.py b/netbox/extras/jobs.py index 733654198..8a039c7c8 100644 --- a/netbox/extras/jobs.py +++ b/netbox/extras/jobs.py @@ -59,6 +59,7 @@ class ScriptJob(JobRunner): else: script.log_failure(msg) logger.error(f"Script aborted with error: {e}") + self.logger.error(f"Script aborted with error: {e}") else: stacktrace = traceback.format_exc() @@ -66,9 +67,11 @@ class ScriptJob(JobRunner): message=_("An exception occurred: ") + f"`{type(e).__name__}: {e}`\n```\n{stacktrace}\n```" ) logger.error(f"Exception raised during script execution: {e}") + self.logger.error(f"Exception raised during script execution: {e}") if type(e) is not AbortTransaction: script.log_info(message=_("Database changes have been reverted due to error.")) + self.logger.info("Database changes have been reverted due to error.") # Clear all pending events. Job termination (including setting the status) is handled by the job framework. if request: @@ -90,7 +93,10 @@ class ScriptJob(JobRunner): request: The WSGI request associated with this execution (if any) commit: Passed through to Script.run() """ - script = ScriptModel.objects.get(pk=self.job.object_id).python_class() + script_model = ScriptModel.objects.get(pk=self.job.object_id) + self.logger.debug(f"Found ScriptModel ID {script_model.pk}") + script = script_model.python_class() + self.logger.debug(f"Loaded script {script.full_name}") # Add files to form data if request: @@ -100,13 +106,16 @@ class ScriptJob(JobRunner): # Add the current request as a property of the script script.request = request + self.logger.debug(f"Request ID: {request.id}") # Execute the script. If commit is True, wrap it with the event_tracking context manager to ensure we process # change logging, event rules, etc. if commit: + self.logger.info("Executing script (commit enabled)") with ExitStack() as stack: for request_processor in registry['request_processors']: stack.enter_context(request_processor(request)) self.run_script(script, request, data, commit) else: + self.logger.warning("Executing script (commit disabled)") self.run_script(script, request, data, commit) diff --git a/netbox/extras/management/commands/housekeeping.py b/netbox/extras/management/commands/housekeeping.py index b8c7eab7d..0d8a7e0b9 100644 --- a/netbox/extras/management/commands/housekeeping.py +++ b/netbox/extras/management/commands/housekeeping.py @@ -14,9 +14,16 @@ from utilities.proxy import resolve_proxies class Command(BaseCommand): - help = "Perform nightly housekeeping tasks. (This command can be run at any time.)" + help = "Perform nightly housekeeping tasks [DEPRECATED]" def handle(self, *args, **options): + self.stdout.write( + "Running this command is no longer necessary: All housekeeping tasks\n" + "are addressed automatically via NetBox's built-in job scheduler. It\n" + "will be removed in a future release.", + self.style.WARNING + ) + config = Config() # Clear expired authentication sessions (essentially replicating the `clearsessions` command) diff --git a/netbox/extras/migrations/0111_rename_content_types.py b/netbox/extras/migrations/0111_rename_content_types.py index a9f80b146..8a5db53ff 100644 --- a/netbox/extras/migrations/0111_rename_content_types.py +++ b/netbox/extras/migrations/0111_rename_content_types.py @@ -24,7 +24,7 @@ class Migration(migrations.Migration): model_name='customfield', name='object_type', field=models.ForeignKey( - blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='core.objecttype' + blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='contenttypes.contenttype' ), ), migrations.RunSQL(( diff --git a/netbox/extras/migrations/0128_tableconfig.py b/netbox/extras/migrations/0128_tableconfig.py index e6d45199d..98048ee27 100644 --- a/netbox/extras/migrations/0128_tableconfig.py +++ b/netbox/extras/migrations/0128_tableconfig.py @@ -37,7 +37,9 @@ class Migration(migrations.Migration): ( 'object_type', models.ForeignKey( - on_delete=django.db.models.deletion.CASCADE, related_name='table_configs', to='core.objecttype' + on_delete=django.db.models.deletion.CASCADE, + related_name='table_configs', + to='contenttypes.contenttype' ), ), ( diff --git a/netbox/extras/migrations/0130_imageattachment_description.py b/netbox/extras/migrations/0130_imageattachment_description.py new file mode 100644 index 000000000..1e6dd8867 --- /dev/null +++ b/netbox/extras/migrations/0130_imageattachment_description.py @@ -0,0 +1,16 @@ +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('extras', '0129_fix_script_paths'), + ] + + operations = [ + migrations.AddField( + model_name='imageattachment', + name='description', + field=models.CharField(blank=True, max_length=200), + ), + ] diff --git a/netbox/extras/migrations/0131_concrete_objecttype.py b/netbox/extras/migrations/0131_concrete_objecttype.py new file mode 100644 index 000000000..6aed4d97d --- /dev/null +++ b/netbox/extras/migrations/0131_concrete_objecttype.py @@ -0,0 +1,42 @@ +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('contenttypes', '0002_remove_content_type_name'), + ('extras', '0130_imageattachment_description'), + ] + + operations = [ + migrations.AlterField( + model_name='customfield', + name='object_types', + field=models.ManyToManyField(related_name='custom_fields', to='contenttypes.contenttype'), + ), + migrations.AlterField( + model_name='customlink', + name='object_types', + field=models.ManyToManyField(related_name='custom_links', to='contenttypes.contenttype'), + ), + migrations.AlterField( + model_name='eventrule', + name='object_types', + field=models.ManyToManyField(related_name='event_rules', to='contenttypes.contenttype'), + ), + migrations.AlterField( + model_name='exporttemplate', + name='object_types', + field=models.ManyToManyField(related_name='export_templates', to='contenttypes.contenttype'), + ), + migrations.AlterField( + model_name='savedfilter', + name='object_types', + field=models.ManyToManyField(related_name='saved_filters', to='contenttypes.contenttype'), + ), + migrations.AlterField( + model_name='tag', + name='object_types', + field=models.ManyToManyField(blank=True, related_name='+', to='contenttypes.contenttype'), + ), + ] diff --git a/netbox/extras/migrations/0132_configcontextprofile.py b/netbox/extras/migrations/0132_configcontextprofile.py new file mode 100644 index 000000000..adf9a9b83 --- /dev/null +++ b/netbox/extras/migrations/0132_configcontextprofile.py @@ -0,0 +1,75 @@ +# Generated by Django 5.2.4 on 2025-08-08 16:40 + +import django.db.models.deletion +import netbox.models.deletion +import taggit.managers +import utilities.json +import utilities.jsonschema +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ('core', '0018_concrete_objecttype'), + ('extras', '0131_concrete_objecttype'), + ] + + operations = [ + migrations.CreateModel( + name='ConfigContextProfile', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False)), + ('created', models.DateTimeField(auto_now_add=True, null=True)), + ('last_updated', models.DateTimeField(auto_now=True, null=True)), + ( + 'custom_field_data', + models.JSONField(blank=True, default=dict, encoder=utilities.json.CustomFieldJSONEncoder), + ), + ('data_path', models.CharField(blank=True, editable=False, max_length=1000)), + ('auto_sync_enabled', models.BooleanField(default=False)), + ('data_synced', models.DateTimeField(blank=True, editable=False, null=True)), + ('comments', models.TextField(blank=True)), + ('name', models.CharField(max_length=100, unique=True)), + ('description', models.CharField(blank=True, max_length=200)), + ('schema', models.JSONField(blank=True, null=True, validators=[utilities.jsonschema.validate_schema])), + ( + 'data_file', + models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.SET_NULL, + related_name='+', + to='core.datafile', + ), + ), + ( + 'data_source', + models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.PROTECT, + related_name='+', + to='core.datasource', + ), + ), + ('tags', taggit.managers.TaggableManager(through='extras.TaggedItem', to='extras.Tag')), + ], + options={ + 'verbose_name': 'config context profile', + 'verbose_name_plural': 'config context profiles', + 'ordering': ('name',), + }, + bases=(netbox.models.deletion.DeleteMixin, models.Model), + ), + migrations.AddField( + model_name='configcontext', + name='profile', + field=models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.PROTECT, + related_name='config_contexts', + to='extras.configcontextprofile', + ), + ), + ] diff --git a/netbox/extras/models/configs.py b/netbox/extras/models/configs.py index 8d6b8d999..a9d233568 100644 --- a/netbox/extras/models/configs.py +++ b/netbox/extras/models/configs.py @@ -1,20 +1,25 @@ -from django.apps import apps +import jsonschema +from collections import defaultdict +from jsonschema.exceptions import ValidationError as JSONValidationError + from django.conf import settings from django.core.validators import ValidationError from django.db import models from django.urls import reverse from django.utils.translation import gettext_lazy as _ +from core.models import ObjectType from extras.models.mixins import RenderTemplateMixin from extras.querysets import ConfigContextQuerySet -from netbox.models import ChangeLoggedModel +from netbox.models import ChangeLoggedModel, PrimaryModel from netbox.models.features import CloningMixin, CustomLinksMixin, ExportTemplatesMixin, SyncedDataMixin, TagsMixin -from netbox.registry import registry from utilities.data import deepmerge +from utilities.jsonschema import validate_schema __all__ = ( 'ConfigContext', 'ConfigContextModel', + 'ConfigContextProfile', 'ConfigTemplate', ) @@ -23,6 +28,46 @@ __all__ = ( # Config contexts # +class ConfigContextProfile(SyncedDataMixin, PrimaryModel): + """ + A profile which can be used to enforce parameters on a ConfigContext. + """ + name = models.CharField( + verbose_name=_('name'), + max_length=100, + unique=True + ) + description = models.CharField( + verbose_name=_('description'), + max_length=200, + blank=True + ) + schema = models.JSONField( + blank=True, + null=True, + validators=[validate_schema], + verbose_name=_('schema'), + help_text=_('A JSON schema specifying the structure of the context data for this profile') + ) + + clone_fields = ('schema',) + + class Meta: + ordering = ('name',) + verbose_name = _('config context profile') + verbose_name_plural = _('config context profiles') + + def __str__(self): + return self.name + + def sync_data(self): + """ + Synchronize schema from the designated DataFile (if any). + """ + self.schema = self.data_file.get_data() + sync_data.alters_data = True + + class ConfigContext(SyncedDataMixin, CloningMixin, CustomLinksMixin, ChangeLoggedModel): """ A ConfigContext represents a set of arbitrary data available to any Device or VirtualMachine matching its assigned @@ -34,6 +79,13 @@ class ConfigContext(SyncedDataMixin, CloningMixin, CustomLinksMixin, ChangeLogge max_length=100, unique=True ) + profile = models.ForeignKey( + to='extras.ConfigContextProfile', + on_delete=models.PROTECT, + blank=True, + null=True, + related_name='config_contexts', + ) weight = models.PositiveSmallIntegerField( verbose_name=_('weight'), default=1000 @@ -117,9 +169,8 @@ class ConfigContext(SyncedDataMixin, CloningMixin, CustomLinksMixin, ChangeLogge objects = ConfigContextQuerySet.as_manager() clone_fields = ( - 'weight', 'is_active', 'regions', 'site_groups', 'sites', 'locations', 'device_types', - 'roles', 'platforms', 'cluster_types', 'cluster_groups', 'clusters', 'tenant_groups', - 'tenants', 'tags', 'data', + 'weight', 'profile', 'is_active', 'regions', 'site_groups', 'sites', 'locations', 'device_types', 'roles', + 'platforms', 'cluster_types', 'cluster_groups', 'clusters', 'tenant_groups', 'tenants', 'tags', 'data', ) class Meta: @@ -146,6 +197,13 @@ class ConfigContext(SyncedDataMixin, CloningMixin, CustomLinksMixin, ChangeLogge {'data': _('JSON data must be in object form. Example:') + ' {"foo": 123}'} ) + # Validate config data against the assigned profile's schema (if any) + if self.profile and self.profile.schema: + try: + jsonschema.validate(self.data, schema=self.profile.schema) + except JSONValidationError as e: + raise ValidationError(_("Data does not conform to profile schema: {error}").format(error=e)) + def sync_data(self): """ Synchronize context data from the designated DataFile (if any). @@ -239,15 +297,12 @@ class ConfigTemplate( sync_data.alters_data = True def get_context(self, context=None, queryset=None): - _context = dict() - for app, model_names in registry['models'].items(): - _context.setdefault(app, {}) - for model_name in model_names: - try: - model = apps.get_registered_model(app, model_name) - _context[app][model.__name__] = model - except LookupError: - pass + _context = defaultdict(dict) + + # Populate all public models for reference within the template + for object_type in ObjectType.objects.public(): + if model := object_type.model_class(): + _context[object_type.app_label][model.__name__] = model # Apply the provided context data, if any if context is not None: diff --git a/netbox/extras/models/customfields.py b/netbox/extras/models/customfields.py index 19d9e1ded..caf113f97 100644 --- a/netbox/extras/models/customfields.py +++ b/netbox/extras/models/customfields.py @@ -72,7 +72,7 @@ class CustomFieldManager(models.Manager.from_queryset(RestrictedQuerySet)): class CustomField(CloningMixin, ExportTemplatesMixin, ChangeLoggedModel): object_types = models.ManyToManyField( - to='core.ObjectType', + to='contenttypes.ContentType', related_name='custom_fields', help_text=_('The object(s) to which this field applies.') ) @@ -84,7 +84,7 @@ class CustomField(CloningMixin, ExportTemplatesMixin, ChangeLoggedModel): help_text=_('The type of data this custom field holds') ) related_object_type = models.ForeignKey( - to='core.ObjectType', + to='contenttypes.ContentType', on_delete=models.PROTECT, blank=True, null=True, diff --git a/netbox/extras/models/models.py b/netbox/extras/models/models.py index f5a3c4040..be4c44d63 100644 --- a/netbox/extras/models/models.py +++ b/netbox/extras/models/models.py @@ -1,4 +1,5 @@ import json +import os import urllib.parse from django.conf import settings @@ -8,20 +9,21 @@ from django.core.validators import ValidationError from django.db import models from django.urls import reverse from django.utils import timezone +from django.utils.html import escape +from django.utils.safestring import mark_safe from django.utils.translation import gettext_lazy as _ from rest_framework.utils.encoders import JSONEncoder -from core.models import ObjectType from extras.choices import * from extras.conditions import ConditionSet, InvalidCondition from extras.constants import * -from extras.utils import image_upload from extras.models.mixins import RenderTemplateMixin +from extras.utils import image_upload from netbox.config import get_config from netbox.events import get_event_type_choices from netbox.models import ChangeLoggedModel from netbox.models.features import ( - CloningMixin, CustomFieldsMixin, CustomLinksMixin, ExportTemplatesMixin, SyncedDataMixin, TagsMixin + CloningMixin, CustomFieldsMixin, CustomLinksMixin, ExportTemplatesMixin, SyncedDataMixin, TagsMixin, has_feature ) from utilities.html import clean_html from utilities.jinja2 import render_jinja2 @@ -49,7 +51,7 @@ class EventRule(CustomFieldsMixin, ExportTemplatesMixin, TagsMixin, ChangeLogged webhook or executing a custom script. """ object_types = models.ManyToManyField( - to='core.ObjectType', + to='contenttypes.ContentType', related_name='event_rules', verbose_name=_('object types'), help_text=_("The object(s) to which this rule applies.") @@ -298,7 +300,7 @@ class CustomLink(CloningMixin, ExportTemplatesMixin, ChangeLoggedModel): code to be rendered with an object as context. """ object_types = models.ManyToManyField( - to='core.ObjectType', + to='contenttypes.ContentType', related_name='custom_links', help_text=_('The object type(s) to which this link applies.') ) @@ -394,7 +396,7 @@ class CustomLink(CloningMixin, ExportTemplatesMixin, ChangeLoggedModel): class ExportTemplate(SyncedDataMixin, CloningMixin, ExportTemplatesMixin, ChangeLoggedModel, RenderTemplateMixin): object_types = models.ManyToManyField( - to='core.ObjectType', + to='contenttypes.ContentType', related_name='export_templates', help_text=_('The object type(s) to which this template applies.') ) @@ -459,7 +461,7 @@ class SavedFilter(CloningMixin, ExportTemplatesMixin, ChangeLoggedModel): A set of predefined keyword parameters that can be reused to filter for specific objects. """ object_types = models.ManyToManyField( - to='core.ObjectType', + to='contenttypes.ContentType', related_name='saved_filters', help_text=_('The object type(s) to which this filter applies.') ) @@ -539,7 +541,7 @@ class TableConfig(CloningMixin, ChangeLoggedModel): A saved configuration of columns and ordering which applies to a specific table. """ object_type = models.ForeignKey( - to='core.ObjectType', + to='contenttypes.ContentType', on_delete=models.CASCADE, related_name='table_configs', help_text=_("The table's object type"), @@ -678,6 +680,11 @@ class ImageAttachment(ChangeLoggedModel): max_length=50, blank=True ) + description = models.CharField( + verbose_name=_('description'), + max_length=200, + blank=True + ) objects = RestrictedQuerySet.as_manager() @@ -692,16 +699,16 @@ class ImageAttachment(ChangeLoggedModel): verbose_name_plural = _('image attachments') def __str__(self): - if self.name: - return self.name - filename = self.image.name.rsplit('/', 1)[-1] - return filename.split('_', 2)[2] + return self.name or self.filename + + def get_absolute_url(self): + return reverse('extras:imageattachment', args=[self.pk]) def clean(self): super().clean() # Validate the assigned object type - if self.object_type not in ObjectType.objects.with_feature('image_attachments'): + if not has_feature(self.object_type, 'image_attachments'): raise ValidationError( _("Image attachments cannot be assigned to this object type ({type}).").format(type=self.object_type) ) @@ -719,6 +726,22 @@ class ImageAttachment(ChangeLoggedModel): # before the request finishes. (For example, to display a message indicating the ImageAttachment was deleted.) self.image.name = _name + @property + def filename(self): + return os.path.basename(self.image.name).split('_', 2)[2] + + @property + def html_tag(self): + """ + Returns a complete tag suitable for embedding in an HTML document. + """ + return mark_safe('{alt_text}'.format( + url=self.image.url, + height=self.image_height, + width=self.image_width, + alt_text=escape(self.description or self.name), + )) + @property def size(self): """ @@ -797,7 +820,7 @@ class JournalEntry(CustomFieldsMixin, CustomLinksMixin, TagsMixin, ExportTemplat super().clean() # Validate the assigned object type - if self.assigned_object_type not in ObjectType.objects.with_feature('journaling'): + if not has_feature(self.assigned_object_type, 'journaling'): raise ValidationError( _("Journaling is not supported for this object type ({type}).").format(type=self.assigned_object_type) ) @@ -856,7 +879,7 @@ class Bookmark(models.Model): super().clean() # Validate the assigned object type - if self.object_type not in ObjectType.objects.with_feature('bookmarks'): + if not has_feature(self.object_type, 'bookmarks'): raise ValidationError( _("Bookmarks cannot be assigned to this object type ({type}).").format(type=self.object_type) ) diff --git a/netbox/extras/models/notifications.py b/netbox/extras/models/notifications.py index 3c8bd411b..f813a0d29 100644 --- a/netbox/extras/models/notifications.py +++ b/netbox/extras/models/notifications.py @@ -7,9 +7,9 @@ from django.db import models from django.urls import reverse from django.utils.translation import gettext_lazy as _ -from core.models import ObjectType from extras.querysets import NotificationQuerySet from netbox.models import ChangeLoggedModel +from netbox.models.features import has_feature from netbox.registry import registry from users.models import User from utilities.querysets import RestrictedQuerySet @@ -94,7 +94,7 @@ class Notification(models.Model): super().clean() # Validate the assigned object type - if self.object_type not in ObjectType.objects.with_feature('notifications'): + if not has_feature(self.object_type, 'notifications'): raise ValidationError( _("Objects of this type ({type}) do not support notifications.").format(type=self.object_type) ) @@ -238,7 +238,7 @@ class Subscription(models.Model): super().clean() # Validate the assigned object type - if self.object_type not in ObjectType.objects.with_feature('notifications'): + if not has_feature(self.object_type, 'notifications'): raise ValidationError( _("Objects of this type ({type}) do not support notifications.").format(type=self.object_type) ) diff --git a/netbox/extras/models/tags.py b/netbox/extras/models/tags.py index 7d52d9eb6..0df76d7b3 100644 --- a/netbox/extras/models/tags.py +++ b/netbox/extras/models/tags.py @@ -35,7 +35,7 @@ class Tag(CloningMixin, ExportTemplatesMixin, ChangeLoggedModel, TagBase): blank=True, ) object_types = models.ManyToManyField( - to='core.ObjectType', + to='contenttypes.ContentType', related_name='+', blank=True, help_text=_("The object type(s) to which this tag can be applied.") diff --git a/netbox/extras/scripts.py b/netbox/extras/scripts.py index f96066fb1..a14eba556 100644 --- a/netbox/extras/scripts.py +++ b/netbox/extras/scripts.py @@ -31,6 +31,7 @@ __all__ = ( 'DateTimeVar', 'FileVar', 'IntegerVar', + 'DecimalVar', 'IPAddressVar', 'IPAddressWithMaskVar', 'IPNetworkVar', @@ -135,6 +136,26 @@ class IntegerVar(ScriptVariable): self.field_attrs['max_value'] = max_value +class DecimalVar(ScriptVariable): + """ + Decimal representation. Can enforce minimum/maximum values, maximum digits and decimal places. + """ + form_field = forms.DecimalField + + def __init__(self, min_value=None, max_value=None, max_digits=None, decimal_places=None, *args, **kwargs,): + super().__init__(*args, **kwargs) + + # Optional constraints + if min_value: + self.field_attrs["min_value"] = min_value + if max_value: + self.field_attrs["max_value"] = max_value + if max_digits: + self.field_attrs["max_digits"] = max_digits + if decimal_places: + self.field_attrs["decimal_places"] = decimal_places + + class BooleanVar(ScriptVariable): """ Boolean representation (true/false). Renders as a checkbox. @@ -567,9 +588,9 @@ class BaseScript: """ Return data from a YAML file """ - # TODO: DEPRECATED: Remove this method in v4.4 + # TODO: DEPRECATED: Remove this method in v4.5 self._log( - _("load_yaml is deprecated and will be removed in v4.4"), + _("load_yaml is deprecated and will be removed in v4.5"), level=LogLevelChoices.LOG_WARNING ) file_path = os.path.join(settings.SCRIPTS_ROOT, filename) @@ -582,9 +603,9 @@ class BaseScript: """ Return data from a JSON file """ - # TODO: DEPRECATED: Remove this method in v4.4 + # TODO: DEPRECATED: Remove this method in v4.5 self._log( - _("load_json is deprecated and will be removed in v4.4"), + _("load_json is deprecated and will be removed in v4.5"), level=LogLevelChoices.LOG_WARNING ) file_path = os.path.join(settings.SCRIPTS_ROOT, filename) diff --git a/netbox/extras/search.py b/netbox/extras/search.py index feb235c29..67a20d017 100644 --- a/netbox/extras/search.py +++ b/netbox/extras/search.py @@ -2,6 +2,17 @@ from netbox.search import SearchIndex, register_search from . import models +@register_search +class ConfigContextProfileIndex(SearchIndex): + model = models.ConfigContextProfile + fields = ( + ('name', 100), + ('description', 500), + ('comments', 5000), + ) + display_attrs = ('description',) + + @register_search class CustomFieldIndex(SearchIndex): model = models.CustomField @@ -14,6 +25,17 @@ class CustomFieldIndex(SearchIndex): display_attrs = ('description',) +@register_search +class ImageAttachmentIndex(SearchIndex): + model = models.ImageAttachment + fields = ( + ('name', 100), + ('filename', 110), + ('description', 500), + ) + display_attrs = ('description',) + + @register_search class JournalEntryIndex(SearchIndex): model = models.JournalEntry diff --git a/netbox/extras/signals.py b/netbox/extras/signals.py index 10c3f73c5..7105c38b4 100644 --- a/netbox/extras/signals.py +++ b/netbox/extras/signals.py @@ -3,12 +3,11 @@ from django.db.models.signals import m2m_changed, post_save, pre_delete from django.dispatch import receiver from core.events import * -from core.models import ObjectType from core.signals import job_end, job_start from extras.events import process_event_rules from extras.models import EventRule, Notification, Subscription from netbox.config import get_config -from netbox.registry import registry +from netbox.models.features import has_feature from netbox.signals import post_clean from utilities.exceptions import AbortRequest from .models import CustomField, TaggedItem @@ -82,7 +81,7 @@ def validate_assigned_tags(sender, instance, action, model, pk_set, **kwargs): """ if action != 'pre_add': return - ct = ObjectType.objects.get_for_model(instance) + ct = ContentType.objects.get_for_model(instance) # Retrieve any applied Tags that are restricted to certain object types for tag in model.objects.filter(pk__in=pk_set, object_types__isnull=False).prefetch_related('object_types'): if ct not in tag.object_types.all(): @@ -150,17 +149,25 @@ def notify_object_changed(sender, instance, **kwargs): event_type = OBJECT_DELETED # Skip unsupported object types - ct = ContentType.objects.get_for_model(instance) - if ct.model not in registry['model_features']['notifications'].get(ct.app_label, []): + if not has_feature(instance, 'notifications'): return + ct = ContentType.objects.get_for_model(instance) + # Find all subscribed Users - subscribed_users = Subscription.objects.filter(object_type=ct, object_id=instance.pk).values_list('user', flat=True) + subscribed_users = Subscription.objects.filter( + object_type=ct, + object_id=instance.pk + ).values_list('user', flat=True) if not subscribed_users: return # Delete any existing Notifications for the object - Notification.objects.filter(object_type=ct, object_id=instance.pk, user__in=subscribed_users).delete() + Notification.objects.filter( + object_type=ct, + object_id=instance.pk, + user__in=subscribed_users + ).delete() # Create Notifications for Subscribers Notification.objects.bulk_create([ diff --git a/netbox/extras/tables/tables.py b/netbox/extras/tables/tables.py index 34ba3fb01..5c1a63d26 100644 --- a/netbox/extras/tables/tables.py +++ b/netbox/extras/tables/tables.py @@ -1,6 +1,7 @@ import json import django_tables2 as tables +from django.template.defaultfilters import filesizeformat from django.utils.html import format_html from django.utils.translation import gettext_lazy as _ @@ -14,6 +15,7 @@ from .columns import NotificationActionsColumn __all__ = ( 'BookmarkTable', + 'ConfigContextProfileTable', 'ConfigContextTable', 'ConfigTemplateTable', 'CustomFieldChoiceSetTable', @@ -38,10 +40,10 @@ __all__ = ( IMAGEATTACHMENT_IMAGE = """ {% if record.image %} - {{ record }} -{% else %} - — + + {% endif %} +{{ record }} """ NOTIFICATION_ICON = """ @@ -230,29 +232,51 @@ class ImageAttachmentTable(NetBoxTable): verbose_name=_('ID'), linkify=False ) + image = columns.TemplateColumn( + verbose_name=_('Image'), + template_code=IMAGEATTACHMENT_IMAGE, + attrs={'td': {'class': 'text-nowrap'}} + ) + name = tables.Column( + verbose_name=_('Name'), + linkify=True, + ) + filename = tables.Column( + verbose_name=_('Filename'), + linkify=lambda record: record.image.url, + orderable=False, + ) + dimensions = columns.TemplateColumn( + verbose_name=_('Dimensions'), + orderable=False, + template_code="{{ record.image_width }}×{{ record.image_height }}", + ) object_type = columns.ContentTypeColumn( verbose_name=_('Object Type'), ) parent = tables.Column( - verbose_name=_('Parent'), - linkify=True - ) - image = tables.TemplateColumn( - verbose_name=_('Image'), - template_code=IMAGEATTACHMENT_IMAGE, + verbose_name=_('Object'), + linkify=True, + orderable=False, ) size = tables.Column( orderable=False, - verbose_name=_('Size (Bytes)') + verbose_name=_('Size') ) class Meta(NetBoxTable.Meta): model = ImageAttachment fields = ( - 'pk', 'object_type', 'parent', 'image', 'name', 'image_height', 'image_width', 'size', 'created', - 'last_updated', + 'pk', 'object_type', 'parent', 'image', 'name', 'filename', 'description', 'image_height', 'image_width', + 'size', 'created', 'last_updated', ) - default_columns = ('object_type', 'parent', 'image', 'name', 'size', 'created') + default_columns = ('image', 'parent', 'description', 'dimensions', 'size') + + def render_size(self, value): + return filesizeformat(value) + + def value_size(self, value): + return value class SavedFilterTable(NetBoxTable): @@ -523,7 +547,41 @@ class TaggedItemTable(NetBoxTable): fields = ('id', 'content_type', 'content_object') +class ConfigContextProfileTable(NetBoxTable): + name = tables.Column( + verbose_name=_('Name'), + linkify=True + ) + data_source = tables.Column( + verbose_name=_('Data Source'), + linkify=True + ) + data_file = tables.Column( + verbose_name=_('Data File'), + linkify=True + ) + is_synced = columns.BooleanColumn( + orderable=False, + verbose_name=_('Synced') + ) + tags = columns.TagColumn( + url_name='extras:configcontextprofile_list' + ) + + class Meta(NetBoxTable.Meta): + model = ConfigContextProfile + fields = ( + 'pk', 'id', 'name', 'description', 'comments', 'data_source', 'data_file', 'is_synced', 'tags', 'created', + 'last_updated', + ) + default_columns = ('pk', 'name', 'is_synced', 'description') + + class ConfigContextTable(NetBoxTable): + profile = tables.Column( + linkify=True, + verbose_name=_('Profile'), + ) data_source = tables.Column( verbose_name=_('Data Source'), linkify=True @@ -550,11 +608,11 @@ class ConfigContextTable(NetBoxTable): class Meta(NetBoxTable.Meta): model = ConfigContext fields = ( - 'pk', 'id', 'name', 'weight', 'is_active', 'is_synced', 'description', 'regions', 'sites', 'locations', - 'roles', 'platforms', 'cluster_types', 'cluster_groups', 'clusters', 'tenant_groups', 'tenants', - 'data_source', 'data_file', 'data_synced', 'tags', 'created', 'last_updated', + 'pk', 'id', 'name', 'weight', 'profile', 'is_active', 'is_synced', 'description', 'regions', 'sites', + 'locations', 'roles', 'platforms', 'cluster_types', 'cluster_groups', 'clusters', 'tenant_groups', + 'tenants', 'data_source', 'data_file', 'data_synced', 'tags', 'created', 'last_updated', ) - default_columns = ('pk', 'name', 'weight', 'is_active', 'is_synced', 'description') + default_columns = ('pk', 'name', 'weight', 'profile', 'is_active', 'is_synced', 'description') class ConfigTemplateTable(NetBoxTable): diff --git a/netbox/extras/tests/test_api.py b/netbox/extras/tests/test_api.py index 29af3f96d..d635916e4 100644 --- a/netbox/extras/tests/test_api.py +++ b/netbox/extras/tests/test_api.py @@ -3,7 +3,6 @@ import datetime from django.contrib.contenttypes.models import ContentType from django.urls import reverse from django.utils.timezone import make_aware, now -from rest_framework import status from core.choices import ManagedFileRootPathChoices from core.events import * @@ -580,7 +579,7 @@ class ImageAttachmentTest( APIViewTestCases.GraphQLTestCase ): model = ImageAttachment - brief_fields = ['display', 'id', 'image', 'name', 'url'] + brief_fields = ['description', 'display', 'id', 'image', 'name', 'url'] @classmethod def setUpTestData(cls): @@ -667,6 +666,70 @@ class JournalEntryTest(APIViewTestCases.APIViewTestCase): ] +class ConfigContextProfileTest(APIViewTestCases.APIViewTestCase): + model = ConfigContextProfile + brief_fields = ['description', 'display', 'id', 'name', 'url'] + create_data = [ + { + 'name': 'Config Context Profile 4', + }, + { + 'name': 'Config Context Profile 5', + }, + { + 'name': 'Config Context Profile 6', + }, + ] + bulk_update_data = { + 'description': 'New description', + } + + @classmethod + def setUpTestData(cls): + profiles = ( + ConfigContextProfile( + name='Config Context Profile 1', + schema={ + "properties": { + "foo": { + "type": "string" + } + }, + "required": [ + "foo" + ] + } + ), + ConfigContextProfile( + name='Config Context Profile 2', + schema={ + "properties": { + "bar": { + "type": "string" + } + }, + "required": [ + "bar" + ] + } + ), + ConfigContextProfile( + name='Config Context Profile 3', + schema={ + "properties": { + "baz": { + "type": "string" + } + }, + "required": [ + "baz" + ] + } + ), + ) + ConfigContextProfile.objects.bulk_create(profiles) + + class ConfigContextTest(APIViewTestCases.APIViewTestCase): model = ConfigContext brief_fields = ['description', 'display', 'id', 'name', 'url'] @@ -921,22 +984,6 @@ class CreatedUpdatedFilterTest(APITestCase): self.assertEqual(response.data['results'][0]['id'], rack2.pk) -class ObjectTypeTest(APITestCase): - - def test_list_objects(self): - object_type_count = ObjectType.objects.count() - - response = self.client.get(reverse('extras-api:objecttype-list'), **self.header) - self.assertHttpStatus(response, status.HTTP_200_OK) - self.assertEqual(response.data['count'], object_type_count) - - def test_get_object(self): - object_type = ObjectType.objects.first() - - url = reverse('extras-api:objecttype-detail', kwargs={'pk': object_type.pk}) - self.assertHttpStatus(self.client.get(url, **self.header), status.HTTP_200_OK) - - class SubscriptionTest(APIViewTestCases.APIViewTestCase): model = Subscription brief_fields = ['display', 'id', 'object_id', 'object_type', 'url', 'user'] diff --git a/netbox/extras/tests/test_dashboard.py b/netbox/extras/tests/test_dashboard.py index 19ce5a43d..fb94710c7 100644 --- a/netbox/extras/tests/test_dashboard.py +++ b/netbox/extras/tests/test_dashboard.py @@ -45,4 +45,4 @@ class ObjectListWidgetTests(TestCase): mock_request = Request() widget = ObjectListWidget(id='2829fd9b-5dee-4c9a-81f2-5bd84c350a27', **config) rendered = widget.render(mock_request) - self.assertTrue('Unable to load content. Invalid view name:' in rendered) + self.assertTrue('Unable to load content. Could not resolve list URL for:' in rendered) diff --git a/netbox/extras/tests/test_event_rules.py b/netbox/extras/tests/test_event_rules.py index 2565e5bde..0c9c25de3 100644 --- a/netbox/extras/tests/test_event_rules.py +++ b/netbox/extras/tests/test_event_rules.py @@ -135,7 +135,7 @@ class EventRuleTest(APITestCase): job = self.queue.jobs[0] self.assertEqual(job.kwargs['event_rule'], EventRule.objects.get(name='Event Rule 1')) self.assertEqual(job.kwargs['event_type'], OBJECT_CREATED) - self.assertEqual(job.kwargs['model_name'], 'site') + self.assertEqual(job.kwargs['object_type'], ObjectType.objects.get_for_model(Site)) self.assertEqual(job.kwargs['data']['id'], response.data['id']) self.assertEqual(job.kwargs['data']['foo'], 1) self.assertEqual(len(job.kwargs['data']['tags']), len(response.data['tags'])) @@ -186,7 +186,7 @@ class EventRuleTest(APITestCase): for i, job in enumerate(self.queue.jobs): self.assertEqual(job.kwargs['event_rule'], EventRule.objects.get(name='Event Rule 1')) self.assertEqual(job.kwargs['event_type'], OBJECT_CREATED) - self.assertEqual(job.kwargs['model_name'], 'site') + self.assertEqual(job.kwargs['object_type'], ObjectType.objects.get_for_model(Site)) self.assertEqual(job.kwargs['data']['id'], response.data[i]['id']) self.assertEqual(job.kwargs['data']['foo'], 1) self.assertEqual(len(job.kwargs['data']['tags']), len(response.data[i]['tags'])) @@ -218,7 +218,7 @@ class EventRuleTest(APITestCase): job = self.queue.jobs[0] self.assertEqual(job.kwargs['event_rule'], EventRule.objects.get(name='Event Rule 2')) self.assertEqual(job.kwargs['event_type'], OBJECT_UPDATED) - self.assertEqual(job.kwargs['model_name'], 'site') + self.assertEqual(job.kwargs['object_type'], ObjectType.objects.get_for_model(Site)) self.assertEqual(job.kwargs['data']['id'], site.pk) self.assertEqual(job.kwargs['data']['foo'], 2) self.assertEqual(len(job.kwargs['data']['tags']), len(response.data['tags'])) @@ -275,7 +275,7 @@ class EventRuleTest(APITestCase): for i, job in enumerate(self.queue.jobs): self.assertEqual(job.kwargs['event_rule'], EventRule.objects.get(name='Event Rule 2')) self.assertEqual(job.kwargs['event_type'], OBJECT_UPDATED) - self.assertEqual(job.kwargs['model_name'], 'site') + self.assertEqual(job.kwargs['object_type'], ObjectType.objects.get_for_model(Site)) self.assertEqual(job.kwargs['data']['id'], data[i]['id']) self.assertEqual(job.kwargs['data']['foo'], 2) self.assertEqual(len(job.kwargs['data']['tags']), len(response.data[i]['tags'])) @@ -302,7 +302,7 @@ class EventRuleTest(APITestCase): job = self.queue.jobs[0] self.assertEqual(job.kwargs['event_rule'], EventRule.objects.get(name='Event Rule 3')) self.assertEqual(job.kwargs['event_type'], OBJECT_DELETED) - self.assertEqual(job.kwargs['model_name'], 'site') + self.assertEqual(job.kwargs['object_type'], ObjectType.objects.get_for_model(Site)) self.assertEqual(job.kwargs['data']['id'], site.pk) self.assertEqual(job.kwargs['data']['foo'], 3) self.assertEqual(job.kwargs['snapshots']['prechange']['name'], 'Site 1') @@ -336,7 +336,7 @@ class EventRuleTest(APITestCase): for i, job in enumerate(self.queue.jobs): self.assertEqual(job.kwargs['event_rule'], EventRule.objects.get(name='Event Rule 3')) self.assertEqual(job.kwargs['event_type'], OBJECT_DELETED) - self.assertEqual(job.kwargs['model_name'], 'site') + self.assertEqual(job.kwargs['object_type'], ObjectType.objects.get_for_model(Site)) self.assertEqual(job.kwargs['data']['id'], sites[i].pk) self.assertEqual(job.kwargs['data']['foo'], 3) self.assertEqual(job.kwargs['snapshots']['prechange']['name'], sites[i].name) @@ -368,18 +368,23 @@ class EventRuleTest(APITestCase): self.assertEqual(body['request_id'], str(request_id)) self.assertEqual(body['data']['name'], 'Site 1') self.assertEqual(body['data']['foo'], 1) + self.assertEqual(body['context']['foo'], 123) # From netbox.tests.dummy_plugin return HttpResponse() + # Create a dummy request + request = RequestFactory().get(reverse('dcim:site_add')) + request.id = request_id + request.user = self.user + # Enqueue a webhook for processing webhooks_queue = {} site = Site.objects.create(name='Site 1', slug='site-1') enqueue_event( webhooks_queue, instance=site, - user=self.user, - request_id=request_id, - event_type=OBJECT_CREATED + request=request, + event_type=OBJECT_CREATED, ) flush_events(list(webhooks_queue.values())) diff --git a/netbox/extras/tests/test_filtersets.py b/netbox/extras/tests/test_filtersets.py index f9147a30c..3b9a65dec 100644 --- a/netbox/extras/tests/test_filtersets.py +++ b/netbox/extras/tests/test_filtersets.py @@ -871,6 +871,39 @@ class JournalEntryTestCase(TestCase, ChangeLoggedFilterSetTests): self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) +class ConfigContextProfileTestCase(TestCase, ChangeLoggedFilterSetTests): + queryset = ConfigContextProfile.objects.all() + filterset = ConfigContextProfileFilterSet + ignore_fields = ('schema', 'data_path') + + @classmethod + def setUpTestData(cls): + profiles = ( + ConfigContextProfile( + name='Config Context Profile 1', + description='foo', + ), + ConfigContextProfile( + name='Config Context Profile 2', + description='bar', + ), + ConfigContextProfile( + name='Config Context Profile 3', + description='baz', + ), + ) + ConfigContextProfile.objects.bulk_create(profiles) + + def test_q(self): + params = {'q': 'foo'} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1) + + def test_name(self): + profiles = self.queryset.all()[:2] + params = {'name': [profiles[0].name, profiles[1].name]} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) + + class ConfigContextTestCase(TestCase, ChangeLoggedFilterSetTests): queryset = ConfigContext.objects.all() filterset = ConfigContextFilterSet @@ -878,6 +911,12 @@ class ConfigContextTestCase(TestCase, ChangeLoggedFilterSetTests): @classmethod def setUpTestData(cls): + profiles = ( + ConfigContextProfile(name='Config Context Profile 1'), + ConfigContextProfile(name='Config Context Profile 2'), + ConfigContextProfile(name='Config Context Profile 3'), + ) + ConfigContextProfile.objects.bulk_create(profiles) regions = ( Region(name='Region 1', slug='region-1'), @@ -931,7 +970,8 @@ class ConfigContextTestCase(TestCase, ChangeLoggedFilterSetTests): Platform(name='Platform 2', slug='platform-2'), Platform(name='Platform 3', slug='platform-3'), ) - Platform.objects.bulk_create(platforms) + for platform in platforms: + platform.save() cluster_types = ( ClusterType(name='Cluster Type 1', slug='cluster-type-1'), @@ -975,6 +1015,7 @@ class ConfigContextTestCase(TestCase, ChangeLoggedFilterSetTests): is_active = bool(i % 2) c = ConfigContext.objects.create( name=f"Config Context {i + 1}", + profile=profiles[i], is_active=is_active, data='{"foo": 123}', description=f"foobar{i + 1}" @@ -1011,6 +1052,13 @@ class ConfigContextTestCase(TestCase, ChangeLoggedFilterSetTests): params = {'description': ['foobar1', 'foobar2']} self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) + def test_profile(self): + profiles = ConfigContextProfile.objects.all()[:2] + params = {'profile_id': [profiles[0].pk, profiles[1].pk]} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) + params = {'profile': [profiles[0].name, profiles[1].name]} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) + def test_region(self): regions = Region.objects.all()[:2] params = {'region_id': [regions[0].pk, regions[1].pk]} @@ -1184,6 +1232,7 @@ class TagTestCase(TestCase, ChangeLoggedFilterSetTests): 'cluster', 'clustergroup', 'clustertype', + 'configcontextprofile', 'configtemplate', 'consoleport', 'consoleserverport', diff --git a/netbox/extras/tests/test_models.py b/netbox/extras/tests/test_models.py index 6b718569c..341920a81 100644 --- a/netbox/extras/tests/test_models.py +++ b/netbox/extras/tests/test_models.py @@ -6,7 +6,7 @@ from django.test import tag, TestCase from core.models import DataSource, ObjectType from dcim.models import Device, DeviceRole, DeviceType, Location, Manufacturer, Platform, Region, Site, SiteGroup -from extras.models import ConfigContext, ConfigTemplate, Tag +from extras.models import ConfigContext, ConfigContextProfile, ConfigTemplate, Tag from tenancy.models import Tenant, TenantGroup from utilities.exceptions import AbortRequest from virtualization.models import Cluster, ClusterGroup, ClusterType, VirtualMachine @@ -159,6 +159,32 @@ class ConfigContextTest(TestCase): } self.assertEqual(device.get_config_context(), expected_data) + def test_schema_validation(self): + """ + Check that the JSON schema defined by the assigned profile is enforced. + """ + profile = ConfigContextProfile.objects.create( + name="Config context profile 1", + schema={ + "properties": { + "foo": { + "type": "string" + } + }, + "required": [ + "foo" + ] + } + ) + + with self.assertRaises(ValidationError): + # Missing required attribute + ConfigContext(name="CC1", profile=profile, data={}).clean() + with self.assertRaises(ValidationError): + # Invalid attribute type + ConfigContext(name="CC1", profile=profile, data={"foo": 123}).clean() + ConfigContext(name="CC1", profile=profile, data={"foo": "bar"}).clean() + def test_annotation_same_as_get_for_object(self): """ This test incorporates features from all of the above tests cases to ensure diff --git a/netbox/extras/tests/test_scripts.py b/netbox/extras/tests/test_scripts.py index 17eb5a31a..4f5d0187a 100644 --- a/netbox/extras/tests/test_scripts.py +++ b/netbox/extras/tests/test_scripts.py @@ -1,6 +1,7 @@ import logging import tempfile from datetime import date, datetime, timezone +from decimal import Decimal from django.core.files.uploadedfile import SimpleUploadedFile from django.test import TestCase @@ -138,6 +139,54 @@ class ScriptVariablesTest(TestCase): self.assertTrue(form.is_valid()) self.assertEqual(form.cleaned_data['var1'], data['var1']) + def test_decimalvar(self): + + class TestScript(Script): + + var1 = DecimalVar( + min_value=-100.500, + max_value=100.500, + max_digits=6, + decimal_places=3, + required=False + ) + + var2 = DecimalVar( + max_digits=3, + decimal_places=1, + required=False + ) + + # Validate min_value enforcement + data = {'var1': -100.501} + form = TestScript().as_form(data, None) + self.assertFalse(form.is_valid()) + self.assertIn('var1', form.errors) + + # Validate max_value enforcement + data = {'var1': 100.501} + form = TestScript().as_form(data, None) + self.assertFalse(form.is_valid()) + self.assertIn('var1', form.errors) + + # Validate max_digits enforcement + data = {'var2': 123.4} + form = TestScript().as_form(data, None) + self.assertFalse(form.is_valid()) + self.assertIn('var2', form.errors) + + # Validate decimal_places + data = {'var2': 1.23} + form = TestScript().as_form(data, None) + self.assertFalse(form.is_valid()) + self.assertIn('var2', form.errors) + + # Validate valid data + data = {'var1': '50.123'} + form = TestScript().as_form(data, None) + self.assertTrue(form.is_valid()) + self.assertEqual(form.cleaned_data['var1'], Decimal(data['var1'])) + def test_booleanvar(self): class TestScript(Script): diff --git a/netbox/extras/tests/test_views.py b/netbox/extras/tests/test_views.py index ac3f5b23a..9da6f047a 100644 --- a/netbox/extras/tests/test_views.py +++ b/netbox/extras/tests/test_views.py @@ -481,6 +481,78 @@ class TagTestCase(ViewTestCases.OrganizationalObjectViewTestCase): } +class ConfigContextProfileTestCase(ViewTestCases.PrimaryObjectViewTestCase): + model = ConfigContextProfile + + @classmethod + def setUpTestData(cls): + profiles = ( + ConfigContextProfile( + name='Config Context Profile 1', + schema={ + "properties": { + "foo": { + "type": "string" + } + }, + "required": [ + "foo" + ] + } + ), + ConfigContextProfile( + name='Config Context Profile 2', + schema={ + "properties": { + "bar": { + "type": "string" + } + }, + "required": [ + "bar" + ] + } + ), + ConfigContextProfile( + name='Config Context Profile 3', + schema={ + "properties": { + "baz": { + "type": "string" + } + }, + "required": [ + "baz" + ] + } + ), + ) + ConfigContextProfile.objects.bulk_create(profiles) + + cls.form_data = { + 'name': 'Config Context Profile X', + 'description': 'A new config context profile', + } + + cls.bulk_edit_data = { + 'description': 'New description', + } + + cls.csv_data = ( + 'name,description', + 'Config context profile 1,Foo', + 'Config context profile 2,Bar', + 'Config context profile 3,Baz', + ) + + cls.csv_update_data = ( + "id,description", + f"{profiles[0].pk},New description", + f"{profiles[1].pk},New description", + f"{profiles[2].pk},New description", + ) + + # TODO: Change base class to PrimaryObjectViewTestCase # Blocked by absence of standard create/edit, bulk create views class ConfigContextTestCase( diff --git a/netbox/extras/urls.py b/netbox/extras/urls.py index ca07ba903..5635f0c01 100644 --- a/netbox/extras/urls.py +++ b/netbox/extras/urls.py @@ -47,6 +47,9 @@ urlpatterns = [ path('tags/', include(get_model_urls('extras', 'tag', detail=False))), path('tags//', include(get_model_urls('extras', 'tag'))), + path('config-context-profiles/', include(get_model_urls('extras', 'configcontextprofile', detail=False))), + path('config-context-profiles//', include(get_model_urls('extras', 'configcontextprofile'))), + path('config-contexts/', include(get_model_urls('extras', 'configcontext', detail=False))), path('config-contexts//', include(get_model_urls('extras', 'configcontext'))), diff --git a/netbox/extras/views.py b/netbox/extras/views.py index fae8eb63e..c76afbd15 100644 --- a/netbox/extras/views.py +++ b/netbox/extras/views.py @@ -14,12 +14,13 @@ from jinja2.exceptions import TemplateError from core.choices import ManagedFileRootPathChoices from core.models import Job +from core.object_actions import BulkSync from dcim.models import Device, DeviceRole, Platform from extras.choices import LogLevelChoices from extras.dashboard.forms import DashboardWidgetAddForm, DashboardWidgetForm from extras.dashboard.utils import get_widget_class from extras.utils import SharedObjectViewMixin -from netbox.constants import DEFAULT_ACTION_PERMISSIONS +from netbox.object_actions import * from netbox.views import generic from netbox.views.generic.mixins import TableMixin from utilities.forms import ConfirmationForm, get_field_value @@ -30,7 +31,7 @@ from utilities.querydict import normalize_querydict from utilities.request import copy_safe_request from utilities.rqworker import get_workers_for_queue from utilities.templatetags.builtins.filters import render_markdown -from utilities.views import ContentTypePermissionRequiredMixin, get_viewname, register_model_view +from utilities.views import ContentTypePermissionRequiredMixin, get_action_url, register_model_view from virtualization.models import VirtualMachine from . import filtersets, forms, tables from .constants import LOG_LEVEL_RANK @@ -96,6 +97,11 @@ class CustomFieldBulkEditView(generic.BulkEditView): form = forms.CustomFieldBulkEditForm +@register_model_view(CustomField, 'bulk_rename', path='rename', detail=False) +class CustomFieldBulkRenameView(generic.BulkRenameView): + queryset = CustomField.objects.all() + + @register_model_view(CustomField, 'bulk_delete', path='delete', detail=False) class CustomFieldBulkDeleteView(generic.BulkDeleteView): queryset = CustomField.objects.select_related('choice_set') @@ -165,6 +171,11 @@ class CustomFieldChoiceSetBulkEditView(generic.BulkEditView): form = forms.CustomFieldChoiceSetBulkEditForm +@register_model_view(CustomFieldChoiceSet, 'bulk_rename', path='rename', detail=False) +class CustomFieldChoiceSetBulkRenameView(generic.BulkRenameView): + queryset = CustomFieldChoiceSet.objects.all() + + @register_model_view(CustomFieldChoiceSet, 'bulk_delete', path='delete', detail=False) class CustomFieldChoiceSetBulkDeleteView(generic.BulkDeleteView): queryset = CustomFieldChoiceSet.objects.all() @@ -215,6 +226,11 @@ class CustomLinkBulkEditView(generic.BulkEditView): form = forms.CustomLinkBulkEditForm +@register_model_view(CustomLink, 'bulk_rename', path='rename', detail=False) +class CustomLinkBulkRenameView(generic.BulkRenameView): + queryset = CustomLink.objects.all() + + @register_model_view(CustomLink, 'bulk_delete', path='delete', detail=False) class CustomLinkBulkDeleteView(generic.BulkDeleteView): queryset = CustomLink.objects.all() @@ -232,11 +248,7 @@ class ExportTemplateListView(generic.ObjectListView): filterset = filtersets.ExportTemplateFilterSet filterset_form = forms.ExportTemplateFilterForm table = tables.ExportTemplateTable - template_name = 'extras/exporttemplate_list.html' - actions = { - **DEFAULT_ACTION_PERMISSIONS, - 'bulk_sync': {'sync'}, - } + actions = (AddObject, BulkImport, BulkSync, BulkExport, BulkEdit, BulkRename, BulkDelete) @register_model_view(ExportTemplate) @@ -270,6 +282,11 @@ class ExportTemplateBulkEditView(generic.BulkEditView): form = forms.ExportTemplateBulkEditForm +@register_model_view(ExportTemplate, 'bulk_rename', path='rename', detail=False) +class ExportTemplateBulkRenameView(generic.BulkRenameView): + queryset = ExportTemplate.objects.all() + + @register_model_view(ExportTemplate, 'bulk_delete', path='delete', detail=False) class ExportTemplateBulkDeleteView(generic.BulkDeleteView): queryset = ExportTemplate.objects.all() @@ -330,6 +347,11 @@ class SavedFilterBulkEditView(SharedObjectViewMixin, generic.BulkEditView): form = forms.SavedFilterBulkEditForm +@register_model_view(SavedFilter, 'bulk_rename', path='rename', detail=False) +class SavedFilterBulkRenameView(generic.BulkRenameView): + queryset = SavedFilter.objects.all() + + @register_model_view(SavedFilter, 'bulk_delete', path='delete', detail=False) class SavedFilterBulkDeleteView(SharedObjectViewMixin, generic.BulkDeleteView): queryset = SavedFilter.objects.all() @@ -347,9 +369,7 @@ class TableConfigListView(SharedObjectViewMixin, generic.ObjectListView): filterset = filtersets.TableConfigFilterSet filterset_form = forms.TableConfigFilterForm table = tables.TableConfigTable - actions = { - 'export': {'view'}, - } + actions = (BulkExport, BulkEdit, BulkRename, BulkDelete) @register_model_view(TableConfig) @@ -389,6 +409,11 @@ class TableConfigBulkEditView(SharedObjectViewMixin, generic.BulkEditView): form = forms.TableConfigBulkEditForm +@register_model_view(TableConfig, 'bulk_rename', path='rename', detail=False) +class TableConfigBulkRenameView(generic.BulkRenameView): + queryset = TableConfig.objects.all() + + @register_model_view(TableConfig, 'bulk_delete', path='delete', detail=False) class TableConfigBulkDeleteView(SharedObjectViewMixin, generic.BulkDeleteView): queryset = TableConfig.objects.all() @@ -470,6 +495,11 @@ class NotificationGroupBulkEditView(generic.BulkEditView): form = forms.NotificationGroupBulkEditForm +@register_model_view(NotificationGroup, 'bulk_rename', path='rename', detail=False) +class NotificationGroupBulkRenameView(generic.BulkRenameView): + queryset = NotificationGroup.objects.all() + + @register_model_view(NotificationGroup, 'bulk_delete', path='delete', detail=False) class NotificationGroupBulkDeleteView(generic.BulkDeleteView): queryset = NotificationGroup.objects.all() @@ -616,6 +646,11 @@ class WebhookBulkEditView(generic.BulkEditView): form = forms.WebhookBulkEditForm +@register_model_view(Webhook, 'bulk_rename', path='rename', detail=False) +class WebhookBulkRenameView(generic.BulkRenameView): + queryset = Webhook.objects.all() + + @register_model_view(Webhook, 'bulk_delete', path='delete', detail=False) class WebhookBulkDeleteView(generic.BulkDeleteView): queryset = Webhook.objects.all() @@ -666,6 +701,11 @@ class EventRuleBulkEditView(generic.BulkEditView): form = forms.EventRuleBulkEditForm +@register_model_view(EventRule, 'bulk_rename', path='rename', detail=False) +class EventRuleBulkRenameView(generic.BulkRenameView): + queryset = EventRule.objects.all() + + @register_model_view(EventRule, 'bulk_delete', path='delete', detail=False) class EventRuleBulkDeleteView(generic.BulkDeleteView): queryset = EventRule.objects.all() @@ -740,6 +780,11 @@ class TagBulkEditView(generic.BulkEditView): form = forms.TagBulkEditForm +@register_model_view(Tag, 'bulk_rename', path='rename', detail=False) +class TagBulkRenameView(generic.BulkRenameView): + queryset = Tag.objects.all() + + @register_model_view(Tag, 'bulk_delete', path='delete', detail=False) class TagBulkDeleteView(generic.BulkDeleteView): queryset = Tag.objects.annotate( @@ -748,6 +793,67 @@ class TagBulkDeleteView(generic.BulkDeleteView): table = tables.TagTable +# +# Config context profiles +# + +@register_model_view(ConfigContextProfile, 'list', path='', detail=False) +class ConfigContextProfileListView(generic.ObjectListView): + queryset = ConfigContextProfile.objects.all() + filterset = filtersets.ConfigContextProfileFilterSet + filterset_form = forms.ConfigContextProfileFilterForm + table = tables.ConfigContextProfileTable + actions = (AddObject, BulkSync, BulkEdit, BulkRename, BulkDelete) + + +@register_model_view(ConfigContextProfile) +class ConfigContextProfileView(generic.ObjectView): + queryset = ConfigContextProfile.objects.all() + + +@register_model_view(ConfigContextProfile, 'add', detail=False) +@register_model_view(ConfigContextProfile, 'edit') +class ConfigContextProfileEditView(generic.ObjectEditView): + queryset = ConfigContextProfile.objects.all() + form = forms.ConfigContextProfileForm + + +@register_model_view(ConfigContextProfile, 'delete') +class ConfigContextProfileDeleteView(generic.ObjectDeleteView): + queryset = ConfigContextProfile.objects.all() + + +@register_model_view(ConfigContextProfile, 'bulk_import', path='import', detail=False) +class ConfigContextProfileBulkImportView(generic.BulkImportView): + queryset = ConfigContextProfile.objects.all() + model_form = forms.ConfigContextProfileImportForm + + +@register_model_view(ConfigContextProfile, 'bulk_edit', path='edit', detail=False) +class ConfigContextProfileBulkEditView(generic.BulkEditView): + queryset = ConfigContextProfile.objects.all() + filterset = filtersets.ConfigContextProfileFilterSet + table = tables.ConfigContextProfileTable + form = forms.ConfigContextProfileBulkEditForm + + +@register_model_view(ConfigContextProfile, 'bulk_rename', path='rename', detail=False) +class ConfigContextProfileBulkRenameView(generic.BulkRenameView): + queryset = ConfigContextProfile.objects.all() + + +@register_model_view(ConfigContextProfile, 'bulk_delete', path='delete', detail=False) +class ConfigContextProfileBulkDeleteView(generic.BulkDeleteView): + queryset = ConfigContextProfile.objects.all() + filterset = filtersets.ConfigContextProfileFilterSet + table = tables.ConfigContextProfileTable + + +@register_model_view(ConfigContextProfile, 'bulk_sync', path='sync', detail=False) +class ConfigContextProfileBulkSyncDataView(generic.BulkSyncDataView): + queryset = ConfigContextProfile.objects.all() + + # # Config contexts # @@ -758,13 +864,7 @@ class ConfigContextListView(generic.ObjectListView): filterset = filtersets.ConfigContextFilterSet filterset_form = forms.ConfigContextFilterForm table = tables.ConfigContextTable - template_name = 'extras/configcontext_list.html' - actions = { - 'add': {'add'}, - 'bulk_edit': {'change'}, - 'bulk_delete': {'delete'}, - 'bulk_sync': {'sync'}, - } + actions = (AddObject, BulkSync, BulkEdit, BulkRename, BulkDelete) @register_model_view(ConfigContext) @@ -825,6 +925,11 @@ class ConfigContextBulkEditView(generic.BulkEditView): form = forms.ConfigContextBulkEditForm +@register_model_view(ConfigContext, 'bulk_rename', path='rename', detail=False) +class ConfigContextBulkRenameView(generic.BulkRenameView): + queryset = ConfigContext.objects.all() + + @register_model_view(ConfigContext, 'bulk_delete', path='delete', detail=False) class ConfigContextBulkDeleteView(generic.BulkDeleteView): queryset = ConfigContext.objects.all() @@ -877,11 +982,7 @@ class ConfigTemplateListView(generic.ObjectListView): filterset = filtersets.ConfigTemplateFilterSet filterset_form = forms.ConfigTemplateFilterForm table = tables.ConfigTemplateTable - template_name = 'extras/configtemplate_list.html' - actions = { - **DEFAULT_ACTION_PERMISSIONS, - 'bulk_sync': {'sync'}, - } + actions = (AddObject, BulkImport, BulkExport, BulkSync, BulkEdit, BulkRename, BulkDelete) @register_model_view(ConfigTemplate) @@ -915,6 +1016,11 @@ class ConfigTemplateBulkEditView(generic.BulkEditView): form = forms.ConfigTemplateBulkEditForm +@register_model_view(ConfigTemplate, 'bulk_rename', path='rename', detail=False) +class ConfigTemplateBulkRenameView(generic.BulkRenameView): + queryset = ConfigTemplate.objects.all() + + @register_model_view(ConfigTemplate, 'bulk_delete', path='delete', detail=False) class ConfigTemplateBulkDeleteView(generic.BulkDeleteView): queryset = ConfigTemplate.objects.all() @@ -992,9 +1098,12 @@ class ImageAttachmentListView(generic.ObjectListView): filterset = filtersets.ImageAttachmentFilterSet filterset_form = forms.ImageAttachmentFilterForm table = tables.ImageAttachmentTable - actions = { - 'export': {'view'}, - } + actions = (BulkExport, BulkEdit, BulkRename, BulkDelete) + + +@register_model_view(ImageAttachment) +class ImageAttachmentView(generic.ObjectView): + queryset = ImageAttachment.objects.all() @register_model_view(ImageAttachment, 'add', detail=False) @@ -1010,9 +1119,6 @@ class ImageAttachmentEditView(generic.ObjectEditView): instance.parent = get_object_or_404(object_type.model_class(), pk=request.GET.get('object_id')) return instance - def get_return_url(self, request, obj=None): - return obj.parent.get_absolute_url() if obj else super().get_return_url(request) - def get_extra_addanother_params(self, request): return { 'object_type': request.GET.get('object_type'), @@ -1024,8 +1130,25 @@ class ImageAttachmentEditView(generic.ObjectEditView): class ImageAttachmentDeleteView(generic.ObjectDeleteView): queryset = ImageAttachment.objects.all() - def get_return_url(self, request, obj=None): - return obj.parent.get_absolute_url() if obj else super().get_return_url(request) + +@register_model_view(ImageAttachment, 'bulk_edit', path='edit', detail=False) +class ImageAttachmentBulkEditView(generic.BulkEditView): + queryset = ImageAttachment.objects.all() + filterset = filtersets.ImageAttachmentFilterSet + table = tables.ImageAttachmentTable + form = forms.ImageAttachmentBulkEditForm + + +@register_model_view(ImageAttachment, 'bulk_rename', path='rename', detail=False) +class ImageAttachmentBulkRenameView(generic.BulkRenameView): + queryset = ImageAttachment.objects.all() + + +@register_model_view(ImageAttachment, 'bulk_delete', path='delete', detail=False) +class ImageAttachmentBulkDeleteView(generic.BulkDeleteView): + queryset = ImageAttachment.objects.all() + filterset = filtersets.ImageAttachmentFilterSet + table = tables.ImageAttachmentTable # @@ -1038,12 +1161,7 @@ class JournalEntryListView(generic.ObjectListView): filterset = filtersets.JournalEntryFilterSet filterset_form = forms.JournalEntryFilterForm table = tables.JournalEntryTable - actions = { - 'export': {'view'}, - 'bulk_import': {'add'}, - 'bulk_edit': {'change'}, - 'bulk_delete': {'delete'}, - } + actions = (BulkImport, BulkEdit, BulkDelete) @register_model_view(JournalEntry) @@ -1066,8 +1184,7 @@ class JournalEntryEditView(generic.ObjectEditView): if not instance.assigned_object: return reverse('extras:journalentry_list') obj = instance.assigned_object - viewname = get_viewname(obj, 'journal') - return reverse(viewname, kwargs={'pk': obj.pk}) + return get_action_url(obj, action='journal', kwargs={'pk': obj.pk}) @register_model_view(JournalEntry, 'delete') @@ -1076,8 +1193,7 @@ class JournalEntryDeleteView(generic.ObjectDeleteView): def get_return_url(self, request, instance): obj = instance.assigned_object - viewname = get_viewname(obj, 'journal') - return reverse(viewname, kwargs={'pk': obj.pk}) + return get_action_url(obj, action='journal', kwargs={'pk': obj.pk}) @register_model_view(JournalEntry, 'bulk_import', path='import', detail=False) diff --git a/netbox/extras/webhooks.py b/netbox/extras/webhooks.py index 368075217..a68f219bd 100644 --- a/netbox/extras/webhooks.py +++ b/netbox/extras/webhooks.py @@ -6,12 +6,28 @@ import requests from django_rq import job from jinja2.exceptions import TemplateError +from netbox.registry import registry from utilities.proxy import resolve_proxies from .constants import WEBHOOK_EVENT_TYPES +__all__ = ( + 'generate_signature', + 'register_webhook_callback', + 'send_webhook', +) + logger = logging.getLogger('netbox.webhooks') +def register_webhook_callback(func): + """ + Register a function as a webhook callback. + """ + registry['webhook_callbacks'].append(func) + logger.debug(f'Registered webhook callback {func.__module__}.{func.__name__}') + return func + + def generate_signature(request_body, secret): """ Return a cryptographic signature that can be used to verify the authenticity of webhook data. @@ -25,7 +41,7 @@ def generate_signature(request_body, secret): @job('default') -def send_webhook(event_rule, model_name, event_type, data, timestamp, username, request_id=None, snapshots=None): +def send_webhook(event_rule, object_type, event_type, data, timestamp, username, request=None, snapshots=None): """ Make a POST request to the defined Webhook """ @@ -35,9 +51,10 @@ def send_webhook(event_rule, model_name, event_type, data, timestamp, username, context = { 'event': WEBHOOK_EVENT_TYPES.get(event_type, event_type), 'timestamp': timestamp, - 'model': model_name, + 'object_type': '.'.join(object_type.natural_key()), + 'model': object_type.model, 'username': username, - 'request_id': request_id, + 'request_id': request.id if request else None, 'data': data, } if snapshots: @@ -45,6 +62,18 @@ def send_webhook(event_rule, model_name, event_type, data, timestamp, username, 'snapshots': snapshots }) + # Add any additional context from plugins + callback_data = {} + for callback in registry['webhook_callbacks']: + try: + if ret := callback(object_type, event_type, data, request): + callback_data.update(**ret) + except Exception as e: + logger.warning(f"Caught exception when processing callback {callback}: {e}") + pass + if callback_data: + context['context'] = callback_data + # Build the headers for the HTTP request headers = { 'Content-Type': webhook.http_content_type, diff --git a/netbox/ipam/filtersets.py b/netbox/ipam/filtersets.py index 5e6ffb2ac..7f8cd2f04 100644 --- a/netbox/ipam/filtersets.py +++ b/netbox/ipam/filtersets.py @@ -660,7 +660,7 @@ class IPAddressFilterSet(NetBoxModelFilterSet, TenancyFilterSet, ContactModelFil service_id = django_filters.ModelMultipleChoiceFilter( field_name='services', queryset=Service.objects.all(), - label=_('Service (ID)'), + label=_('Application Service (ID)'), ) nat_inside_id = django_filters.ModelMultipleChoiceFilter( field_name='nat_inside', diff --git a/netbox/ipam/forms/model_forms.py b/netbox/ipam/forms/model_forms.py index fffe21425..1b4a3d596 100644 --- a/netbox/ipam/forms/model_forms.py +++ b/netbox/ipam/forms/model_forms.py @@ -20,6 +20,7 @@ from utilities.forms.fields import ( from utilities.forms.rendering import FieldSet, InlineFields, ObjectAttribute, TabbedGroups from utilities.forms.utils import get_field_value from utilities.forms.widgets import DatePicker, HTMXSelect +from django.utils.safestring import mark_safe from utilities.templatetags.builtins.filters import bettertitle from virtualization.models import VMInterface, VirtualMachine @@ -680,7 +681,15 @@ class VLANForm(TenancyForm, NetBoxModelForm): queryset=Site.objects.all(), required=False, null_option='None', - selector=True + selector=True, + help_text=mark_safe( + ' {text}'.format( + text=_( + 'The direct assignment of VLANs to a site is deprecated and will be removed in a future release. ' + 'Users are encouraged to utilize VLAN groups for this purpose.' + ) + ) + ) ) role = DynamicModelChoiceField( label=_('Role'), @@ -749,7 +758,7 @@ class ServiceTemplateForm(NetBoxModelForm): comments = CommentField() fieldsets = ( - FieldSet('name', 'protocol', 'ports', 'description', 'tags', name=_('Service Template')), + FieldSet('name', 'protocol', 'ports', 'description', 'tags', name=_('Application Service Template')), ) class Meta: @@ -790,7 +799,7 @@ class ServiceForm(NetBoxModelForm): FieldSet( 'parent_object_type', 'parent', 'name', InlineFields('protocol', 'ports', label=_('Port(s)')), - 'ipaddresses', 'description', 'tags', name=_('Service') + 'ipaddresses', 'description', 'tags', name=_('Application Service') ), ) @@ -844,7 +853,7 @@ class ServiceForm(NetBoxModelForm): class ServiceCreateForm(ServiceForm): service_template = DynamicModelChoiceField( - label=_('Service template'), + label=_('Application Service template'), queryset=ServiceTemplate.objects.all(), required=False ) @@ -856,7 +865,7 @@ class ServiceCreateForm(ServiceForm): FieldSet('service_template', name=_('From Template')), FieldSet('name', 'protocol', 'ports', name=_('Custom')), ), - 'ipaddresses', 'description', 'tags', name=_('Service') + 'ipaddresses', 'description', 'tags', name=_('Application Service') ), ) @@ -885,4 +894,6 @@ class ServiceCreateForm(ServiceForm): if not self.cleaned_data['description']: self.cleaned_data['description'] = service_template.description elif not all(self.cleaned_data[f] for f in ('name', 'protocol', 'ports')): - raise forms.ValidationError(_("Must specify name, protocol, and port(s) if not using a service template.")) + raise forms.ValidationError( + _("Must specify name, protocol, and port(s) if not using an application service template.") + ) diff --git a/netbox/ipam/models/asns.py b/netbox/ipam/models/asns.py index c1d251301..ee1a5416e 100644 --- a/netbox/ipam/models/asns.py +++ b/netbox/ipam/models/asns.py @@ -5,6 +5,7 @@ from django.utils.translation import gettext_lazy as _ from ipam.fields import ASNField from ipam.querysets import ASNRangeQuerySet from netbox.models import OrganizationalModel, PrimaryModel +from netbox.models.features import ContactsMixin __all__ = ( 'ASN', @@ -88,7 +89,7 @@ class ASNRange(OrganizationalModel): return available_asns -class ASN(PrimaryModel): +class ASN(ContactsMixin, PrimaryModel): """ An autonomous system (AS) number is typically used to represent an independent routing domain. A site can have one or more ASNs assigned to it. diff --git a/netbox/ipam/models/ip.py b/netbox/ipam/models/ip.py index fea8af3ed..cef979d3f 100644 --- a/netbox/ipam/models/ip.py +++ b/netbox/ipam/models/ip.py @@ -1,5 +1,6 @@ import netaddr from django.contrib.contenttypes.fields import GenericForeignKey +from django.contrib.contenttypes.models import ContentType from django.contrib.postgres.indexes import GistIndex from django.core.exceptions import ValidationError from django.db import models @@ -8,7 +9,6 @@ from django.db.models.functions import Cast from django.utils.functional import cached_property from django.utils.translation import gettext_lazy as _ -from core.models import ObjectType from dcim.models.mixins import CachedScopeMixin from ipam.choices import * from ipam.constants import * @@ -925,7 +925,7 @@ class IPAddress(ContactsMixin, PrimaryModel): if self._original_assigned_object_id and self._original_assigned_object_type_id: parent = getattr(self.assigned_object, 'parent_object', None) - ct = ObjectType.objects.get_for_id(self._original_assigned_object_type_id) + ct = ContentType.objects.get_for_id(self._original_assigned_object_type_id) original_assigned_object = ct.get_object_for_this_type(pk=self._original_assigned_object_id) original_parent = getattr(original_assigned_object, 'parent_object', None) diff --git a/netbox/ipam/models/services.py b/netbox/ipam/models/services.py index 2afd16076..c2c9ca444 100644 --- a/netbox/ipam/models/services.py +++ b/netbox/ipam/models/services.py @@ -55,8 +55,8 @@ class ServiceTemplate(ServiceBase, PrimaryModel): class Meta: ordering = ('name',) - verbose_name = _('service template') - verbose_name_plural = _('service templates') + verbose_name = _('application service template') + verbose_name_plural = _('application service templates') class Service(ContactsMixin, ServiceBase, PrimaryModel): @@ -84,7 +84,7 @@ class Service(ContactsMixin, ServiceBase, PrimaryModel): related_name='services', blank=True, verbose_name=_('IP addresses'), - help_text=_("The specific IP addresses (if any) to which this service is bound") + help_text=_("The specific IP addresses (if any) to which this application service is bound") ) clone_fields = ['protocol', 'ports', 'description', 'parent', 'ipaddresses', ] @@ -94,5 +94,5 @@ class Service(ContactsMixin, ServiceBase, PrimaryModel): models.Index(fields=('parent_object_type', 'parent_object_id')), ) ordering = ('protocol', 'ports', 'pk') # (protocol, port) may be non-unique - verbose_name = _('service') - verbose_name_plural = _('services') + verbose_name = _('application service') + verbose_name_plural = _('application services') diff --git a/netbox/ipam/tests/test_api.py b/netbox/ipam/tests/test_api.py index a7562a53b..16a24b773 100644 --- a/netbox/ipam/tests/test_api.py +++ b/netbox/ipam/tests/test_api.py @@ -1108,6 +1108,10 @@ class VLANTranslationRuleTest(APIViewTestCases.APIViewTestCase): name='Policy 2', description='foobar2', ), + VLANTranslationPolicy( + name='Policy 3', + description='foobar2', + ), ) VLANTranslationPolicy.objects.bulk_create(vlan_translation_policies) @@ -1152,7 +1156,8 @@ class VLANTranslationRuleTest(APIViewTestCases.APIViewTestCase): ] cls.bulk_update_data = { - 'policy': vlan_translation_policies[1].pk, + 'policy': vlan_translation_policies[2].pk, + 'description': 'New description', } @@ -1162,6 +1167,7 @@ class ServiceTemplateTest(APIViewTestCases.APIViewTestCase): bulk_update_data = { 'description': 'New description', } + graphql_base_name = 'service_template' @classmethod def setUpTestData(cls): @@ -1197,6 +1203,7 @@ class ServiceTest(APIViewTestCases.APIViewTestCase): bulk_update_data = { 'description': 'New description', } + graphql_base_name = 'service' @classmethod def setUpTestData(cls): diff --git a/netbox/ipam/tests/test_filtersets.py b/netbox/ipam/tests/test_filtersets.py index 852fd3ea9..54ad5df90 100644 --- a/netbox/ipam/tests/test_filtersets.py +++ b/netbox/ipam/tests/test_filtersets.py @@ -1101,6 +1101,9 @@ class IPAddressTestCase(TestCase, ChangeLoggedFilterSetTests): queryset = IPAddress.objects.all() filterset = IPAddressFilterSet ignore_fields = ('fhrpgroup',) + filter_name_map = { + 'application_service': 'service', + } @classmethod def setUpTestData(cls): diff --git a/netbox/ipam/views.py b/netbox/ipam/views.py index 37e56ff3d..53e42c4fa 100644 --- a/netbox/ipam/views.py +++ b/netbox/ipam/views.py @@ -10,6 +10,7 @@ from dcim.filtersets import InterfaceFilterSet from dcim.forms import InterfaceFilterForm from dcim.models import Device, Interface, Site from ipam.tables import VLANTranslationRuleTable +from netbox.object_actions import AddObject, BulkDelete, BulkEdit, BulkExport, BulkImport from netbox.views import generic from utilities.query import count_related from utilities.tables import get_table_ordering @@ -104,6 +105,11 @@ class VRFBulkEditView(generic.BulkEditView): form = forms.VRFBulkEditForm +@register_model_view(VRF, 'bulk_rename', path='rename', detail=False) +class VRFBulkRenameView(generic.BulkRenameView): + queryset = VRF.objects.all() + + @register_model_view(VRF, 'bulk_delete', path='delete', detail=False) class VRFBulkDeleteView(generic.BulkDeleteView): queryset = VRF.objects.all() @@ -154,6 +160,11 @@ class RouteTargetBulkEditView(generic.BulkEditView): form = forms.RouteTargetBulkEditForm +@register_model_view(RouteTarget, 'bulk_rename', path='rename', detail=False) +class RouteTargetBulkRenameView(generic.BulkRenameView): + queryset = RouteTarget.objects.all() + + @register_model_view(RouteTarget, 'bulk_delete', path='delete', detail=False) class RouteTargetBulkDeleteView(generic.BulkDeleteView): queryset = RouteTarget.objects.all() @@ -213,6 +224,11 @@ class RIRBulkEditView(generic.BulkEditView): form = forms.RIRBulkEditForm +@register_model_view(RIR, 'bulk_rename', path='rename', detail=False) +class RIRBulkRenameView(generic.BulkRenameView): + queryset = RIR.objects.all() + + @register_model_view(RIR, 'bulk_delete', path='delete', detail=False) class RIRBulkDeleteView(generic.BulkDeleteView): queryset = RIR.objects.annotate( @@ -286,6 +302,11 @@ class ASNRangeBulkEditView(generic.BulkEditView): form = forms.ASNRangeBulkEditForm +@register_model_view(ASNRange, 'bulk_rename', path='rename', detail=False) +class ASNRangeBulkRenameView(generic.BulkRenameView): + queryset = ASNRange.objects.all() + + @register_model_view(ASNRange, 'bulk_delete', path='delete', detail=False) class ASNRangeBulkDeleteView(generic.BulkDeleteView): queryset = ASNRange.objects.annotate_asn_counts() @@ -353,6 +374,11 @@ class ASNBulkEditView(generic.BulkEditView): form = forms.ASNBulkEditForm +@register_model_view(ASN, 'bulk_rename', path='rename', detail=False) +class ASNBulkRenameView(generic.BulkRenameView): + queryset = ASN.objects.all() + + @register_model_view(ASN, 'bulk_delete', path='delete', detail=False) class ASNBulkDeleteView(generic.BulkDeleteView): queryset = ASN.objects.annotate( @@ -374,6 +400,7 @@ class AggregateListView(generic.ObjectListView): filterset = filtersets.AggregateFilterSet filterset_form = forms.AggregateFilterForm table = tables.AggregateTable + actions = (AddObject, BulkImport, BulkExport, BulkEdit, BulkDelete) @register_model_view(Aggregate) @@ -506,6 +533,11 @@ class RoleBulkEditView(generic.BulkEditView): form = forms.RoleBulkEditForm +@register_model_view(Role, 'bulk_rename', path='rename', detail=False) +class RoleBulkRenameView(generic.BulkRenameView): + queryset = Role.objects.all() + + @register_model_view(Role, 'bulk_delete', path='delete', detail=False) class RoleBulkDeleteView(generic.BulkDeleteView): queryset = Role.objects.all() @@ -524,6 +556,7 @@ class PrefixListView(generic.ObjectListView): filterset_form = forms.PrefixFilterForm table = tables.PrefixTable template_name = 'ipam/prefix_list.html' + actions = (AddObject, BulkImport, BulkExport, BulkEdit, BulkDelete) @register_model_view(Prefix) @@ -784,6 +817,11 @@ class IPRangeBulkEditView(generic.BulkEditView): form = forms.IPRangeBulkEditForm +@register_model_view(IPRange, 'bulk_rename', path='rename', detail=False) +class IPRangeBulkRenameView(generic.BulkRenameView): + queryset = IPRange.objects.all() + + @register_model_view(IPRange, 'bulk_delete', path='delete', detail=False) class IPRangeBulkDeleteView(generic.BulkDeleteView): queryset = IPRange.objects.all() @@ -801,6 +839,7 @@ class IPAddressListView(generic.ObjectListView): filterset = filtersets.IPAddressFilterSet filterset_form = forms.IPAddressFilterForm table = tables.IPAddressTable + actions = (AddObject, BulkImport, BulkExport, BulkEdit, BulkDelete) @register_model_view(IPAddress) @@ -1024,6 +1063,11 @@ class VLANGroupBulkEditView(generic.BulkEditView): form = forms.VLANGroupBulkEditForm +@register_model_view(VLANGroup, 'bulk_rename', path='rename', detail=False) +class VLANGroupBulkRenameView(generic.BulkRenameView): + queryset = VLANGroup.objects.all() + + @register_model_view(VLANGroup, 'bulk_delete', path='delete', detail=False) class VLANGroupBulkDeleteView(generic.BulkDeleteView): queryset = VLANGroup.objects.annotate_utilization().prefetch_related('tags') @@ -1113,6 +1157,11 @@ class VLANTranslationPolicyBulkEditView(generic.BulkEditView): form = forms.VLANTranslationPolicyBulkEditForm +@register_model_view(VLANTranslationPolicy, 'bulk_rename', path='rename', detail=False) +class VLANTranslationPolicyBulkRenameView(generic.BulkRenameView): + queryset = VLANTranslationPolicy.objects.all() + + @register_model_view(VLANTranslationPolicy, 'bulk_delete', path='delete', detail=False) class VLANTranslationPolicyBulkDeleteView(generic.BulkDeleteView): queryset = VLANTranslationPolicy.objects.all() @@ -1130,6 +1179,7 @@ class VLANTranslationRuleListView(generic.ObjectListView): filterset = filtersets.VLANTranslationRuleFilterSet filterset_form = forms.VLANTranslationRuleFilterForm table = tables.VLANTranslationRuleTable + actions = (AddObject, BulkImport, BulkExport, BulkEdit, BulkDelete) @register_model_view(VLANTranslationRule) @@ -1262,6 +1312,11 @@ class FHRPGroupBulkEditView(generic.BulkEditView): form = forms.FHRPGroupBulkEditForm +@register_model_view(FHRPGroup, 'bulk_rename', path='rename', detail=False) +class FHRPGroupBulkRenameView(generic.BulkRenameView): + queryset = FHRPGroup.objects.all() + + @register_model_view(FHRPGroup, 'bulk_delete', path='delete', detail=False) class FHRPGroupBulkDeleteView(generic.BulkDeleteView): queryset = FHRPGroup.objects.all() @@ -1389,6 +1444,11 @@ class VLANBulkEditView(generic.BulkEditView): form = forms.VLANBulkEditForm +@register_model_view(VLAN, 'bulk_rename', path='rename', detail=False) +class VLANBulkRenameView(generic.BulkRenameView): + queryset = VLAN.objects.all() + + @register_model_view(VLAN, 'bulk_delete', path='delete', detail=False) class VLANBulkDeleteView(generic.BulkDeleteView): queryset = VLAN.objects.all() @@ -1439,6 +1499,11 @@ class ServiceTemplateBulkEditView(generic.BulkEditView): form = forms.ServiceTemplateBulkEditForm +@register_model_view(ServiceTemplate, 'bulk_rename', path='rename', detail=False) +class ServiceTemplateBulkRenameView(generic.BulkRenameView): + queryset = ServiceTemplate.objects.all() + + @register_model_view(ServiceTemplate, 'bulk_delete', path='delete', detail=False) class ServiceTemplateBulkDeleteView(generic.BulkDeleteView): queryset = ServiceTemplate.objects.all() @@ -1506,6 +1571,11 @@ class ServiceBulkEditView(generic.BulkEditView): form = forms.ServiceBulkEditForm +@register_model_view(Service, 'bulk_rename', path='rename', detail=False) +class ServiceBulkRenameView(generic.BulkRenameView): + queryset = Service.objects.all() + + @register_model_view(Service, 'bulk_delete', path='delete', detail=False) class ServiceBulkDeleteView(generic.BulkDeleteView): queryset = Service.objects.prefetch_related('parent') diff --git a/netbox/media/devicetype-images/.gitignore b/netbox/media/devicetype-images/.gitignore deleted file mode 100644 index d6b7ef32c..000000000 --- a/netbox/media/devicetype-images/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -* -!.gitignore diff --git a/netbox/media/image-attachments/.gitignore b/netbox/media/image-attachments/.gitignore deleted file mode 100644 index d6b7ef32c..000000000 --- a/netbox/media/image-attachments/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -* -!.gitignore diff --git a/netbox/netbox/api/serializers/__init__.py b/netbox/netbox/api/serializers/__init__.py index 0ec3ab5f3..d7ad19565 100644 --- a/netbox/netbox/api/serializers/__init__.py +++ b/netbox/netbox/api/serializers/__init__.py @@ -10,7 +10,12 @@ from .nested import * # Base model serializers # -class NetBoxModelSerializer(TaggableModelSerializer, CustomFieldModelSerializer, ValidatedModelSerializer): +class NetBoxModelSerializer( + ChangeLogMessageSerializer, + TaggableModelSerializer, + CustomFieldModelSerializer, + ValidatedModelSerializer +): """ Adds support for custom fields and tags. """ @@ -24,5 +29,5 @@ class NestedGroupModelSerializer(NetBoxModelSerializer): _depth = serializers.IntegerField(source='level', read_only=True) -class BulkOperationSerializer(serializers.Serializer): +class BulkOperationSerializer(ChangeLogMessageSerializer): id = serializers.IntegerField() diff --git a/netbox/netbox/api/serializers/features.py b/netbox/netbox/api/serializers/features.py index 3bd5c8a2d..1ee92e828 100644 --- a/netbox/netbox/api/serializers/features.py +++ b/netbox/netbox/api/serializers/features.py @@ -5,6 +5,7 @@ from extras.api.customfields import CustomFieldsDataField, CustomFieldDefaultVal from .nested import NestedTagSerializer __all__ = ( + 'ChangeLogMessageSerializer', 'CustomFieldModelSerializer', 'TaggableModelSerializer', ) @@ -54,3 +55,24 @@ class TaggableModelSerializer(serializers.Serializer): instance.tags.clear() return instance + + +class ChangeLogMessageSerializer(serializers.Serializer): + changelog_message = serializers.CharField( + write_only=True, + required=False, + ) + + def to_internal_value(self, data): + ret = super().to_internal_value(data) + + # Workaround to bypass requirement to include changelog_message in Meta.fields on every serializer + if type(data) is dict and 'changelog_message' in data: + ret['changelog_message'] = data['changelog_message'] + + return ret + + def save(self, **kwargs): + if self.instance is not None: + self.instance._changelog_message = self.validated_data.get('changelog_message') + return super().save(**kwargs) diff --git a/netbox/netbox/api/views.py b/netbox/netbox/api/views.py index 1befda371..6740700b8 100644 --- a/netbox/netbox/api/views.py +++ b/netbox/netbox/api/views.py @@ -1,7 +1,6 @@ import platform from django import __version__ as DJANGO_VERSION -from django.apps import apps from django.conf import settings from django_rq.queues import get_connection from drf_spectacular.types import OpenApiTypes @@ -13,6 +12,7 @@ from rq.worker import Worker from netbox.api.authentication import IsAuthenticatedOrLoginNotRequired from netbox.plugins.utils import get_installed_plugins +from utilities.apps import get_installed_apps class APIRootView(APIView): @@ -52,20 +52,10 @@ class StatusView(APIView): @extend_schema(responses={200: OpenApiTypes.OBJECT}) def get(self, request): - # Gather the version numbers from all installed Django apps - installed_apps = {} - for app_config in apps.get_app_configs(): - app = app_config.module - version = getattr(app, 'VERSION', getattr(app, '__version__', None)) - if version: - if type(version) is tuple: - version = '.'.join(str(n) for n in version) - installed_apps[app_config.name] = version - installed_apps = {k: v for k, v in sorted(installed_apps.items())} - return Response({ 'django-version': DJANGO_VERSION, - 'installed-apps': installed_apps, + 'hostname': settings.HOSTNAME, + 'installed_apps': get_installed_apps(), 'netbox-version': settings.RELEASE.version, 'netbox-full-version': settings.RELEASE.full_version, 'plugins': get_installed_plugins(), diff --git a/netbox/netbox/api/viewsets/__init__.py b/netbox/netbox/api/viewsets/__init__.py index 2039f735b..6241be4cd 100644 --- a/netbox/netbox/api/viewsets/__init__.py +++ b/netbox/netbox/api/viewsets/__init__.py @@ -7,9 +7,11 @@ from django.db.models import ProtectedError, RestrictedError from django_pglocks import advisory_lock from netbox.constants import ADVISORY_LOCK_KEYS from rest_framework import mixins as drf_mixins +from rest_framework import status from rest_framework.response import Response from rest_framework.viewsets import GenericViewSet +from netbox.api.serializers.features import ChangeLogMessageSerializer from utilities.api import get_annotations_for_serializer, get_prefetches_for_serializer from utilities.exceptions import AbortRequest from utilities.query import reapply_model_ordering @@ -199,9 +201,16 @@ class NetBoxModelViewSet( # Deletes def destroy(self, request, *args, **kwargs): - # Hotwire get_object() to ensure we save a pre-change snapshot - self.get_object = self.get_object_with_snapshot - return super().destroy(request, *args, **kwargs) + instance = self.get_object_with_snapshot() + + # Attach changelog message (if any) + serializer = ChangeLogMessageSerializer(data=request.data) + serializer.is_valid(raise_exception=True) + instance._changelog_message = serializer.validated_data.get('changelog_message') + + self.perform_destroy(instance) + + return Response(status=status.HTTP_204_NO_CONTENT) def perform_destroy(self, instance): model = self.queryset.model diff --git a/netbox/netbox/api/viewsets/mixins.py b/netbox/netbox/api/viewsets/mixins.py index 4fedebad5..e74488164 100644 --- a/netbox/netbox/api/viewsets/mixins.py +++ b/netbox/netbox/api/viewsets/mixins.py @@ -149,18 +149,25 @@ class BulkDestroyModelMixin: serializer = BulkOperationSerializer(data=request.data, many=True) serializer.is_valid(raise_exception=True) qs = self.get_bulk_destroy_queryset().filter( - pk__in=[o['id'] for o in serializer.data] + pk__in=[o['id'] for o in serializer.validated_data] ) - self.perform_bulk_destroy(qs) + # Compile any changelog messages to be recorded on the objects being deleted + changelog_messages = { + o['id']: o.get('changelog_message') for o in serializer.validated_data + } + + self.perform_bulk_destroy(qs, changelog_messages) return Response(status=status.HTTP_204_NO_CONTENT) - def perform_bulk_destroy(self, objects): + def perform_bulk_destroy(self, objects, changelog_messages=None): + changelog_messages = changelog_messages or {} with transaction.atomic(using=router.db_for_write(self.queryset.model)): for obj in objects: if hasattr(obj, 'snapshot'): obj.snapshot() + obj._changelog_message = changelog_messages.get(obj.pk) self.perform_destroy(obj) diff --git a/netbox/netbox/choices.py b/netbox/netbox/choices.py index 5c3110745..4c2b2478a 100644 --- a/netbox/netbox/choices.py +++ b/netbox/netbox/choices.py @@ -151,12 +151,14 @@ class CSVDelimiterChoices(ChoiceSet): AUTO = 'auto' COMMA = CSV_DELIMITERS['comma'] SEMICOLON = CSV_DELIMITERS['semicolon'] + PIPE = CSV_DELIMITERS['pipe'] TAB = CSV_DELIMITERS['tab'] CHOICES = [ (AUTO, _('Auto-detect')), (COMMA, _('Comma')), (SEMICOLON, _('Semicolon')), + (PIPE, _('Pipe')), (TAB, _('Tab')), ] diff --git a/netbox/netbox/constants.py b/netbox/netbox/constants.py index 8d20fed45..a0ba966d7 100644 --- a/netbox/netbox/constants.py +++ b/netbox/netbox/constants.py @@ -1,3 +1,18 @@ +CORE_APPS = ( + 'account', + 'circuits', + 'core', + 'dcim', + 'extras', + 'ipam', + 'tenancy', + 'users', + 'utilities', + 'virtualization', + 'vpn', + 'wireless', +) + # RQ queue names RQ_QUEUE_DEFAULT = 'default' RQ_QUEUE_HIGH = 'high' @@ -23,12 +38,14 @@ ADVISORY_LOCK_KEYS = { 'wirelesslangroup': 105600, 'inventoryitem': 105700, 'inventoryitemtemplate': 105800, + 'platform': 105900, # Jobs 'job-schedules': 110100, } -# Default view action permission mapping +# TODO: Remove in NetBox v4.5 +# Legacy default view action permission mapping DEFAULT_ACTION_PERMISSIONS = { 'add': {'add'}, 'export': {'view'}, diff --git a/netbox/netbox/forms/base.py b/netbox/netbox/forms/base.py index 632d5ecb1..14916a733 100644 --- a/netbox/netbox/forms/base.py +++ b/netbox/netbox/forms/base.py @@ -8,10 +8,10 @@ from django.utils.translation import gettext_lazy as _ from core.models import ObjectType from extras.choices import * from extras.models import CustomField, Tag -from utilities.forms import CSVModelForm +from utilities.forms import BulkEditForm, CSVModelForm from utilities.forms.fields import CSVModelMultipleChoiceField, DynamicModelMultipleChoiceField from utilities.forms.mixins import CheckLastUpdatedMixin -from .mixins import CustomFieldsMixin, SavedFiltersMixin, TagsMixin +from .mixins import ChangelogMessageMixin, CustomFieldsMixin, SavedFiltersMixin, TagsMixin __all__ = ( 'NetBoxModelForm', @@ -21,7 +21,7 @@ __all__ = ( ) -class NetBoxModelForm(CheckLastUpdatedMixin, CustomFieldsMixin, TagsMixin, forms.ModelForm): +class NetBoxModelForm(ChangelogMessageMixin, CheckLastUpdatedMixin, CustomFieldsMixin, TagsMixin, forms.ModelForm): """ Base form for creating & editing NetBox models. Extends Django's ModelForm to add support for custom fields. @@ -100,7 +100,7 @@ class NetBoxModelImportForm(CSVModelForm, NetBoxModelForm): return customfield.to_form_field(for_csv_import=True) -class NetBoxModelBulkEditForm(CustomFieldsMixin, forms.Form): +class NetBoxModelBulkEditForm(ChangelogMessageMixin, CustomFieldsMixin, BulkEditForm): """ Base form for modifying multiple NetBox objects (of the same type) in bulk via the UI. Adds support for custom fields and adding/removing tags. @@ -108,9 +108,8 @@ class NetBoxModelBulkEditForm(CustomFieldsMixin, forms.Form): Attributes: fieldsets: An iterable of two-tuples which define a heading and field set to display per section of the rendered form (optional). If not defined, the all fields will be rendered as a single section. - nullable_fields: A list of field names indicating which fields support being set to null/empty """ - nullable_fields = () + fieldsets = None pk = forms.ModelMultipleChoiceField( queryset=None, # Set from self.model on init diff --git a/netbox/netbox/forms/mixins.py b/netbox/netbox/forms/mixins.py index c569343ee..4096ffb25 100644 --- a/netbox/netbox/forms/mixins.py +++ b/netbox/netbox/forms/mixins.py @@ -7,12 +7,32 @@ from extras.models import * from utilities.forms.fields import DynamicModelMultipleChoiceField __all__ = ( + 'ChangelogMessageMixin', 'CustomFieldsMixin', 'SavedFiltersMixin', 'TagsMixin', ) +class ChangelogMessageMixin(forms.Form): + """ + Adds an optional field for recording a message on the resulting changelog record(s). + """ + changelog_message = forms.CharField( + required=False, + max_length=200 + ) + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + # Declare changelog_message a meta field + if hasattr(self, 'meta_fields'): + self.meta_fields.append('changelog_message') + else: + self.meta_fields = ['changelog_message'] + + class CustomFieldsMixin: """ Extend a Form to include custom field support. diff --git a/netbox/netbox/jobs.py b/netbox/netbox/jobs.py index 7b688e7a2..559619ac0 100644 --- a/netbox/netbox/jobs.py +++ b/netbox/netbox/jobs.py @@ -12,8 +12,10 @@ from core.exceptions import JobFailed from core.models import Job, ObjectType from netbox.constants import ADVISORY_LOCK_KEYS from netbox.registry import registry +from utilities.request import apply_request_processors __all__ = ( + 'AsyncViewJob', 'JobRunner', 'system_job', ) @@ -35,6 +37,19 @@ def system_job(interval): return _wrapper +class JobLogHandler(logging.Handler): + """ + A logging handler which records entries on a Job. + """ + def __init__(self, job, *args, **kwargs): + super().__init__(*args, **kwargs) + self.job = job + + def emit(self, record): + # Enter the record in the log of the associated Job + self.job.log(record) + + class JobRunner(ABC): """ Background Job helper class. @@ -53,6 +68,11 @@ class JobRunner(ABC): """ self.job = job + # Initiate the system logger + self.logger = logging.getLogger(f"netbox.jobs.{self.__class__.__name__}") + self.logger.setLevel(logging.DEBUG) + self.logger.addHandler(JobLogHandler(job)) + @classproperty def name(cls): return getattr(cls.Meta, 'name', cls.__name__) @@ -161,3 +181,22 @@ class JobRunner(ABC): job.delete() return cls.enqueue(instance=instance, schedule_at=schedule_at, interval=interval, *args, **kwargs) + + +class AsyncViewJob(JobRunner): + """ + Execute a view as a background job. + """ + class Meta: + name = 'Async View' + + def run(self, view_cls, request, **kwargs): + view = view_cls.as_view() + request.job = self + + # Apply all registered request processors (e.g. event_tracking) + with apply_request_processors(request): + view(request) + + if self.job.error: + raise JobFailed() diff --git a/netbox/netbox/metrics.py b/netbox/netbox/metrics.py new file mode 100644 index 000000000..59619d34a --- /dev/null +++ b/netbox/netbox/metrics.py @@ -0,0 +1,40 @@ +from django_prometheus.conf import NAMESPACE +from django_prometheus import middleware +from prometheus_client import Counter + +__all__ = ( + 'Metrics', +) + + +class Metrics(middleware.Metrics): + """ + Expand the stock Metrics class from django_prometheus to add our own counters. + """ + + def register(self): + super().register() + + # REST API metrics + self.rest_api_requests = self.register_metric( + Counter, + "rest_api_requests_total_by_method", + "Count of total REST API requests by method", + ["method"], + namespace=NAMESPACE, + ) + self.rest_api_requests_by_view_method = self.register_metric( + Counter, + "rest_api_requests_total_by_view_method", + "Count of REST API requests by view & method", + ["view", "method"], + namespace=NAMESPACE, + ) + + # GraphQL API metrics + self.graphql_api_requests = self.register_metric( + Counter, + "graphql_api_requests_total", + "Count of total GraphQL API requests", + namespace=NAMESPACE, + ) diff --git a/netbox/netbox/middleware.py b/netbox/netbox/middleware.py index 3a9cb0f78..66c980778 100644 --- a/netbox/netbox/middleware.py +++ b/netbox/netbox/middleware.py @@ -1,8 +1,5 @@ -from contextlib import ExitStack - import logging import uuid -import warnings from django.conf import settings from django.contrib import auth, messages @@ -11,16 +8,20 @@ from django.core.exceptions import ImproperlyConfigured from django.db import connection, ProgrammingError from django.db.utils import InternalError from django.http import Http404, HttpResponseRedirect +from django_prometheus import middleware from netbox.config import clear_config, get_config -from netbox.registry import registry +from netbox.metrics import Metrics from netbox.views import handler_500 -from utilities.api import is_api_request +from utilities.api import is_api_request, is_graphql_request from utilities.error_handlers import handle_rest_api_exception +from utilities.request import apply_request_processors __all__ = ( 'CoreMiddleware', 'MaintenanceModeMiddleware', + 'PrometheusAfterMiddleware', + 'PrometheusBeforeMiddleware', 'RemoteUserMiddleware', ) @@ -36,12 +37,7 @@ class CoreMiddleware: request.id = uuid.uuid4() # Apply all registered request processors - with ExitStack() as stack: - for request_processor in registry['request_processors']: - try: - stack.enter_context(request_processor(request)) - except Exception as e: - warnings.warn(f'Failed to initialize request processor {request_processor}: {e}') + with apply_request_processors(request): response = self.get_response(request) # Check if language cookie should be renewed @@ -188,6 +184,30 @@ class RemoteUserMiddleware(RemoteUserMiddleware_): return groups +class PrometheusBeforeMiddleware(middleware.PrometheusBeforeMiddleware): + metrics_cls = Metrics + + +class PrometheusAfterMiddleware(middleware.PrometheusAfterMiddleware): + metrics_cls = Metrics + + def process_response(self, request, response): + response = super().process_response(request, response) + + # Increment REST API request counters + if is_api_request(request): + method = self._method(request) + name = self._get_view_name(request) + self.label_metric(self.metrics.rest_api_requests, request, method=method).inc() + self.label_metric(self.metrics.rest_api_requests_by_view_method, request, method=method, view=name).inc() + + # Increment GraphQL API request counters + elif is_graphql_request(request): + self.metrics.graphql_api_requests.inc() + + return response + + class MaintenanceModeMiddleware: """ Middleware that checks if the application is in maintenance mode diff --git a/netbox/netbox/models/features.py b/netbox/netbox/models/features.py index 79145ce70..09c2722ad 100644 --- a/netbox/netbox/models/features.py +++ b/netbox/netbox/models/features.py @@ -3,6 +3,7 @@ from collections import defaultdict from functools import cached_property from django.contrib.contenttypes.fields import GenericRelation +from django.contrib.contenttypes.models import ContentType from django.core.validators import ValidationError from django.db import models from django.db.models import Q @@ -16,9 +17,12 @@ from extras.choices import * from extras.constants import CUSTOMFIELD_EMPTY_VALUES from extras.utils import is_taggable from netbox.config import get_config +from netbox.constants import CORE_APPS from netbox.models.deletion import DeleteMixin +from netbox.plugins import PluginConfig from netbox.registry import registry from netbox.signals import post_clean +from netbox.utils import register_model_feature from utilities.json import CustomFieldJSONEncoder from utilities.serialization import serialize_object @@ -38,6 +42,9 @@ __all__ = ( 'NotificationsMixin', 'SyncedDataMixin', 'TagsMixin', + 'get_model_features', + 'has_feature', + 'model_is_public', 'register_models', ) @@ -66,6 +73,11 @@ class ChangeLoggingMixin(DeleteMixin, models.Model): class Meta: abstract = True + def __init__(self, *args, **kwargs): + changelog_message = kwargs.pop('changelog_message', None) + super().__init__(*args, **kwargs) + self._changelog_message = changelog_message + def serialize_object(self, exclude=None): """ Return a JSON representation of the instance. Models can override this method to replace or extend the default @@ -103,7 +115,8 @@ class ChangeLoggingMixin(DeleteMixin, models.Model): objectchange = ObjectChange( changed_object=self, object_repr=str(self)[:200], - action=action + action=action, + message=self._changelog_message or '', ) if hasattr(self, '_prechange_snapshot'): objectchange.prechange_data = self._prechange_snapshot @@ -615,27 +628,62 @@ class SyncedDataMixin(models.Model): # Feature registration # -FEATURES_MAP = { - 'bookmarks': BookmarksMixin, - 'change_logging': ChangeLoggingMixin, - 'cloning': CloningMixin, - 'contacts': ContactsMixin, - 'custom_fields': CustomFieldsMixin, - 'custom_links': CustomLinksMixin, - 'custom_validation': CustomValidationMixin, - 'event_rules': EventRulesMixin, - 'export_templates': ExportTemplatesMixin, - 'image_attachments': ImageAttachmentsMixin, - 'jobs': JobsMixin, - 'journaling': JournalingMixin, - 'notifications': NotificationsMixin, - 'synced_data': SyncedDataMixin, - 'tags': TagsMixin, -} +register_model_feature('bookmarks', lambda model: issubclass(model, BookmarksMixin)) +register_model_feature('change_logging', lambda model: issubclass(model, ChangeLoggingMixin)) +register_model_feature('cloning', lambda model: issubclass(model, CloningMixin)) +register_model_feature('contacts', lambda model: issubclass(model, ContactsMixin)) +register_model_feature('custom_fields', lambda model: issubclass(model, CustomFieldsMixin)) +register_model_feature('custom_links', lambda model: issubclass(model, CustomLinksMixin)) +register_model_feature('custom_validation', lambda model: issubclass(model, CustomValidationMixin)) +register_model_feature('event_rules', lambda model: issubclass(model, EventRulesMixin)) +register_model_feature('export_templates', lambda model: issubclass(model, ExportTemplatesMixin)) +register_model_feature('image_attachments', lambda model: issubclass(model, ImageAttachmentsMixin)) +register_model_feature('jobs', lambda model: issubclass(model, JobsMixin)) +register_model_feature('journaling', lambda model: issubclass(model, JournalingMixin)) +register_model_feature('notifications', lambda model: issubclass(model, NotificationsMixin)) +register_model_feature('synced_data', lambda model: issubclass(model, SyncedDataMixin)) +register_model_feature('tags', lambda model: issubclass(model, TagsMixin)) -registry['model_features'].update({ - feature: defaultdict(set) for feature in FEATURES_MAP.keys() -}) + +def model_is_public(model): + """ + Return True if the model is considered "public use;" otherwise return False. + + All non-core and non-plugin models are excluded. + """ + opts = model._meta + if opts.app_label not in CORE_APPS and not isinstance(opts.app_config, PluginConfig): + return False + return not getattr(model, '_netbox_private', False) + + +def get_model_features(model): + """ + Return all features supported by the given model. + """ + return [ + feature for feature, test_func in registry['model_features'].items() if test_func(model) + ] + + +def has_feature(model_or_ct, feature): + """ + Returns True if the model supports the specified feature. + """ + # If an ObjectType was passed, we can use it directly + if type(model_or_ct) is ObjectType: + ot = model_or_ct + # If a ContentType was passed, resolve its model class + elif type(model_or_ct) is ContentType: + model_class = model_or_ct.model_class() + ot = ObjectType.objects.get_for_model(model_class) if model_class else None + # For anything else, look up the ObjectType + else: + ot = ObjectType.objects.get_for_model(model_or_ct) + # ObjectType is invalid/deleted + if ot is None: + return False + return feature in ot.features def register_models(*models): @@ -653,22 +701,11 @@ def register_models(*models): for model in models: app_label, model_name = model._meta.label_lower.split('.') + # TODO: Remove in NetBox v4.5 # Register public models if not getattr(model, '_netbox_private', False): registry['models'][app_label].add(model_name) - # Record each applicable feature for the model in the registry - features = { - feature for feature, cls in FEATURES_MAP.items() if issubclass(model, cls) - } - for feature in features: - try: - registry['model_features'][feature][app_label].add(model_name) - except KeyError: - raise KeyError( - f"{feature} is not a valid model feature! Valid keys are: {registry['model_features'].keys()}" - ) - # Register applicable feature views for the model if issubclass(model, ContactsMixin): register_model_view(model, 'contacts', kwargs={'model': model})( @@ -686,6 +723,10 @@ def register_models(*models): register_model_view(model, 'jobs', kwargs={'model': model})( 'netbox.views.generic.ObjectJobsView' ) + if issubclass(model, ImageAttachmentsMixin): + register_model_view(model, 'image-attachments', kwargs={'model': model})( + 'netbox.views.generic.ObjectImageAttachmentsView' + ) if issubclass(model, SyncedDataMixin): register_model_view(model, 'sync', kwargs={'model': model})( 'netbox.views.generic.ObjectSyncDataView' diff --git a/netbox/netbox/navigation/menu.py b/netbox/netbox/navigation/menu.py index 982d40829..c4aac5bfe 100644 --- a/netbox/netbox/navigation/menu.py +++ b/netbox/netbox/navigation/menu.py @@ -209,8 +209,8 @@ IPAM_MENU = Menu( label=_('Other'), items=( get_model_item('ipam', 'fhrpgroup', _('FHRP Groups')), - get_model_item('ipam', 'servicetemplate', _('Service Templates')), - get_model_item('ipam', 'service', _('Services')), + get_model_item('ipam', 'servicetemplate', _('Application Service Templates')), + get_model_item('ipam', 'service', _('Application Services')), ), ), ), @@ -331,6 +331,7 @@ PROVISIONING_MENU = Menu( label=_('Configurations'), items=( get_model_item('extras', 'configcontext', _('Config Contexts'), actions=['add']), + get_model_item('extras', 'configcontextprofile', _('Config Context Profiles')), get_model_item('extras', 'configtemplate', _('Config Templates'), actions=['add']), ), ), diff --git a/netbox/netbox/object_actions.py b/netbox/netbox/object_actions.py new file mode 100644 index 000000000..f812c6b40 --- /dev/null +++ b/netbox/netbox/object_actions.py @@ -0,0 +1,189 @@ +from django.template import loader +from django.urls.exceptions import NoReverseMatch +from django.utils.translation import gettext as _ + +from core.models import ObjectType +from extras.models import ExportTemplate +from utilities.querydict import prepare_cloned_fields +from utilities.views import get_action_url + +__all__ = ( + 'AddObject', + 'BulkDelete', + 'BulkEdit', + 'BulkExport', + 'BulkImport', + 'BulkRename', + 'CloneObject', + 'DeleteObject', + 'EditObject', + 'ObjectAction', +) + + +class ObjectAction: + """ + Base class for single- and multi-object operations. + + Params: + name: The action name appended to the module for view resolution + label: Human-friendly label for the rendered button + template_name: Name of the HTML template which renders the button + multi: Set to True if this action is performed by selecting multiple objects (i.e. using a table) + permissions_required: The set of permissions a user must have to perform the action + url_kwargs: The set of URL keyword arguments to pass when resolving the view's URL + """ + name = '' + label = None + template_name = None + multi = False + permissions_required = set() + url_kwargs = [] + + @classmethod + def get_url(cls, obj): + kwargs = { + kwarg: getattr(obj, kwarg) for kwarg in cls.url_kwargs + } + try: + return get_action_url(obj, action=cls.name, kwargs=kwargs) + except NoReverseMatch: + return + + @classmethod + def get_context(cls, context, obj): + """ + Return any additional context data needed to render the button. + """ + return {} + + @classmethod + def render(cls, context, obj, **kwargs): + ctx = { + 'perms': context['perms'], + 'request': context['request'], + 'url': cls.get_url(obj), + 'label': cls.label, + **cls.get_context(context, obj), + **kwargs, + } + return loader.render_to_string(cls.template_name, ctx) + + +class AddObject(ObjectAction): + """ + Create a new object. + """ + name = 'add' + label = _('Add') + permissions_required = {'add'} + template_name = 'buttons/add.html' + + +class CloneObject(ObjectAction): + """ + Populate the new object form with select details from an existing object. + """ + name = 'add' + label = _('Clone') + permissions_required = {'add'} + template_name = 'buttons/clone.html' + + @classmethod + def get_url(cls, obj): + url = super().get_url(obj) + param_string = prepare_cloned_fields(obj).urlencode() + return f'{url}?{param_string}' if param_string else None + + +class EditObject(ObjectAction): + """ + Edit a single object. + """ + name = 'edit' + label = _('Edit') + permissions_required = {'change'} + url_kwargs = ['pk'] + template_name = 'buttons/edit.html' + + +class DeleteObject(ObjectAction): + """ + Delete a single object. + """ + name = 'delete' + label = _('Delete') + permissions_required = {'delete'} + url_kwargs = ['pk'] + template_name = 'buttons/delete.html' + + +class BulkImport(ObjectAction): + """ + Import multiple objects at once. + """ + name = 'bulk_import' + label = _('Import') + permissions_required = {'add'} + template_name = 'buttons/import.html' + + +class BulkExport(ObjectAction): + """ + Export multiple objects at once. + """ + name = 'export' + label = _('Export') + permissions_required = {'view'} + template_name = 'buttons/export.html' + + @classmethod + def get_context(cls, context, model): + object_type = ObjectType.objects.get_for_model(model) + user = context['request'].user + + # Determine if the "all data" export returns CSV or YAML + data_format = 'YAML' if hasattr(object_type.model_class(), 'to_yaml') else 'CSV' + + # Retrieve all export templates for this model + export_templates = ExportTemplate.objects.restrict(user, 'view').filter(object_types=object_type) + + return { + 'object_type': object_type, + 'url_params': context['request'].GET.urlencode() if context['request'].GET else '', + 'export_templates': export_templates, + 'data_format': data_format, + } + + +class BulkEdit(ObjectAction): + """ + Change the value of one or more fields on a set of objects. + """ + name = 'bulk_edit' + label = _('Edit Selected') + multi = True + permissions_required = {'change'} + template_name = 'buttons/bulk_edit.html' + + +class BulkRename(ObjectAction): + """ + Rename multiple objects at once. + """ + name = 'bulk_rename' + label = _('Rename Selected') + multi = True + permissions_required = {'change'} + template_name = 'buttons/bulk_rename.html' + + +class BulkDelete(ObjectAction): + """ + Delete each of a set of objects. + """ + name = 'bulk_delete' + label = _('Delete Selected') + multi = True + permissions_required = {'delete'} + template_name = 'buttons/bulk_delete.html' diff --git a/netbox/netbox/preferences.py b/netbox/netbox/preferences.py index 31e99824e..d8fb130f4 100644 --- a/netbox/netbox/preferences.py +++ b/netbox/netbox/preferences.py @@ -3,6 +3,7 @@ from django.utils.translation import gettext_lazy as _ from netbox.registry import registry from users.preferences import UserPreference +from utilities.constants import CSV_DELIMITERS from utilities.paginator import EnhancedPaginator @@ -12,6 +13,16 @@ def get_page_lengths(): ] +def get_csv_delimiters(): + choices = [] + for k, v in CSV_DELIMITERS.items(): + label = _(k.title()) + if v.strip(): + label = f'{label} ({v})' + choices.append((k, label)) + return choices + + PREFERENCES = { # User interface @@ -72,6 +83,12 @@ PREFERENCES = { ), description=_('The preferred syntax for displaying generic data within the UI') ), + 'csv_delimiter': UserPreference( + label=_('CSV delimiter'), + choices=get_csv_delimiters(), + default='comma', + description=_('The character used to separate fields in CSV data') + ), } diff --git a/netbox/netbox/registry.py b/netbox/netbox/registry.py index 02b741779..fe5ce4301 100644 --- a/netbox/netbox/registry.py +++ b/netbox/netbox/registry.py @@ -34,5 +34,6 @@ registry = Registry({ 'system_jobs': dict(), 'tables': collections.defaultdict(dict), 'views': collections.defaultdict(dict), + 'webhook_callbacks': list(), 'widgets': dict(), }) diff --git a/netbox/netbox/settings.py b/netbox/netbox/settings.py index 239b5978f..c9eed75e1 100644 --- a/netbox/netbox/settings.py +++ b/netbox/netbox/settings.py @@ -27,7 +27,6 @@ from utilities.string import trailing_slash RELEASE = load_release_data() VERSION = RELEASE.full_version # Retained for backward compatibility -HOSTNAME = platform.node() # Set the base directory two levels up BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) @@ -125,6 +124,7 @@ EXEMPT_VIEW_PERMISSIONS = getattr(configuration, 'EXEMPT_VIEW_PERMISSIONS', []) FIELD_CHOICES = getattr(configuration, 'FIELD_CHOICES', {}) FILE_UPLOAD_MAX_MEMORY_SIZE = getattr(configuration, 'FILE_UPLOAD_MAX_MEMORY_SIZE', 2621440) GRAPHQL_MAX_ALIASES = getattr(configuration, 'GRAPHQL_MAX_ALIASES', 10) +HOSTNAME = getattr(configuration, 'HOSTNAME', platform.node()) HTTP_PROXIES = getattr(configuration, 'HTTP_PROXIES', {}) INTERNAL_IPS = getattr(configuration, 'INTERNAL_IPS', ('127.0.0.1', '::1')) ISOLATED_DEPLOYMENT = getattr(configuration, 'ISOLATED_DEPLOYMENT', False) @@ -424,6 +424,7 @@ INSTALLED_APPS = [ 'mptt', 'rest_framework', 'social_django', + 'sorl.thumbnail', 'taggit', 'timezone_field', 'core', @@ -471,9 +472,9 @@ if DEBUG: if METRICS_ENABLED: # If metrics are enabled, add the before & after Prometheus middleware MIDDLEWARE = [ - 'django_prometheus.middleware.PrometheusBeforeMiddleware', + 'netbox.middleware.PrometheusBeforeMiddleware', *MIDDLEWARE, - 'django_prometheus.middleware.PrometheusAfterMiddleware', + 'netbox.middleware.PrometheusAfterMiddleware', ] # URLs diff --git a/netbox/netbox/tables/columns.py b/netbox/netbox/tables/columns.py index c13a8333b..f480c2085 100644 --- a/netbox/netbox/tables/columns.py +++ b/netbox/netbox/tables/columns.py @@ -21,7 +21,7 @@ from extras.choices import CustomFieldTypeChoices from utilities.object_types import object_type_identifier, object_type_name from utilities.permissions import get_permission_for_model from utilities.templatetags.builtins.filters import render_markdown -from utilities.views import get_viewname +from utilities.views import get_action_url __all__ = ( 'ActionsColumn', @@ -289,7 +289,7 @@ class ActionsColumn(tables.Column): for idx, (action, attrs) in enumerate(self.actions.items()): permission = get_permission_for_model(model, attrs.permission) if attrs.permission is None or user.has_perm(permission): - url = reverse(get_viewname(model, action), kwargs={'pk': record.pk}) + url = get_action_url(model, action=action, kwargs={'pk': record.pk}) # Render a separate button if a) only one action exists, or b) if split_actions is True if len(self.actions) == 1 or (self.split_actions and idx == 0): diff --git a/netbox/netbox/tables/tables.py b/netbox/netbox/tables/tables.py index 63fd0ea0e..89a9c1ac2 100644 --- a/netbox/netbox/tables/tables.py +++ b/netbox/netbox/tables/tables.py @@ -8,7 +8,6 @@ from django.contrib.contenttypes.fields import GenericForeignKey from django.core.exceptions import FieldDoesNotExist from django.db.models.fields.related import RelatedField from django.db.models.fields.reverse_related import ManyToOneRel -from django.urls import reverse from django.urls.exceptions import NoReverseMatch from django.utils.safestring import mark_safe from django.utils.translation import gettext_lazy as _ @@ -23,7 +22,7 @@ from netbox.tables import columns from utilities.html import highlight from utilities.paginator import EnhancedPaginator, get_paginate_count from utilities.string import title -from utilities.views import get_viewname +from utilities.views import get_action_url from .template_code import * __all__ = ( @@ -261,9 +260,8 @@ class NetBoxTable(BaseTable): Return the base HTML request URL for embedded tables. """ if self.embedded: - viewname = get_viewname(self._meta.model, action='list') try: - return reverse(viewname) + return get_action_url(self._meta.model, action='list') except NoReverseMatch: pass return '' diff --git a/netbox/netbox/tests/dummy_plugin/__init__.py b/netbox/netbox/tests/dummy_plugin/__init__.py index 2ca7c290c..01eb0baa5 100644 --- a/netbox/netbox/tests/dummy_plugin/__init__.py +++ b/netbox/netbox/tests/dummy_plugin/__init__.py @@ -24,7 +24,7 @@ class DummyPluginConfig(PluginConfig): def ready(self): super().ready() - from . import jobs # noqa: F401 + from . import jobs, webhook_callbacks # noqa: F401 config = DummyPluginConfig diff --git a/netbox/netbox/tests/dummy_plugin/urls.py b/netbox/netbox/tests/dummy_plugin/urls.py index 6cdd48f7e..ff6b1cee6 100644 --- a/netbox/netbox/tests/dummy_plugin/urls.py +++ b/netbox/netbox/tests/dummy_plugin/urls.py @@ -7,5 +7,8 @@ urlpatterns = ( path('models/', views.DummyModelsView.as_view(), name='dummy_model_list'), path('models/add/', views.DummyModelAddView.as_view(), name='dummy_model_add'), + path('netboxmodel/', views.DummyNetBoxModelView.as_view(), name='dummynetboxmodel_list'), + path('netboxmodel/add/', views.DummyNetBoxModelView.as_view(), name='dummynetboxmodel_add'), + path('netboxmodel/import/', views.DummyNetBoxModelView.as_view(), name='dummynetboxmodel_bulk_import'), path('netboxmodel//', views.DummyNetBoxModelView.as_view(), name='dummynetboxmodel'), ) diff --git a/netbox/netbox/tests/dummy_plugin/webhook_callbacks.py b/netbox/netbox/tests/dummy_plugin/webhook_callbacks.py new file mode 100644 index 000000000..095f545b5 --- /dev/null +++ b/netbox/netbox/tests/dummy_plugin/webhook_callbacks.py @@ -0,0 +1,8 @@ +from extras.webhooks import register_webhook_callback + + +@register_webhook_callback +def set_context(object_type, event_type, data, request): + return { + 'foo': 123, + } diff --git a/netbox/netbox/tests/test_jobs.py b/netbox/netbox/tests/test_jobs.py index 9885f73c5..dea8b30d2 100644 --- a/netbox/netbox/tests/test_jobs.py +++ b/netbox/netbox/tests/test_jobs.py @@ -16,6 +16,10 @@ class TestJobRunner(JobRunner): def run(self, *args, **kwargs): if kwargs.get('make_fail', False): raise JobFailed() + self.logger.debug("Debug message") + self.logger.info("Info message") + self.logger.warning("Warning message") + self.logger.error("Error message") class JobRunnerTestCase(TestCase): @@ -51,8 +55,16 @@ class JobRunnerTest(JobRunnerTestCase): def test_handle(self): job = TestJobRunner.enqueue(immediate=True) + # Check job status self.assertEqual(job.status, JobStatusChoices.STATUS_COMPLETED) + # Check logging + self.assertEqual(len(job.log_entries), 4) + self.assertEqual(job.log_entries[0]['message'], "Debug message") + self.assertEqual(job.log_entries[1]['message'], "Info message") + self.assertEqual(job.log_entries[2]['message'], "Warning message") + self.assertEqual(job.log_entries[3]['message'], "Error message") + def test_handle_failed(self): with disable_warnings('netbox.jobs'): job = TestJobRunner.enqueue(immediate=True, make_fail=True) diff --git a/netbox/netbox/tests/test_model_features.py b/netbox/netbox/tests/test_model_features.py new file mode 100644 index 000000000..190c177ea --- /dev/null +++ b/netbox/netbox/tests/test_model_features.py @@ -0,0 +1,53 @@ +from django.test import TestCase + +from core.models import AutoSyncRecord, DataSource +from extras.models import CustomLink +from netbox.models.features import get_model_features, has_feature, model_is_public +from netbox.tests.dummy_plugin.models import DummyModel +from taggit.models import Tag + + +class ModelFeaturesTestCase(TestCase): + + def test_model_is_public(self): + """ + Test that the is_public() utility function returns True for public models only. + """ + # Public model + self.assertFalse(hasattr(DataSource, '_netbox_private')) + self.assertTrue(model_is_public(DataSource)) + + # Private model + self.assertTrue(getattr(AutoSyncRecord, '_netbox_private')) + self.assertFalse(model_is_public(AutoSyncRecord)) + + # Plugin model + self.assertFalse(hasattr(DummyModel, '_netbox_private')) + self.assertTrue(model_is_public(DummyModel)) + + # Non-core model + self.assertFalse(hasattr(Tag, '_netbox_private')) + self.assertFalse(model_is_public(Tag)) + + def test_has_feature(self): + """ + Test the functionality of the has_feature() utility function. + """ + # Sanity checking + self.assertTrue(hasattr(DataSource, 'bookmarks'), "Invalid test?") + self.assertFalse(hasattr(AutoSyncRecord, 'bookmarks'), "Invalid test?") + + self.assertTrue(has_feature(DataSource, 'bookmarks')) + self.assertFalse(has_feature(AutoSyncRecord, 'bookmarks')) + + def test_get_model_features(self): + """ + Check that get_model_features() returns the expected features for a model. + """ + # Sanity checking + self.assertTrue(hasattr(CustomLink, 'clone'), "Invalid test?") + self.assertFalse(hasattr(CustomLink, 'bookmarks'), "Invalid test?") + + features = get_model_features(CustomLink) + self.assertIn('cloning', features) + self.assertNotIn('bookmarks', features) diff --git a/netbox/netbox/tests/test_object_actions.py b/netbox/netbox/tests/test_object_actions.py new file mode 100644 index 000000000..7e3b16bf1 --- /dev/null +++ b/netbox/netbox/tests/test_object_actions.py @@ -0,0 +1,32 @@ +from unittest import skipIf + +from django.conf import settings +from django.test import TestCase + +from dcim.models import Device +from netbox.object_actions import AddObject, BulkImport +from netbox.tests.dummy_plugin.models import DummyNetBoxModel + + +class ObjectActionTest(TestCase): + + def test_get_url_core_model(self): + """Test URL generation for core NetBox models""" + obj = Device() + + url = AddObject.get_url(obj) + self.assertEqual(url, '/dcim/devices/add/') + + url = BulkImport.get_url(obj) + self.assertEqual(url, '/dcim/devices/import/') + + @skipIf('netbox.tests.dummy_plugin' not in settings.PLUGINS, "dummy_plugin not in settings.PLUGINS") + def test_get_url_plugin_model(self): + """Test URL generation for plugin models includes plugins: namespace""" + obj = DummyNetBoxModel() + + url = AddObject.get_url(obj) + self.assertEqual(url, '/plugins/dummy-plugin/netboxmodel/add/') + + url = BulkImport.get_url(obj) + self.assertEqual(url, '/plugins/dummy-plugin/netboxmodel/import/') diff --git a/netbox/netbox/tests/test_plugins.py b/netbox/netbox/tests/test_plugins.py index 264c8e6f9..550dca514 100644 --- a/netbox/netbox/tests/test_plugins.py +++ b/netbox/netbox/tests/test_plugins.py @@ -6,9 +6,11 @@ from django.test import Client, TestCase, override_settings from django.urls import reverse from core.choices import JobIntervalChoices +from core.models import ObjectType from netbox.tests.dummy_plugin import config as dummy_config from netbox.tests.dummy_plugin.data_backends import DummyBackend from netbox.tests.dummy_plugin.jobs import DummySystemJob +from netbox.tests.dummy_plugin.webhook_callbacks import set_context from netbox.plugins.navigation import PluginMenu from netbox.plugins.utils import get_plugin_config from netbox.graphql.schema import Query @@ -23,8 +25,9 @@ class PluginTest(TestCase): self.assertIn('netbox.tests.dummy_plugin.DummyPluginConfig', settings.INSTALLED_APPS) def test_model_registration(self): - self.assertIn('dummy_plugin', registry['models']) - self.assertIn('dummymodel', registry['models']['dummy_plugin']) + self.assertTrue( + ObjectType.objects.filter(app_label='dummy_plugin', model='dummymodel').exists() + ) def test_models(self): from netbox.tests.dummy_plugin.models import DummyModel @@ -218,3 +221,9 @@ class PluginTest(TestCase): Check that events pipeline is registered. """ self.assertIn('netbox.tests.dummy_plugin.events.process_events_queue', settings.EVENTS_PIPELINE) + + def test_webhook_callbacks(self): + """ + Test the registration of webhook callbacks. + """ + self.assertIn(set_context, registry['webhook_callbacks']) diff --git a/netbox/netbox/utils.py b/netbox/netbox/utils.py index f2c34722c..2003a606a 100644 --- a/netbox/netbox/utils.py +++ b/netbox/netbox/utils.py @@ -3,6 +3,7 @@ from netbox.registry import registry __all__ = ( 'get_data_backend_choices', 'register_data_backend', + 'register_model_feature', 'register_request_processor', ) @@ -27,6 +28,35 @@ def register_data_backend(): return _wrapper +def register_model_feature(name, func=None): + """ + Register a model feature with its qualifying function. + + The qualifying function must accept a single `model` argument. It will be called to determine whether the given + model supports the corresponding feature. + + This function can be used directly: + + register_model_feature('my_feature', my_func) + + Or as a decorator: + + @register_model_feature('my_feature') + def my_func(model): + ... + """ + def decorator(f): + registry['model_features'][name] = f + return f + + if name in registry['model_features']: + raise ValueError(f"A model feature named {name} is already registered.") + + if func is None: + return decorator + return decorator(func) + + def register_request_processor(func): """ Decorator for registering a request processor. diff --git a/netbox/netbox/views/generic/bulk_views.py b/netbox/netbox/views/generic/bulk_views.py index b52d12d98..d9b2b875f 100644 --- a/netbox/netbox/views/generic/bulk_views.py +++ b/netbox/netbox/views/generic/bulk_views.py @@ -12,26 +12,29 @@ from django.db.models.fields.reverse_related import ManyToManyRel from django.forms import ModelMultipleChoiceField, MultipleHiddenInput from django.http import HttpResponse from django.shortcuts import get_object_or_404, redirect, render -from django.urls import reverse from django.utils.safestring import mark_safe from django.utils.translation import gettext as _ -from django_tables2.export import TableExport from mptt.models import MPTTModel +from core.exceptions import JobFailed from core.models import ObjectType from core.signals import clear_events from extras.choices import CustomFieldUIEditableChoices from extras.models import CustomField, ExportTemplate +from netbox.models.features import ChangeLoggingMixin +from netbox.object_actions import AddObject, BulkDelete, BulkEdit, BulkExport, BulkImport, BulkRename from utilities.error_handlers import handle_protectederror -from utilities.exceptions import AbortRequest, AbortTransaction, PermissionsViolation -from utilities.forms import BulkRenameForm, ConfirmationForm, restrict_form_fields +from utilities.exceptions import AbortRequest, PermissionsViolation +from utilities.export import TableExport +from utilities.forms import BulkDeleteForm, BulkRenameForm, restrict_form_fields from utilities.forms.bulk_import import BulkImportForm from utilities.htmx import htmx_partial +from utilities.jobs import is_background_request, process_request_as_job from utilities.permissions import get_permission_for_model from utilities.query import reapply_model_ordering from utilities.request import safe_for_redirect from utilities.tables import get_table_configs -from utilities.views import GetReturnURLMixin, get_viewname +from utilities.views import GetReturnURLMixin, get_action_url from .base import BaseMultiObjectView from .mixins import ActionsMixin, TableMixin from .utils import get_prerequisite_model @@ -54,12 +57,12 @@ class ObjectListView(BaseMultiObjectView, ActionsMixin, TableMixin): Attributes: filterset: A django-filter FilterSet that is applied to the queryset filterset_form: The form class used to render filter options - actions: A mapping of supported actions to their required permissions. When adding custom actions, bulk - action names must be prefixed with `bulk_`. (See ActionsMixin.) + actions: An iterable of ObjectAction subclasses (see ActionsMixin) """ template_name = 'generic/object_list.html' filterset = None filterset_form = None + actions = (AddObject, BulkImport, BulkExport, BulkEdit, BulkRename, BulkDelete) def get_required_permission(self): return get_permission_for_model(self.queryset.model, 'view') @@ -76,7 +79,7 @@ class ObjectListView(BaseMultiObjectView, ActionsMixin, TableMixin): return '---\n'.join(yaml_data) - def export_table(self, table, columns=None, filename=None): + def export_table(self, table, columns=None, filename=None, delimiter=None): """ Export all table data in CSV format. @@ -85,6 +88,7 @@ class ObjectListView(BaseMultiObjectView, ActionsMixin, TableMixin): columns: A list of specific columns to include. If None, all columns will be exported. filename: The name of the file attachment sent to the client. If None, will be determined automatically from the queryset model name. + delimiter: The character used to separate columns (a comma is used by default) """ exclude_columns = {'pk', 'actions'} if columns: @@ -95,7 +99,8 @@ class ObjectListView(BaseMultiObjectView, ActionsMixin, TableMixin): exporter = TableExport( export_format=TableExport.CSV, table=table, - exclude_columns=exclude_columns + exclude_columns=exclude_columns, + delimiter=delimiter, ) return exporter.response( filename=filename or f'netbox_{self.queryset.model._meta.verbose_name_plural}.csv' @@ -125,7 +130,7 @@ class ObjectListView(BaseMultiObjectView, ActionsMixin, TableMixin): redirect_url = f'{request.path}?{query_params.urlencode()}' if safe_for_redirect(redirect_url): return redirect(redirect_url) - return redirect(get_viewname(self.queryset.model, 'list')) + return redirect(get_action_url(self.queryset.model, action='list')) # # Request handlers @@ -150,15 +155,16 @@ class ObjectListView(BaseMultiObjectView, ActionsMixin, TableMixin): # Determine the available actions actions = self.get_permitted_actions(request.user) - has_bulk_actions = any([a.startswith('bulk_') for a in actions]) + has_table_actions = any(action.multi for action in actions) if 'export' in request.GET: # Export the current table view if request.GET['export'] == 'table': - table = self.get_table(self.queryset, request, has_bulk_actions) + table = self.get_table(self.queryset, request, has_table_actions) columns = [name for name, _ in table.selected_columns] - return self.export_table(table, columns) + delimiter = request.user.config.get('csv_delimiter') + return self.export_table(table, columns, delimiter=delimiter) # Render an ExportTemplate elif request.GET['export']: @@ -174,11 +180,12 @@ class ObjectListView(BaseMultiObjectView, ActionsMixin, TableMixin): # Fall back to default table/YAML export else: - table = self.get_table(self.queryset, request, has_bulk_actions) - return self.export_table(table) + table = self.get_table(self.queryset, request, has_table_actions) + delimiter = request.user.config.get('csv_delimiter') + return self.export_table(table, delimiter=delimiter) # Render the objects table - table = self.get_table(self.queryset, request, has_bulk_actions) + table = self.get_table(self.queryset, request, has_table_actions) # If this is an HTMX request, return only the rendered table HTML if htmx_partial(request): @@ -351,7 +358,18 @@ class BulkImportView(GetReturnURLMixin, BaseMultiObjectView): return {**required_fields, **optional_fields} - def _save_object(self, import_form, model_form, request): + def _compile_form_errors(self, errors, index, prefix=None): + error_messages = [] + for field_name, errors in errors.items(): + prefix = f'{prefix}.' if prefix else '' + if field_name == '__all__': + field_name = '' + for err in errors: + error_messages.append(f"Record {index} {prefix}{field_name}: {err}") + return error_messages + + def _save_object(self, model_form, request): + _action = 'Updated' if model_form.instance.pk else 'Created' # Save the primary object obj = self.save_object(model_form, request) @@ -377,20 +395,18 @@ class BulkImportView(GetReturnURLMixin, BaseMultiObjectView): related_obj_pks.append(related_obj.pk) else: # Replicate errors on the related object form to the import form for display and abort - for subfield_name, errors in f.errors.items(): - for err in errors: - if subfield_name == '__all__': - err_msg = f"{field_name}[{i}]: {err}" - else: - err_msg = f"{field_name}[{i}] {subfield_name}: {err}" - import_form.add_error(None, err_msg) - raise AbortTransaction() + raise ValidationError( + self._compile_form_errors(f.errors, index=i, prefix=f'{field_name}[{i}]') + ) # Enforce object-level permissions on related objects model = related_object_form.Meta.model if model.objects.filter(pk__in=related_obj_pks).count() != len(related_obj_pks): raise ObjectDoesNotExist + if is_background_request(request): + request.job.logger.info(f'{_action} {obj}') + return obj def save_object(self, object_form, request): @@ -416,7 +432,6 @@ class BulkImportView(GetReturnURLMixin, BaseMultiObjectView): } if prefetch_ids else {} for i, record in enumerate(records, start=1): - instance = None object_id = int(record.pop('id')) if record.get('id') else None # Determine whether this object is being created or updated @@ -432,6 +447,8 @@ class BulkImportView(GetReturnURLMixin, BaseMultiObjectView): instance.snapshot() else: + instance = self.queryset.model() + # For newly created objects, apply any default custom field values custom_fields = CustomField.objects.filter( object_types=ContentType.objects.get_for_model(self.queryset.model), @@ -442,6 +459,9 @@ class BulkImportView(GetReturnURLMixin, BaseMultiObjectView): if field_name not in record: record[field_name] = cf.default + # Record changelog message (if any) + instance._changelog_message = form.cleaned_data.get('changelog_message', '') + # Instantiate the model form for the object model_form_kwargs = { 'data': record, @@ -461,18 +481,13 @@ class BulkImportView(GetReturnURLMixin, BaseMultiObjectView): restrict_form_fields(model_form, request.user) if model_form.is_valid(): - obj = self._save_object(form, model_form, request) + obj = self._save_object(model_form, request) saved_objects.append(obj) else: - # Replicate model form errors for display - for field, errors in model_form.errors.items(): - for err in errors: - if field == '__all__': - form.add_error(None, f'Record {i}: {err}') - else: - form.add_error(None, f'Record {i} {field}: {err}') - - raise ValidationError("") + # Raise model form errors + raise ValidationError( + self._compile_form_errors(model_form.errors, index=i) + ) return saved_objects @@ -481,10 +496,13 @@ class BulkImportView(GetReturnURLMixin, BaseMultiObjectView): # def get(self, request): + model = self.model_form._meta.model form = BulkImportForm() + if not issubclass(model, ChangeLoggingMixin): + form.fields.pop('changelog_message') return render(request, self.template_name, { - 'model': self.model_form._meta.model, + 'model': model, 'form': form, 'fields': self._get_form_fields(), 'return_url': self.get_return_url(request), @@ -495,35 +513,56 @@ class BulkImportView(GetReturnURLMixin, BaseMultiObjectView): logger = logging.getLogger('netbox.views.BulkImportView') model = self.model_form._meta.model form = BulkImportForm(request.POST, request.FILES) + if not issubclass(model, ChangeLoggingMixin): + form.fields.pop('changelog_message') if form.is_valid(): logger.debug("Import form validation was successful") + redirect_url = get_action_url(model, action='list') + + # If indicated, defer this request to a background job & redirect the user + if form.cleaned_data['background_job']: + job_name = _('Bulk import {count} {object_type}').format( + count=len(form.cleaned_data['data']), + object_type=model._meta.verbose_name_plural, + ) + if process_request_as_job(self.__class__, request, name=job_name): + return redirect(redirect_url) try: # Iterate through data and bind each record to a new model form instance. with transaction.atomic(using=router.db_for_write(model)): - new_objs = self.create_and_update_objects(form, request) + new_objects = self.create_and_update_objects(form, request) # Enforce object-level permissions - if self.queryset.filter(pk__in=[obj.pk for obj in new_objs]).count() != len(new_objs): + if self.queryset.filter(pk__in=[obj.pk for obj in new_objects]).count() != len(new_objects): raise PermissionsViolation - if new_objs: - msg = f"Imported {len(new_objs)} {model._meta.verbose_name_plural}" - logger.info(msg) - messages.success(request, msg) + msg = _('Imported {count} {object_type}').format( + count=len(new_objects), + object_type=model._meta.verbose_name_plural + ) + logger.info(msg) - view_name = get_viewname(model, action='list') - results_url = f"{reverse(view_name)}?modified_by_request={request.id}" - return redirect(results_url) + # Handle background job + if is_background_request(request): + request.job.logger.info(msg) + return - except (AbortTransaction, ValidationError): - clear_events.send(sender=self) - - except (AbortRequest, PermissionsViolation) as e: - logger.debug(e.message) - form.add_error(None, e.message) + messages.success(request, msg) + return redirect(f"{redirect_url}?modified_by_request={request.id}") + + except (AbortRequest, PermissionsViolation, ValidationError) as e: + err_messages = e.messages if type(e) is ValidationError else [e.message] + for msg in err_messages: + logger.debug(msg) + form.add_error(None, msg) + if is_background_request(request): + request.job.logger.error(msg) + request.job.logger.warning("Bulk import aborted") clear_events.send(sender=self) + if is_background_request(request): + raise JobFailed else: logger.debug("Form validation failed") @@ -594,6 +633,9 @@ class BulkEditView(GetReturnURLMixin, BaseMultiObjectView): if hasattr(obj, 'snapshot'): obj.snapshot() + # Attach the changelog message (if any) to the object + obj._changelog_message = form.cleaned_data.get('changelog_message') + # Update standard fields. If a field is listed in _nullify, delete its value. for name, model_field in model_fields.items(): # Handle nullification @@ -636,6 +678,9 @@ class BulkEditView(GetReturnURLMixin, BaseMultiObjectView): self.post_save_operations(form, obj) + if is_background_request(request): + request.job.logger.info(f"Updated {obj}") + # Rebuild the tree for MPTT models if issubclass(self.queryset.model, MPTTModel): self.queryset.model.objects.rebuild() @@ -680,6 +725,16 @@ class BulkEditView(GetReturnURLMixin, BaseMultiObjectView): if '_apply' in request.POST: if form.is_valid(): logger.debug("Form validation was successful") + + # If indicated, defer this request to a background job & redirect the user + if form.cleaned_data['background_job']: + job_name = _('Bulk edit {count} {object_type}').format( + count=len(form.cleaned_data['pk']), + object_type=model._meta.verbose_name_plural, + ) + if process_request_as_job(self.__class__, request, name=job_name): + return redirect(self.get_return_url(request)) + try: with transaction.atomic(using=router.db_for_write(model)): updated_objects = self._update_objects(form, request) @@ -689,21 +744,30 @@ class BulkEditView(GetReturnURLMixin, BaseMultiObjectView): if object_count != len(updated_objects): raise PermissionsViolation - if updated_objects: - msg = f'Updated {len(updated_objects)} {model._meta.verbose_name_plural}' - logger.info(msg) - messages.success(self.request, msg) + msg = _('Updated {count} {object_type}').format( + count=len(updated_objects), + object_type=model._meta.verbose_name_plural, + ) + logger.info(msg) + # Handle background job + if is_background_request(request): + request.job.logger.info(msg) + return + + messages.success(self.request, msg) return redirect(self.get_return_url(request)) - except ValidationError as e: - messages.error(self.request, ", ".join(e.messages)) - clear_events.send(sender=self) - - except (AbortRequest, PermissionsViolation) as e: - logger.debug(e.message) - form.add_error(None, e.message) + except (AbortRequest, PermissionsViolation, ValidationError) as e: + err_messages = e.messages if type(e) is ValidationError else [e.message] + for msg in err_messages: + logger.debug(msg) + form.add_error(None, msg) + if is_background_request(request): + request.job.logger.error(msg) clear_events.send(sender=self) + if is_background_request(request): + raise JobFailed else: logger.debug("Form validation failed") @@ -729,7 +793,11 @@ class BulkEditView(GetReturnURLMixin, BaseMultiObjectView): class BulkRenameView(GetReturnURLMixin, BaseMultiObjectView): """ An extendable view for renaming objects in bulk. + + Attributes: + field_name: The name of the object attribute for which the value is being updated (defaults to "name") """ + field_name = 'name' template_name = 'generic/bulk_rename.html' def __init__(self, *args, **kwargs): @@ -759,12 +827,12 @@ class BulkRenameView(GetReturnURLMixin, BaseMultiObjectView): replace = form.cleaned_data['replace'] if form.cleaned_data['use_regex']: try: - obj.new_name = re.sub(find, replace, obj.name or '') + obj.new_name = re.sub(find, replace, getattr(obj, self.field_name, '')) # Catch regex group reference errors except re.error: - obj.new_name = obj.name + obj.new_name = getattr(obj, self.field_name) else: - obj.new_name = (obj.name or '').replace(find, replace) + obj.new_name = getattr(obj, self.field_name, '').replace(find, replace) renamed_pks.append(obj.pk) return renamed_pks @@ -783,7 +851,7 @@ class BulkRenameView(GetReturnURLMixin, BaseMultiObjectView): if '_apply' in request.POST: for obj in selected_objects: - obj.name = obj.new_name + setattr(obj, self.field_name, obj.new_name) obj.save() # Enforce constrained permissions @@ -813,6 +881,7 @@ class BulkRenameView(GetReturnURLMixin, BaseMultiObjectView): selected_objects = self.queryset.filter(pk__in=form.initial['pk']) return render(request, self.template_name, { + 'field_name': self.field_name, 'form': form, 'obj_type_plural': self.queryset.model._meta.verbose_name_plural, 'selected_objects': selected_objects, @@ -835,15 +904,6 @@ class BulkDeleteView(GetReturnURLMixin, BaseMultiObjectView): def get_required_permission(self): return get_permission_for_model(self.queryset.model, 'delete') - def get_form(self): - """ - Provide a standard bulk delete form if none has been specified for the view - """ - class BulkDeleteForm(ConfirmationForm): - pk = ModelMultipleChoiceField(queryset=self.queryset, widget=MultipleHiddenInput) - - return BulkDeleteForm - # # Request handlers # @@ -864,47 +924,76 @@ class BulkDeleteView(GetReturnURLMixin, BaseMultiObjectView): else: pk_list = [int(pk) for pk in request.POST.getlist('pk')] - form_cls = self.get_form() - if '_confirm' in request.POST: - form = form_cls(request.POST) + form = BulkDeleteForm(model, request.POST) if form.is_valid(): logger.debug("Form validation was successful") + # If indicated, defer this request to a background job & redirect the user + if form.cleaned_data['background_job']: + job_name = _('Bulk delete {count} {object_type}').format( + count=len(form.cleaned_data['pk']), + object_type=model._meta.verbose_name_plural, + ) + if process_request_as_job(self.__class__, request, name=job_name): + return redirect(self.get_return_url(request)) + # Delete objects queryset = self.queryset.filter(pk__in=pk_list) deleted_count = queryset.count() try: with transaction.atomic(using=router.db_for_write(model)): for obj in queryset: + # Take a snapshot of change-logged models if hasattr(obj, 'snapshot'): obj.snapshot() + + # Attach the changelog message (if any) to the object + obj._changelog_message = form.cleaned_data.get('changelog_message') + + # Delete the object obj.delete() + if is_background_request(request): + request.job.logger.info(f"Deleted {obj}") + + msg = _('Deleted {count} {object_type}').format( + count=deleted_count, + object_type=model._meta.verbose_name_plural + ) + logger.info(msg) + + # Handle background job + if is_background_request(request): + request.job.logger.info(msg) + return + + messages.success(request, msg) + except (ProtectedError, RestrictedError) as e: - logger.info(f"Caught {type(e)} while attempting to delete objects") + logger.warning(f"Caught {type(e)} while attempting to delete objects") + if is_background_request(request): + request.job.logger.error( + _("Deletion failed due to the presence of one or more dependent objects.") + ) + raise JobFailed handle_protectederror(queryset, request, e) - return redirect(self.get_return_url(request)) except AbortRequest as e: logger.debug(e.message) + if is_background_request(request): + request.job.logger.error(e.message) + raise JobFailed messages.error(request, mark_safe(e.message)) - return redirect(self.get_return_url(request)) - msg = _("Deleted {count} {object_type}").format( - count=deleted_count, - object_type=model._meta.verbose_name_plural - ) - logger.info(msg) - messages.success(request, msg) return redirect(self.get_return_url(request)) else: logger.debug("Form validation failed") else: - form = form_cls(initial={ + form = BulkDeleteForm(model, initial={ 'pk': pk_list, 'return_url': self.get_return_url(request), }) diff --git a/netbox/netbox/views/generic/feature_views.py b/netbox/netbox/views/generic/feature_views.py index d8ba2b475..338ed0628 100644 --- a/netbox/netbox/views/generic/feature_views.py +++ b/netbox/netbox/views/generic/feature_views.py @@ -10,7 +10,7 @@ from django.views.generic import View from core.models import Job, ObjectChange from core.tables import JobTable, ObjectChangeTable from extras.forms import JournalEntryForm -from extras.models import JournalEntry +from extras.models import ImageAttachment, JournalEntry from extras.tables import JournalEntryTable from tenancy.models import ContactAssignment from tenancy.tables import ContactAssignmentTable @@ -25,6 +25,7 @@ __all__ = ( 'BulkSyncDataView', 'ObjectChangeLogView', 'ObjectContactsView', + 'ObjectImageAttachmentsView', 'ObjectJobsView', 'ObjectJournalView', 'ObjectSyncDataView', @@ -84,6 +85,41 @@ class ObjectChangeLogView(ConditionalLoginRequiredMixin, View): }) +class ObjectImageAttachmentsView(ConditionalLoginRequiredMixin, View): + """ + Render all images attached to the object as linked thumbnails. + + Attributes: + base_template: The name of the template to extend. If not provided, "{app}/{model}.html" will be used. + """ + base_template = None + tab = ViewTab( + label=_('Images'), + badge=lambda obj: obj.images.count(), + permission='extras.view_imageattachment', + weight=6000 + ) + + def get(self, request, model, **kwargs): + obj = get_object_or_404(model.objects.restrict(request.user, 'view'), **kwargs) + image_attachments = ImageAttachment.objects.filter( + object_type=ContentType.objects.get_for_model(obj), + object_id=obj.pk, + ) + + # Default to using "/.html" as the template, if it exists. Otherwise, + # fall back to using base.html. + if self.base_template is None: + self.base_template = f"{model._meta.app_label}/{model._meta.model_name}.html" + + return render(request, 'extras/object_imageattachments.html', { + 'object': obj, + 'image_attachments': image_attachments, + 'base_template': self.base_template, + 'tab': self.tab, + }) + + class ObjectJournalView(ConditionalLoginRequiredMixin, View): """ Show all journal entries for an object. The model class must be passed as a keyword argument when referencing this diff --git a/netbox/netbox/views/generic/mixins.py b/netbox/netbox/views/generic/mixins.py index 5f9f62120..079164ed9 100644 --- a/netbox/netbox/views/generic/mixins.py +++ b/netbox/netbox/views/generic/mixins.py @@ -1,7 +1,7 @@ from django.shortcuts import get_object_or_404 from extras.models import TableConfig -from netbox.constants import DEFAULT_ACTION_PERMISSIONS +from netbox import object_actions from utilities.permissions import get_permission_for_model __all__ = ( @@ -9,6 +9,18 @@ __all__ = ( 'TableMixin', ) +# TODO: Remove in NetBox v4.5 +LEGACY_ACTIONS = { + 'add': object_actions.AddObject, + 'edit': object_actions.EditObject, + 'delete': object_actions.DeleteObject, + 'export': object_actions.BulkExport, + 'bulk_import': object_actions.BulkImport, + 'bulk_edit': object_actions.BulkEdit, + 'bulk_rename': object_actions.BulkRename, + 'bulk_delete': object_actions.BulkDelete, +} + class ActionsMixin: """ @@ -19,7 +31,24 @@ class ActionsMixin: Standard actions include: add, import, export, bulk_edit, and bulk_delete. Some views extend this default map with custom actions, such as bulk_sync. """ - actions = DEFAULT_ACTION_PERMISSIONS + actions = tuple() + + # TODO: Remove in NetBox v4.5 + def _convert_legacy_actions(self): + """ + Convert a legacy dictionary mapping action name to required permissions to a list of ObjectAction subclasses. + """ + if type(self.actions) is not dict: + return + + actions = [] + for name in self.actions.keys(): + try: + actions.append(LEGACY_ACTIONS[name]) + except KeyError: + raise ValueError(f"Unsupported legacy action: {name}") + + self.actions = actions def get_permitted_actions(self, user, model=None): """ @@ -27,11 +56,15 @@ class ActionsMixin: """ model = model or self.queryset.model + # TODO: Remove in NetBox v4.5 + # Handle legacy action sets + self._convert_legacy_actions() + # Resolve required permissions for each action permitted_actions = [] for action in self.actions: required_permissions = [ - get_permission_for_model(model, name) for name in self.actions.get(action, set()) + get_permission_for_model(model, perm) for perm in action.permissions_required ] if not required_permissions or user.has_perms(required_permissions): permitted_actions.append(action) diff --git a/netbox/netbox/views/generic/object_views.py b/netbox/netbox/views/generic/object_views.py index a7acbffc0..f45d75adc 100644 --- a/netbox/netbox/views/generic/object_views.py +++ b/netbox/netbox/views/generic/object_views.py @@ -14,15 +14,18 @@ from django.utils.safestring import mark_safe from django.utils.translation import gettext as _ from core.signals import clear_events +from netbox.object_actions import ( + AddObject, BulkDelete, BulkEdit, BulkExport, BulkImport, CloneObject, DeleteObject, EditObject, +) from utilities.error_handlers import handle_protectederror from utilities.exceptions import AbortRequest, PermissionsViolation -from utilities.forms import ConfirmationForm, restrict_form_fields +from utilities.forms import DeleteForm, restrict_form_fields from utilities.htmx import htmx_partial from utilities.permissions import get_permission_for_model from utilities.querydict import normalize_querydict, prepare_cloned_fields from utilities.request import safe_for_redirect from utilities.tables import get_table_configs -from utilities.views import GetReturnURLMixin, get_viewname +from utilities.views import GetReturnURLMixin, get_action_url from .base import BaseObjectView from .mixins import ActionsMixin, TableMixin from .utils import get_prerequisite_model @@ -36,7 +39,7 @@ __all__ = ( ) -class ObjectView(BaseObjectView): +class ObjectView(ActionsMixin, BaseObjectView): """ Retrieve a single object for display. @@ -44,8 +47,10 @@ class ObjectView(BaseObjectView): Attributes: tab: A ViewTab instance for the view + actions: An iterable of ObjectAction subclasses (see ActionsMixin) """ tab = None + actions = (CloneObject, EditObject, DeleteObject) def get_required_permission(self): return get_permission_for_model(self.queryset.model, 'view') @@ -72,9 +77,11 @@ class ObjectView(BaseObjectView): request: The current request """ instance = self.get_object(**kwargs) + actions = self.get_permitted_actions(request.user, model=instance) return render(request, self.get_template_name(), { 'object': instance, + 'actions': actions, 'tab': self.tab, **self.get_extra_context(request, instance), }) @@ -90,13 +97,13 @@ class ObjectChildrenView(ObjectView, ActionsMixin, TableMixin): table: The django-tables2 Table class used to render the child objects list filterset: A django-filter FilterSet that is applied to the queryset filterset_form: The form class used to render filter options - actions: A mapping of supported actions to their required permissions. When adding custom actions, bulk - action names must be prefixed with `bulk_`. (See ActionsMixin.) + actions: An iterable of ObjectAction subclasses (see ActionsMixin) """ child_model = None table = None filterset = None filterset_form = None + actions = (AddObject, BulkImport, BulkEdit, BulkExport, BulkDelete) template_name = 'generic/object_children.html' def get_children(self, request, parent): @@ -138,10 +145,10 @@ class ObjectChildrenView(ObjectView, ActionsMixin, TableMixin): # Determine the available actions actions = self.get_permitted_actions(request.user, model=self.child_model) - has_bulk_actions = any([a.startswith('bulk_') for a in actions]) + has_table_actions = any(action.multi for action in actions) table_data = self.prep_table_data(request, child_objects, instance) - table = self.get_table(table_data, request, has_bulk_actions) + table = self.get_table(table_data, request, has_table_actions) # If this is an HTMX request, return only the rendered table HTML if htmx_partial(request): @@ -281,6 +288,9 @@ class ObjectEditView(GetReturnURLMixin, BaseObjectView): if form.is_valid(): logger.debug("Form validation was successful") + # Record changelog message (if any) + obj._changelog_message = form.cleaned_data.pop('changelog_message', '') + try: with transaction.atomic(using=router.db_for_write(model)): object_created = form.instance.pk is None @@ -415,7 +425,7 @@ class ObjectDeleteView(GetReturnURLMixin, BaseObjectView): request: The current request """ obj = self.get_object(**kwargs) - form = ConfirmationForm(initial=request.GET) + form = DeleteForm(instance=obj, initial=request.GET) try: dependent_objects = self._get_dependent_objects(obj) @@ -426,8 +436,7 @@ class ObjectDeleteView(GetReturnURLMixin, BaseObjectView): # If this is an HTMX request, return only the rendered deletion form as modal content if htmx_partial(request): - viewname = get_viewname(self.queryset.model, action='delete') - form_url = reverse(viewname, kwargs={'pk': obj.pk}) + form_url = get_action_url(self.queryset.model, action='delete', kwargs={'pk': obj.pk}) return render(request, 'htmx/delete_form.html', { 'object': obj, 'object_type': self.queryset.model._meta.verbose_name, @@ -454,23 +463,25 @@ class ObjectDeleteView(GetReturnURLMixin, BaseObjectView): """ logger = logging.getLogger('netbox.views.ObjectDeleteView') obj = self.get_object(**kwargs) - form = ConfirmationForm(request.POST) - - # Take a snapshot of change-logged models - if hasattr(obj, 'snapshot'): - obj.snapshot() + form = DeleteForm(request.POST, instance=obj) if form.is_valid(): logger.debug("Form validation was successful") + # Take a snapshot of change-logged models + if hasattr(obj, 'snapshot'): + obj.snapshot() + + # Record changelog message (if any) + obj._changelog_message = form.cleaned_data.pop('changelog_message', '') + + # Delete the object try: obj.delete() - except (ProtectedError, RestrictedError) as e: logger.info(f"Caught {type(e)} while attempting to delete objects") handle_protectederror([obj], request, e) return redirect(obj.get_absolute_url()) - except AbortRequest as e: logger.debug(e.message) messages.error(request, mark_safe(e.message)) diff --git a/netbox/project-static/dist/netbox.css b/netbox/project-static/dist/netbox.css index ee53c5b6f..2768f7714 100644 Binary files a/netbox/project-static/dist/netbox.css and b/netbox/project-static/dist/netbox.css differ diff --git a/netbox/project-static/dist/netbox.js b/netbox/project-static/dist/netbox.js index a65167798..1f5e199d8 100644 Binary files a/netbox/project-static/dist/netbox.js and b/netbox/project-static/dist/netbox.js differ diff --git a/netbox/project-static/dist/netbox.js.map b/netbox/project-static/dist/netbox.js.map index 2443fc05d..2d0c72c2d 100644 Binary files a/netbox/project-static/dist/netbox.js.map and b/netbox/project-static/dist/netbox.js.map differ diff --git a/netbox/project-static/package.json b/netbox/project-static/package.json index e2cb21a45..2567b67a8 100644 --- a/netbox/project-static/package.json +++ b/netbox/project-static/package.json @@ -1,6 +1,6 @@ { "name": "netbox", - "version": "4.3.0", + "version": "4.4.0", "main": "dist/netbox.js", "license": "Apache-2.0", "private": true, diff --git a/netbox/project-static/src/colorMode.ts b/netbox/project-static/src/colorMode.ts index 453617740..1d05c955d 100644 --- a/netbox/project-static/src/colorMode.ts +++ b/netbox/project-static/src/colorMode.ts @@ -43,6 +43,11 @@ function updateElements(targetMode: ColorMode): void { export function setColorMode(mode: ColorMode): void { storeColorMode(mode); updateElements(mode); + window.dispatchEvent( + new CustomEvent('netbox.colorModeChanged', { + detail: { netboxColorMode: mode }, + }), + ); } /** diff --git a/netbox/project-static/src/global.d.ts b/netbox/project-static/src/global.d.ts index d7244339a..5b75be1ca 100644 --- a/netbox/project-static/src/global.d.ts +++ b/netbox/project-static/src/global.d.ts @@ -79,3 +79,6 @@ type FormControls = HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement; type ColorMode = 'light' | 'dark'; type ColorModePreference = ColorMode | 'none'; +type ColorModeData = { + netboxColorMode: ColorMode; +}; diff --git a/netbox/project-static/styles/custom/_misc.scss b/netbox/project-static/styles/custom/_misc.scss index 2ba3262f6..5233616ac 100644 --- a/netbox/project-static/styles/custom/_misc.scss +++ b/netbox/project-static/styles/custom/_misc.scss @@ -81,6 +81,14 @@ img.plugin-icon { height: auto; } +// Image attachment thumbnails +.thumbnail { + max-width: 200px; + img { + border: 1px solid #606060; + } +} + body[data-bs-theme=dark] { // Assuming icon is black/white line art, invert it and tone down brightness img.plugin-icon { diff --git a/netbox/release.yaml b/netbox/release.yaml index b5e8dab86..952df9e5d 100644 --- a/netbox/release.yaml +++ b/netbox/release.yaml @@ -1,3 +1,3 @@ -version: "4.3.7" +version: "4.4.0" edition: "Community" -published: "2025-08-26" +published: "2025-09-02" diff --git a/netbox/templates/account/bookmarks.html b/netbox/templates/account/bookmarks.html index f7aa9bf57..2e3ff5691 100644 --- a/netbox/templates/account/bookmarks.html +++ b/netbox/templates/account/bookmarks.html @@ -24,9 +24,7 @@ {# Form buttons #}
- {% if 'bulk_delete' in actions %} - {% bulk_delete_button model query_params=request.GET %} - {% endif %} + {% bulk_delete_button model query_params=request.GET %}
{% endblock %} diff --git a/netbox/templates/account/notifications.html b/netbox/templates/account/notifications.html index 5a471ef25..46feb80b0 100644 --- a/netbox/templates/account/notifications.html +++ b/netbox/templates/account/notifications.html @@ -24,9 +24,7 @@ {# Form buttons #}
- {% if 'bulk_delete' in actions %} - {% bulk_delete_button model query_params=request.GET %} - {% endif %} + {% bulk_delete_button model query_params=request.GET %}
{% endblock %} diff --git a/netbox/templates/account/subscriptions.html b/netbox/templates/account/subscriptions.html index d97053d63..0ae1060cc 100644 --- a/netbox/templates/account/subscriptions.html +++ b/netbox/templates/account/subscriptions.html @@ -24,9 +24,7 @@ {# Form buttons #}
- {% if 'bulk_delete' in actions %} - {% bulk_delete_button model query_params=request.GET %} - {% endif %} + {% bulk_delete_button model query_params=request.GET %}
{% endblock %} diff --git a/netbox/templates/core/buttons/bulk_sync.html b/netbox/templates/core/buttons/bulk_sync.html new file mode 100644 index 000000000..e70b3a459 --- /dev/null +++ b/netbox/templates/core/buttons/bulk_sync.html @@ -0,0 +1,3 @@ + diff --git a/netbox/templates/core/datafile.html b/netbox/templates/core/datafile.html index 175a0e2bc..0747547b1 100644 --- a/netbox/templates/core/datafile.html +++ b/netbox/templates/core/datafile.html @@ -11,12 +11,6 @@ {% endblock %} -{% block control-buttons %} - {% if request.user|can_delete:object %} - {% delete_button object %} - {% endif %} -{% endblock control-buttons %} - {% block content %}
diff --git a/netbox/templates/core/inc/datafile_panel.html b/netbox/templates/core/inc/datafile_panel.html new file mode 100644 index 000000000..e4d8eca74 --- /dev/null +++ b/netbox/templates/core/inc/datafile_panel.html @@ -0,0 +1,36 @@ +{% load i18n %} + +
+

{% trans "Data File" %}

+ + + + + + + + + + + + + +
{% trans "Data Source" %} + {% if object.data_source %} + {{ object.data_source }} + {% else %} + {{ ''|placeholder }} + {% endif %} +
{% trans "Data File" %} + {% if object.data_file %} + {{ object.data_file }} + {% elif object.data_path %} +
+ +
+ {{ object.data_path }} + {% else %} + {{ ''|placeholder }} + {% endif %} +
{% trans "Data Synced" %}{{ object.data_synced|placeholder }}
+
diff --git a/netbox/templates/core/job.html b/netbox/templates/core/job.html index a38c3650a..3371f164e 100644 --- a/netbox/templates/core/job.html +++ b/netbox/templates/core/job.html @@ -1,33 +1,6 @@ -{% extends 'generic/object.html' %} -{% load buttons %} -{% load helpers %} -{% load perms %} +{% extends 'core/job/base.html' %} {% load i18n %} -{% block breadcrumbs %} - {{ block.super }} - {% if object.object %} - - {% with parent_jobs_viewname=object.object|viewname:"jobs" %} - - {% endwith %} - {% else %} - - {% endif %} -{% endblock breadcrumbs %} - -{% block control-buttons %} - {% if request.user|can_delete:object %} - {% delete_button object %} - {% endif %} -{% endblock control-buttons %} - {% block content %}
@@ -94,9 +67,7 @@

{% trans "Data" %}

-
-
{{ object.data|json }}
-
+
{{ object.data|json }}
diff --git a/netbox/templates/core/job/base.html b/netbox/templates/core/job/base.html new file mode 100644 index 000000000..151702946 --- /dev/null +++ b/netbox/templates/core/job/base.html @@ -0,0 +1,21 @@ +{% extends 'generic/object.html' %} +{% load buttons %} +{% load helpers %} +{% load perms %} +{% load i18n %} + +{% block breadcrumbs %} + {{ block.super }} + {% if object.object %} + + + {% else %} + + {% endif %} +{% endblock breadcrumbs %} diff --git a/netbox/templates/core/job/log.html b/netbox/templates/core/job/log.html new file mode 100644 index 000000000..b8c727299 --- /dev/null +++ b/netbox/templates/core/job/log.html @@ -0,0 +1,12 @@ +{% extends 'core/job/base.html' %} +{% load render_table from django_tables2 %} + +{% block content %} +
+
+
+ {% render_table table %} +
+
+
+{% endblock %} diff --git a/netbox/templates/core/objectchange.html b/netbox/templates/core/objectchange.html index ae32e44db..e4c7d4900 100644 --- a/netbox/templates/core/objectchange.html +++ b/netbox/templates/core/objectchange.html @@ -64,10 +64,16 @@ {% endif %} + + {% trans "Message" %} + + {{ object.message|placeholder }} + + {% trans "Request ID" %} - {{ object.request_id }} + {{ object.request_id }} diff --git a/netbox/templates/core/system.html b/netbox/templates/core/system.html index fce91f601..092bc708d 100644 --- a/netbox/templates/core/system.html +++ b/netbox/templates/core/system.html @@ -8,82 +8,168 @@ {% block controls %} - {% trans "Export" %} + {% trans "Export All" %} {% endblock controls %} {% block tabs %} -