blob: 120a4aa962dbce41412f2581dc82b5946801694f [file] [log] [blame]
Matteo Scandolo35113f72017-08-08 13:05:25 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
Scott Baker619de672016-06-20 12:49:38 -070017from rest_framework.decorators import api_view
18from rest_framework.response import Response
19from rest_framework.reverse import reverse
20from rest_framework import serializers
21from rest_framework import generics
22from rest_framework import status
23from core.models import *
24from django.forms import widgets
25from xos.apibase import XOSListCreateAPIView, XOSRetrieveUpdateDestroyAPIView, XOSPermissionDenied
26from api.xosapi_helpers import PlusModelSerializer, XOSViewSet, ReadOnlyField
27
28from services.exampleservice.models import ExampleTenant, ExampleService
29
30def get_default_example_service():
Scott Baker589e6c42017-05-25 16:55:18 -070031 example_services = ExampleService.objects.all()
Scott Baker619de672016-06-20 12:49:38 -070032 if example_services:
33 return example_services[0]
34 return None
35
36class ExampleTenantSerializer(PlusModelSerializer):
37 id = ReadOnlyField()
Scott Baker589e6c42017-05-25 16:55:18 -070038 provider_service = serializers.PrimaryKeyRelatedField(queryset=ExampleService.objects.all(), default=get_default_example_service)
Scott Baker619de672016-06-20 12:49:38 -070039 tenant_message = serializers.CharField(required=False)
40 backend_status = ReadOnlyField()
41
42 humanReadableName = serializers.SerializerMethodField("getHumanReadableName")
43
44 class Meta:
45 model = ExampleTenant
46 fields = ('humanReadableName', 'id', 'provider_service', 'tenant_message', 'backend_status')
47
48 def getHumanReadableName(self, obj):
49 return obj.__unicode__()
50
51class ExampleTenantViewSet(XOSViewSet):
52 base_name = "exampletenant"
53 method_name = "exampletenant"
54 method_kind = "viewset"
Scott Baker589e6c42017-05-25 16:55:18 -070055 queryset = ExampleTenant.objects.all()
Scott Baker619de672016-06-20 12:49:38 -070056 serializer_class = ExampleTenantSerializer
57
58 @classmethod
59 def get_urlpatterns(self, api_path="^"):
60 patterns = super(ExampleTenantViewSet, self).get_urlpatterns(api_path=api_path)
61
62 # example to demonstrate adding a custom endpoint
63 patterns.append( self.detail_url("message/$", {"get": "get_message", "put": "set_message"}, "message") )
64
65 return patterns
66
67 def list(self, request):
68 queryset = self.filter_queryset(self.get_queryset())
69
70 serializer = self.get_serializer(queryset, many=True)
71
72 return Response(serializer.data)
73
74 def get_message(self, request, pk=None):
75 example_tenant = self.get_object()
76 return Response({"tenant_message": example_tenant.tenant_message})
77
78 def set_message(self, request, pk=None):
79 example_tenant = self.get_object()
80 example_tenant.tenant_message = request.data["tenant_message"]
81 example_tenant.save()
82 return Response({"tenant_message": example_tenant.tenant_message})
83