[PR #9576] [MERGED] Closes #7702: Add Power feed defaults to user configurations #13489

Closed
opened 2025-12-29 23:19:09 +01:00 by adam · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/netbox-community/netbox/pull/9576
Author: @huntabyte
Created: 6/21/2022
Status: Merged
Merged: 6/23/2022
Merged by: @jeremystretch

Base: developHead: feature-7702


📝 Commits (3)

  • 52178f7 Closes #7702: Add Powerfeed Defaults to User Configurations
  • db807ab Closes #7702: Add power feed defaults to user configurations
  • c330282 Fix syntax error

📊 Changes

3 files changed (+32 additions, -3 deletions)

View changed files

📝 netbox/dcim/models/power.py (+4 -3)
📝 netbox/extras/admin.py (+3 -0)
📝 netbox/netbox/config/parameters.py (+25 -0)

📄 Description

Fixes: #7702

Implemented the ability for users to override the default power feed values via user-configurable preferences.

I ran into a bit of a fork in the road when adding this functionality. Initially, I was replacing the model defaults with config items, for example:

class PowerFeed(NetBoxModel, PathEndpoint, LinkTermination):
    """
    An electrical circuit delivered from a PowerPanel.
    """
    voltage = models.SmallIntegerField(
        default=ConfigItem('POWERFEED_DEFAULT_VOLTAGE')
        validators=[ExclusionValidator([0])]
    )
    amperage = models.PositiveSmallIntegerField(
        validators=[MinValueValidator(1)],
        default=ConfigItem('POWERFEED_DEFAULT_AMPERAGE')
    )
    max_utilization = models.PositiveSmallIntegerField(
        validators=[MinValueValidator(1), MaxValueValidator(100)],
        default=ConfigItem('POWERFEED_DEFAULT_MAX_UTILIZATION')
        help_text="Maximum permissible draw (percentage)"

Django's documentation regarding migrations does state the following:

Django never sets database defaults and always applies them in the Django ORM code.

But after investing the rest of the codebase, I didn't see user-defined values as defaults for any of the other models, so I assumed the standard is to let the view handle it:

class PowerFeedCreateView(generic.ObjectEditView):
    queryset = PowerFeed.objects.all()
    form = forms.PowerFeedForm

    def alter_object(self, obj, request, args, kwargs):
        obj.voltage = ConfigItem('POWERFEED_DEFAULT_VOLTAGE')
        obj.amperage = ConfigItem('POWERFEED_DEFAULT_AMPERAGE')
        obj.max_utilization = ConfigItem('POWERFEED_DEFAULT_MAX_UTILIZATION')
        return obj

However, if the maintainers decide otherwise or see no reason not to allow users to define the model defaults, I can make that adjustment.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/netbox-community/netbox/pull/9576 **Author:** [@huntabyte](https://github.com/huntabyte) **Created:** 6/21/2022 **Status:** ✅ Merged **Merged:** 6/23/2022 **Merged by:** [@jeremystretch](https://github.com/jeremystretch) **Base:** `develop` ← **Head:** `feature-7702` --- ### 📝 Commits (3) - [`52178f7`](https://github.com/netbox-community/netbox/commit/52178f78d1b22478aeee567dddeab511c8cc2e3a) Closes #7702: Add Powerfeed Defaults to User Configurations - [`db807ab`](https://github.com/netbox-community/netbox/commit/db807ab4a6f5d0ff9ff84ae39b5f18688bb19a1a) Closes #7702: Add power feed defaults to user configurations - [`c330282`](https://github.com/netbox-community/netbox/commit/c330282919f7903315abc0263c9d3f5d22321fa2) Fix syntax error ### 📊 Changes **3 files changed** (+32 additions, -3 deletions) <details> <summary>View changed files</summary> 📝 `netbox/dcim/models/power.py` (+4 -3) 📝 `netbox/extras/admin.py` (+3 -0) 📝 `netbox/netbox/config/parameters.py` (+25 -0) </details> ### 📄 Description <!-- Thank you for your interest in contributing to NetBox! Please note that our contribution policy requires that a feature request or bug report be opened for approval prior to filing a pull request. This helps avoid wasting time and effort on something that we might not be able to accept. Please indicate the relevant feature request or bug report below. IF YOUR PULL REQUEST DOES NOT REFERENCE AN ACCEPTED BUG REPORT OR FEATURE REQUEST, IT WILL BE MARKED AS INVALID AND CLOSED. --> ### Fixes: #7702 <!-- Please include a summary of the proposed changes below. --> Implemented the ability for users to override the default power feed values via user-configurable preferences. I ran into a bit of a fork in the road when adding this functionality. Initially, I was replacing the model defaults with config items, for example: ```python class PowerFeed(NetBoxModel, PathEndpoint, LinkTermination): """ An electrical circuit delivered from a PowerPanel. """ voltage = models.SmallIntegerField( default=ConfigItem('POWERFEED_DEFAULT_VOLTAGE') validators=[ExclusionValidator([0])] ) amperage = models.PositiveSmallIntegerField( validators=[MinValueValidator(1)], default=ConfigItem('POWERFEED_DEFAULT_AMPERAGE') ) max_utilization = models.PositiveSmallIntegerField( validators=[MinValueValidator(1), MaxValueValidator(100)], default=ConfigItem('POWERFEED_DEFAULT_MAX_UTILIZATION') help_text="Maximum permissible draw (percentage)" ``` Django's documentation regarding migrations does state the following: > Django never sets database defaults and always applies them in the Django ORM code. But after investing the rest of the codebase, I didn't see user-defined values as defaults for any of the other models, so I assumed the standard is to let the view handle it: ```python class PowerFeedCreateView(generic.ObjectEditView): queryset = PowerFeed.objects.all() form = forms.PowerFeedForm def alter_object(self, obj, request, args, kwargs): obj.voltage = ConfigItem('POWERFEED_DEFAULT_VOLTAGE') obj.amperage = ConfigItem('POWERFEED_DEFAULT_AMPERAGE') obj.max_utilization = ConfigItem('POWERFEED_DEFAULT_MAX_UTILIZATION') return obj ``` However, if the maintainers decide otherwise or see no reason not to allow users to define the model defaults, I can make that adjustment. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
adam added the pull-request label 2025-12-29 23:19:09 +01:00
adam closed this issue 2025-12-29 23:19:10 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#13489