Matteo Scandolo | 35113f7 | 2017-08-08 13:05:25 -0700 | [diff] [blame^] | 1 | |
| 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 Baker | 619de67 | 2016-06-20 12:49:38 -0700 | [diff] [blame] | 17 | from rest_framework.decorators import api_view |
| 18 | from rest_framework.response import Response |
| 19 | from rest_framework.reverse import reverse |
| 20 | from rest_framework import serializers |
| 21 | from rest_framework import generics |
| 22 | from rest_framework import status |
| 23 | from core.models import * |
| 24 | from django.forms import widgets |
| 25 | from xos.apibase import XOSListCreateAPIView, XOSRetrieveUpdateDestroyAPIView, XOSPermissionDenied |
| 26 | from api.xosapi_helpers import PlusModelSerializer, XOSViewSet, ReadOnlyField |
| 27 | |
| 28 | from services.exampleservice.models import ExampleTenant, ExampleService |
| 29 | |
| 30 | def get_default_example_service(): |
Scott Baker | 589e6c4 | 2017-05-25 16:55:18 -0700 | [diff] [blame] | 31 | example_services = ExampleService.objects.all() |
Scott Baker | 619de67 | 2016-06-20 12:49:38 -0700 | [diff] [blame] | 32 | if example_services: |
| 33 | return example_services[0] |
| 34 | return None |
| 35 | |
| 36 | class ExampleTenantSerializer(PlusModelSerializer): |
| 37 | id = ReadOnlyField() |
Scott Baker | 589e6c4 | 2017-05-25 16:55:18 -0700 | [diff] [blame] | 38 | provider_service = serializers.PrimaryKeyRelatedField(queryset=ExampleService.objects.all(), default=get_default_example_service) |
Scott Baker | 619de67 | 2016-06-20 12:49:38 -0700 | [diff] [blame] | 39 | 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 | |
| 51 | class ExampleTenantViewSet(XOSViewSet): |
| 52 | base_name = "exampletenant" |
| 53 | method_name = "exampletenant" |
| 54 | method_kind = "viewset" |
Scott Baker | 589e6c4 | 2017-05-25 16:55:18 -0700 | [diff] [blame] | 55 | queryset = ExampleTenant.objects.all() |
Scott Baker | 619de67 | 2016-06-20 12:49:38 -0700 | [diff] [blame] | 56 | 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 | |