diff --git a/docs/admin/install.md b/docs/admin/install.md index 944f398b..73a8f6c1 100644 --- a/docs/admin/install.md +++ b/docs/admin/install.md @@ -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. | diff --git a/nautobot_device_lifecycle_mgmt/forms.py b/nautobot_device_lifecycle_mgmt/forms.py index c9d0410d..51126e7d 100644 --- a/nautobot_device_lifecycle_mgmt/forms.py +++ b/nautobot_device_lifecycle_mgmt/forms.py @@ -39,6 +39,7 @@ ValidatedSoftwareLCM, VulnerabilityLCM, ) +from nautobot_device_lifecycle_mgmt.utils import add_custom_contract_types logger = logging.getLogger("nautobot_device_lifecycle_mgmt") @@ -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) diff --git a/nautobot_device_lifecycle_mgmt/utils.py b/nautobot_device_lifecycle_mgmt/utils.py index d6dbc4a9..2bf207d9 100644 --- a/nautobot_device_lifecycle_mgmt/utils.py +++ b/nautobot_device_lifecycle_mgmt/utils.py @@ -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)