| |
| # Copyright 2017-present Open Networking Foundation |
| # |
| # Licensed under the Apache License, Version 2.0 (the "License"); |
| # you may not use this file except in compliance with the License. |
| # You may obtain a copy of the License at |
| # |
| # http://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, software |
| # distributed under the License is distributed on an "AS IS" BASIS, |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| # See the License for the specific language governing permissions and |
| # limitations under the License. |
| |
| |
| 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 xos.apibase import XOSListCreateAPIView, XOSRetrieveUpdateDestroyAPIView, XOSPermissionDenied |
| from api.xosapi_helpers import PlusModelSerializer, XOSViewSet, ReadOnlyField |
| |
| from services.exampleservice.models import ExampleTenant, ExampleService |
| |
| def get_default_example_service(): |
| example_services = ExampleService.objects.all() |
| if example_services: |
| return example_services[0] |
| return None |
| |
| class ExampleTenantSerializer(PlusModelSerializer): |
| id = ReadOnlyField() |
| provider_service = serializers.PrimaryKeyRelatedField(queryset=ExampleService.objects.all(), default=get_default_example_service) |
| tenant_message = serializers.CharField(required=False) |
| backend_status = ReadOnlyField() |
| |
| humanReadableName = serializers.SerializerMethodField("getHumanReadableName") |
| |
| class Meta: |
| model = ExampleTenant |
| fields = ('humanReadableName', 'id', 'provider_service', 'tenant_message', 'backend_status') |
| |
| def getHumanReadableName(self, obj): |
| return obj.__unicode__() |
| |
| class ExampleTenantViewSet(XOSViewSet): |
| base_name = "exampletenant" |
| method_name = "exampletenant" |
| method_kind = "viewset" |
| queryset = ExampleTenant.objects.all() |
| serializer_class = ExampleTenantSerializer |
| |
| @classmethod |
| def get_urlpatterns(self, api_path="^"): |
| patterns = super(ExampleTenantViewSet, self).get_urlpatterns(api_path=api_path) |
| |
| # example to demonstrate adding a custom endpoint |
| patterns.append( self.detail_url("message/$", {"get": "get_message", "put": "set_message"}, "message") ) |
| |
| return patterns |
| |
| def list(self, request): |
| queryset = self.filter_queryset(self.get_queryset()) |
| |
| serializer = self.get_serializer(queryset, many=True) |
| |
| return Response(serializer.data) |
| |
| def get_message(self, request, pk=None): |
| example_tenant = self.get_object() |
| return Response({"tenant_message": example_tenant.tenant_message}) |
| |
| def set_message(self, request, pk=None): |
| example_tenant = self.get_object() |
| example_tenant.tenant_message = request.data["tenant_message"] |
| example_tenant.save() |
| return Response({"tenant_message": example_tenant.tenant_message}) |
| |