base class stuff for xoslib methods from genapi
Scott Baker [Mon, 12 Jan 2015 20:57:22 +0000 (12:57 -0800)]
planetstack/core/xoslib/methods/plus.py
planetstack/core/xoslib/methods/sliceplus.py

index ca89c79..3a6f858 100644 (file)
@@ -25,6 +25,28 @@ class PlusSerializerMixin():
     def getBackendHtml(self, obj):
         return obj.getBackendHtml()
 
+# XXX this was lifted and hacked up a bit from genapi.py
+class PlusListCreateAPIView(generics.ListCreateAPIView):
+    def create(self, request, *args, **kwargs):
+        serializer = self.get_serializer(data=request.DATA, files=request.FILES)
+        if not (serializer.is_valid()):
+            response = {"error": "validation",
+                        "specific_error": "not serializer.is_valid()",\r
+                        "reasons": serializer.errors}\r
+            return Response(response, status=status.HTTP_400_BAD_REQUEST)
+        obj = serializer.object
+        obj.caller = request.user
+        if obj.can_update(request.user):
+            return super(PlusListCreateAPIView, self).create(request, *args, **kwargs)
+        else:
+            raise Exception("failed obj.can_update")
+
+        ret = super(PlusListCreateAPIView, self).create(request, *args, **kwargs)
+        if (ret.status_code%100 != 200):
+            raise Exception(ret.data)
+
+        return ret
+
 # XXX this is taken from genapi.py
 # XXX find a better way to re-use the code
 class PlusRetrieveUpdateDestroyAPIView(generics.RetrieveUpdateDestroyAPIView):
index f29e200..fa17c1c 100644 (file)
@@ -6,7 +6,7 @@ from rest_framework import generics
 from core.models import *\r
 from django.forms import widgets
 from core.xoslib.objects.sliceplus import SlicePlus
-from plus import PlusSerializerMixin, PlusRetrieveUpdateDestroyAPIView
+from plus import PlusSerializerMixin, PlusRetrieveUpdateDestroyAPIView, PlusListCreateAPIView
 
 if hasattr(serializers, "ReadOnlyField"):
     # rest_framework 3.x
@@ -51,7 +51,7 @@ class SlicePlusIdSerializer(serializers.ModelSerializer, PlusSerializerMixin):
             fields = ('humanReadableName', 'id','created','updated','enacted','name','enabled','omf_friendly','description','slice_url','site','max_slivers','image_preference','service','network','mount_data_sets',
                       'serviceClass','creator','networks','sliceInfo','network_ports','backendIcon','backendHtml','site_allocation')
 
-class SlicePlusList(generics.ListCreateAPIView):
+class SlicePlusList(PlusListCreateAPIView): #generics.ListCreateAPIView):
     queryset = SlicePlus.objects.select_related().all()
     serializer_class = SlicePlusIdSerializer
 
@@ -61,14 +61,6 @@ class SlicePlusList(generics.ListCreateAPIView):
     def get_queryset(self):
         return SlicePlus.select_by_user(self.request.user)
 
-    def create(self, request, *args, **kwargs):
-        obj = self.get_object()
-        obj.caller = request.user
-        if obj.can_update(request.user):
-            return super(SliceList, self).create(request, *args, **kwargs)
-        else:
-            return Response(status=status.HTTP_400_BAD_REQUEST)
-
 class SlicePlusDetail(PlusRetrieveUpdateDestroyAPIView):
     queryset = SlicePlus.objects.select_related().all()
     serializer_class = SlicePlusIdSerializer