Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame^] | 1 | from rest_framework.decorators import api_view |
| 2 | from rest_framework.response import Response |
| 3 | from rest_framework.reverse import reverse |
| 4 | from rest_framework import serializers |
| 5 | from rest_framework import generics |
| 6 | from rest_framework import status |
| 7 | from core.models import * |
| 8 | from django.forms import widgets |
| 9 | from services.vtr.models import VTRTenant, VTRService |
| 10 | from xos.apibase import XOSListCreateAPIView, XOSRetrieveUpdateDestroyAPIView, XOSPermissionDenied |
| 11 | from api.xosapi_helpers import PlusModelSerializer, XOSViewSet, ReadOnlyField |
| 12 | |
| 13 | def 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 | |
| 19 | class VTRTenantForAPI(VTRTenant): |
| 20 | class Meta: |
| 21 | proxy = True |
| 22 | app_label = "cord" |
| 23 | |
| 24 | class 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 | |
| 48 | class 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 | |