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):
|
| 16 | return slice.getSliceInfo()
|
| 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 | |
| 29 | def get_queryset(self): |
| 30 | return SlicePlus.select_by_user(self.request.user) |
| 31 | |
| 32 | def create(self, request, *args, **kwargs): |
| 33 | obj = self.get_object() |
| 34 | obj.caller = request.user |
| 35 | if obj.can_update(request.user): |
| 36 | return super(SliceList, self).create(request, *args, **kwargs) |
| 37 | else: |
| 38 | return Response(status=status.HTTP_400_BAD_REQUEST) |
| 39 | |
| 40 | class SlicePlusDetail(generics.RetrieveUpdateDestroyAPIView): |
| 41 | queryset = SlicePlus.objects.select_related().all() |
| 42 | serializer_class = SlicePlusIdSerializer |
| 43 | |
| 44 | def get_queryset(self): |
| 45 | return SlicePlus.select_by_user(self.request.user) |
| 46 | |
| 47 | def update(self, request, *args, **kwargs): |
| 48 | obj = self.get_object() |
| 49 | if obj.can_update(request.user): |
| 50 | return super(SlicePlusDetail, self).update(request, *args, **kwargs) |
| 51 | else: |
| 52 | return Response(status=status.HTTP_400_BAD_REQUEST) |
| 53 | |
| 54 | def destroy(self, request, *args, **kwargs): |
| 55 | obj = self.get_object() |
| 56 | if obj.can_update(request.user): |
| 57 | return super(SlicePlusDetail, self).destroy(request, *args, **kwargs) |
| 58 | else: |
| 59 | return Response(status=status.HTTP_400_BAD_REQUEST) |