2020-05-08 16:29:18 +00:00
|
|
|
"""Flow API Views"""
|
2020-11-27 17:37:45 +00:00
|
|
|
from django.core.cache import cache
|
2020-05-08 17:46:39 +00:00
|
|
|
from rest_framework.serializers import ModelSerializer, SerializerMethodField
|
|
|
|
from rest_framework.viewsets import ModelViewSet, ReadOnlyModelViewSet
|
2020-05-08 16:29:18 +00:00
|
|
|
|
2020-05-08 17:46:39 +00:00
|
|
|
from passbook.flows.models import Flow, FlowStageBinding, Stage
|
2020-11-27 17:37:45 +00:00
|
|
|
from passbook.flows.planner import cache_key
|
2020-05-08 16:29:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
class FlowSerializer(ModelSerializer):
|
|
|
|
"""Flow Serializer"""
|
|
|
|
|
2020-11-27 17:37:45 +00:00
|
|
|
cache_count = SerializerMethodField()
|
|
|
|
|
|
|
|
def get_cache_count(self, flow: Flow):
|
|
|
|
"""Get count of cached flows"""
|
|
|
|
return len(cache.keys(f"{cache_key(flow)}*"))
|
|
|
|
|
2020-05-08 16:29:18 +00:00
|
|
|
class Meta:
|
|
|
|
|
|
|
|
model = Flow
|
2020-11-27 17:37:45 +00:00
|
|
|
fields = [
|
|
|
|
"pk",
|
|
|
|
"name",
|
|
|
|
"slug",
|
|
|
|
"title",
|
|
|
|
"designation",
|
|
|
|
"stages",
|
|
|
|
"policies",
|
|
|
|
"cache_count",
|
|
|
|
]
|
2020-05-08 16:29:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
class FlowViewSet(ModelViewSet):
|
|
|
|
"""Flow Viewset"""
|
|
|
|
|
|
|
|
queryset = Flow.objects.all()
|
|
|
|
serializer_class = FlowSerializer
|
|
|
|
|
|
|
|
|
2020-05-08 17:46:39 +00:00
|
|
|
class FlowStageBindingSerializer(ModelSerializer):
|
|
|
|
"""FlowStageBinding Serializer"""
|
2020-05-08 16:29:18 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
2020-05-08 17:46:39 +00:00
|
|
|
model = FlowStageBinding
|
2020-10-20 13:06:36 +00:00
|
|
|
fields = [
|
|
|
|
"pk",
|
|
|
|
"target",
|
|
|
|
"stage",
|
|
|
|
"evaluate_on_plan",
|
2020-10-20 13:41:50 +00:00
|
|
|
"re_evaluate_policies",
|
2020-10-20 13:06:36 +00:00
|
|
|
"order",
|
|
|
|
"policies",
|
|
|
|
]
|
2020-05-08 16:29:18 +00:00
|
|
|
|
|
|
|
|
2020-05-08 17:46:39 +00:00
|
|
|
class FlowStageBindingViewSet(ModelViewSet):
|
|
|
|
"""FlowStageBinding Viewset"""
|
2020-05-08 16:29:18 +00:00
|
|
|
|
2020-05-08 17:46:39 +00:00
|
|
|
queryset = FlowStageBinding.objects.all()
|
|
|
|
serializer_class = FlowStageBindingSerializer
|
2020-05-13 09:57:10 +00:00
|
|
|
filterset_fields = "__all__"
|
2020-05-08 17:46:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
class StageSerializer(ModelSerializer):
|
|
|
|
"""Stage Serializer"""
|
|
|
|
|
|
|
|
__type__ = SerializerMethodField(method_name="get_type")
|
2020-05-13 09:57:10 +00:00
|
|
|
verbose_name = SerializerMethodField(method_name="get_verbose_name")
|
2020-05-08 17:46:39 +00:00
|
|
|
|
2020-05-13 09:57:10 +00:00
|
|
|
def get_type(self, obj: Stage) -> str:
|
2020-05-08 17:46:39 +00:00
|
|
|
"""Get object type so that we know which API Endpoint to use to get the full object"""
|
|
|
|
return obj._meta.object_name.lower().replace("stage", "")
|
|
|
|
|
2020-05-13 09:57:10 +00:00
|
|
|
def get_verbose_name(self, obj: Stage) -> str:
|
|
|
|
"""Get verbose name for UI"""
|
|
|
|
return obj._meta.verbose_name
|
|
|
|
|
2020-05-08 17:46:39 +00:00
|
|
|
class Meta:
|
|
|
|
|
|
|
|
model = Stage
|
2020-05-13 09:57:10 +00:00
|
|
|
fields = ["pk", "name", "__type__", "verbose_name"]
|
2020-05-08 17:46:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
class StageViewSet(ReadOnlyModelViewSet):
|
|
|
|
"""Stage Viewset"""
|
|
|
|
|
|
|
|
queryset = Stage.objects.all()
|
|
|
|
serializer_class = StageSerializer
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
return Stage.objects.select_subclasses()
|