Scott Baker | 7a32759 | 2016-06-20 17:34:06 -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.onos.models import ONOSService, ONOSApp |
| 10 | from xos.apibase import XOSListCreateAPIView, XOSRetrieveUpdateDestroyAPIView, XOSPermissionDenied |
| 11 | from api.xosapi_helpers import PlusModelSerializer, XOSViewSet, ReadOnlyField |
| 12 | |
| 13 | def get_default_onos_service(): |
| 14 | onos_services = ONOSService.get_service_objects().all() |
| 15 | if onos_services: |
| 16 | return onos_services[0].id |
| 17 | return None |
| 18 | |
| 19 | class ONOSAppSerializer(PlusModelSerializer): |
| 20 | id = ReadOnlyField() |
| 21 | name = serializers.CharField() |
| 22 | dependencies = serializers.CharField() |
| 23 | |
| 24 | humanReadableName = serializers.SerializerMethodField("getHumanReadableName") |
| 25 | class Meta: |
| 26 | model = ONOSApp |
| 27 | fields = ('humanReadableName', 'id', 'name', 'dependencies') |
| 28 | |
| 29 | def getHumanReadableName(self, obj): |
| 30 | return obj.__unicode__() |
| 31 | |
| 32 | class TenantAttributeSerializer(serializers.Serializer): |
| 33 | id = ReadOnlyField() |
| 34 | name = serializers.CharField(required=False) |
| 35 | value = serializers.CharField(required=False) |
| 36 | |
| 37 | class ONOSAppViewSet(XOSViewSet): |
| 38 | base_name = "app" |
| 39 | method_name = "app" |
| 40 | method_kind = "viewset" |
| 41 | queryset = ONOSApp.get_tenant_objects().all() |
| 42 | serializer_class = ONOSAppSerializer |
| 43 | |
| 44 | custom_serializers = {"set_attribute": TenantAttributeSerializer} |
| 45 | |
| 46 | @classmethod |
| 47 | def get_urlpatterns(self, api_path="^"): |
| 48 | patterns = super(ONOSAppViewSet, self).get_urlpatterns(api_path=api_path) |
| 49 | |
| 50 | patterns.append( self.detail_url("attributes/$", {"get": "get_attributes", "post": "add_attribute"}, "attributes") ) |
| 51 | patterns.append( self.detail_url("attributes/(?P<attribute>[0-9]+)/$", {"get": "get_attribute", "put": "set_attribute", "delete": "delete_attribute"}, "attribute") ) |
| 52 | |
| 53 | return patterns |
| 54 | |
| 55 | def get_attributes(self, request, pk=None): |
| 56 | app = self.get_object() |
| 57 | return Response(TenantAttributeSerializer(app.tenantattributes.all(), many=True).data) |
| 58 | |
| 59 | def add_attribute(self, request, pk=None): |
| 60 | app = self.get_object() |
| 61 | ser = TenantAttributeSerializer(data=request.data) |
| 62 | ser.is_valid(raise_exception = True) |
| 63 | att = TenantAttribute(tenant=app, **ser.validated_data) |
| 64 | att.save() |
| 65 | return Response(TenantAttributeSerializer(att).data) |
| 66 | |
| 67 | def get_attribute(self, request, pk=None, attribute=None): |
| 68 | app = self.get_object() |
| 69 | att = TenantAttribute.objects.get(pk=attribute) |
| 70 | return Response(TenantAttributeSerializer(att).data) |
| 71 | |
| 72 | def set_attribute(self, request, pk=None, attribute=None): |
| 73 | app = self.get_object() |
| 74 | att = TenantAttribute.objects.get(pk=attribute) |
| 75 | ser = TenantAttributeSerializer(att, data=request.data) |
| 76 | ser.is_valid(raise_exception = True) |
| 77 | att.name = ser.validated_data.get("name", att.name) |
| 78 | att.value = ser.validated_data.get("value", att.value) |
| 79 | att.save() |
| 80 | return Response(TenantAttributeSerializer(att).data) |
| 81 | |
| 82 | def delete_attribute(self, request, pk=None, attribute=None): |
| 83 | att = TenantAttribute.objects.get(pk=attribute) |
| 84 | att.delete() |
| 85 | return Response(status=status.HTTP_204_NO_CONTENT) |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |