blob: 7e9ae89a22faa40056fe5fa7c4daad7992dba2f6 [file] [log] [blame]
Scott Baker88e34372014-07-13 11:46:36 -07001from rest_framework.decorators import api_view
2from rest_framework.response import Response
3from rest_framework.reverse import reverse
4from rest_framework import serializers
5from rest_framework import generics
6from core.models import *
7from django.forms import widgets
8from core.xoslib.objects.sliceplus import SlicePlus
Scott Bakere791dc62014-08-28 14:02:54 -07009from plus import PlusSerializerMixin
Scott Baker88e34372014-07-13 11:46:36 -070010
Scott Bakerc8af5752014-12-23 11:12:07 -080011if hasattr(serializers, "ReadOnlyField"):
12 # rest_framework 3.x
13 IdField = serializers.ReadOnlyField
14else:
15 # rest_framework 2.x
16 IdField = serializers.Field
17
Scott Bakere791dc62014-08-28 14:02:54 -070018class SlicePlusIdSerializer(serializers.ModelSerializer, PlusSerializerMixin):
Scott Bakerc8af5752014-12-23 11:12:07 -080019 id = IdField()
Scott Baker88e34372014-07-13 11:46:36 -070020
21 sliceInfo = serializers.SerializerMethodField("getSliceInfo")
Scott Baker196ffef2014-11-21 11:32:24 -080022 humanReadableName = serializers.SerializerMethodField("getHumanReadableName")
Scott Baker88e34372014-07-13 11:46:36 -070023
24 def getSliceInfo(self, slice):
Scott Bakerdb236c32014-07-13 17:36:19 -070025 return slice.getSliceInfo(user=self.context['request'].user)
Scott Baker88e34372014-07-13 11:46:36 -070026
Scott Baker196ffef2014-11-21 11:32:24 -080027 def getHumanReadableName(self, obj):
28 return str(obj)
29
Scott Baker88e34372014-07-13 11:46:36 -070030 networks = serializers.HyperlinkedRelatedField(many=True, read_only=True, view_name='network-detail')
31 availableNetworks = serializers.HyperlinkedRelatedField(many=True, read_only=True, view_name='network-detail')
32
33 class Meta:
34 model = SlicePlus
Scott Baker196ffef2014-11-21 11:32:24 -080035 fields = ('humanReadableName', 'id','created','updated','enacted','name','enabled','omf_friendly','description','slice_url','site','max_slivers','imagePreference','service','network','mountDataSets','serviceClass','creator','networks','availableNetworks','sliceInfo','backendIcon','backendHtml')
Scott Baker88e34372014-07-13 11:46:36 -070036
37class SlicePlusList(generics.ListCreateAPIView):
38 queryset = SlicePlus.objects.select_related().all()
39 serializer_class = SlicePlusIdSerializer
40
Scott Bakerdb236c32014-07-13 17:36:19 -070041 method_kind = "list"
42 method_name = "slicesplus"
43
Scott Baker88e34372014-07-13 11:46:36 -070044 def get_queryset(self):
45 return SlicePlus.select_by_user(self.request.user)
46
47 def create(self, request, *args, **kwargs):
48 obj = self.get_object()
49 obj.caller = request.user
50 if obj.can_update(request.user):
51 return super(SliceList, self).create(request, *args, **kwargs)
52 else:
53 return Response(status=status.HTTP_400_BAD_REQUEST)
54
55class SlicePlusDetail(generics.RetrieveUpdateDestroyAPIView):
56 queryset = SlicePlus.objects.select_related().all()
57 serializer_class = SlicePlusIdSerializer
58
Scott Bakerdb236c32014-07-13 17:36:19 -070059 method_kind = "detail"
60 method_name = "slicesplus"
61
Scott Baker88e34372014-07-13 11:46:36 -070062 def get_queryset(self):
63 return SlicePlus.select_by_user(self.request.user)
64
65 def update(self, request, *args, **kwargs):
66 obj = self.get_object()
67 if obj.can_update(request.user):
68 return super(SlicePlusDetail, self).update(request, *args, **kwargs)
69 else:
70 return Response(status=status.HTTP_400_BAD_REQUEST)
71
72 def destroy(self, request, *args, **kwargs):
73 obj = self.get_object()
74 if obj.can_update(request.user):
75 return super(SlicePlusDetail, self).destroy(request, *args, **kwargs)
76 else:
77 return Response(status=status.HTTP_400_BAD_REQUEST)