forked from uncloud/uncloud
Merge branch 'master' of code.ungleich.ch:nico/meow-pay
This commit is contained in:
commit
df851bee08
32 changed files with 560 additions and 444 deletions
|
|
@ -1,4 +1,4 @@
|
|||
# Generated by Django 3.0.3 on 2020-02-23 17:09
|
||||
# Generated by Django 3.0.3 on 2020-02-23 17:12
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
|
|
|
|||
|
|
@ -0,0 +1,19 @@
|
|||
# Generated by Django 3.0.3 on 2020-02-25 18:16
|
||||
|
||||
from django.db import migrations, models
|
||||
import uuid
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('uncloud_api', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='vmsnapshotproduct',
|
||||
name='vm_uuid',
|
||||
field=models.UUIDField(default=uuid.uuid4, editable=False),
|
||||
),
|
||||
]
|
||||
36
uncloud/uncloud_api/migrations/0003_auto_20200225_1950.py
Normal file
36
uncloud/uncloud_api/migrations/0003_auto_20200225_1950.py
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
# Generated by Django 3.0.3 on 2020-02-25 19:50
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
('uncloud_api', '0002_vmsnapshotproduct_vm_uuid'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='vmsnapshotproduct',
|
||||
name='gb_hdd',
|
||||
field=models.FloatField(editable=False),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='vmsnapshotproduct',
|
||||
name='gb_ssd',
|
||||
field=models.FloatField(editable=False),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='vmsnapshotproduct',
|
||||
name='owner',
|
||||
field=models.ForeignKey(editable=False, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='vmsnapshotproduct',
|
||||
name='vm_uuid',
|
||||
field=models.UUIDField(),
|
||||
),
|
||||
]
|
||||
|
|
@ -34,7 +34,8 @@ from django.contrib.auth import get_user_model
|
|||
class Product(models.Model):
|
||||
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
||||
owner = models.ForeignKey(get_user_model(),
|
||||
on_delete=models.CASCADE)
|
||||
on_delete=models.CASCADE,
|
||||
editable=False)
|
||||
|
||||
# override these fields by default
|
||||
|
||||
|
|
@ -52,8 +53,8 @@ class Product(models.Model):
|
|||
)
|
||||
|
||||
# This is calculated by each product and saved in the DB
|
||||
recurring_price = models.FloatField()
|
||||
one_time_price = models.FloatField()
|
||||
recurring_price = models.FloatField(editable=False)
|
||||
one_time_price = models.FloatField(editable=False)
|
||||
|
||||
# FIXME: need recurring_time_frame
|
||||
|
||||
|
|
@ -69,14 +70,13 @@ class VMSnapshotProduct(Product):
|
|||
price_per_gb_hdd = 1.5/100
|
||||
|
||||
# This we need to get from the VM
|
||||
gb_ssd = models.FloatField()
|
||||
gb_hdd = models.FloatField()
|
||||
gb_ssd = models.FloatField(editable=False)
|
||||
gb_hdd = models.FloatField(editable=False)
|
||||
|
||||
vm_uuid = models.UUIDField()
|
||||
|
||||
# Need to setup recurring_price and one_time_price and recurring period
|
||||
|
||||
|
||||
sample_ssd = 10
|
||||
sample_hdd = 100
|
||||
|
||||
|
|
@ -137,13 +137,3 @@ class Feature(models.Model):
|
|||
|
||||
def __str__(self):
|
||||
return "'{}' - '{}'".format(self.product, self.name)
|
||||
|
||||
|
||||
# class Order(models.Model):
|
||||
# uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
||||
|
||||
# owner = models.ForeignKey(get_user_model(),
|
||||
# on_delete=models.CASCADE)
|
||||
|
||||
# product = models.ForeignKey(Product,
|
||||
# on_delete=models.CASCADE)
|
||||
|
|
|
|||
|
|
@ -3,17 +3,24 @@ from django.contrib.auth import get_user_model
|
|||
|
||||
from rest_framework import serializers
|
||||
|
||||
from .models import VMSnapshotProduct
|
||||
|
||||
class UserSerializer(serializers.HyperlinkedModelSerializer):
|
||||
class UserSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = get_user_model()
|
||||
fields = ['url', 'username', 'email', 'groups']
|
||||
|
||||
fields = ['url', 'username', 'email']
|
||||
|
||||
class GroupSerializer(serializers.HyperlinkedModelSerializer):
|
||||
class Meta:
|
||||
model = Group
|
||||
fields = ['url', 'name']
|
||||
|
||||
class VMSnapshotSerializer(serializers.Serializer):
|
||||
pass
|
||||
class VMSnapshotSerializer(serializers.HyperlinkedModelSerializer):
|
||||
class Meta:
|
||||
model = VMSnapshotProduct
|
||||
fields = ['uuid', 'status', 'recurring_price', 'one_time_price' ]
|
||||
|
||||
class VMSnapshotCreateSerializer(serializers.HyperlinkedModelSerializer):
|
||||
class Meta:
|
||||
model = VMSnapshotProduct
|
||||
fields = '__all__'
|
||||
|
|
|
|||
|
|
@ -3,52 +3,92 @@ from django.contrib.auth import get_user_model
|
|||
from django.contrib.auth.models import Group
|
||||
|
||||
from rest_framework import viewsets, permissions, generics
|
||||
from .serializers import UserSerializer, GroupSerializer
|
||||
|
||||
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
|
||||
|
||||
class UserViewSet(viewsets.ModelViewSet):
|
||||
|
||||
"""
|
||||
API endpoint that allows users to be viewed or edited.
|
||||
"""
|
||||
queryset = get_user_model().objects.all().order_by('-date_joined')
|
||||
serializer_class = UserSerializer
|
||||
|
||||
permission_classes = [permissions.IsAuthenticated]
|
||||
|
||||
|
||||
# POST /vm/snapshot/ vmuuid=... => create snapshot, returns snapshot uuid
|
||||
# GET /vm/snapshot => list
|
||||
# DEL /vm/snapshot/<uuid:uuid> => delete
|
||||
# create-list -> get, post => ListCreateAPIView
|
||||
# del on other!
|
||||
class VMSnapshotView(generics.ListCreateAPIView):
|
||||
#lookup_field = 'uuid'
|
||||
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)
|
||||
|
||||
# Next: create /order/<productname> urls
|
||||
# Next: strip off "Product" at the end
|
||||
class ProductsView(APIView):
|
||||
def get(self, request, format=None):
|
||||
clsmembers = inspect.getmembers(sys.modules['uncloud_api.models'], inspect.isclass)
|
||||
products = []
|
||||
for name, c in clsmembers:
|
||||
# Include everything that ends in Product, but not Product itself
|
||||
m = re.match(r'(?P<pname>.+)Product$', name)
|
||||
if m:
|
||||
products.append({
|
||||
'name': m.group('pname'),
|
||||
'description': c.description,
|
||||
'recurring_period': c.recurring_period,
|
||||
'pricing_model': c.pricing_model()
|
||||
}
|
||||
)
|
||||
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")
|
||||
|
||||
|
||||
return Response(products)
|
||||
# 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<pname>.+)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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue