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