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