Scott Baker | bbd84c3 | 2016-04-04 15:54:56 -0700 | [diff] [blame] | 1 | from rest_framework.decorators import api_view |
| 2 | from rest_framework.response import Response |
| 3 | from rest_framework.reverse import reverse |
| 4 | from rest_framework import serializers |
| 5 | from rest_framework import generics |
| 6 | from rest_framework import status |
| 7 | from core.models import * |
| 8 | from django.forms import widgets |
| 9 | from services.cord.models import CordSubscriberRoot |
| 10 | from xos.apibase import XOSListCreateAPIView, XOSRetrieveUpdateDestroyAPIView, XOSPermissionDenied |
| 11 | from api.xosapi_helpers import PlusModelSerializer, XOSViewSet, ReadOnlyField |
| 12 | |
| 13 | from services.exampleservice.models import ExampleTenant, ExampleService |
| 14 | |
| 15 | def 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 | |
| 21 | class 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 | |
| 36 | class 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 | |