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