blob: fa17c1c68cb5ae9f78bc6bf8758c4532a64fb691 [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 Baker77eba942015-01-12 12:57:22 -08009from plus import PlusSerializerMixin, PlusRetrieveUpdateDestroyAPIView, PlusListCreateAPIView
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 Baker9062b0c2015-01-08 22:34:51 -080018class NetworkPortsField(serializers.WritableField): # note: maybe just Field in rest_framework 3.x instead of WritableField
19 def to_representation(self, obj):
20 return obj
21
22 def to_internal_value(self, data):
23 return data
24
Scott Baker03696ee2015-01-11 13:45:19 -080025class SiteAllocationField(serializers.WritableField): # note: maybe just Field in rest_framework 3.x instead of WritableField
26 def to_representation(self, obj):
27 return json.dumps(obj)
28
29 def to_internal_value(self, data):
30 return json.loads(data)
31
Scott Bakere791dc62014-08-28 14:02:54 -070032class SlicePlusIdSerializer(serializers.ModelSerializer, PlusSerializerMixin):
Scott Bakerf0480fb2014-12-23 11:12:07 -080033 id = IdField()
Scott Baker88e34372014-07-13 11:46:36 -070034
35 sliceInfo = serializers.SerializerMethodField("getSliceInfo")
Scott Bakerb744f1c2014-11-21 11:32:24 -080036 humanReadableName = serializers.SerializerMethodField("getHumanReadableName")
Scott Baker03696ee2015-01-11 13:45:19 -080037 network_ports = NetworkPortsField(required=False)
38 site_allocation = SiteAllocationField(required=False)
Scott Baker88e34372014-07-13 11:46:36 -070039
40 def getSliceInfo(self, slice):
Scott Bakerdb236c32014-07-13 17:36:19 -070041 return slice.getSliceInfo(user=self.context['request'].user)
Scott Baker88e34372014-07-13 11:46:36 -070042
Scott Bakerb744f1c2014-11-21 11:32:24 -080043 def getHumanReadableName(self, obj):
44 return str(obj)
45
Scott Baker9062b0c2015-01-08 22:34:51 -080046 networks = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
47# availableNetworks = serializers.PrimaryKeyRelatedField(many=True, read_only=True, view_name='network-detail')
Scott Baker88e34372014-07-13 11:46:36 -070048
49 class Meta:
50 model = SlicePlus
Scott Baker03696ee2015-01-11 13:45:19 -080051 fields = ('humanReadableName', 'id','created','updated','enacted','name','enabled','omf_friendly','description','slice_url','site','max_slivers','image_preference','service','network','mount_data_sets',
52 'serviceClass','creator','networks','sliceInfo','network_ports','backendIcon','backendHtml','site_allocation')
Scott Baker88e34372014-07-13 11:46:36 -070053
Scott Baker77eba942015-01-12 12:57:22 -080054class SlicePlusList(PlusListCreateAPIView): #generics.ListCreateAPIView):
Scott Baker88e34372014-07-13 11:46:36 -070055 queryset = SlicePlus.objects.select_related().all()
56 serializer_class = SlicePlusIdSerializer
57
Scott Bakerdb236c32014-07-13 17:36:19 -070058 method_kind = "list"
59 method_name = "slicesplus"
60
Scott Baker88e34372014-07-13 11:46:36 -070061 def get_queryset(self):
62 return SlicePlus.select_by_user(self.request.user)
63
Scott Baker03696ee2015-01-11 13:45:19 -080064class SlicePlusDetail(PlusRetrieveUpdateDestroyAPIView):
Scott Baker88e34372014-07-13 11:46:36 -070065 queryset = SlicePlus.objects.select_related().all()
66 serializer_class = SlicePlusIdSerializer
67
Scott Bakerdb236c32014-07-13 17:36:19 -070068 method_kind = "detail"
69 method_name = "slicesplus"
70
Scott Baker88e34372014-07-13 11:46:36 -070071 def get_queryset(self):
72 return SlicePlus.select_by_user(self.request.user)
73
74 def update(self, request, *args, **kwargs):
75 obj = self.get_object()
76 if obj.can_update(request.user):
77 return super(SlicePlusDetail, self).update(request, *args, **kwargs)
78 else:
79 return Response(status=status.HTTP_400_BAD_REQUEST)
80
81 def destroy(self, request, *args, **kwargs):
82 obj = self.get_object()
83 if obj.can_update(request.user):
84 return super(SlicePlusDetail, self).destroy(request, *args, **kwargs)
85 else:
86 return Response(status=status.HTTP_400_BAD_REQUEST)