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