forked from uncloud/uncloud
Add naive GenericServiceProduct
This commit is contained in:
parent
83a0ca0e4e
commit
c57780fb4d
6 changed files with 146 additions and 5 deletions
|
|
@ -1,8 +1,9 @@
|
|||
import uuid
|
||||
|
||||
from django.db import models
|
||||
from uncloud_pay.models import Product, RecurringPeriod
|
||||
from uncloud_pay.models import Product, RecurringPeriod, AMOUNT_MAX_DIGITS, AMOUNT_DECIMALS
|
||||
from uncloud_vm.models import VMProduct, VMDiskImageProduct
|
||||
from django.core.validators import MinValueValidator
|
||||
|
||||
class MatrixServiceProduct(Product):
|
||||
monthly_managment_fee = 20
|
||||
|
|
@ -33,3 +34,30 @@ class MatrixServiceProduct(Product):
|
|||
@property
|
||||
def one_time_price(self):
|
||||
return 30
|
||||
|
||||
class GenericServiceProduct(Product):
|
||||
custom_description = models.TextField()
|
||||
custom_recurring_price = models.DecimalField(default=0.0,
|
||||
max_digits=AMOUNT_MAX_DIGITS,
|
||||
decimal_places=AMOUNT_DECIMALS,
|
||||
validators=[MinValueValidator(0)])
|
||||
custom_one_time_price = models.DecimalField(default=0.0,
|
||||
max_digits=AMOUNT_MAX_DIGITS,
|
||||
decimal_places=AMOUNT_DECIMALS,
|
||||
validators=[MinValueValidator(0)])
|
||||
|
||||
@property
|
||||
def recurring_price(self):
|
||||
return self.custom_recurring_price
|
||||
|
||||
@property
|
||||
def description(self):
|
||||
return self.custom_description
|
||||
|
||||
@property
|
||||
def one_time_price(self):
|
||||
return self.custom_one_time_price
|
||||
|
||||
@staticmethod
|
||||
def allowed_recurring_periods():
|
||||
return RecurringPeriod.choices
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
from rest_framework import serializers
|
||||
from .models import MatrixServiceProduct
|
||||
from .models import *
|
||||
from uncloud_vm.serializers import ManagedVMProductSerializer
|
||||
from uncloud_vm.models import VMProduct
|
||||
from uncloud_pay.models import RecurringPeriod
|
||||
|
|
@ -15,3 +15,14 @@ class MatrixServiceProductSerializer(serializers.ModelSerializer):
|
|||
model = MatrixServiceProduct
|
||||
fields = ['uuid', 'order', 'owner', 'status', 'vm', 'domain', 'recurring_period']
|
||||
read_only_fields = ['uuid', 'order', 'owner', 'status']
|
||||
|
||||
class GenericServiceProductSerializer(serializers.ModelSerializer):
|
||||
# Custom field used at creation (= ordering) only.
|
||||
recurring_period = serializers.ChoiceField(
|
||||
choices=GenericServiceProduct.allowed_recurring_periods())
|
||||
|
||||
class Meta:
|
||||
model = GenericServiceProduct
|
||||
fields = ['uuid', 'order', 'owner', 'status', 'custom_recurring_price',
|
||||
'custom_description', 'custom_one_time_price', 'recurring_period']
|
||||
read_only_fields = ['uuid', 'order', 'owner', 'status']
|
||||
|
|
|
|||
|
|
@ -1,9 +1,10 @@
|
|||
from rest_framework import viewsets, permissions
|
||||
from rest_framework.response import Response
|
||||
from django.db import transaction
|
||||
from django.utils import timezone
|
||||
|
||||
from .models import MatrixServiceProduct
|
||||
from .serializers import MatrixServiceProductSerializer
|
||||
from .models import *
|
||||
from .serializers import *
|
||||
|
||||
from uncloud_pay.helpers import ProductViewSet
|
||||
from uncloud_pay.models import Order
|
||||
|
|
@ -47,7 +48,10 @@ class MatrixServiceProductViewSet(ProductViewSet):
|
|||
# Create base order.)
|
||||
order = Order.objects.create(
|
||||
recurring_period=order_recurring_period,
|
||||
owner=request.user)
|
||||
owner=request.user,
|
||||
starting_date=timezone.now()
|
||||
)
|
||||
order.save()
|
||||
|
||||
# Create unerderlying VM.
|
||||
data = serializer.validated_data.pop('vm')
|
||||
|
|
@ -65,3 +69,45 @@ class MatrixServiceProductViewSet(ProductViewSet):
|
|||
vm=vm)
|
||||
|
||||
return Response(serializer.data)
|
||||
|
||||
class GenericServiceProductViewSet(ProductViewSet):
|
||||
permission_classes = [permissions.IsAuthenticated]
|
||||
serializer_class = GenericServiceProductSerializer
|
||||
|
||||
def get_queryset(self):
|
||||
return GenericServiceProduct.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,
|
||||
starting_date=timezone.now()
|
||||
)
|
||||
order.save()
|
||||
|
||||
# Create service.
|
||||
print(serializer.validated_data)
|
||||
service = serializer.save(order=order, owner=request.user)
|
||||
|
||||
# XXX: Move this to some kind of on_create hook in parent
|
||||
# Product class?
|
||||
order.add_record(
|
||||
service.one_time_price,
|
||||
service.recurring_price,
|
||||
service.description)
|
||||
|
||||
# XXX: Move this to some kind of on_create hook in parent
|
||||
# Product class?
|
||||
order.add_record(
|
||||
service.one_time_price,
|
||||
service.recurring_price,
|
||||
service.description)
|
||||
|
||||
return Response(serializer.data)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue