from django.shortcuts import render from django.contrib.auth import get_user_model from django.contrib.auth.models import Group from rest_framework import viewsets, permissions, generics from rest_framework.views import APIView from rest_framework.response import Response from uncloud_vm.models import VMProduct from .models import VMSnapshotProduct from .serializers import UserSerializer, GroupSerializer, VMSnapshotSerializer, VMSnapshotCreateSerializer import inspect import sys import re # POST /vm/snapshot/ vmuuid=... => create snapshot, returns snapshot uuid # GET /vm/snapshot => list # DEL /vm/snapshot/ => delete # create-list -> get, post => ListCreateAPIView # del on other! class VMSnapshotView(viewsets.ViewSet): permission_classes = [permissions.IsAuthenticated] def list(self, request): queryset = VMSnapshotProduct.objects.filter(owner=request.user) serializer = VMSnapshotSerializer(queryset, many=True, context={'request': request}) return Response(serializer.data) def retrieve(self, request, pk=None): queryset = VMSnapshotProduct.objects.filter(owner=request.user) vm = get_object_or_404(queryset, pk=pk) serializer = VMSnapshotSerializer(vm, context={'request': request}) return Response(serializer.data) def create(self, request): print(request.data) serializer = VMSnapshotCreateSerializer(data=request.data) serializer.gb_ssd = 12 serializer.gb_hdd = 120 print("F") serializer.is_valid(raise_exception=True) print(serializer) print("A") serializer.save() print("B") # snapshot = VMSnapshotProduct(owner=request.user, # **serialzer.data) return Response(serializer.data) # maybe drop or not --- we need something to guide the user! # class ProductsViewSet(viewsets.ViewSet): # permission_classes = [permissions.IsAuthenticated] # def list(self, request): # clsmembers = [] # for modules in [ 'uncloud_api.models', 'uncloud_vm.models' ]: # clsmembers.extend(inspect.getmembers(sys.modules[modules], inspect.isclass)) # products = [] # for name, c in clsmembers: # # Include everything that ends in Product, but not Product itself # m = re.match(r'(?P.+)Product$', name) # if m: # products.append({ # 'name': m.group('pname'), # 'description': c.description, # 'recurring_period': c.recurring_period, # 'pricing_model': c.pricing_model() # } # ) # return Response(products) class UserViewSet(viewsets.ModelViewSet): serializer_class = UserSerializer permission_classes = [permissions.IsAuthenticated] def get_queryset(self): return self.request.user