From 288a65f2192a93aaa2660b244be67b9bb8faee4a Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Thu, 27 Feb 2020 15:29:15 +0100 Subject: [PATCH] ++update Signed-off-by: Nico Schottelius --- .../migrations/0002_auto_20200227_1230.py | 18 ++++++++++ .../migrations/0005_auto_20200227_1230.py | 36 +++++++++++++++++++ uncloud/uncloud_vm/models.py | 15 ++++---- uncloud/uncloud_vm/serializers.py | 10 ++++++ uncloud/uncloud_vm/views.py | 13 ++++++- 5 files changed, 83 insertions(+), 9 deletions(-) create mode 100644 uncloud/uncloud_pay/migrations/0002_auto_20200227_1230.py create mode 100644 uncloud/uncloud_vm/migrations/0005_auto_20200227_1230.py diff --git a/uncloud/uncloud_pay/migrations/0002_auto_20200227_1230.py b/uncloud/uncloud_pay/migrations/0002_auto_20200227_1230.py new file mode 100644 index 0000000..0643e9a --- /dev/null +++ b/uncloud/uncloud_pay/migrations/0002_auto_20200227_1230.py @@ -0,0 +1,18 @@ +# Generated by Django 3.0.3 on 2020-02-27 12:30 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('uncloud_pay', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='payment', + name='source', + field=models.CharField(choices=[('wire', 'Wire Transfer'), ('stripe', 'Stripe'), ('voucher', 'Voucher'), ('referral', 'Referral'), ('unknown', 'Unknown')], default='unknown', max_length=256), + ), + ] diff --git a/uncloud/uncloud_vm/migrations/0005_auto_20200227_1230.py b/uncloud/uncloud_vm/migrations/0005_auto_20200227_1230.py new file mode 100644 index 0000000..1bd711b --- /dev/null +++ b/uncloud/uncloud_vm/migrations/0005_auto_20200227_1230.py @@ -0,0 +1,36 @@ +# Generated by Django 3.0.3 on 2020-02-27 12:30 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('uncloud_pay', '0002_auto_20200227_1230'), + ('uncloud_vm', '0004_vmsnapshotproduct'), + ] + + operations = [ + migrations.RemoveField( + model_name='vmsnapshotproduct', + name='vm_uuid', + ), + migrations.AddField( + model_name='vmproduct', + name='order', + field=models.ForeignKey(default=0, editable=False, on_delete=django.db.models.deletion.CASCADE, to='uncloud_pay.Order'), + preserve_default=False, + ), + migrations.AddField( + model_name='vmproduct', + name='status', + field=models.CharField(choices=[('pending', 'Pending'), ('being_created', 'Being created'), ('active', 'Active'), ('deleted', 'Deleted')], default='pending', max_length=256), + ), + migrations.AddField( + model_name='vmsnapshotproduct', + name='vm', + field=models.ForeignKey(default=0, on_delete=django.db.models.deletion.CASCADE, to='uncloud_vm.VMProduct'), + preserve_default=False, + ), + ] diff --git a/uncloud/uncloud_vm/models.py b/uncloud/uncloud_vm/models.py index 12d188e..4ebae25 100644 --- a/uncloud/uncloud_vm/models.py +++ b/uncloud/uncloud_vm/models.py @@ -32,13 +32,7 @@ class VMHost(models.Model): ) -class VMProduct(models.Model): - uuid = models.UUIDField(primary_key=True, - default=uuid.uuid4, - editable=False) - owner = models.ForeignKey(get_user_model(), - on_delete=models.CASCADE, - editable=False) +class VMProduct(Product): vmhost = models.ForeignKey(VMHost, on_delete=models.CASCADE, editable=False, @@ -72,8 +66,12 @@ class OperatingSystemDisk(VMDiskProduct): class VMNetworkCard(models.Model): vm = models.ForeignKey(VMProduct, on_delete=models.CASCADE) + mac_address = models.IntegerField() + ip_address = models.GenericIPAddressField(blank=True, + null=True) + class VMSnapshotProduct(Product): price_per_gb_ssd = 0.35 @@ -83,7 +81,8 @@ class VMSnapshotProduct(Product): gb_ssd = models.FloatField(editable=False) gb_hdd = models.FloatField(editable=False) - vm_uuid = models.UUIDField() + vm = models.ForeignKey(VMProduct, on_delete=models.CASCADE) + #vm_uuid = models.UUIDField() # Need to setup recurring_price and one_time_price and recurring period diff --git a/uncloud/uncloud_vm/serializers.py b/uncloud/uncloud_vm/serializers.py index c1eafe2..b247709 100644 --- a/uncloud/uncloud_vm/serializers.py +++ b/uncloud/uncloud_vm/serializers.py @@ -14,7 +14,17 @@ class VMProductSerializer(serializers.HyperlinkedModelSerializer): model = VMProduct fields = '__all__' + +# def create(self, validated_data): +# return VMSnapshotProduct() + class VMSnapshotProductSerializer(serializers.ModelSerializer): class Meta: model = VMSnapshotProduct fields = '__all__' + + + # verify that vm.owner == user.request + def validate_vm(self, value): + print(value) + return True diff --git a/uncloud/uncloud_vm/views.py b/uncloud/uncloud_vm/views.py index 444d134..7e517f5 100644 --- a/uncloud/uncloud_vm/views.py +++ b/uncloud/uncloud_vm/views.py @@ -11,6 +11,7 @@ from uncloud_pay.models import Order from .serializers import VMHostSerializer, VMProductSerializer, VMSnapshotProductSerializer + import datetime class VMHostViewSet(viewsets.ModelViewSet): @@ -29,7 +30,17 @@ class VMProductViewSet(viewsets.ModelViewSet): def create(self, request): serializer = VMProductSerializer(data=request.data, context={'request': request}) serializer.is_valid(raise_exception=True) - serializer.save(owner=request.user) + # Create order + now = datetime.datetime.now() + order = Order(owner=request.user, + creation_date=now, + starting_date=now, + recurring_price=20, + one_time_price=0, + recurring_period="per_month") + order.save() + + serializer.save(owner=request.user, order=order) return Response(serializer.data)