Skip to content

Commit

Permalink
fix(api): little improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
tomjeannesson committed Oct 9, 2023
1 parent 1c99414 commit a1fb18b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
2 changes: 0 additions & 2 deletions django_napse/api/exchanges/__init__.py

This file was deleted.

2 changes: 0 additions & 2 deletions django_napse/api/exchanges/views/exchange_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
from django_napse.api.exchanges.serializers import ExchangeAccountDetailSerializer, ExchangeAccountSerializer
from django_napse.core.models import ExchangeAccount

# from rest_framework.decorators import action


class ExchangeAccountView(GenericViewSet):
permission_classes = []
Expand Down
14 changes: 10 additions & 4 deletions django_napse/api/spaces/serializers/space_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,23 @@

class SpaceSerializer(serializers.ModelSerializer):
fleet_count = serializers.SerializerMethodField(read_only=True)
exchange_account = serializers.CharField(source="exchange_account.uuid", read_only=True)

class Meta:
model = NapseSpace
fields = [
"name",
# read-only
"id",
"uuid",
"value",
"fleet_count",
"exchange_account",
]
read_only_fields = [
"id",
"uuid",
"value",
"fleet_count",
"exchange_account",
]

def get_fleet_count(self, instance) -> int:
Expand All @@ -28,21 +31,24 @@ def get_fleet_count(self, instance) -> int:

class SpaceDetailSerializer(serializers.ModelSerializer):
fleets = FleetSerializer(many=True, read_only=True)
exchange_account = serializers.CharField(source="exchange_account.uuid", read_only=True)

class Meta:
model = NapseSpace
fields = [
"name",
"description",
# read-only
"id",
"uuid",
"value",
"created_at",
"fleets",
"exchange_account",
]
read_only_fields = [
"id",
"uuid",
"value",
"fleet",
"created_at",
"exchange_account",
]
11 changes: 8 additions & 3 deletions django_napse/api/spaces/views/space_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@
from rest_framework.response import Response
from rest_framework.viewsets import GenericViewSet

from django_napse.api.spaces.serializers import SpaceDetailSerializer, SpaceSerializer
from django_napse.core.models import NapseSpace


class SpaceView(GenericViewSet):
permission_classes = []
serializer_class = SpaceSerializer
# serializer_class = SpaceSerializer

def get_object(self):
return self.get_queryset().get(uuid=self.kwargs["pk"])

def get_queryset(self):
return NapseSpace.objects.all()

def get_serializer_class(self, *args, **kwargs):
from django_napse.api.spaces.serializers import SpaceDetailSerializer, SpaceSerializer

actions: dict = {
"list": SpaceSerializer,
"retrieve": SpaceDetailSerializer,
Expand All @@ -22,12 +26,13 @@ def get_serializer_class(self, *args, **kwargs):
return result if result else super().get_serializer_class()

def list(self, request):
serializer = self.serializer_class(self.get_queryset(), many=True)
serializer = self.get_serializer(self.get_queryset(), many=True)
return Response(serializer.data, status=status.HTTP_200_OK)

def retrieve(self, request, pk=None):
instance = self.get_object()
serializer = self.get_serializer(instance)
print(serializer.data)
return Response(serializer.data, status=status.HTTP_200_OK)

def create(self, request, *args, **kwargs):
Expand Down

0 comments on commit a1fb18b

Please sign in to comment.