2020-05-14 11:51:05 +00:00
|
|
|
"""policy API Views"""
|
2020-05-23 22:57:25 +00:00
|
|
|
from rest_framework.serializers import ModelSerializer, SerializerMethodField
|
|
|
|
from rest_framework.viewsets import ModelViewSet, ReadOnlyModelViewSet
|
2020-05-14 11:51:05 +00:00
|
|
|
|
2020-05-23 22:57:25 +00:00
|
|
|
from passbook.policies.forms import GENERAL_FIELDS
|
|
|
|
from passbook.policies.models import Policy, PolicyBinding
|
2020-05-14 11:51:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PolicyBindingSerializer(ModelSerializer):
|
|
|
|
"""PolicyBinding Serializer"""
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
|
|
|
model = PolicyBinding
|
2020-05-28 19:45:54 +00:00
|
|
|
fields = ["policy", "target", "enabled", "order", "timeout"]
|
2020-05-14 11:51:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PolicyBindingViewSet(ModelViewSet):
|
|
|
|
"""PolicyBinding Viewset"""
|
|
|
|
|
|
|
|
queryset = PolicyBinding.objects.all()
|
|
|
|
serializer_class = PolicyBindingSerializer
|
2020-05-23 22:57:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PolicySerializer(ModelSerializer):
|
|
|
|
"""Policy Serializer"""
|
|
|
|
|
|
|
|
__type__ = SerializerMethodField(method_name="get_type")
|
|
|
|
|
|
|
|
def get_type(self, obj):
|
|
|
|
"""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", "")
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
|
|
|
model = Policy
|
|
|
|
fields = ["pk"] + GENERAL_FIELDS + ["__type__"]
|
|
|
|
|
|
|
|
|
|
|
|
class PolicyViewSet(ReadOnlyModelViewSet):
|
|
|
|
"""Policy Viewset"""
|
|
|
|
|
|
|
|
queryset = Policy.objects.all()
|
|
|
|
serializer_class = PolicySerializer
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
return Policy.objects.select_subclasses()
|