Skip to content

Commit

Permalink
Added ability to add custom contract types
Browse files Browse the repository at this point in the history
  • Loading branch information
qduk committed Oct 18, 2023
1 parent f9506c1 commit f676ca7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/admin/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,4 @@ The plugin behavior can be controlled with the following list of settings.
| enable_backup | True | True | A boolean to represent whether or not to run backup configurations within the plugin. |
| platform_slug_map | {"cisco_wlc": "cisco_aireos"} | None | A dictionary in which the key is the platform slug and the value is what netutils uses in any "network_os" parameter. |
| per_feature_bar_width | 0.15 | 0.15 | The width of the table bar within the overview report |
| contract_types | ["Software Maintenance", "Software License"] | None | A list of contract types to append to the existing Hardware and Software types. |
5 changes: 4 additions & 1 deletion nautobot_device_lifecycle_mgmt/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
ValidatedSoftwareLCM,
VulnerabilityLCM,
)
from nautobot_device_lifecycle_mgmt.utils import add_custom_contract_types

logger = logging.getLogger("nautobot_device_lifecycle_mgmt")

Expand Down Expand Up @@ -632,7 +633,9 @@ class ContractLCMForm(NautobotModelForm):
to_field_name="pk",
required=True,
)
contract_type = forms.ChoiceField(choices=add_blank_choice(ContractTypeChoices.CHOICES), label="Contract Type")
contract_type = forms.ChoiceField(
choices=add_blank_choice(add_custom_contract_types(ContractTypeChoices.CHOICES)), label="Contract Type"
)
currency = forms.ChoiceField(required=False, choices=add_blank_choice(CurrencyChoices.CHOICES))
tags = DynamicModelMultipleChoiceField(queryset=Tag.objects.all(), required=False)
devices = DynamicModelMultipleChoiceField(queryset=Device.objects.all(), required=False)
Expand Down
13 changes: 13 additions & 0 deletions nautobot_device_lifecycle_mgmt/utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
"""Utility functions and classes used by the plugin."""
from django.conf import settings
from django.db.models import Count, OuterRef, Subquery
from django.db.models.functions import Coalesce

PLUGIN_SETTINGS = settings.PLUGINS_CONFIG["nautobot_device_lifecycle_mgmt"]


def count_related_m2m(model, field):
"""Return a Subquery suitable for annotating a m2m field count."""
subquery = Subquery(model.objects.filter(**{"pk": OuterRef("pk")}).order_by().annotate(c=Count(field)).values("c"))

return Coalesce(subquery, 0)


def add_custom_contract_types(choices):
"""Add custom defined choices to existing Contract Type choices.
Args:
choices (tuple): Existing Device Lifecycle Contract Type tuple.
"""
defined_contract_types = PLUGIN_SETTINGS.get("contract_types", [])
return tuple((type.upper(), type) for type in defined_contract_types) + tuple(choices)

0 comments on commit f676ca7

Please sign in to comment.