Skip to content

Commit

Permalink
fix(bot): add space containerization
Browse files Browse the repository at this point in the history
  • Loading branch information
Xenepix committed Jan 7, 2024
1 parent 884ebb1 commit 66bbba6
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
23 changes: 23 additions & 0 deletions django_napse/api/bots/serializers/bot_serializers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from rest_framework import serializers
from rest_framework.fields import empty

from django_napse.api.bots.serializers.strategy_serializer import StrategySerializer
from django_napse.core.models import Bot, BotHistory
Expand All @@ -7,6 +8,7 @@
class BotSerializer(serializers.ModelSerializer):
strategy = StrategySerializer()
delta = serializers.SerializerMethodField(read_only=True)
space = serializers.SerializerMethodField(read_only=True)

class Meta:
model = Bot
Expand All @@ -16,17 +18,38 @@ class Meta:
"strategy",
"value",
"delta",
"fleet",
"space",
"exchange_account",
]
read_only_fields = [
"uuid",
"value",
"delta",
"fleet",
"space",
"exchange_account",
]

def __init__(self, instance=None, data=empty, space=None, **kwargs):
self.space = space
super().__init__(instance=instance, data=data, **kwargs)

def get_delta(self, instance) -> float:
"""Delta on the last 30 days."""
try:
history = BotHistory.objects.get(owner=instance)
except BotHistory.DoesNotExist:
return 0
return history.get_delta()

def get_space(self, instance):
if self.space is None:
return None
return self.space.uuid

def to_representation(self, instance):
representation = super().to_representation(instance)
representation["fleet"] = representation["fleet"].uuid
representation["exchange_account"] = representation["exchange_account"].uuid
return representation
45 changes: 44 additions & 1 deletion django_napse/api/bots/views/bot_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django_napse.api.bots.serializers.bot_serializers import BotSerializer
from django_napse.api.custom_permissions import HasSpace
from django_napse.api.custom_viewset import CustomViewSet
from django_napse.core.models import Bot
from django_napse.core.models import Bot, NapseSpace


class BotView(CustomViewSet):
Expand All @@ -29,7 +29,50 @@ def get_permissions(self):
case _:
return super().get_permissions()

def _get_boolean_query_param(self, param: str) -> bool | None:
"""Return None if a boolean cannot be found."""
if isinstance(param, bool):
return param

if not isinstance(param, str):
return None

match param.lower():
case "true" | "1":
return True
case "false" | "0":
return False
case _:
return None

def list(self, request):
space_containers = self._get_boolean_query_param(request.query_params.get("space_containers", True))
space_uuid = request.query_params.get("space", None)
api_key = self.get_api_key(request)

if not space_containers and api_key.is_master_key:
# Not space_containers mode is only available for master key
serializer = self.get_serializer(self.get_queryset(), many=True)
return Response(serializer.data, status=status.HTTP_200_OK)

# Get spaces from API key
spaces = NapseSpace.objects.all() if api_key.is_master_key else [permission.space for permission in api_key.permissions.all()]
# Filter by specific space
if space_uuid is not None:
space = NapseSpace.objects.get(uuid=space_uuid)
if space not in spaces:
return Response(status=status.HTTP_403_FORBIDDEN)
spaces = [space]

# Bot list
bots = []
for space in spaces:
serializer = self.get_serializer(space.bots, many=True, space=space)
if serializer.data != []:
bots += serializer.data
return Response(bots, status=status.HTTP_200_OK)

# TODO: make space containerization
serializer = self.get_serializer(self.get_queryset(), many=True)
return Response(serializer.data, status=status.HTTP_200_OK)

Expand Down

0 comments on commit 66bbba6

Please sign in to comment.