-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ability to add custom contract types
- Loading branch information
Showing
3 changed files
with
18 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |