blob: 4de2a6ed5070df202467d69ade9fdb121973aead [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 Baker8974e552015-02-10 19:26:00 -08009from plus import PlusSerializerMixin
Scott Bakereeea5472015-02-16 23:46:01 -080010from xos.apibase import XOSListCreateAPIView, XOSRetrieveUpdateDestroyAPIView, XOSPermissionDenied
Scott Baker88e34372014-07-13 11:46:36 -070011
Scott Bakerc8af5752014-12-23 11:12:07 -080012if hasattr(serializers, "ReadOnlyField"):
13 # rest_framework 3.x
14 IdField = serializers.ReadOnlyField
15else:
16 # rest_framework 2.x
17 IdField = serializers.Field
18
Scott Baker8b89d302015-01-08 22:34:51 -080019class NetworkPortsField(serializers.WritableField): # note: maybe just Field in rest_framework 3.x instead of WritableField
20 def to_representation(self, obj):
21 return obj
22
23 def to_internal_value(self, data):
24 return data
25
Scott Baker97acad92015-01-12 19:45:40 -080026class DictionaryField(serializers.WritableField): # note: maybe just Field in rest_framework 3.x instead of WritableField
27 def to_representation(self, obj):
28 return json.dumps(obj)
29
30 def to_internal_value(self, data):
31 return json.loads(data)
32
33class ListField(serializers.WritableField): # note: maybe just Field in rest_framework 3.x instead of WritableField
Scott Bakerdcf6fbf2015-01-11 13:45:19 -080034 def to_representation(self, obj):
35 return json.dumps(obj)
36
37 def to_internal_value(self, data):
38 return json.loads(data)
39
Scott Bakere791dc62014-08-28 14:02:54 -070040class SlicePlusIdSerializer(serializers.ModelSerializer, PlusSerializerMixin):
Scott Bakerc8af5752014-12-23 11:12:07 -080041 id = IdField()
Scott Baker88e34372014-07-13 11:46:36 -070042
43 sliceInfo = serializers.SerializerMethodField("getSliceInfo")
Scott Baker196ffef2014-11-21 11:32:24 -080044 humanReadableName = serializers.SerializerMethodField("getHumanReadableName")
Scott Bakerdcf6fbf2015-01-11 13:45:19 -080045 network_ports = NetworkPortsField(required=False)
Scott Baker97acad92015-01-12 19:45:40 -080046 site_allocation = DictionaryField(required=False)
Scott Baker21909342015-01-22 15:21:24 -080047 site_ready = DictionaryField(required=False)
Scott Baker97acad92015-01-12 19:45:40 -080048 users = ListField(required=False)
Scott Baker7a76f322015-01-16 19:07:36 -080049 user_names = ListField(required=False) # readonly = True ?
Scott Bakere0e407a2015-01-20 17:02:53 -080050 current_user_can_see = serializers.SerializerMethodField("getCurrentUserCanSee")
51
52 def getCurrentUserCanSee(self, slice):
53 # user can 'see' the slice if he is the creator or he has a role
54 current_user = self.context['request'].user
55 if (slice.creator and slice.creator==current_user):
56 return True;
57 return (len(slice.getSliceInfo(current_user)["roles"]) > 0)
Scott Baker88e34372014-07-13 11:46:36 -070058
59 def getSliceInfo(self, slice):
Scott Bakerdb236c32014-07-13 17:36:19 -070060 return slice.getSliceInfo(user=self.context['request'].user)
Scott Baker88e34372014-07-13 11:46:36 -070061
Scott Baker196ffef2014-11-21 11:32:24 -080062 def getHumanReadableName(self, obj):
63 return str(obj)
64
Scott Baker8b89d302015-01-08 22:34:51 -080065 networks = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
Scott Baker88e34372014-07-13 11:46:36 -070066
67 class Meta:
68 model = SlicePlus
Scott Baker339d5122015-01-12 13:36:40 -080069 fields = ('humanReadableName', 'id','created','updated','enacted','name','enabled','omf_friendly','description','slice_url','site','max_slivers','service','network','mount_data_sets',
70 'default_image', 'default_flavor',
Scott Baker21909342015-01-22 15:21:24 -080071 'serviceClass','creator','networks','sliceInfo','network_ports','backendIcon','backendHtml','site_allocation','site_ready','users',"user_names","current_user_can_see")
Scott Baker88e34372014-07-13 11:46:36 -070072
Scott Baker8974e552015-02-10 19:26:00 -080073class SlicePlusList(XOSListCreateAPIView):
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 = "list"
78 method_name = "slicesplus"
79
Scott Baker88e34372014-07-13 11:46:36 -070080 def get_queryset(self):
Scott Bakere0e407a2015-01-20 17:02:53 -080081 current_user_can_see = self.request.QUERY_PARAMS.get('current_user_can_see', False)
82
Scott Baker960431e2015-02-02 10:41:12 -080083 if (not self.request.user.is_authenticated()):
Scott Baker9fd6c1c2015-02-16 23:34:31 -080084 raise XOSPermissionDenied("You must be authenticated in order to use this API")
Scott Baker960431e2015-02-02 10:41:12 -080085
Scott Bakere0e407a2015-01-20 17:02:53 -080086 slices = SlicePlus.select_by_user(self.request.user)
87
88 # If current_user_can_see is set, then filter the queryset to return
89 # only those slices that the user is either creator or has privilege
90 # on.
91 if (current_user_can_see):
92 slice_ids = []
93 for slice in slices:
94 if (self.request.user == slice.creator) or (len(slice.getSliceInfo(self.request.user)["roles"]) > 0):
95 slice_ids.append(slice.id)
96
97 slices = SlicePlus.objects.filter(id__in=slice_ids)
98
99 return slices
Scott Baker88e34372014-07-13 11:46:36 -0700100
Scott Baker8974e552015-02-10 19:26:00 -0800101class SlicePlusDetail(XOSRetrieveUpdateDestroyAPIView):
Scott Baker88e34372014-07-13 11:46:36 -0700102 queryset = SlicePlus.objects.select_related().all()
103 serializer_class = SlicePlusIdSerializer
104
Scott Bakerdb236c32014-07-13 17:36:19 -0700105 method_kind = "detail"
106 method_name = "slicesplus"
107
Scott Baker88e34372014-07-13 11:46:36 -0700108 def get_queryset(self):
Scott Baker960431e2015-02-02 10:41:12 -0800109 if (not self.request.user.is_authenticated()):
Scott Baker9fd6c1c2015-02-16 23:34:31 -0800110 raise XOSPermissionDenied("You must be authenticated in order to use this API")
Scott Baker88e34372014-07-13 11:46:36 -0700111 return SlicePlus.select_by_user(self.request.user)
112
Scott Baker88e34372014-07-13 11:46:36 -0700113