From 2417d5a59eccc49c692cabc857cc4eeb65eb7484 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Sun, 29 Nov 2020 13:57:35 +0100 Subject: [PATCH] policies: add policy_obj to Binding API --- passbook/flows/transfer/importer.py | 9 ++++- passbook/policies/api.py | 57 +++++++++++++++++------------ swagger.yaml | 2 + 3 files changed, 43 insertions(+), 25 deletions(-) diff --git a/passbook/flows/transfer/importer.py b/passbook/flows/transfer/importer.py index 44911d65d..f5bf464a3 100644 --- a/passbook/flows/transfer/importer.py +++ b/passbook/flows/transfer/importer.py @@ -65,7 +65,10 @@ class FlowImporter: return value for key, value in attrs.items(): - if isinstance(value, (list, dict)): + if isinstance(value, dict): + for idx, _inner_key in enumerate(value): + value[_inner_key] = updater(value[_inner_key]) + elif isinstance(value, list): for idx, _inner_value in enumerate(value): attrs[key][idx] = updater(_inner_value) else: @@ -97,6 +100,10 @@ class FlowImporter: # Because a model might have multiple unique columns, we chain all identifiers together # to create an OR query. updated_identifiers = self.__update_pks_for_attrs(entry.identifiers) + for key, value in list(updated_identifiers.items()): + if isinstance(value, dict) and "pk" in value: + del updated_identifiers[key] + updated_identifiers[f"{key}"] = value["pk"] existing_models = model.objects.filter( self.__query_from_identifier(updated_identifiers) ) diff --git a/passbook/policies/api.py b/passbook/policies/api.py index 3c5a45b5d..f48540bd4 100644 --- a/passbook/policies/api.py +++ b/passbook/policies/api.py @@ -41,30 +41,6 @@ class PolicyBindingModelForeignKey(PrimaryKeyRelatedField): return correct_model.pk -class PolicyBindingSerializer(ModelSerializer): - """PolicyBinding Serializer""" - - # Because we're not interested in the PolicyBindingModel's PK but rather the subclasses PK, - # we have to manually declare this field - target = PolicyBindingModelForeignKey( - queryset=PolicyBindingModel.objects.select_subclasses(), - required=True, - ) - - class Meta: - - model = PolicyBinding - fields = ["pk", "policy", "target", "enabled", "order", "timeout"] - - -class PolicyBindingViewSet(ModelViewSet): - """PolicyBinding Viewset""" - - queryset = PolicyBinding.objects.all() - serializer_class = PolicyBindingSerializer - filterset_fields = ["policy", "target", "enabled", "order", "timeout"] - - class PolicySerializer(ModelSerializer): """Policy Serializer""" @@ -74,10 +50,17 @@ class PolicySerializer(ModelSerializer): """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", "") + def to_representation(self, instance: Policy): + # pyright: reportGeneralTypeIssues=false + if instance.__class__ == Policy: + return super().to_representation(instance) + return instance.serializer(instance=instance).data + class Meta: model = Policy fields = ["pk"] + GENERAL_FIELDS + ["__type__"] + depth = 3 class PolicyViewSet(ReadOnlyModelViewSet): @@ -88,3 +71,29 @@ class PolicyViewSet(ReadOnlyModelViewSet): def get_queryset(self): return Policy.objects.select_subclasses() + + +class PolicyBindingSerializer(ModelSerializer): + """PolicyBinding Serializer""" + + # Because we're not interested in the PolicyBindingModel's PK but rather the subclasses PK, + # we have to manually declare this field + target = PolicyBindingModelForeignKey( + queryset=PolicyBindingModel.objects.select_subclasses(), + required=True, + ) + + policy_obj = PolicySerializer(read_only=True, source="policy") + + class Meta: + + model = PolicyBinding + fields = ["pk", "policy", "policy_obj", "target", "enabled", "order", "timeout"] + + +class PolicyBindingViewSet(ModelViewSet): + """PolicyBinding Viewset""" + + queryset = PolicyBinding.objects.all() + serializer_class = PolicyBindingSerializer + filterset_fields = ["policy", "target", "enabled", "order", "timeout"] diff --git a/swagger.yaml b/swagger.yaml index 8cf01d50b..5ac41263e 100755 --- a/swagger.yaml +++ b/swagger.yaml @@ -7058,6 +7058,8 @@ definitions: title: Policy type: string format: uuid + policy_obj: + $ref: '#/definitions/Policy' target: title: Target type: string