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 | 03696ee | 2015-01-11 13:45:19 -0800 | [diff] [blame] | 9 | from plus import PlusSerializerMixin, PlusRetrieveUpdateDestroyAPIView |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 10 | |
Scott Baker | f0480fb | 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 | 9062b0c | 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 | 03696ee | 2015-01-11 13:45:19 -0800 | [diff] [blame] | 25 | class SiteAllocationField(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 | |
Scott Baker | e791dc6 | 2014-08-28 14:02:54 -0700 | [diff] [blame] | 32 | class SlicePlusIdSerializer(serializers.ModelSerializer, PlusSerializerMixin): |
Scott Baker | f0480fb | 2014-12-23 11:12:07 -0800 | [diff] [blame] | 33 | id = IdField() |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 34 |
|
| 35 | sliceInfo = serializers.SerializerMethodField("getSliceInfo")
|
Scott Baker | b744f1c | 2014-11-21 11:32:24 -0800 | [diff] [blame] | 36 | humanReadableName = serializers.SerializerMethodField("getHumanReadableName")
|
Scott Baker | 03696ee | 2015-01-11 13:45:19 -0800 | [diff] [blame] | 37 | network_ports = NetworkPortsField(required=False)
|
| 38 | site_allocation = SiteAllocationField(required=False)
|
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 39 |
|
| 40 | def getSliceInfo(self, slice):
|
Scott Baker | db236c3 | 2014-07-13 17:36:19 -0700 | [diff] [blame] | 41 | return slice.getSliceInfo(user=self.context['request'].user)
|
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 42 |
|
Scott Baker | b744f1c | 2014-11-21 11:32:24 -0800 | [diff] [blame] | 43 | def getHumanReadableName(self, obj):
|
| 44 | return str(obj)
|
| 45 |
|
Scott Baker | 9062b0c | 2015-01-08 22:34:51 -0800 | [diff] [blame] | 46 | networks = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
|
| 47 | # availableNetworks = serializers.PrimaryKeyRelatedField(many=True, read_only=True, view_name='network-detail')
|
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 48 |
|
| 49 | class Meta:
|
| 50 | model = SlicePlus
|
Scott Baker | 03696ee | 2015-01-11 13:45:19 -0800 | [diff] [blame] | 51 | fields = ('humanReadableName', 'id','created','updated','enacted','name','enabled','omf_friendly','description','slice_url','site','max_slivers','image_preference','service','network','mount_data_sets', |
| 52 | 'serviceClass','creator','networks','sliceInfo','network_ports','backendIcon','backendHtml','site_allocation') |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 53 | |
| 54 | class SlicePlusList(generics.ListCreateAPIView): |
| 55 | queryset = SlicePlus.objects.select_related().all() |
| 56 | serializer_class = SlicePlusIdSerializer |
| 57 | |
Scott Baker | db236c3 | 2014-07-13 17:36:19 -0700 | [diff] [blame] | 58 | method_kind = "list" |
| 59 | method_name = "slicesplus" |
| 60 | |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 61 | def get_queryset(self): |
| 62 | return SlicePlus.select_by_user(self.request.user) |
| 63 | |
| 64 | def create(self, request, *args, **kwargs): |
| 65 | obj = self.get_object() |
| 66 | obj.caller = request.user |
| 67 | if obj.can_update(request.user): |
| 68 | return super(SliceList, self).create(request, *args, **kwargs) |
| 69 | else: |
| 70 | return Response(status=status.HTTP_400_BAD_REQUEST) |
| 71 | |
Scott Baker | 03696ee | 2015-01-11 13:45:19 -0800 | [diff] [blame] | 72 | class SlicePlusDetail(PlusRetrieveUpdateDestroyAPIView): |
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 = "detail" |
| 77 | method_name = "slicesplus" |
| 78 | |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 79 | def get_queryset(self): |
| 80 | return SlicePlus.select_by_user(self.request.user) |
| 81 | |
| 82 | def update(self, request, *args, **kwargs): |
| 83 | obj = self.get_object() |
| 84 | if obj.can_update(request.user): |
| 85 | return super(SlicePlusDetail, self).update(request, *args, **kwargs) |
| 86 | else: |
| 87 | return Response(status=status.HTTP_400_BAD_REQUEST) |
| 88 | |
| 89 | def destroy(self, request, *args, **kwargs): |
| 90 | obj = self.get_object() |
| 91 | if obj.can_update(request.user): |
| 92 | return super(SlicePlusDetail, self).destroy(request, *args, **kwargs) |
| 93 | else: |
| 94 | return Response(status=status.HTTP_400_BAD_REQUEST) |