Scott Baker | 619de67 | 2016-06-20 12:49:38 -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 xos.apibase import XOSListCreateAPIView, XOSRetrieveUpdateDestroyAPIView, XOSPermissionDenied |
| 10 | from api.xosapi_helpers import PlusModelSerializer, XOSViewSet, ReadOnlyField |
| 11 | |
| 12 | from services.exampleservice.models import ExampleTenant, ExampleService |
| 13 | |
| 14 | def get_default_example_service(): |
| 15 | example_services = ExampleService.get_service_objects().all() |
| 16 | if example_services: |
| 17 | return example_services[0] |
| 18 | return None |
| 19 | |
| 20 | class ExampleTenantSerializer(PlusModelSerializer): |
| 21 | id = ReadOnlyField() |
| 22 | provider_service = serializers.PrimaryKeyRelatedField(queryset=ExampleService.get_service_objects().all(), default=get_default_example_service) |
| 23 | 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 | |
| 35 | class ExampleTenantViewSet(XOSViewSet): |
| 36 | base_name = "exampletenant" |
| 37 | method_name = "exampletenant" |
| 38 | method_kind = "viewset" |
| 39 | queryset = ExampleTenant.get_tenant_objects().all() |
| 40 | 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 | |