blob: bb93dc6c92eef65d466ebe5da540906bf176d990 [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")
15
16 def getSliceInfo(self, slice):
Scott Bakerdb236c32014-07-13 17:36:19 -070017 return slice.getSliceInfo(user=self.context['request'].user)
Scott Baker88e34372014-07-13 11:46:36 -070018
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 Bakere791dc62014-08-28 14:02:54 -070024 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 Baker88e34372014-07-13 11:46:36 -070025
26class SlicePlusList(generics.ListCreateAPIView):
27 queryset = SlicePlus.objects.select_related().all()
28 serializer_class = SlicePlusIdSerializer
29
Scott Bakerdb236c32014-07-13 17:36:19 -070030 method_kind = "list"
31 method_name = "slicesplus"
32
Scott Baker88e34372014-07-13 11:46:36 -070033 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
44class SlicePlusDetail(generics.RetrieveUpdateDestroyAPIView):
45 queryset = SlicePlus.objects.select_related().all()
46 serializer_class = SlicePlusIdSerializer
47
Scott Bakerdb236c32014-07-13 17:36:19 -070048 method_kind = "detail"
49 method_name = "slicesplus"
50
Scott Baker88e34372014-07-13 11:46:36 -070051 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)