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(): |
Scott Baker | 2d20057 | 2017-05-25 10:52:00 -0700 | [diff] [blame^] | 14 | onos_services = ONOSService.objects.all() |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 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" |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 41 | serializer_class = ONOSAppSerializer |
| 42 | |
| 43 | custom_serializers = {"set_attribute": TenantAttributeSerializer} |
| 44 | |
| 45 | @classmethod |
| 46 | def get_urlpatterns(self, api_path="^"): |
| 47 | patterns = super(ONOSAppViewSet, self).get_urlpatterns(api_path=api_path) |
| 48 | |
| 49 | patterns.append( self.detail_url("attributes/$", {"get": "get_attributes", "post": "add_attribute"}, "attributes") ) |
| 50 | patterns.append( self.detail_url("attributes/(?P<attribute>[0-9]+)/$", {"get": "get_attribute", "put": "set_attribute", "delete": "delete_attribute"}, "attribute") ) |
| 51 | |
| 52 | return patterns |
| 53 | |
Scott Baker | 9507fc9 | 2016-07-27 09:49:14 -0700 | [diff] [blame] | 54 | def get_queryset(self): |
Scott Baker | 2d20057 | 2017-05-25 10:52:00 -0700 | [diff] [blame^] | 55 | queryset = ONOSApp.objects.all() |
Scott Baker | 9507fc9 | 2016-07-27 09:49:14 -0700 | [diff] [blame] | 56 | |
| 57 | # Since name isn't a real database field of the Tenant object, we have |
| 58 | # to go through some extra work... |
| 59 | name = self.request.query_params.get('name', None) |
| 60 | if name is not None: |
| 61 | ids = [x.id for x in queryset.all() if x.name == name] |
| 62 | return queryset.filter(id__in=ids) |
| 63 | |
| 64 | return queryset |
| 65 | |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 66 | def get_attributes(self, request, pk=None): |
| 67 | app = self.get_object() |
Scott Baker | 9507fc9 | 2016-07-27 09:49:14 -0700 | [diff] [blame] | 68 | qs = app.tenantattributes.all() |
| 69 | name = self.request.query_params.get('attribute_name', None) |
| 70 | if name is not None: |
| 71 | qs = qs.filter(name=name) |
| 72 | return Response(TenantAttributeSerializer(qs, many=True).data) |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 73 | |
| 74 | def add_attribute(self, request, pk=None): |
| 75 | app = self.get_object() |
| 76 | ser = TenantAttributeSerializer(data=request.data) |
| 77 | ser.is_valid(raise_exception = True) |
| 78 | att = TenantAttribute(tenant=app, **ser.validated_data) |
| 79 | att.save() |
| 80 | return Response(TenantAttributeSerializer(att).data) |
| 81 | |
| 82 | def get_attribute(self, request, pk=None, attribute=None): |
| 83 | app = self.get_object() |
| 84 | att = TenantAttribute.objects.get(pk=attribute) |
| 85 | return Response(TenantAttributeSerializer(att).data) |
| 86 | |
| 87 | def set_attribute(self, request, pk=None, attribute=None): |
| 88 | app = self.get_object() |
| 89 | att = TenantAttribute.objects.get(pk=attribute) |
| 90 | ser = TenantAttributeSerializer(att, data=request.data) |
| 91 | ser.is_valid(raise_exception = True) |
| 92 | att.name = ser.validated_data.get("name", att.name) |
| 93 | att.value = ser.validated_data.get("value", att.value) |
| 94 | att.save() |
| 95 | return Response(TenantAttributeSerializer(att).data) |
| 96 | |
| 97 | def delete_attribute(self, request, pk=None, attribute=None): |
| 98 | att = TenantAttribute.objects.get(pk=attribute) |
| 99 | att.delete() |
| 100 | return Response(status=status.HTTP_204_NO_CONTENT) |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |