Coverage for django_napse/api/exchanges/views/exchange_account_view.py: 41%
54 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 rest_framework import status
2from rest_framework.decorators import action
3from rest_framework.response import Response
5from django_napse.api.custom_permissions import HasMasterKey
6from django_napse.api.custom_viewset import CustomViewSet
7from django_napse.api.exchanges.serializers.exchange_account_detail_serializer import ExchangeAccountDetailSerializer
8from django_napse.api.exchanges.serializers.exchange_account_serializer import ExchangeAccountSerializer
9from django_napse.core.models import Exchange, ExchangeAccount
10from django_napse.utils.constants import EXCHANGES
13class ExchangeAccountView(CustomViewSet):
14 permission_classes = [HasMasterKey]
15 serializer_class = ExchangeAccountSerializer
17 def get_object(self):
18 return self.get_queryset().get(uuid=self.kwargs["pk"])
20 def get_queryset(self):
21 return ExchangeAccount.objects.all()
23 def get_serializer_class(self, *args, **kwargs):
24 actions: dict = {
25 "list": ExchangeAccountSerializer,
26 "retrieve": ExchangeAccountDetailSerializer,
27 }
28 result = actions.get(self.action)
29 return result if result else super().get_serializer_class()
31 def list(self, request):
32 serializer = self.get_serializer(self.get_queryset(), many=True)
33 return Response(serializer.data, status=status.HTTP_200_OK)
35 def retrieve(self, request, pk=None):
36 instance = self.get_object()
37 serializer = self.get_serializer(instance)
38 return Response(serializer.data, status=status.HTTP_200_OK)
40 def create(self, request):
41 if "name" not in request.data:
42 return Response({"error": "Missing name"}, status=status.HTTP_400_BAD_REQUEST)
43 if "exchange" not in request.data:
44 return Response({"error": "Missing exchange"}, status=status.HTTP_400_BAD_REQUEST)
45 if "testing" not in request.data:
46 return Response({"error": "Missing testing"}, status=status.HTTP_400_BAD_REQUEST)
48 exchange = Exchange.objects.get(name=request.data["exchange"])
49 exchange_account = ExchangeAccount.objects.create(
50 exchange=exchange,
51 name=request.data["name"],
52 testing=request.data["testing"],
53 description=request.data.get("description", ""),
54 )
55 serializer = self.get_serializer(exchange_account)
56 return Response(serializer.data, status=status.HTTP_201_CREATED)
58 def destroy(self, request, pk=None):
59 instance = self.get_object()
60 instance.delete()
61 return Response(status=status.HTTP_204_NO_CONTENT)
63 def patch(self, request, pk=None):
64 instance = self.get_object()
65 if "name" in request.data:
66 instance.name = request.data["name"]
67 if "description" in request.data:
68 instance.description = request.data["description"]
69 instance.save()
70 serializer = self.get_serializer(instance)
71 return Response(serializer.data, status=status.HTTP_200_OK)
73 @action(detail=False, methods=["GET"])
74 def possible_exchanges(self, request):
75 return Response(list(EXCHANGES), status=status.HTTP_200_OK)