Rename / prepare for merge with uncloud repo

This commit is contained in:
Nico Schottelius 2020-04-02 19:31:03 +02:00
commit 7a6c8739f6
118 changed files with 1499 additions and 0 deletions

View file

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

View file

@ -0,0 +1,5 @@
from django.apps import AppConfig
class UngleichServiceConfig(AppConfig):
name = 'ungleich_service'

View file

@ -0,0 +1,34 @@
# Generated by Django 3.0.3 on 2020-03-17 11:45
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import uuid
class Migration(migrations.Migration):
initial = True
dependencies = [
('uncloud_vm', '0003_remove_vmhost_vms'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('uncloud_pay', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='MatrixServiceProduct',
fields=[
('uuid', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
('status', models.CharField(choices=[('PENDING', 'Pending'), ('AWAITING_PAYMENT', 'Awaiting payment'), ('BEING_CREATED', 'Being created'), ('ACTIVE', 'Active'), ('DELETED', 'Deleted')], default='PENDING', max_length=32)),
('domain', models.CharField(default='domain.tld', max_length=255)),
('order', models.ForeignKey(editable=False, null=True, on_delete=django.db.models.deletion.CASCADE, to='uncloud_pay.Order')),
('owner', models.ForeignKey(editable=False, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
('vm', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='uncloud_vm.VMProduct')),
],
options={
'abstract': False,
},
),
]

View file

@ -0,0 +1,19 @@
# Generated by Django 3.0.3 on 2020-03-21 10:58
import django.contrib.postgres.fields.jsonb
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('ungleich_service', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='matrixserviceproduct',
name='extra_data',
field=django.contrib.postgres.fields.jsonb.JSONField(blank=True, editable=False, null=True),
),
]

View file

@ -0,0 +1,18 @@
# Generated by Django 3.0.3 on 2020-03-22 17:58
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('ungleich_service', '0002_matrixserviceproduct_extra_data'),
]
operations = [
migrations.AlterField(
model_name='matrixserviceproduct',
name='status',
field=models.CharField(choices=[('PENDING', 'Pending'), ('AWAITING_PAYMENT', 'Awaiting payment'), ('BEING_CREATED', 'Being created'), ('ACTIVE', 'Active'), ('DELETED', 'Deleted'), ('DISABLED', 'Disabled'), ('UNUSABLE', 'Unusable')], default='PENDING', max_length=32),
),
]

View file

@ -0,0 +1,32 @@
import uuid
from django.db import models
from uncloud_pay.models import Product, RecurringPeriod
from uncloud_vm.models import VMProduct
class MatrixServiceProduct(Product):
monthly_managment_fee = 20
description = "Managed Matrix HomeServer"
# Specific to Matrix-as-a-Service
vm = models.ForeignKey(
VMProduct, on_delete=models.CASCADE
)
domain = models.CharField(max_length=255, default='domain.tld')
def recurring_price(self, recurring_period=RecurringPeriod.PER_MONTH):
if recurring_period == RecurringPeriod.PER_MONTH:
return self.monthly_managment_fee
else:
raise Exception('Invalid recurring period for VM Product pricing.')
@staticmethod
def allowed_recurring_periods():
return list(filter(
lambda pair: pair[0] in [RecurringPeriod.PER_MONTH],
RecurringPeriod.choices))
@property
def one_time_price(self):
return 30

View file

@ -0,0 +1,17 @@
from rest_framework import serializers
from .models import MatrixServiceProduct
from uncloud_vm.serializers import ManagedVMProductSerializer
from uncloud_vm.models import VMProduct
from uncloud_pay.models import RecurringPeriod
class MatrixServiceProductSerializer(serializers.ModelSerializer):
vm = ManagedVMProductSerializer()
# Custom field used at creation (= ordering) only.
recurring_period = serializers.ChoiceField(
choices=MatrixServiceProduct.allowed_recurring_periods())
class Meta:
model = MatrixServiceProduct
fields = ['uuid', 'order', 'owner', 'status', 'vm', 'domain', 'recurring_period']
read_only_fields = ['uuid', 'order', 'owner', 'status']

View file

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

View file

@ -0,0 +1,61 @@
from rest_framework import viewsets, permissions
from rest_framework.response import Response
from django.db import transaction
from .models import MatrixServiceProduct
from .serializers import MatrixServiceProductSerializer
from uncloud_pay.helpers import ProductViewSet
from uncloud_pay.models import Order
from uncloud_vm.models import VMProduct
class MatrixServiceProductViewSet(ProductViewSet):
permission_classes = [permissions.IsAuthenticated]
serializer_class = MatrixServiceProductSerializer
def get_queryset(self):
return MatrixServiceProduct.objects.filter(owner=self.request.user)
@transaction.atomic
def create(self, request):
# Extract serializer data.
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
order_recurring_period = serializer.validated_data.pop("recurring_period")
# Create base order.
order = Order.objects.create(
recurring_period=order_recurring_period,
owner=request.user
)
order.save()
# Create unerderlying VM.
# TODO: move this logic to a method for use with other
# products.
vm_data = serializer.validated_data.pop('vm')
vm_data['owner'] = request.user
vm_data['order'] = order
vm = VMProduct.objects.create(**vm_data)
# XXX: Move this to some kind of on_create hook in parent
# Product class?
order.add_record(
vm.one_time_price,
vm.recurring_price(order.recurring_period),
vm.description)
# Create service.
service = serializer.save(
order=order,
owner=self.request.user,
vm=vm)
# XXX: Move this to some kind of on_create hook in parent
# Product class?
order.add_record(
service.one_time_price,
service.recurring_price(order.recurring_period),
service.description)
return Response(serializer.data)