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