blob: fffd9972cd8741b5305f05c036d8a7570128b2a1 [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
Scott Bakere791dc62014-08-28 14:02:54 -07009from plus import PlusSerializerMixin
Scott Baker88e34372014-07-13 11:46:36 -070010
Scott Bakere791dc62014-08-28 14:02:54 -070011class SlicePlusIdSerializer(serializers.ModelSerializer, PlusSerializerMixin):
Scott Baker88e34372014-07-13 11:46:36 -070012 id = serializers.Field()
13
14 sliceInfo = serializers.SerializerMethodField("getSliceInfo")
Scott Baker196ffef2014-11-21 11:32:24 -080015 humanReadableName = serializers.SerializerMethodField("getHumanReadableName")
Scott Baker88e34372014-07-13 11:46:36 -070016
17 def getSliceInfo(self, slice):
Scott Bakerdb236c32014-07-13 17:36:19 -070018 return slice.getSliceInfo(user=self.context['request'].user)
Scott Baker88e34372014-07-13 11:46:36 -070019
Scott Baker196ffef2014-11-21 11:32:24 -080020 def getHumanReadableName(self, obj):
21 return str(obj)
22
Scott Baker88e34372014-07-13 11:46:36 -070023 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 Baker196ffef2014-11-21 11:32:24 -080028 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 Baker88e34372014-07-13 11:46:36 -070029
30class SlicePlusList(generics.ListCreateAPIView):
31 queryset = SlicePlus.objects.select_related().all()
32 serializer_class = SlicePlusIdSerializer
33
Scott Bakerdb236c32014-07-13 17:36:19 -070034 method_kind = "list"
35 method_name = "slicesplus"
36
Scott Baker88e34372014-07-13 11:46:36 -070037 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
48class SlicePlusDetail(generics.RetrieveUpdateDestroyAPIView):
49 queryset = SlicePlus.objects.select_related().all()
50 serializer_class = SlicePlusIdSerializer
51
Scott Bakerdb236c32014-07-13 17:36:19 -070052 method_kind = "detail"
53 method_name = "slicesplus"
54
Scott Baker88e34372014-07-13 11:46:36 -070055 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)