This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
2019-10-28 16:40:57 +00:00
|
|
|
"""Policy API Views"""
|
|
|
|
from rest_framework.serializers import ModelSerializer, SerializerMethodField
|
|
|
|
from rest_framework.viewsets import ReadOnlyModelViewSet
|
|
|
|
|
|
|
|
from passbook.policies.forms import GENERAL_FIELDS
|
2020-05-15 20:15:01 +00:00
|
|
|
from passbook.policies.models import Policy
|
2019-10-28 16:40:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PolicySerializer(ModelSerializer):
|
|
|
|
"""Policy Serializer"""
|
|
|
|
|
2019-12-31 11:51:16 +00:00
|
|
|
__type__ = SerializerMethodField(method_name="get_type")
|
2019-10-28 16:40:57 +00:00
|
|
|
|
|
|
|
def get_type(self, obj):
|
|
|
|
"""Get object type so that we know which API Endpoint to use to get the full object"""
|
2019-12-31 11:51:16 +00:00
|
|
|
return obj._meta.object_name.lower().replace("policy", "")
|
2019-10-28 16:40:57 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
|
|
|
model = Policy
|
2019-12-31 11:51:16 +00:00
|
|
|
fields = ["pk"] + GENERAL_FIELDS + ["__type__"]
|
2019-10-28 16:40:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PolicyViewSet(ReadOnlyModelViewSet):
|
|
|
|
"""Policy Viewset"""
|
|
|
|
|
|
|
|
queryset = Policy.objects.all()
|
|
|
|
serializer_class = PolicySerializer
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
return Policy.objects.select_subclasses()
|