blob: cc8d62b84f96eeb778b112c38477f0d7bc5e4be8 [file] [log] [blame]
Scott Baker171d35e2016-06-20 17:36:29 -07001from rest_framework.decorators import api_view
2from rest_framework.response import Response
3from rest_framework.reverse import reverse
4from rest_framework import serializers
5from rest_framework import generics
6from rest_framework import status
7from core.models import *
8from django.forms import widgets
9from services.vtr.models import VTRTenant, VTRService
10from xos.apibase import XOSListCreateAPIView, XOSRetrieveUpdateDestroyAPIView, XOSPermissionDenied
11from api.xosapi_helpers import PlusModelSerializer, XOSViewSet, ReadOnlyField
12
13def get_default_vtr_service():
14 vtr_services = VTRService.get_service_objects().all()
15 if vtr_services:
16 return vtr_services[0]
17 return None
18
19class VTRTenantForAPI(VTRTenant):
20 class Meta:
21 proxy = True
22 app_label = "cord"
23
24class VTRTenantSerializer(PlusModelSerializer):
25 id = ReadOnlyField()
26 target_id = serializers.IntegerField()
27 test = serializers.CharField()
28 scope = serializers.CharField()
29 argument = serializers.CharField(required=False)
30 provider_service = serializers.PrimaryKeyRelatedField(queryset=VTRService.get_service_objects().all(), default=get_default_vtr_service)
31 result = serializers.CharField(required=False)
32 result_code = serializers.CharField(required=False)
33 backend_status = ReadOnlyField()
34
35 humanReadableName = serializers.SerializerMethodField("getHumanReadableName")
36 is_synced = serializers.SerializerMethodField("isSynced")
37
38 class Meta:
39 model = VTRTenantForAPI
40 fields = ('humanReadableName', 'id', 'provider_service', 'target_id', 'scope', 'test', 'argument', 'result', 'result_code', 'is_synced', 'backend_status' )
41
42 def getHumanReadableName(self, obj):
43 return obj.__unicode__()
44
45 def isSynced(self, obj):
46 return (obj.enacted is not None) and (obj.enacted >= obj.updated)
47
48class TruckRollViewSet(XOSViewSet):
49 base_name = "truckroll"
50 method_name = "truckroll"
51 method_kind = "viewset"
52 queryset = VTRTenantForAPI.get_tenant_objects().all() # select_related().all()
53 serializer_class = VTRTenantSerializer
54
55 @classmethod
56 def get_urlpatterns(self, api_path="^"):
57 patterns = super(TruckRollViewSet, self).get_urlpatterns(api_path=api_path)
58
59 return patterns
60
61 def list(self, request):
62 queryset = self.filter_queryset(self.get_queryset())
63
64 serializer = self.get_serializer(queryset, many=True)
65
66 return Response(serializer.data)
67