blob: 9e93e6de8a2898ac74321b7ffb3c89b53fcfacef [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 Bakerfcf655e2015-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 Baker03696ee2015-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 Bakerf0480fb2014-12-23 11:12:07 -080040 id = IdField()
Scott Baker88e34372014-07-13 11:46:36 -070041
42 sliceInfo = serializers.SerializerMethodField("getSliceInfo")
Scott Bakerb744f1c2014-11-21 11:32:24 -080043 humanReadableName = serializers.SerializerMethodField("getHumanReadableName")
Scott Baker03696ee2015-01-11 13:45:19 -080044 network_ports = NetworkPortsField(required=False)
Scott Bakerfcf655e2015-01-12 19:45:40 -080045 site_allocation = DictionaryField(required=False)
46 users = ListField(required=False)
Scott Bakera31bb242015-01-16 19:07:36 -080047 user_names = ListField(required=False) # readonly = True ?
Scott Bakerc18f52b2015-01-20 17:02:53 -080048 current_user_can_see = serializers.SerializerMethodField("getCurrentUserCanSee")
49
50 def getCurrentUserCanSee(self, slice):
51 # user can 'see' the slice if he is the creator or he has a role
52 current_user = self.context['request'].user
53 if (slice.creator and slice.creator==current_user):
54 return True;
55 return (len(slice.getSliceInfo(current_user)["roles"]) > 0)
Scott Baker88e34372014-07-13 11:46:36 -070056
57 def getSliceInfo(self, slice):
Scott Bakerdb236c32014-07-13 17:36:19 -070058 return slice.getSliceInfo(user=self.context['request'].user)
Scott Baker88e34372014-07-13 11:46:36 -070059
Scott Bakerb744f1c2014-11-21 11:32:24 -080060 def getHumanReadableName(self, obj):
61 return str(obj)
62
Scott Baker9062b0c2015-01-08 22:34:51 -080063 networks = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
Scott Baker88e34372014-07-13 11:46:36 -070064
65 class Meta:
66 model = SlicePlus
Scott Baker4618a232015-01-12 13:36:40 -080067 fields = ('humanReadableName', 'id','created','updated','enacted','name','enabled','omf_friendly','description','slice_url','site','max_slivers','service','network','mount_data_sets',
68 'default_image', 'default_flavor',
Scott Bakerc18f52b2015-01-20 17:02:53 -080069 'serviceClass','creator','networks','sliceInfo','network_ports','backendIcon','backendHtml','site_allocation','users',"user_names","current_user_can_see")
Scott Baker88e34372014-07-13 11:46:36 -070070
Scott Bakerc18f52b2015-01-20 17:02:53 -080071class SlicePlusList(PlusListCreateAPIView):
Scott Baker88e34372014-07-13 11:46:36 -070072 queryset = SlicePlus.objects.select_related().all()
73 serializer_class = SlicePlusIdSerializer
74
Scott Bakerdb236c32014-07-13 17:36:19 -070075 method_kind = "list"
76 method_name = "slicesplus"
77
Scott Baker88e34372014-07-13 11:46:36 -070078 def get_queryset(self):
Scott Bakerc18f52b2015-01-20 17:02:53 -080079 current_user_can_see = self.request.QUERY_PARAMS.get('current_user_can_see', False)
80
81 slices = SlicePlus.select_by_user(self.request.user)
82
83 # If current_user_can_see is set, then filter the queryset to return
84 # only those slices that the user is either creator or has privilege
85 # on.
86 if (current_user_can_see):
87 slice_ids = []
88 for slice in slices:
89 if (self.request.user == slice.creator) or (len(slice.getSliceInfo(self.request.user)["roles"]) > 0):
90 slice_ids.append(slice.id)
91
92 slices = SlicePlus.objects.filter(id__in=slice_ids)
93
94 return slices
Scott Baker88e34372014-07-13 11:46:36 -070095
Scott Baker03696ee2015-01-11 13:45:19 -080096class SlicePlusDetail(PlusRetrieveUpdateDestroyAPIView):
Scott Baker88e34372014-07-13 11:46:36 -070097 queryset = SlicePlus.objects.select_related().all()
98 serializer_class = SlicePlusIdSerializer
99
Scott Bakerdb236c32014-07-13 17:36:19 -0700100 method_kind = "detail"
101 method_name = "slicesplus"
102
Scott Baker88e34372014-07-13 11:46:36 -0700103 def get_queryset(self):
104 return SlicePlus.select_by_user(self.request.user)
105
106 def update(self, request, *args, **kwargs):
107 obj = self.get_object()
108 if obj.can_update(request.user):
109 return super(SlicePlusDetail, self).update(request, *args, **kwargs)
110 else:
111 return Response(status=status.HTTP_400_BAD_REQUEST)
112
113 def destroy(self, request, *args, **kwargs):
114 obj = self.get_object()
115 if obj.can_update(request.user):
116 return super(SlicePlusDetail, self).destroy(request, *args, **kwargs)
117 else:
118 return Response(status=status.HTTP_400_BAD_REQUEST)