blob: 25e4d1ed4213bcbd737f64ef551b09a2b77d53b9 [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 Baker8b89d302015-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 Bakere791dc62014-08-28 14:02:54 -070025class SlicePlusIdSerializer(serializers.ModelSerializer, PlusSerializerMixin):
Scott Bakerc8af5752014-12-23 11:12:07 -080026 id = IdField()
Scott Baker88e34372014-07-13 11:46:36 -070027
28 sliceInfo = serializers.SerializerMethodField("getSliceInfo")
Scott Baker196ffef2014-11-21 11:32:24 -080029 humanReadableName = serializers.SerializerMethodField("getHumanReadableName")
Scott Bakerd3a6b2c2015-01-08 22:37:34 -080030 network_ports = NetworkPortsField()
Scott Baker88e34372014-07-13 11:46:36 -070031
32 def getSliceInfo(self, slice):
Scott Bakerdb236c32014-07-13 17:36:19 -070033 return slice.getSliceInfo(user=self.context['request'].user)
Scott Baker88e34372014-07-13 11:46:36 -070034
Scott Baker196ffef2014-11-21 11:32:24 -080035 def getHumanReadableName(self, obj):
36 return str(obj)
37
Scott Baker8b89d302015-01-08 22:34:51 -080038 networks = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
39# availableNetworks = serializers.PrimaryKeyRelatedField(many=True, read_only=True, view_name='network-detail')
Scott Baker88e34372014-07-13 11:46:36 -070040
41 class Meta:
42 model = SlicePlus
Scott Bakerd3a6b2c2015-01-08 22:37:34 -080043 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','sliceInfo','network_ports','backendIcon','backendHtml')
Scott Baker88e34372014-07-13 11:46:36 -070044
45class SlicePlusList(generics.ListCreateAPIView):
46 queryset = SlicePlus.objects.select_related().all()
47 serializer_class = SlicePlusIdSerializer
48
Scott Bakerdb236c32014-07-13 17:36:19 -070049 method_kind = "list"
50 method_name = "slicesplus"
51
Scott Baker88e34372014-07-13 11:46:36 -070052 def get_queryset(self):
53 return SlicePlus.select_by_user(self.request.user)
54
55 def create(self, request, *args, **kwargs):
56 obj = self.get_object()
57 obj.caller = request.user
58 if obj.can_update(request.user):
59 return super(SliceList, self).create(request, *args, **kwargs)
60 else:
61 return Response(status=status.HTTP_400_BAD_REQUEST)
62
63class SlicePlusDetail(generics.RetrieveUpdateDestroyAPIView):
64 queryset = SlicePlus.objects.select_related().all()
65 serializer_class = SlicePlusIdSerializer
66
Scott Bakerdb236c32014-07-13 17:36:19 -070067 method_kind = "detail"
68 method_name = "slicesplus"
69
Scott Baker88e34372014-07-13 11:46:36 -070070 def get_queryset(self):
71 return SlicePlus.select_by_user(self.request.user)
72
73 def update(self, request, *args, **kwargs):
74 obj = self.get_object()
75 if obj.can_update(request.user):
76 return super(SlicePlusDetail, self).update(request, *args, **kwargs)
77 else:
78 return Response(status=status.HTTP_400_BAD_REQUEST)
79
80 def destroy(self, request, *args, **kwargs):
81 obj = self.get_object()
82 if obj.can_update(request.user):
83 return super(SlicePlusDetail, self).destroy(request, *args, **kwargs)
84 else:
85 return Response(status=status.HTTP_400_BAD_REQUEST)