Fix admin order creation
This commit is contained in:
parent
892b2b6f13
commit
db3c29d17e
2 changed files with 32 additions and 4 deletions
|
@ -1,5 +1,6 @@
|
||||||
from django.contrib.auth import get_user_model
|
from django.contrib.auth import get_user_model
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
|
from uncloud_auth.serializers import UserSerializer
|
||||||
from .models import *
|
from .models import *
|
||||||
|
|
||||||
###
|
###
|
||||||
|
@ -41,11 +42,36 @@ class OrderRecordSerializer(serializers.ModelSerializer):
|
||||||
|
|
||||||
|
|
||||||
class OrderSerializer(serializers.ModelSerializer):
|
class OrderSerializer(serializers.ModelSerializer):
|
||||||
records = OrderRecordSerializer(many=True, read_only=True)
|
owner = serializers.PrimaryKeyRelatedField(queryset=get_user_model().objects.all())
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
# Don't pass the 'fields' arg up to the superclass
|
||||||
|
admin = kwargs.pop('admin', None)
|
||||||
|
|
||||||
|
# Instantiate the superclass normally
|
||||||
|
super(OrderSerializer, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
# Only allows owner in admin mode.
|
||||||
|
if not admin:
|
||||||
|
self.fields.pop('owner')
|
||||||
|
|
||||||
|
def create(self, validated_data):
|
||||||
|
billing_address = BillingAddress.get_preferred_address_for(validated_data["owner"])
|
||||||
|
instance = Order(billing_address=billing_address, **validated_data)
|
||||||
|
instance.save()
|
||||||
|
|
||||||
|
return instance
|
||||||
|
|
||||||
|
def validate_owner(self, value):
|
||||||
|
if BillingAddress.get_preferred_address_for(value) == None:
|
||||||
|
raise serializers.ValidationError("Owner does not have a valid billing address.")
|
||||||
|
|
||||||
|
return value
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Order
|
model = Order
|
||||||
fields = ['uuid', 'creation_date', 'starting_date', 'ending_date',
|
fields = ['uuid', 'owner', 'creation_date', 'starting_date', 'ending_date',
|
||||||
'bill', 'recurring_period', 'records', 'recurring_price', 'one_time_price']
|
'bill', 'recurring_period', 'recurring_price', 'one_time_price']
|
||||||
|
|
||||||
|
|
||||||
###
|
###
|
||||||
|
|
|
@ -279,9 +279,11 @@ class AdminBillViewSet(viewsets.ModelViewSet):
|
||||||
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
|
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
|
||||||
|
|
||||||
class AdminOrderViewSet(viewsets.ModelViewSet):
|
class AdminOrderViewSet(viewsets.ModelViewSet):
|
||||||
serializer_class = OrderSerializer
|
|
||||||
permission_classes = [permissions.IsAuthenticated]
|
permission_classes = [permissions.IsAuthenticated]
|
||||||
|
|
||||||
|
def get_serializer(self, *args, **kwargs):
|
||||||
|
return OrderSerializer(*args, **kwargs, admin=True)
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
return Order.objects.all()
|
return Order.objects.all()
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue