From aa123091d4e171479d95fca6127e94cc38af22ba Mon Sep 17 00:00:00 2001 From: Xenepix Date: Mon, 25 Dec 2023 00:51:25 +0100 Subject: [PATCH] fix(fleet): finished list, retrieve & create fleet endpoints --- .../fleets/serializers/cluster_serialisers.py | 22 ++++++----------- .../fleets/serializers/fleet_serializers.py | 18 ++++++++------ django_napse/api/fleets/views/fleet_view.py | 24 ++++++++++++------- 3 files changed, 34 insertions(+), 30 deletions(-) diff --git a/django_napse/api/fleets/serializers/cluster_serialisers.py b/django_napse/api/fleets/serializers/cluster_serialisers.py index db209ecc..33915ceb 100644 --- a/django_napse/api/fleets/serializers/cluster_serialisers.py +++ b/django_napse/api/fleets/serializers/cluster_serialisers.py @@ -1,7 +1,7 @@ from rest_framework import serializers from django_napse.api.bots.serializers import BotSerializer -from django_napse.core.models import Bot, Cluster, Strategy +from django_napse.core.models import Bot, Cluster class ClusterSerializerV1(serializers.ModelSerializer): @@ -20,9 +20,7 @@ class Meta: class ClusterFormatterSerializer(serializers.Serializer): """Format cluster dictionnary for fleet creation.""" - # TODO: use bot UUID instead of bot name - bot_strategy = serializers.UUIDField(required=True) - bot_name = serializers.CharField(required=True) + template_bot = serializers.UUIDField(required=True) share = serializers.FloatField(required=True) breakpoint = serializers.IntegerField(required=True) autoscale = serializers.BooleanField(required=True) @@ -30,18 +28,12 @@ class ClusterFormatterSerializer(serializers.Serializer): def validate(self, data): data = super().validate(data) try: - strategy = Strategy.objects.get(uuid=data["bot_strategy"]) - except Strategy.DoesNotExist: - error_msg: str = "Strategy does not exist" + bot = Bot.objects.get(uuid=data.pop("template_bot")) + except Bot.DoesNotExist: + error_msg: str = "Template bot does not exist." raise serializers.ValidationError(error_msg) from None - else: - data["bot_strategy"] = strategy + data["template_bot"] = bot return data def create(self, validated_data): - """Return cluster dict for fleet creation.""" - template_bot = Bot.objects.create( - name=validated_data.pop("bot_name"), - strategy=validated_data.pop("bot_strategy"), - ) - return {"template_bot": template_bot, **validated_data} + return super().create(validated_data) diff --git a/django_napse/api/fleets/serializers/fleet_serializers.py b/django_napse/api/fleets/serializers/fleet_serializers.py index 6cbd8c27..e94010f2 100644 --- a/django_napse/api/fleets/serializers/fleet_serializers.py +++ b/django_napse/api/fleets/serializers/fleet_serializers.py @@ -36,7 +36,7 @@ def __init__(self, instance=None, data=empty, space=None, **kwargs): def get_value(self, instance): if self.space is None: - return instance.value() + return instance.value return instance.space_frame_value(space=self.space) def get_bot_count(self, instance): @@ -45,15 +45,17 @@ def get_bot_count(self, instance): return len(query_bot) return len([bot for bot in query_bot if bot.space == self.space]) + def validate(self, attrs): + data = super().validate(attrs) + data["exchange_account"] = self.space.exchange_account + return data + def create(self, validated_data): - clusters = validated_data.pop("clusters") - fleet = Fleet.objects.create(**validated_data) - for cluster in clusters: - fleet.clusters.create(**cluster) - return fleet + print(f"validated_data: {validated_data}") + return Fleet.objects.create(**validated_data) -class FleetDetailSerializer(serializers.Serializer): +class FleetDetailSerializer(serializers.ModelSerializer): wallet = serializers.SerializerMethodField(read_only=True) statistics = serializers.SerializerMethodField(read_only=True) bots = BotSerializer(many=True, read_only=True) @@ -97,6 +99,8 @@ def _update_merged_wallet(index: int, currency: str, merged_wallet) -> None: else: merged_wallet[index]["amount"] += currency.amount + if self.space is None: + return None wallets = ConnectionWallet.objects.filter(owner__owner=self.space.wallet, owner__bot__in=instance.bots) merged_wallet: list[dict[str, str | float]] = [] diff --git a/django_napse/api/fleets/views/fleet_view.py b/django_napse/api/fleets/views/fleet_view.py index 40977312..e674de71 100644 --- a/django_napse/api/fleets/views/fleet_view.py +++ b/django_napse/api/fleets/views/fleet_view.py @@ -26,7 +26,7 @@ def get_queryset(self): self.get_space = NapseSpace.objects.get(uuid=space_uuid) return self.get_space.fleets - def get_serialiser_class(self, *args, **kwargs): + def get_serializer_class(self, *args, **kwargs): actions: dict = { "list": FleetSerializer, "retrieve": FleetDetailSerializer, @@ -42,10 +42,16 @@ def get_permissions(self): case _: return super().get_permissions() + def get_object(self): + uuid = self.kwargs.get("pk", None) + if uuid is None: + return super().get_object() + return Fleet.objects.get(uuid=uuid) + def list(self, request): space_containers = request.query_params.get("space_containers", True) if not space_containers: - 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) # Get spaces from API key @@ -54,21 +60,23 @@ def list(self, request): fleets = [] for space in spaces: print("FLEET", space.fleets) - serializer = self.serializer_class(space.fleets, many=True) + serializer = self.get_serializer(space.fleets, many=True) if serializer.data != []: - fleets.append(*serializer.data) + fleets += serializer.data return Response(fleets, status=status.HTTP_200_OK) def retrieve(self, request, pk=None): - pass + instance = self.get_object() + space = self.get_space(request) + serializer = self.get_serializer(instance, space=space) + return Response(serializer.data, status=status.HTTP_200_OK) def create(self, request, *args, **kwargs): - return Response(status=status.HTTP_501_NOT_IMPLEMENTED) space = self.get_space(request) - serializer = self.serializer_class(data=request.data, space=space) + serializer = self.get_serializer(data=request.data, space=space) serializer.is_valid(raise_exception=True) fleet = serializer.save() - + fleet.invest(space, 0, "USDT") return Response(status=status.HTTP_201_CREATED) def delete(self):