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