| from rest_framework.decorators import api_view |
| from rest_framework.response import Response |
| from rest_framework.reverse import reverse |
| from rest_framework import serializers |
| from rest_framework import generics |
| from rest_framework import status |
| from core.models import * |
| from django.forms import widgets |
| from services.vtr.models import VTRTenant, VTRService |
| from services.rcord.models import CordSubscriberRoot |
| from xos.apibase import XOSListCreateAPIView, XOSRetrieveUpdateDestroyAPIView, XOSPermissionDenied |
| from api.xosapi_helpers import PlusModelSerializer, XOSViewSet, ReadOnlyField |
| from django.contrib.contenttypes.models import ContentType |
| |
| def get_default_vtr_service(): |
| vtr_services = VTRService.get_service_objects().all() |
| if vtr_services: |
| return vtr_services[0] |
| return None |
| |
| class VTRTenantForAPI(VTRTenant): |
| class Meta: |
| proxy = True |
| app_label = "cord" |
| |
| class VTRTenantSerializer(PlusModelSerializer): |
| id = ReadOnlyField() |
| target_id = serializers.IntegerField() |
| test = serializers.CharField() |
| scope = serializers.CharField() |
| argument = serializers.CharField(required=False) |
| provider_service = serializers.PrimaryKeyRelatedField(queryset=VTRService.get_service_objects().all(), default=get_default_vtr_service) |
| result = serializers.CharField(required=False) |
| result_code = serializers.CharField(required=False) |
| backend_status = ReadOnlyField() |
| |
| humanReadableName = serializers.SerializerMethodField("getHumanReadableName") |
| is_synced = serializers.SerializerMethodField("isSynced") |
| |
| class Meta: |
| model = VTRTenantForAPI |
| fields = ('humanReadableName', 'id', 'provider_service', 'target_id', 'scope', 'test', 'argument', 'result', 'result_code', 'is_synced', 'backend_status' ) |
| |
| def getHumanReadableName(self, obj): |
| return obj.__unicode__() |
| |
| def isSynced(self, obj): |
| return (obj.enacted is not None) and (obj.enacted >= obj.updated) |
| |
| def create(self, validated_data): |
| # force the target_type to be CordSubscriberRoot |
| validated_data["target_type_id"] = ContentType.objects.get_for_model(CordSubscriberRoot).id |
| return super(VTRTenantSerializer, self).create(validated_data) |
| |
| class TruckRollViewSet(XOSViewSet): |
| base_name = "truckroll" |
| method_name = "truckroll" |
| method_kind = "viewset" |
| queryset = VTRTenantForAPI.get_tenant_objects().all() # select_related().all() |
| serializer_class = VTRTenantSerializer |
| |
| @classmethod |
| def get_urlpatterns(self, api_path="^"): |
| patterns = super(TruckRollViewSet, self).get_urlpatterns(api_path=api_path) |
| |
| return patterns |
| |
| def list(self, request): |
| queryset = self.filter_queryset(self.get_queryset()) |
| |
| serializer = self.get_serializer(queryset, many=True) |
| |
| return Response(serializer.data) |
| |