blob: 386a77eaeadb23a734c6c1e507764ba0a60cbd47 [file] [log] [blame]
Scott Baker619de672016-06-20 12:49:38 -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 xos.apibase import XOSListCreateAPIView, XOSRetrieveUpdateDestroyAPIView, XOSPermissionDenied
10from api.xosapi_helpers import PlusModelSerializer, XOSViewSet, ReadOnlyField
11
12from services.exampleservice.models import ExampleTenant, ExampleService
13
14def get_default_example_service():
Scott Baker589e6c42017-05-25 16:55:18 -070015 example_services = ExampleService.objects.all()
Scott Baker619de672016-06-20 12:49:38 -070016 if example_services:
17 return example_services[0]
18 return None
19
20class ExampleTenantSerializer(PlusModelSerializer):
21 id = ReadOnlyField()
Scott Baker589e6c42017-05-25 16:55:18 -070022 provider_service = serializers.PrimaryKeyRelatedField(queryset=ExampleService.objects.all(), default=get_default_example_service)
Scott Baker619de672016-06-20 12:49:38 -070023 tenant_message = serializers.CharField(required=False)
24 backend_status = ReadOnlyField()
25
26 humanReadableName = serializers.SerializerMethodField("getHumanReadableName")
27
28 class Meta:
29 model = ExampleTenant
30 fields = ('humanReadableName', 'id', 'provider_service', 'tenant_message', 'backend_status')
31
32 def getHumanReadableName(self, obj):
33 return obj.__unicode__()
34
35class ExampleTenantViewSet(XOSViewSet):
36 base_name = "exampletenant"
37 method_name = "exampletenant"
38 method_kind = "viewset"
Scott Baker589e6c42017-05-25 16:55:18 -070039 queryset = ExampleTenant.objects.all()
Scott Baker619de672016-06-20 12:49:38 -070040 serializer_class = ExampleTenantSerializer
41
42 @classmethod
43 def get_urlpatterns(self, api_path="^"):
44 patterns = super(ExampleTenantViewSet, self).get_urlpatterns(api_path=api_path)
45
46 # example to demonstrate adding a custom endpoint
47 patterns.append( self.detail_url("message/$", {"get": "get_message", "put": "set_message"}, "message") )
48
49 return patterns
50
51 def list(self, request):
52 queryset = self.filter_queryset(self.get_queryset())
53
54 serializer = self.get_serializer(queryset, many=True)
55
56 return Response(serializer.data)
57
58 def get_message(self, request, pk=None):
59 example_tenant = self.get_object()
60 return Response({"tenant_message": example_tenant.tenant_message})
61
62 def set_message(self, request, pk=None):
63 example_tenant = self.get_object()
64 example_tenant.tenant_message = request.data["tenant_message"]
65 example_tenant.save()
66 return Response({"tenant_message": example_tenant.tenant_message})
67