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 viewsets |
| 23 | from rest_framework import status |
| 24 | from rest_framework.decorators import detail_route, list_route |
| 25 | from rest_framework.views import APIView |
| 26 | from core.models import * |
| 27 | from django.forms import widgets |
| 28 | from django.conf.urls import patterns, url |
| 29 | from api.xosapi_helpers import PlusModelSerializer, XOSViewSet, ReadOnlyField |
| 30 | from django.shortcuts import get_object_or_404 |
| 31 | from xos.apibase import XOSListCreateAPIView, XOSRetrieveUpdateDestroyAPIView, XOSPermissionDenied |
| 32 | from xos.exceptions import * |
| 33 | import json |
| 34 | import subprocess |
| 35 | from services.exampleservice.models import ExampleService |
| 36 | |
| 37 | class ExampleServiceSerializer(PlusModelSerializer): |
| 38 | id = ReadOnlyField() |
| 39 | humanReadableName = serializers.SerializerMethodField("getHumanReadableName") |
| 40 | service_message = serializers.CharField(required=False) |
| 41 | |
| 42 | class Meta: |
| 43 | model = ExampleService |
| 44 | fields = ('humanReadableName', |
| 45 | 'id', |
| 46 | 'service_message') |
| 47 | |
| 48 | def getHumanReadableName(self, obj): |
| 49 | return obj.__unicode__() |
| 50 | |
| 51 | class ExampleServiceViewSet(XOSViewSet): |
| 52 | base_name = "exampleservice" |
| 53 | method_name = "exampleservice" |
| 54 | method_kind = "viewset" |
Scott Baker | 589e6c4 | 2017-05-25 16:55:18 -0700 | [diff] [blame] | 55 | queryset = ExampleService.objects.all() |
Scott Baker | 619de67 | 2016-06-20 12:49:38 -0700 | [diff] [blame] | 56 | serializer_class = ExampleServiceSerializer |
| 57 | |
| 58 | @classmethod |
| 59 | def get_urlpatterns(self, api_path="^"): |
| 60 | patterns = super(ExampleServiceViewSet, self).get_urlpatterns(api_path=api_path) |
| 61 | |
| 62 | return patterns |
| 63 | |
| 64 | def list(self, request): |
| 65 | object_list = self.filter_queryset(self.get_queryset()) |
| 66 | |
| 67 | serializer = self.get_serializer(object_list, many=True) |
| 68 | |
| 69 | return Response(serializer.data) |
| 70 | |