blob: b046a889629d9de1725bfdc20642225432dcd4ca [file] [log] [blame]
Scott Bakerbbd84c32016-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 xos.apibase import XOSListCreateAPIView, XOSRetrieveUpdateDestroyAPIView, XOSPermissionDenied
11from api.xosapi_helpers import PlusModelSerializer, XOSViewSet, ReadOnlyField
12
13from services.exampleservice.models import ExampleTenant, ExampleService
14
15def get_default_example_service():
16 example_services = ExampleService.get_service_objects().all()
17 if example_services:
18 return example_services[0]
19 return None
20
21class ExampleTenantSerializer(PlusModelSerializer):
22 id = ReadOnlyField()
23 provider_service = serializers.PrimaryKeyRelatedField(queryset=ExampleService.get_service_objects().all(), default=get_default_example_service)
24 tenant_message = serializers.CharField(required=False)
25 backend_status = ReadOnlyField()
26
27 humanReadableName = serializers.SerializerMethodField("getHumanReadableName")
28
29 class Meta:
30 model = ExampleTenant
31 fields = ('humanReadableName', 'id', 'provider_service', 'tenant_message', 'backend_status')
32
33 def getHumanReadableName(self, obj):
34 return obj.__unicode__()
35
36class ExampleTenantViewSet(XOSViewSet):
37 base_name = "exampletenant"
38 method_name = "exampletenant"
39 method_kind = "viewset"
40 queryset = ExampleTenant.get_tenant_objects().all()
41 serializer_class = ExampleTenantSerializer
42
43 @classmethod
44 def get_urlpatterns(self, api_path="^"):
45 patterns = super(ExampleTenantViewSet, self).get_urlpatterns(api_path=api_path)
46
47 return patterns
48
49 def list(self, request):
50 queryset = self.filter_queryset(self.get_queryset())
51
52 serializer = self.get_serializer(queryset, many=True)
53
54 return Response(serializer.data)
55