Skip to content

Commit

Permalink
fix(space): add update endpoints & get_premissions
Browse files Browse the repository at this point in the history
  • Loading branch information
Xenepix38 committed Oct 21, 2023
1 parent 4b7885c commit 7f6d69d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
4 changes: 3 additions & 1 deletion django_napse/api/spaces/serializers/space_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ class Meta:
model = NapseSpace
fields = [
"name",
"exchange_account",
"description",
# read-only
"uuid",
"exchange_account",
"value",
"fleet_count",
]
read_only_fields = [
"exchange_account",
"uuid",
"value",
"fleet_count",
Expand Down
25 changes: 22 additions & 3 deletions django_napse/api/spaces/views/space_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,47 @@ def get_serializer_class(self, *args, **kwargs):
"list": SpaceSerializer,
"retrieve": SpaceDetailSerializer,
"create": SpaceSerializer,
"update": SpaceSerializer,
"partial_update": SpaceSerializer,
}
result = actions.get(self.action, None)
return result if result else super().get_serializer_class()

def get_permissions(self):
match self.action:
case "retrieve" | "update" | "partial_update" | "delete":
return [HasAPIKey(), HasSpace()]
case _:
return super().get_permissions()

def list(self, request):
serializer = self.get_serializer(self.get_queryset(), many=True)
return Response(serializer.data, status=status.HTTP_200_OK)

@permission_decorator([HasSpace])
def retrieve(self, request, pk=None):
instance = self.get_object()
serializer = self.get_serializer(instance)
return Response(serializer.data, status=status.HTTP_200_OK)

@permission_decorator([HasSpace])
def create(self, request, *args, **kwargs):
serializer = self.serializer_class(data=request.data)
serializer.is_valid(raise_exception=True)
serializer.save()
return Response(status=status.HTTP_201_CREATED)

@permission_decorator([HasSpace])
def update(self, request, *args, **kwargs):
partial = kwargs.pop("partial", False)
instance = self.get_object()

serializer = self.get_serializer(instance=instance, data=request.data, partial=partial)
serializer.is_valid(raise_exception=True)
serializer.save()
return Response(status=status.HTTP_200_OK)

def partial_update(self, request, **kwargs):
"""Partial update the connected user."""
return self.update(request, partial=True, **kwargs)

def delete(self, request, *args, **kwargs):
instance = self.get_object()
try:
Expand Down

0 comments on commit 7f6d69d

Please sign in to comment.