Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 1 | from rest_framework.decorators import api_view |
| 2 | from rest_framework.response import Response
|
| 3 | from rest_framework.reverse import reverse
|
| 4 | from rest_framework import serializers
|
| 5 | from rest_framework import generics
|
| 6 | from core.models import *
|
| 7 | from django.forms import widgets |
| 8 | from core.xoslib.objects.sliceplus import SlicePlus |
Scott Baker | 8974e55 | 2015-02-10 19:26:00 -0800 | [diff] [blame] | 9 | from plus import PlusSerializerMixin |
| 10 | from xosapibase import XOSListCreateAPIView, XOSRetrieveUpdateDestroyAPIView |
Scott Baker | 864e3dc | 2015-02-10 18:33:59 -0800 | [diff] [blame] | 11 | from rest_framework.exceptions import PermissionDenied as RestFrameworkPermissionDenied |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 12 | |
Scott Baker | c8af575 | 2014-12-23 11:12:07 -0800 | [diff] [blame] | 13 | if hasattr(serializers, "ReadOnlyField"): |
| 14 | # rest_framework 3.x |
| 15 | IdField = serializers.ReadOnlyField |
| 16 | else: |
| 17 | # rest_framework 2.x |
| 18 | IdField = serializers.Field |
| 19 | |
Scott Baker | 8b89d30 | 2015-01-08 22:34:51 -0800 | [diff] [blame] | 20 | class 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 Baker | 97acad9 | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 27 | class 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 | |
| 34 | class ListField(serializers.WritableField): # note: maybe just Field in rest_framework 3.x instead of WritableField |
Scott Baker | dcf6fbf | 2015-01-11 13:45:19 -0800 | [diff] [blame] | 35 | 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 Baker | e791dc6 | 2014-08-28 14:02:54 -0700 | [diff] [blame] | 41 | class SlicePlusIdSerializer(serializers.ModelSerializer, PlusSerializerMixin): |
Scott Baker | c8af575 | 2014-12-23 11:12:07 -0800 | [diff] [blame] | 42 | id = IdField() |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 43 |
|
| 44 | sliceInfo = serializers.SerializerMethodField("getSliceInfo")
|
Scott Baker | 196ffef | 2014-11-21 11:32:24 -0800 | [diff] [blame] | 45 | humanReadableName = serializers.SerializerMethodField("getHumanReadableName")
|
Scott Baker | dcf6fbf | 2015-01-11 13:45:19 -0800 | [diff] [blame] | 46 | network_ports = NetworkPortsField(required=False)
|
Scott Baker | 97acad9 | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 47 | site_allocation = DictionaryField(required=False)
|
Scott Baker | 2190934 | 2015-01-22 15:21:24 -0800 | [diff] [blame] | 48 | site_ready = DictionaryField(required=False)
|
Scott Baker | 97acad9 | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 49 | users = ListField(required=False)
|
Scott Baker | 7a76f32 | 2015-01-16 19:07:36 -0800 | [diff] [blame] | 50 | user_names = ListField(required=False) # readonly = True ?
|
Scott Baker | e0e407a | 2015-01-20 17:02:53 -0800 | [diff] [blame] | 51 | 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 Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 59 |
|
| 60 | def getSliceInfo(self, slice):
|
Scott Baker | db236c3 | 2014-07-13 17:36:19 -0700 | [diff] [blame] | 61 | return slice.getSliceInfo(user=self.context['request'].user)
|
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 62 |
|
Scott Baker | 196ffef | 2014-11-21 11:32:24 -0800 | [diff] [blame] | 63 | def getHumanReadableName(self, obj):
|
| 64 | return str(obj)
|
| 65 |
|
Scott Baker | 8b89d30 | 2015-01-08 22:34:51 -0800 | [diff] [blame] | 66 | networks = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
|
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 67 |
|
| 68 | class Meta:
|
| 69 | model = SlicePlus
|
Scott Baker | 339d512 | 2015-01-12 13:36:40 -0800 | [diff] [blame] | 70 | 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 Baker | 2190934 | 2015-01-22 15:21:24 -0800 | [diff] [blame] | 72 | 'serviceClass','creator','networks','sliceInfo','network_ports','backendIcon','backendHtml','site_allocation','site_ready','users',"user_names","current_user_can_see") |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 73 | |
Scott Baker | 8974e55 | 2015-02-10 19:26:00 -0800 | [diff] [blame] | 74 | class SlicePlusList(XOSListCreateAPIView): |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 75 | queryset = SlicePlus.objects.select_related().all() |
| 76 | serializer_class = SlicePlusIdSerializer |
| 77 | |
Scott Baker | db236c3 | 2014-07-13 17:36:19 -0700 | [diff] [blame] | 78 | method_kind = "list" |
| 79 | method_name = "slicesplus" |
| 80 | |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 81 | def get_queryset(self): |
Scott Baker | e0e407a | 2015-01-20 17:02:53 -0800 | [diff] [blame] | 82 | current_user_can_see = self.request.QUERY_PARAMS.get('current_user_can_see', False) |
| 83 | |
Scott Baker | 960431e | 2015-02-02 10:41:12 -0800 | [diff] [blame] | 84 | if (not self.request.user.is_authenticated()): |
Scott Baker | 864e3dc | 2015-02-10 18:33:59 -0800 | [diff] [blame] | 85 | raise RestFrameworkPermissionDenied("You must be authenticated in order to use this API") |
Scott Baker | 960431e | 2015-02-02 10:41:12 -0800 | [diff] [blame] | 86 | |
Scott Baker | e0e407a | 2015-01-20 17:02:53 -0800 | [diff] [blame] | 87 | 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 Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 101 | |
Scott Baker | 8974e55 | 2015-02-10 19:26:00 -0800 | [diff] [blame] | 102 | class SlicePlusDetail(XOSRetrieveUpdateDestroyAPIView): |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 103 | queryset = SlicePlus.objects.select_related().all() |
| 104 | serializer_class = SlicePlusIdSerializer |
| 105 | |
Scott Baker | db236c3 | 2014-07-13 17:36:19 -0700 | [diff] [blame] | 106 | method_kind = "detail" |
| 107 | method_name = "slicesplus" |
| 108 | |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 109 | def get_queryset(self): |
Scott Baker | 960431e | 2015-02-02 10:41:12 -0800 | [diff] [blame] | 110 | if (not self.request.user.is_authenticated()): |
Scott Baker | 864e3dc | 2015-02-10 18:33:59 -0800 | [diff] [blame] | 111 | raise RestFrameworkPermissionDenied("You must be authenticated in order to use this API") |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 112 | return SlicePlus.select_by_user(self.request.user) |
| 113 | |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 114 | |