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 | 77eba94 | 2015-01-12 12:57:22 -0800 | [diff] [blame] | 9 | from plus import PlusSerializerMixin, PlusRetrieveUpdateDestroyAPIView, PlusListCreateAPIView |
Scott Baker | 20101af | 2015-02-02 10:41:12 -0800 | [diff] [blame] | 10 | from django.core.exceptions import PermissionDenied |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 11 | |
Scott Baker | f0480fb | 2014-12-23 11:12:07 -0800 | [diff] [blame] | 12 | if hasattr(serializers, "ReadOnlyField"): |
| 13 | # rest_framework 3.x |
| 14 | IdField = serializers.ReadOnlyField |
| 15 | else: |
| 16 | # rest_framework 2.x |
| 17 | IdField = serializers.Field |
| 18 | |
Scott Baker | 9062b0c | 2015-01-08 22:34:51 -0800 | [diff] [blame] | 19 | class 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 Baker | fcf655e | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 26 | class 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 | |
| 33 | class ListField(serializers.WritableField): # note: maybe just Field in rest_framework 3.x instead of WritableField |
Scott Baker | 03696ee | 2015-01-11 13:45:19 -0800 | [diff] [blame] | 34 | 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 Baker | e791dc6 | 2014-08-28 14:02:54 -0700 | [diff] [blame] | 40 | class SlicePlusIdSerializer(serializers.ModelSerializer, PlusSerializerMixin): |
Scott Baker | f0480fb | 2014-12-23 11:12:07 -0800 | [diff] [blame] | 41 | id = IdField() |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 42 |
|
| 43 | sliceInfo = serializers.SerializerMethodField("getSliceInfo")
|
Scott Baker | b744f1c | 2014-11-21 11:32:24 -0800 | [diff] [blame] | 44 | humanReadableName = serializers.SerializerMethodField("getHumanReadableName")
|
Scott Baker | 03696ee | 2015-01-11 13:45:19 -0800 | [diff] [blame] | 45 | network_ports = NetworkPortsField(required=False)
|
Scott Baker | fcf655e | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 46 | site_allocation = DictionaryField(required=False)
|
Scott Baker | a4a379c | 2015-01-22 15:21:24 -0800 | [diff] [blame] | 47 | site_ready = DictionaryField(required=False)
|
Scott Baker | fcf655e | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 48 | users = ListField(required=False)
|
Scott Baker | a31bb24 | 2015-01-16 19:07:36 -0800 | [diff] [blame] | 49 | user_names = ListField(required=False) # readonly = True ?
|
Scott Baker | c18f52b | 2015-01-20 17:02:53 -0800 | [diff] [blame] | 50 | 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 Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 58 |
|
| 59 | def getSliceInfo(self, slice):
|
Scott Baker | db236c3 | 2014-07-13 17:36:19 -0700 | [diff] [blame] | 60 | return slice.getSliceInfo(user=self.context['request'].user)
|
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 61 |
|
Scott Baker | b744f1c | 2014-11-21 11:32:24 -0800 | [diff] [blame] | 62 | def getHumanReadableName(self, obj):
|
| 63 | return str(obj)
|
| 64 |
|
Scott Baker | 9062b0c | 2015-01-08 22:34:51 -0800 | [diff] [blame] | 65 | networks = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
|
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 66 |
|
| 67 | class Meta:
|
| 68 | model = SlicePlus
|
Scott Baker | 4618a23 | 2015-01-12 13:36:40 -0800 | [diff] [blame] | 69 | 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 Baker | a4a379c | 2015-01-22 15:21:24 -0800 | [diff] [blame] | 71 | '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] | 72 | |
Scott Baker | c18f52b | 2015-01-20 17:02:53 -0800 | [diff] [blame] | 73 | class SlicePlusList(PlusListCreateAPIView): |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 74 | queryset = SlicePlus.objects.select_related().all() |
| 75 | serializer_class = SlicePlusIdSerializer |
| 76 | |
Scott Baker | db236c3 | 2014-07-13 17:36:19 -0700 | [diff] [blame] | 77 | method_kind = "list" |
| 78 | method_name = "slicesplus" |
| 79 | |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 80 | def get_queryset(self): |
Scott Baker | c18f52b | 2015-01-20 17:02:53 -0800 | [diff] [blame] | 81 | current_user_can_see = self.request.QUERY_PARAMS.get('current_user_can_see', False) |
| 82 | |
Scott Baker | 20101af | 2015-02-02 10:41:12 -0800 | [diff] [blame] | 83 | if (not self.request.user.is_authenticated()): |
| 84 | raise PermissionDenied("You must be authenticated in order to use this API") |
| 85 | |
Scott Baker | c18f52b | 2015-01-20 17:02:53 -0800 | [diff] [blame] | 86 | 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 Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 100 | |
Scott Baker | 03696ee | 2015-01-11 13:45:19 -0800 | [diff] [blame] | 101 | class SlicePlusDetail(PlusRetrieveUpdateDestroyAPIView): |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 102 | queryset = SlicePlus.objects.select_related().all() |
| 103 | serializer_class = SlicePlusIdSerializer |
| 104 | |
Scott Baker | db236c3 | 2014-07-13 17:36:19 -0700 | [diff] [blame] | 105 | method_kind = "detail" |
| 106 | method_name = "slicesplus" |
| 107 | |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 108 | def get_queryset(self): |
Scott Baker | 20101af | 2015-02-02 10:41:12 -0800 | [diff] [blame] | 109 | if (not self.request.user.is_authenticated()): |
| 110 | raise PermissionDenied("You must be authenticated in order to use this API") |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 111 | return SlicePlus.select_by_user(self.request.user) |
| 112 | |
| 113 | def update(self, request, *args, **kwargs): |
| 114 | obj = self.get_object() |
| 115 | if obj.can_update(request.user): |
| 116 | return super(SlicePlusDetail, self).update(request, *args, **kwargs) |
| 117 | else: |
| 118 | return Response(status=status.HTTP_400_BAD_REQUEST) |
| 119 | |
| 120 | def destroy(self, request, *args, **kwargs): |
| 121 | obj = self.get_object() |
| 122 | if obj.can_update(request.user): |
| 123 | return super(SlicePlusDetail, self).destroy(request, *args, **kwargs) |
| 124 | else: |
| 125 | return Response(status=status.HTTP_400_BAD_REQUEST) |