Coverage for django_napse/api/bots/serializers/bot_serializers.py: 56%
71 statements
« prev ^ index » next coverage.py v7.4.3, created at 2024-03-12 13:49 +0000
« prev ^ index » next coverage.py v7.4.3, created at 2024-03-12 13:49 +0000
1from typing import ClassVar
2from uuid import UUID
4from rest_framework import serializers
5from rest_framework.fields import empty
7from django_napse.api.orders.serializers import OrderSerializer
8from django_napse.api.wallets.serializers import WalletSerializer
9from django_napse.core.models import Bot, BotHistory, ConnectionWallet, NapseSpace, Order
12class BotSerializer(serializers.ModelSerializer):
13 """Serialize bot instances."""
15 delta = serializers.SerializerMethodField(read_only=True)
16 space = serializers.SerializerMethodField(read_only=True)
17 exchange_account = serializers.SerializerMethodField(read_only=True)
18 fleet = serializers.CharField(source="fleet.uuid", read_only=True)
20 class Meta: # noqa: D106
21 model = Bot
22 fields: ClassVar = [
23 "name",
24 "uuid",
25 "value",
26 "delta",
27 "fleet",
28 "space",
29 "exchange_account",
30 ]
31 read_only_fields: ClassVar = [
32 "uuid",
33 "value",
34 "delta",
35 "space",
36 ]
38 def __init__(
39 self,
40 instance: Bot = None,
41 data: dict[str, any] = empty,
42 space: NapseSpace = None,
43 **kwargs: dict[str, any],
44 ) -> None:
45 """Add space to the serializer and run the default constructor."""
46 self.space = space
47 super().__init__(instance=instance, data=data, **kwargs)
49 def get_delta(self, instance: Bot) -> float:
50 """Delta on the last 30 days."""
51 try:
52 history = BotHistory.objects.get(owner=instance)
53 except BotHistory.DoesNotExist:
54 return 0
55 return history.get_delta()
57 def get_space(self, instance: Bot) -> UUID | None: # noqa: ARG002
58 """Return the space used for the space containerization."""
59 if self.space is None:
60 return None
61 print("UUID", type(self.space.uuid))
62 return self.space.uuid
64 def get_exchange_account(self, instance: Bot) -> UUID | None:
65 """Return the exchange account of the bot if it exists."""
66 if not instance.is_in_fleet and not instance.is_in_simulation:
67 return None
68 return instance.exchange_account.uuid
71class BotDetailSerializer(serializers.ModelSerializer):
72 """Deep dive in bot's data for serialization."""
74 delta = serializers.SerializerMethodField(read_only=True)
75 space = serializers.SerializerMethodField(read_only=True)
76 exchange_account = serializers.CharField(source="exchange_account.uuid", read_only=True)
77 fleet = serializers.CharField(source="fleet.uuid", read_only=True)
79 statistics = serializers.SerializerMethodField(read_only=True)
80 wallet = serializers.SerializerMethodField(read_only=True)
81 orders = serializers.SerializerMethodField(read_only=True)
83 class Meta: # noqa: D106
84 model = Bot
85 fields: ClassVar = [
86 "name",
87 "uuid",
88 "value",
89 "delta",
90 "statistics",
91 "fleet",
92 "space",
93 "exchange_account",
94 "wallet",
95 "orders",
96 ]
97 read_only_fields: ClassVar = [
98 "uuid",
99 "value",
100 "delta",
101 "statistics",
102 "space",
103 "wallet",
104 "orders",
105 ]
107 def __init__(
108 self,
109 instance: Bot = None,
110 data: dict[str, any] = empty,
111 space: NapseSpace = None,
112 **kwargs: dict[str, any],
113 ) -> None:
114 """Add space to the serializer and run the default constructor."""
115 self.space = space
116 super().__init__(instance=instance, data=data, **kwargs)
118 def get_delta(self, instance: Bot) -> float:
119 """Delta on the last 30 days."""
120 try:
121 history = BotHistory.objects.get(owner=instance)
122 except BotHistory.DoesNotExist:
123 return 0
124 return history.get_delta()
126 def get_space(self, instance: Bot) -> UUID | None: # noqa: ARG002
127 """Return the space used for the space containerization."""
128 if self.space is None:
129 return None
130 return self.space.uuid
132 def get_statistics(self, instance: Bot) -> dict[str, str | int | float]:
133 """Return Bot's statistics."""
134 return instance.get_stats(space=self.space)
136 def get_wallet(self, instance: Bot) -> dict[str, any] | list[dict[str, any]]:
137 """Return space's connections wallet of this bot, or return all connections wallets."""
138 if self.space is not None:
139 wallet = ConnectionWallet.objects.get(
140 owner__owner=self.space.wallet,
141 owner__bot=instance,
142 )
143 return WalletSerializer(wallet).data
145 wallets = [connection.wallet for connection in instance.connections.all()]
146 return WalletSerializer(wallets, many=True).data
148 def get_orders(self, instance: Bot) -> list[dict[str, any]]:
149 """Return all orders of the bot."""
150 if self.space is None:
151 return OrderSerializer(
152 Order.objects.filter(connection__bot=instance),
153 many=True,
154 ).data
156 return OrderSerializer(
157 Order.objects.filter(
158 connection__bot=instance,
159 connection__owner=self.space.wallet,
160 ),
161 many=True,
162 ).data