blob: f990275668b7679dbc9c272d8fb7628e60ff5037 [file] [log] [blame]
Scott Baker88e34372014-07-13 11:46:36 -07001from rest_framework.decorators import api_view
2from rest_framework.response import Response
3from rest_framework.reverse import reverse
4from rest_framework import serializers
5from rest_framework import generics
6from core.models import *
7from django.forms import widgets
8from core.xoslib.objects.sliceplus import SlicePlus
9
10class SlicePlusIdSerializer(serializers.ModelSerializer):
11 id = serializers.Field()
12
13 sliceInfo = serializers.SerializerMethodField("getSliceInfo")
14
15 def getSliceInfo(self, slice):
Scott Bakerdb236c32014-07-13 17:36:19 -070016 return slice.getSliceInfo(user=self.context['request'].user)
Scott Baker88e34372014-07-13 11:46:36 -070017
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
25class SlicePlusList(generics.ListCreateAPIView):
26 queryset = SlicePlus.objects.select_related().all()
27 serializer_class = SlicePlusIdSerializer
28
Scott Bakerdb236c32014-07-13 17:36:19 -070029 method_kind = "list"
30 method_name = "slicesplus"
31
Scott Baker88e34372014-07-13 11:46:36 -070032 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
43class SlicePlusDetail(generics.RetrieveUpdateDestroyAPIView):
44 queryset = SlicePlus.objects.select_related().all()
45 serializer_class = SlicePlusIdSerializer
46
Scott Bakerdb236c32014-07-13 17:36:19 -070047 method_kind = "detail"
48 method_name = "slicesplus"
49
Scott Baker88e34372014-07-13 11:46:36 -070050 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)