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 |
Scott Baker | 6cc5147 | 2017-04-20 15:20:23 -0700 | [diff] [blame] | 10 | from services.rcord.models import CordSubscriberRoot |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 11 | from xos.apibase import XOSListCreateAPIView, XOSRetrieveUpdateDestroyAPIView, XOSPermissionDenied |
| 12 | from api.xosapi_helpers import PlusModelSerializer, XOSViewSet, ReadOnlyField |
Scott Baker | 5db44a9 | 2017-03-06 17:27:52 -0800 | [diff] [blame] | 13 | from django.contrib.contenttypes.models import ContentType |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 14 | |
| 15 | def get_default_vtr_service(): |
Scott Baker | 379404c | 2017-05-24 17:13:45 -0700 | [diff] [blame] | 16 | vtr_services = VTRService.objects.all() |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 17 | if vtr_services: |
| 18 | return vtr_services[0] |
| 19 | return None |
| 20 | |
| 21 | class VTRTenantForAPI(VTRTenant): |
| 22 | class Meta: |
| 23 | proxy = True |
| 24 | app_label = "cord" |
| 25 | |
| 26 | class VTRTenantSerializer(PlusModelSerializer): |
| 27 | id = ReadOnlyField() |
| 28 | target_id = serializers.IntegerField() |
| 29 | test = serializers.CharField() |
| 30 | scope = serializers.CharField() |
| 31 | argument = serializers.CharField(required=False) |
Scott Baker | 379404c | 2017-05-24 17:13:45 -0700 | [diff] [blame] | 32 | provider_service = serializers.PrimaryKeyRelatedField(queryset=VTRService.objects.all(), default=get_default_vtr_service) |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 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 Baker | 5db44a9 | 2017-03-06 17:27:52 -0800 | [diff] [blame] | 50 | 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 Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 55 | class TruckRollViewSet(XOSViewSet): |
| 56 | base_name = "truckroll" |
| 57 | method_name = "truckroll" |
| 58 | method_kind = "viewset" |
Scott Baker | 379404c | 2017-05-24 17:13:45 -0700 | [diff] [blame] | 59 | queryset = VTRTenantForAPI.objects.all() # select_related().all() |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 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 | |