policies: add bound count to api

This commit is contained in:
Jens Langhammer 2021-02-19 16:53:30 +01:00
parent 44e51970e1
commit 79089d8981
2 changed files with 48 additions and 2 deletions

View File

@ -59,15 +59,22 @@ class PolicySerializer(ModelSerializer, MetaNameSerializer):
_resolve_inheritance: bool _resolve_inheritance: bool
object_type = SerializerMethodField() object_type = SerializerMethodField()
bound_to = SerializerMethodField()
def __init__(self, *args, resolve_inheritance: bool = True, **kwargs): def __init__(self, *args, resolve_inheritance: bool = True, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self._resolve_inheritance = resolve_inheritance self._resolve_inheritance = resolve_inheritance
def get_object_type(self, obj): def get_object_type(self, obj: Policy) -> str:
"""Get object type so that we know which API Endpoint to use to get the full object""" """Get object type so that we know which API Endpoint to use to get the full object"""
return obj._meta.object_name.lower().replace("policy", "") return obj._meta.object_name.lower().replace("policy", "")
def get_bound_to(self, obj: Policy) -> int:
"""Return objects policy is bound to"""
if not obj.bindings.exists() and not obj.promptstage_set.exists():
return 0
return obj.bindings.count()
def to_representation(self, instance: Policy): def to_representation(self, instance: Policy):
# pyright: reportGeneralTypeIssues=false # pyright: reportGeneralTypeIssues=false
if instance.__class__ == Policy or not self._resolve_inheritance: if instance.__class__ == Policy or not self._resolve_inheritance:
@ -86,6 +93,7 @@ class PolicySerializer(ModelSerializer, MetaNameSerializer):
"object_type", "object_type",
"verbose_name", "verbose_name",
"verbose_name_plural", "verbose_name_plural",
"bound_to",
] ]
depth = 3 depth = 3
@ -101,7 +109,9 @@ class PolicyViewSet(ReadOnlyModelViewSet):
} }
def get_queryset(self): def get_queryset(self):
return Policy.objects.select_subclasses() return Policy.objects.select_subclasses().prefetch_related(
"bindings", "promptstage_set"
)
@swagger_auto_schema(responses={200: TypeCreateSerializer(many=True)}) @swagger_auto_schema(responses={200: TypeCreateSerializer(many=True)})
@action(detail=False) @action(detail=False)

View File

@ -8853,6 +8853,10 @@ definitions:
title: Verbose name plural title: Verbose name plural
type: string type: string
readOnly: true readOnly: true
bound_to:
title: Bound to
type: integer
readOnly: true
TypeCreate: TypeCreate:
description: Types of an object that can be created description: Types of an object that can be created
type: object type: object
@ -9254,6 +9258,10 @@ definitions:
title: Verbose name plural title: Verbose name plural
type: string type: string
readOnly: true readOnly: true
bound_to:
title: Bound to
type: integer
readOnly: true
result: result:
title: Result title: Result
type: boolean type: boolean
@ -9297,6 +9305,10 @@ definitions:
title: Verbose name plural title: Verbose name plural
type: string type: string
readOnly: true readOnly: true
bound_to:
title: Bound to
type: integer
readOnly: true
action: action:
title: Action title: Action
description: Match created events with this action type. When left empty, description: Match created events with this action type. When left empty,
@ -9410,6 +9422,10 @@ definitions:
title: Verbose name plural title: Verbose name plural
type: string type: string
readOnly: true readOnly: true
bound_to:
title: Bound to
type: integer
readOnly: true
expression: expression:
title: Expression title: Expression
type: string type: string
@ -9444,6 +9460,10 @@ definitions:
title: Verbose name plural title: Verbose name plural
type: string type: string
readOnly: true readOnly: true
bound_to:
title: Bound to
type: integer
readOnly: true
group: group:
title: Group title: Group
type: string type: string
@ -9479,6 +9499,10 @@ definitions:
title: Verbose name plural title: Verbose name plural
type: string type: string
readOnly: true readOnly: true
bound_to:
title: Bound to
type: integer
readOnly: true
password_field: password_field:
title: Password field title: Password field
description: Field key to check, field keys defined in Prompt stages are available. description: Field key to check, field keys defined in Prompt stages are available.
@ -9521,6 +9545,10 @@ definitions:
title: Verbose name plural title: Verbose name plural
type: string type: string
readOnly: true readOnly: true
bound_to:
title: Bound to
type: integer
readOnly: true
password_field: password_field:
title: Password field title: Password field
description: Field key to check, field keys defined in Prompt stages are available. description: Field key to check, field keys defined in Prompt stages are available.
@ -9586,6 +9614,10 @@ definitions:
title: Verbose name plural title: Verbose name plural
type: string type: string
readOnly: true readOnly: true
bound_to:
title: Bound to
type: integer
readOnly: true
days: days:
title: Days title: Days
type: integer type: integer
@ -9624,6 +9656,10 @@ definitions:
title: Verbose name plural title: Verbose name plural
type: string type: string
readOnly: true readOnly: true
bound_to:
title: Bound to
type: integer
readOnly: true
check_ip: check_ip:
title: Check ip title: Check ip
type: boolean type: boolean