from django.shortcuts import get_object_or_404 from rest_framework import status from rest_framework.decorators import api_view, permission_classes from rest_framework.permissions import IsAuthenticated from rest_framework.response import Response from vbv_lernwelt.shop.models import BillingAddress from vbv_lernwelt.shop.serializers import BillingAddressSerializer @api_view(["GET"]) @permission_classes([IsAuthenticated]) def get_billing_address(request): billing_address = get_object_or_404(BillingAddress, user=request.user) serializer = BillingAddressSerializer(billing_address) return Response(serializer.data) @api_view(["PUT"]) @permission_classes([IsAuthenticated]) def update_billing_address(request): try: billing_address = BillingAddress.objects.get(user=request.user) except BillingAddress.DoesNotExist: billing_address = None serializer = BillingAddressSerializer( billing_address, data=request.data, partial=True ) if serializer.is_valid(): serializer.save(user=request.user) return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)