Skip to content

Commit

Permalink
feat(bot): add retrieve view
Browse files Browse the repository at this point in the history
  • Loading branch information
Xenepix committed Jan 18, 2024
1 parent b068a75 commit 806bbee
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion django_napse/api/bots/serializers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .architecture_serializer import ArchitectureSerializer
from .bot_serializers import BotSerializer
from .bot_serializers import BotDetailSerializer, BotSerializer
from .config_serializer import ConfigSerializer
from .plugin_serializer import PluginSerializer
from .strategy_serializer import StrategySerializer
13 changes: 10 additions & 3 deletions django_napse/api/bots/serializers/bot_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,22 @@ class Meta:
"name",
"uuid",
"value",
"delta",
"statistics",
"wallet",
"fleet",
"space",
"exchange_account",
"wallet",
"orders",
]
read_only_fields = [
"uuid",
"value",
"delta",
"statistics",
"wallet",
"space",
"wallet",
"orders",
]

def __init__(self, instance=None, data=empty, space=None, **kwargs):
Expand All @@ -96,6 +100,9 @@ def get_space(self, instance):
return None
return self.space.uuid

def get_statistics(self, instance):
return instance.get_stats(space=self.space)

def get_wallet(self, instance):
def _search_ticker(ticker: str, merged_wallet) -> int | None:
"""Return the index of the currency in the list if found, None otherwise."""
Expand Down Expand Up @@ -143,7 +150,7 @@ def get_orders(self, instance):
return OrderSerializer(
Order.objects.filter(
connection__bot=instance,
connection__owner__owner=self.space.wallet,
connection__owner=self.space.wallet,
),
many=True,
).data
14 changes: 12 additions & 2 deletions django_napse/api/bots/views/bot_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from rest_framework.response import Response
from rest_framework_api_key.permissions import HasAPIKey

from django_napse.api.bots.serializers.bot_serializers import BotSerializer
from django_napse.api.bots.serializers.bot_serializers import BotDetailSerializer, BotSerializer
from django_napse.api.custom_permissions import HasSpace
from django_napse.api.custom_viewset import CustomViewSet
from django_napse.core.models import Bot, NapseSpace
Expand All @@ -18,6 +18,7 @@ def get_queryset(self):
def get_serializer_class(self, *args, **kwargs):
actions: dict = {
"list": BotSerializer,
"retrieve": BotDetailSerializer,
}
result = actions.get(self.action)
return result if result else super().get_serializer_class()
Expand All @@ -29,6 +30,12 @@ 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 Bot.objects.get(uuid=uuid)

def _get_boolean_query_param(self, param: str) -> bool | None:
"""Return None if a boolean cannot be found."""
if isinstance(param, bool):
Expand Down Expand Up @@ -73,4 +80,7 @@ def list(self, request):
return Response(bots, status=status.HTTP_200_OK)

def retrieve(self, request, pk=None):
return Response(status=status.HTTP_501_NOT_IMPLEMENTED)
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)
2 changes: 1 addition & 1 deletion django_napse/core/models/bots/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def copy(self):
def value(self, space=None):
if space is None:
return sum([connection.wallet.value_market() for connection in self.connections.all()])
connection = Connection.objects.get(owner__owner=space.wallet, bot=self)
connection = Connection.objects.get(owner=space.wallet, bot=self)
return connection.wallet.value_market()

def get_stats(self, space=None):
Expand Down

0 comments on commit 806bbee

Please sign in to comment.