blob: 0539e621b7e9a9c953e553e24ac2afc0d213fb66 [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
10from xosapibase import XOSListCreateAPIView, XOSRetrieveUpdateDestroyAPIView
Scott Baker864e3dc2015-02-10 18:33:59 -080011from rest_framework.exceptions import PermissionDenied as RestFrameworkPermissionDenied
Scott Baker88e34372014-07-13 11:46:36 -070012
Scott Bakerc8af5752014-12-23 11:12:07 -080013if hasattr(serializers, "ReadOnlyField"):
14 # rest_framework 3.x
15 IdField = serializers.ReadOnlyField
16else:
17 # rest_framework 2.x
18 IdField = serializers.Field
19
Scott Baker8b89d302015-01-08 22:34:51 -080020class NetworkPortsField(serializers.WritableField): # note: maybe just Field in rest_framework 3.x instead of WritableField
21 def to_representation(self, obj):
22 return obj
23
24 def to_internal_value(self, data):
25 return data
26
Scott Baker97acad92015-01-12 19:45:40 -080027class DictionaryField(serializers.WritableField): # note: maybe just Field in rest_framework 3.x instead of WritableField
28 def to_representation(self, obj):
29 return json.dumps(obj)
30
31 def to_internal_value(self, data):
32 return json.loads(data)
33
34class ListField(serializers.WritableField): # note: maybe just Field in rest_framework 3.x instead of WritableField
Scott Bakerdcf6fbf2015-01-11 13:45:19 -080035 def to_representation(self, obj):
36 return json.dumps(obj)
37
38 def to_internal_value(self, data):
39 return json.loads(data)
40
Scott Bakere791dc62014-08-28 14:02:54 -070041class SlicePlusIdSerializer(serializers.ModelSerializer, PlusSerializerMixin):
Scott Bakerc8af5752014-12-23 11:12:07 -080042 id = IdField()
Scott Baker88e34372014-07-13 11:46:36 -070043
44 sliceInfo = serializers.SerializerMethodField("getSliceInfo")
Scott Baker196ffef2014-11-21 11:32:24 -080045 humanReadableName = serializers.SerializerMethodField("getHumanReadableName")
Scott Bakerdcf6fbf2015-01-11 13:45:19 -080046 network_ports = NetworkPortsField(required=False)
Scott Baker97acad92015-01-12 19:45:40 -080047 site_allocation = DictionaryField(required=False)
Scott Baker21909342015-01-22 15:21:24 -080048 site_ready = DictionaryField(required=False)
Scott Baker97acad92015-01-12 19:45:40 -080049 users = ListField(required=False)
Scott Baker7a76f322015-01-16 19:07:36 -080050 user_names = ListField(required=False) # readonly = True ?
Scott Bakere0e407a2015-01-20 17:02:53 -080051 current_user_can_see = serializers.SerializerMethodField("getCurrentUserCanSee")
52
53 def getCurrentUserCanSee(self, slice):
54 # user can 'see' the slice if he is the creator or he has a role
55 current_user = self.context['request'].user
56 if (slice.creator and slice.creator==current_user):
57 return True;
58 return (len(slice.getSliceInfo(current_user)["roles"]) > 0)
Scott Baker88e34372014-07-13 11:46:36 -070059
60 def getSliceInfo(self, slice):
Scott Bakerdb236c32014-07-13 17:36:19 -070061 return slice.getSliceInfo(user=self.context['request'].user)
Scott Baker88e34372014-07-13 11:46:36 -070062
Scott Baker196ffef2014-11-21 11:32:24 -080063 def getHumanReadableName(self, obj):
64 return str(obj)
65
Scott Baker8b89d302015-01-08 22:34:51 -080066 networks = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
Scott Baker88e34372014-07-13 11:46:36 -070067
68 class Meta:
69 model = SlicePlus
Scott Baker339d5122015-01-12 13:36:40 -080070 fields = ('humanReadableName', 'id','created','updated','enacted','name','enabled','omf_friendly','description','slice_url','site','max_slivers','service','network','mount_data_sets',
71 'default_image', 'default_flavor',
Scott Baker21909342015-01-22 15:21:24 -080072 '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 -070073
Scott Baker8974e552015-02-10 19:26:00 -080074class SlicePlusList(XOSListCreateAPIView):
Scott Baker88e34372014-07-13 11:46:36 -070075 queryset = SlicePlus.objects.select_related().all()
76 serializer_class = SlicePlusIdSerializer
77
Scott Bakerdb236c32014-07-13 17:36:19 -070078 method_kind = "list"
79 method_name = "slicesplus"
80
Scott Baker88e34372014-07-13 11:46:36 -070081 def get_queryset(self):
Scott Bakere0e407a2015-01-20 17:02:53 -080082 current_user_can_see = self.request.QUERY_PARAMS.get('current_user_can_see', False)
83
Scott Baker960431e2015-02-02 10:41:12 -080084 if (not self.request.user.is_authenticated()):
Scott Baker864e3dc2015-02-10 18:33:59 -080085 raise RestFrameworkPermissionDenied("You must be authenticated in order to use this API")
Scott Baker960431e2015-02-02 10:41:12 -080086
Scott Bakere0e407a2015-01-20 17:02:53 -080087 slices = SlicePlus.select_by_user(self.request.user)
88
89 # If current_user_can_see is set, then filter the queryset to return
90 # only those slices that the user is either creator or has privilege
91 # on.
92 if (current_user_can_see):
93 slice_ids = []
94 for slice in slices:
95 if (self.request.user == slice.creator) or (len(slice.getSliceInfo(self.request.user)["roles"]) > 0):
96 slice_ids.append(slice.id)
97
98 slices = SlicePlus.objects.filter(id__in=slice_ids)
99
100 return slices
Scott Baker88e34372014-07-13 11:46:36 -0700101
Scott Baker8974e552015-02-10 19:26:00 -0800102class SlicePlusDetail(XOSRetrieveUpdateDestroyAPIView):
Scott Baker88e34372014-07-13 11:46:36 -0700103 queryset = SlicePlus.objects.select_related().all()
104 serializer_class = SlicePlusIdSerializer
105
Scott Bakerdb236c32014-07-13 17:36:19 -0700106 method_kind = "detail"
107 method_name = "slicesplus"
108
Scott Baker88e34372014-07-13 11:46:36 -0700109 def get_queryset(self):
Scott Baker960431e2015-02-02 10:41:12 -0800110 if (not self.request.user.is_authenticated()):
Scott Baker864e3dc2015-02-10 18:33:59 -0800111 raise RestFrameworkPermissionDenied("You must be authenticated in order to use this API")
Scott Baker88e34372014-07-13 11:46:36 -0700112 return SlicePlus.select_by_user(self.request.user)
113
Scott Baker88e34372014-07-13 11:46:36 -0700114