blob: 4d15d411632bc01fda7afccb1d123e6ca10a19bf [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 Bakerbbc8a5b2015-01-12 12:57:22 -08009from plus import PlusSerializerMixin, PlusRetrieveUpdateDestroyAPIView, PlusListCreateAPIView
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 Baker97acad92015-01-12 19:45:40 -080025class DictionaryField(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
32class ListField(serializers.WritableField): # note: maybe just Field in rest_framework 3.x instead of WritableField
Scott Bakerdcf6fbf2015-01-11 13:45:19 -080033 def to_representation(self, obj):
34 return json.dumps(obj)
35
36 def to_internal_value(self, data):
37 return json.loads(data)
38
Scott Bakere791dc62014-08-28 14:02:54 -070039class SlicePlusIdSerializer(serializers.ModelSerializer, PlusSerializerMixin):
Scott Bakerc8af5752014-12-23 11:12:07 -080040 id = IdField()
Scott Baker88e34372014-07-13 11:46:36 -070041
42 sliceInfo = serializers.SerializerMethodField("getSliceInfo")
Scott Baker196ffef2014-11-21 11:32:24 -080043 humanReadableName = serializers.SerializerMethodField("getHumanReadableName")
Scott Bakerdcf6fbf2015-01-11 13:45:19 -080044 network_ports = NetworkPortsField(required=False)
Scott Baker97acad92015-01-12 19:45:40 -080045 site_allocation = DictionaryField(required=False)
46 users = ListField(required=False)
Scott Baker7a76f322015-01-16 19:07:36 -080047 user_names = ListField(required=False) # readonly = True ?
Scott Baker88e34372014-07-13 11:46:36 -070048
49 def getSliceInfo(self, slice):
Scott Bakerdb236c32014-07-13 17:36:19 -070050 return slice.getSliceInfo(user=self.context['request'].user)
Scott Baker88e34372014-07-13 11:46:36 -070051
Scott Baker196ffef2014-11-21 11:32:24 -080052 def getHumanReadableName(self, obj):
53 return str(obj)
54
Scott Baker8b89d302015-01-08 22:34:51 -080055 networks = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
Scott Baker88e34372014-07-13 11:46:36 -070056
57 class Meta:
58 model = SlicePlus
Scott Baker339d5122015-01-12 13:36:40 -080059 fields = ('humanReadableName', 'id','created','updated','enacted','name','enabled','omf_friendly','description','slice_url','site','max_slivers','service','network','mount_data_sets',
60 'default_image', 'default_flavor',
Scott Baker7a76f322015-01-16 19:07:36 -080061 'serviceClass','creator','networks','sliceInfo','network_ports','backendIcon','backendHtml','site_allocation','users',"user_names")
Scott Baker88e34372014-07-13 11:46:36 -070062
Scott Bakerbbc8a5b2015-01-12 12:57:22 -080063class SlicePlusList(PlusListCreateAPIView): #generics.ListCreateAPIView):
Scott Baker88e34372014-07-13 11:46:36 -070064 queryset = SlicePlus.objects.select_related().all()
65 serializer_class = SlicePlusIdSerializer
66
Scott Bakerdb236c32014-07-13 17:36:19 -070067 method_kind = "list"
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
Scott Bakerdcf6fbf2015-01-11 13:45:19 -080073class SlicePlusDetail(PlusRetrieveUpdateDestroyAPIView):
Scott Baker88e34372014-07-13 11:46:36 -070074 queryset = SlicePlus.objects.select_related().all()
75 serializer_class = SlicePlusIdSerializer
76
Scott Bakerdb236c32014-07-13 17:36:19 -070077 method_kind = "detail"
78 method_name = "slicesplus"
79
Scott Baker88e34372014-07-13 11:46:36 -070080 def get_queryset(self):
81 return SlicePlus.select_by_user(self.request.user)
82
83 def update(self, request, *args, **kwargs):
84 obj = self.get_object()
85 if obj.can_update(request.user):
86 return super(SlicePlusDetail, self).update(request, *args, **kwargs)
87 else:
88 return Response(status=status.HTTP_400_BAD_REQUEST)
89
90 def destroy(self, request, *args, **kwargs):
91 obj = self.get_object()
92 if obj.can_update(request.user):
93 return super(SlicePlusDetail, self).destroy(request, *args, **kwargs)
94 else:
95 return Response(status=status.HTTP_400_BAD_REQUEST)