blob: ac781bf608b28facb6193cd4370127d624199e54 [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
Scott Baker5db44a92017-03-06 17:27:52 -080010from services.volt.models import CordSubscriberRoot
Scott Baker171d35e2016-06-20 17:36:29 -070011from xos.apibase import XOSListCreateAPIView, XOSRetrieveUpdateDestroyAPIView, XOSPermissionDenied
12from api.xosapi_helpers import PlusModelSerializer, XOSViewSet, ReadOnlyField
Scott Baker5db44a92017-03-06 17:27:52 -080013from django.contrib.contenttypes.models import ContentType
Scott Baker171d35e2016-06-20 17:36:29 -070014
15def get_default_vtr_service():
16 vtr_services = VTRService.get_service_objects().all()
17 if vtr_services:
18 return vtr_services[0]
19 return None
20
21class VTRTenantForAPI(VTRTenant):
22 class Meta:
23 proxy = True
24 app_label = "cord"
25
26class VTRTenantSerializer(PlusModelSerializer):
27 id = ReadOnlyField()
28 target_id = serializers.IntegerField()
29 test = serializers.CharField()
30 scope = serializers.CharField()
31 argument = serializers.CharField(required=False)
32 provider_service = serializers.PrimaryKeyRelatedField(queryset=VTRService.get_service_objects().all(), default=get_default_vtr_service)
33 result = serializers.CharField(required=False)
34 result_code = serializers.CharField(required=False)
35 backend_status = ReadOnlyField()
36
37 humanReadableName = serializers.SerializerMethodField("getHumanReadableName")
38 is_synced = serializers.SerializerMethodField("isSynced")
39
40 class Meta:
41 model = VTRTenantForAPI
42 fields = ('humanReadableName', 'id', 'provider_service', 'target_id', 'scope', 'test', 'argument', 'result', 'result_code', 'is_synced', 'backend_status' )
43
44 def getHumanReadableName(self, obj):
45 return obj.__unicode__()
46
47 def isSynced(self, obj):
48 return (obj.enacted is not None) and (obj.enacted >= obj.updated)
49
Scott Baker5db44a92017-03-06 17:27:52 -080050 def create(self, validated_data):
51 # force the target_type to be CordSubscriberRoot
52 validated_data["target_type_id"] = ContentType.objects.get_for_model(CordSubscriberRoot).id
53 return super(VTRTenantSerializer, self).create(validated_data)
54
Scott Baker171d35e2016-06-20 17:36:29 -070055class TruckRollViewSet(XOSViewSet):
56 base_name = "truckroll"
57 method_name = "truckroll"
58 method_kind = "viewset"
59 queryset = VTRTenantForAPI.get_tenant_objects().all() # select_related().all()
60 serializer_class = VTRTenantSerializer
61
62 @classmethod
63 def get_urlpatterns(self, api_path="^"):
64 patterns = super(TruckRollViewSet, self).get_urlpatterns(api_path=api_path)
65
66 return patterns
67
68 def list(self, request):
69 queryset = self.filter_queryset(self.get_queryset())
70
71 serializer = self.get_serializer(queryset, many=True)
72
73 return Response(serializer.data)
74