Skip to content

Commit

Permalink
Added ability to change contract type
Browse files Browse the repository at this point in the history
  • Loading branch information
qduk committed Oct 19, 2023
1 parent 94a0e03 commit ad4e181
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions development/nautobot_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@
"barchart_bar_width": float(os.environ.get("BARCHART_BAR_WIDTH", 0.1)),
"barchart_width": int(os.environ.get("BARCHART_WIDTH", 12)),
"barchart_height": int(os.environ.get("BARCHART_HEIGHT", 5)),
"contract_types": ["Software Licensing", "Software Hardware"],
},
}

Expand Down
5 changes: 4 additions & 1 deletion nautobot_device_lifecycle_mgmt/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,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 @@ -753,7 +754,9 @@ class ContractLCMForm(BootstrapMixin, CustomFieldModelFormMixin, RelationshipMod
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, widget=StaticSelect2, choices=add_blank_choice(CurrencyChoices.CHOICES)
)
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, Subquery, OuterRef
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, type) for type in defined_contract_types) + tuple(choices)

0 comments on commit ad4e181

Please sign in to comment.