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 | bbc8a5b | 2015-01-12 12:57:22 -0800 | [diff] [blame] | 9 | from plus import PlusSerializerMixin, PlusRetrieveUpdateDestroyAPIView, PlusListCreateAPIView |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 10 | |
Scott Baker | c8af575 | 2014-12-23 11:12:07 -0800 | [diff] [blame] | 11 | if hasattr(serializers, "ReadOnlyField"): |
| 12 | # rest_framework 3.x |
| 13 | IdField = serializers.ReadOnlyField |
| 14 | else: |
| 15 | # rest_framework 2.x |
| 16 | IdField = serializers.Field |
| 17 | |
Scott Baker | 8b89d30 | 2015-01-08 22:34:51 -0800 | [diff] [blame] | 18 | class 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 Baker | 97acad9 | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 25 | class 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 | |
| 32 | 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] | 33 | 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 Baker | e791dc6 | 2014-08-28 14:02:54 -0700 | [diff] [blame] | 39 | class SlicePlusIdSerializer(serializers.ModelSerializer, PlusSerializerMixin): |
Scott Baker | c8af575 | 2014-12-23 11:12:07 -0800 | [diff] [blame] | 40 | id = IdField() |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 41 |
|
| 42 | sliceInfo = serializers.SerializerMethodField("getSliceInfo")
|
Scott Baker | 196ffef | 2014-11-21 11:32:24 -0800 | [diff] [blame] | 43 | humanReadableName = serializers.SerializerMethodField("getHumanReadableName")
|
Scott Baker | dcf6fbf | 2015-01-11 13:45:19 -0800 | [diff] [blame] | 44 | network_ports = NetworkPortsField(required=False)
|
Scott Baker | 97acad9 | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 45 | site_allocation = DictionaryField(required=False)
|
Scott Baker | 2190934 | 2015-01-22 15:21:24 -0800 | [diff] [blame] | 46 | site_ready = DictionaryField(required=False)
|
Scott Baker | 97acad9 | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 47 | users = ListField(required=False)
|
Scott Baker | 7a76f32 | 2015-01-16 19:07:36 -0800 | [diff] [blame] | 48 | user_names = ListField(required=False) # readonly = True ?
|
Scott Baker | e0e407a | 2015-01-20 17:02:53 -0800 | [diff] [blame] | 49 | current_user_can_see = serializers.SerializerMethodField("getCurrentUserCanSee")
|
| 50 |
|
| 51 | def getCurrentUserCanSee(self, slice):
|
| 52 | # user can 'see' the slice if he is the creator or he has a role
|
| 53 | current_user = self.context['request'].user
|
| 54 | if (slice.creator and slice.creator==current_user):
|
| 55 | return True;
|
| 56 | return (len(slice.getSliceInfo(current_user)["roles"]) > 0)
|
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 57 |
|
| 58 | def getSliceInfo(self, slice):
|
Scott Baker | db236c3 | 2014-07-13 17:36:19 -0700 | [diff] [blame] | 59 | return slice.getSliceInfo(user=self.context['request'].user)
|
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 60 |
|
Scott Baker | 196ffef | 2014-11-21 11:32:24 -0800 | [diff] [blame] | 61 | def getHumanReadableName(self, obj):
|
| 62 | return str(obj)
|
| 63 |
|
Scott Baker | 8b89d30 | 2015-01-08 22:34:51 -0800 | [diff] [blame] | 64 | networks = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
|
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 65 |
|
| 66 | class Meta:
|
| 67 | model = SlicePlus
|
Scott Baker | 339d512 | 2015-01-12 13:36:40 -0800 | [diff] [blame] | 68 | fields = ('humanReadableName', 'id','created','updated','enacted','name','enabled','omf_friendly','description','slice_url','site','max_slivers','service','network','mount_data_sets', |
| 69 | 'default_image', 'default_flavor', |
Scott Baker | 2190934 | 2015-01-22 15:21:24 -0800 | [diff] [blame] | 70 | '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] | 71 | |
Scott Baker | e0e407a | 2015-01-20 17:02:53 -0800 | [diff] [blame] | 72 | class SlicePlusList(PlusListCreateAPIView): |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 73 | queryset = SlicePlus.objects.select_related().all() |
| 74 | serializer_class = SlicePlusIdSerializer |
| 75 | |
Scott Baker | db236c3 | 2014-07-13 17:36:19 -0700 | [diff] [blame] | 76 | method_kind = "list" |
| 77 | method_name = "slicesplus" |
| 78 | |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 79 | def get_queryset(self): |
Scott Baker | e0e407a | 2015-01-20 17:02:53 -0800 | [diff] [blame] | 80 | current_user_can_see = self.request.QUERY_PARAMS.get('current_user_can_see', False) |
| 81 | |
| 82 | slices = SlicePlus.select_by_user(self.request.user) |
| 83 | |
| 84 | # If current_user_can_see is set, then filter the queryset to return |
| 85 | # only those slices that the user is either creator or has privilege |
| 86 | # on. |
| 87 | if (current_user_can_see): |
| 88 | slice_ids = [] |
| 89 | for slice in slices: |
| 90 | if (self.request.user == slice.creator) or (len(slice.getSliceInfo(self.request.user)["roles"]) > 0): |
| 91 | slice_ids.append(slice.id) |
| 92 | |
| 93 | slices = SlicePlus.objects.filter(id__in=slice_ids) |
| 94 | |
| 95 | return slices |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 96 | |
Scott Baker | dcf6fbf | 2015-01-11 13:45:19 -0800 | [diff] [blame] | 97 | class SlicePlusDetail(PlusRetrieveUpdateDestroyAPIView): |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 98 | queryset = SlicePlus.objects.select_related().all() |
| 99 | serializer_class = SlicePlusIdSerializer |
| 100 | |
Scott Baker | db236c3 | 2014-07-13 17:36:19 -0700 | [diff] [blame] | 101 | method_kind = "detail" |
| 102 | method_name = "slicesplus" |
| 103 | |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 104 | def get_queryset(self): |
| 105 | return SlicePlus.select_by_user(self.request.user) |
| 106 | |
| 107 | def update(self, request, *args, **kwargs): |
| 108 | obj = self.get_object() |
| 109 | if obj.can_update(request.user): |
| 110 | return super(SlicePlusDetail, self).update(request, *args, **kwargs) |
| 111 | else: |
| 112 | return Response(status=status.HTTP_400_BAD_REQUEST) |
| 113 | |
| 114 | def destroy(self, request, *args, **kwargs): |
| 115 | obj = self.get_object() |
| 116 | if obj.can_update(request.user): |
| 117 | return super(SlicePlusDetail, self).destroy(request, *args, **kwargs) |
| 118 | else: |
| 119 | return Response(status=status.HTTP_400_BAD_REQUEST) |