blob: 48102dfe8b4d48f3d0bf1e4457d2d113899e0637 [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 Bakerf0480fb2014-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 Bakerf0480fb2014-12-23 11:12:07 -080019 id = IdField()
Scott Baker88e34372014-07-13 11:46:36 -070020
21 sliceInfo = serializers.SerializerMethodField("getSliceInfo")
Scott Bakerb744f1c2014-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 Bakerb744f1c2014-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 Bakerffce7852015-01-03 16:26:38 -080035 fields = ('humanReadableName', 'id','created','updated','enacted','name','enabled','omf_friendly','description','slice_url','site','max_slivers','image_preference','service','network','mount_data_sets','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)