| 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 services.onos.models import ONOSService, ONOSApp |
| from xos.apibase import XOSListCreateAPIView, XOSRetrieveUpdateDestroyAPIView, XOSPermissionDenied |
| from api.xosapi_helpers import PlusModelSerializer, XOSViewSet, ReadOnlyField |
| |
| def get_default_onos_service(): |
| onos_services = ONOSService.objects.all() |
| if onos_services: |
| return onos_services[0].id |
| return None |
| |
| class ONOSAppSerializer(PlusModelSerializer): |
| id = ReadOnlyField() |
| name = serializers.CharField() |
| dependencies = serializers.CharField() |
| |
| humanReadableName = serializers.SerializerMethodField("getHumanReadableName") |
| class Meta: |
| model = ONOSApp |
| fields = ('humanReadableName', 'id', 'name', 'dependencies') |
| |
| def getHumanReadableName(self, obj): |
| return obj.__unicode__() |
| |
| class TenantAttributeSerializer(serializers.Serializer): |
| id = ReadOnlyField() |
| name = serializers.CharField(required=False) |
| value = serializers.CharField(required=False) |
| |
| class ONOSAppViewSet(XOSViewSet): |
| base_name = "app" |
| method_name = "app" |
| method_kind = "viewset" |
| serializer_class = ONOSAppSerializer |
| |
| custom_serializers = {"set_attribute": TenantAttributeSerializer} |
| |
| @classmethod |
| def get_urlpatterns(self, api_path="^"): |
| patterns = super(ONOSAppViewSet, self).get_urlpatterns(api_path=api_path) |
| |
| patterns.append( self.detail_url("attributes/$", {"get": "get_attributes", "post": "add_attribute"}, "attributes") ) |
| patterns.append( self.detail_url("attributes/(?P<attribute>[0-9]+)/$", {"get": "get_attribute", "put": "set_attribute", "delete": "delete_attribute"}, "attribute") ) |
| |
| return patterns |
| |
| def get_queryset(self): |
| queryset = ONOSApp.objects.all() |
| |
| # Since name isn't a real database field of the Tenant object, we have |
| # to go through some extra work... |
| name = self.request.query_params.get('name', None) |
| if name is not None: |
| ids = [x.id for x in queryset.all() if x.name == name] |
| return queryset.filter(id__in=ids) |
| |
| return queryset |
| |
| def get_attributes(self, request, pk=None): |
| app = self.get_object() |
| qs = app.tenantattributes.all() |
| name = self.request.query_params.get('attribute_name', None) |
| if name is not None: |
| qs = qs.filter(name=name) |
| return Response(TenantAttributeSerializer(qs, many=True).data) |
| |
| def add_attribute(self, request, pk=None): |
| app = self.get_object() |
| ser = TenantAttributeSerializer(data=request.data) |
| ser.is_valid(raise_exception = True) |
| att = TenantAttribute(tenant=app, **ser.validated_data) |
| att.save() |
| return Response(TenantAttributeSerializer(att).data) |
| |
| def get_attribute(self, request, pk=None, attribute=None): |
| app = self.get_object() |
| att = TenantAttribute.objects.get(pk=attribute) |
| return Response(TenantAttributeSerializer(att).data) |
| |
| def set_attribute(self, request, pk=None, attribute=None): |
| app = self.get_object() |
| att = TenantAttribute.objects.get(pk=attribute) |
| ser = TenantAttributeSerializer(att, data=request.data) |
| ser.is_valid(raise_exception = True) |
| att.name = ser.validated_data.get("name", att.name) |
| att.value = ser.validated_data.get("value", att.value) |
| att.save() |
| return Response(TenantAttributeSerializer(att).data) |
| |
| def delete_attribute(self, request, pk=None, attribute=None): |
| att = TenantAttribute.objects.get(pk=attribute) |
| att.delete() |
| return Response(status=status.HTTP_204_NO_CONTENT) |
| |
| |
| |
| |
| |
| |