Skip to content

Commit

Permalink
update: update NodesView
Browse files Browse the repository at this point in the history
  • Loading branch information
thepsalmist committed Oct 22, 2024
1 parent bc82a90 commit a2f06d8
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions sensorsafrica/api/v2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

from rest_framework import mixins, pagination, viewsets
from rest_framework.authentication import SessionAuthentication, TokenAuthentication
from rest_framework.decorators import action
from rest_framework.exceptions import ValidationError
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated, AllowAny
Expand Down Expand Up @@ -129,6 +130,7 @@ class CitiesView(mixins.ListModelMixin, viewsets.GenericViewSet):


class NodesView(viewsets.ViewSet):
"""Create and list nodes, with the option to list authenticated user's nodes."""
authentication_classes = [SessionAuthentication, TokenAuthentication]

def get_permissions(self):
Expand All @@ -139,7 +141,9 @@ def get_permissions(self):

return [permission() for permission in permission_classes]

def list(self, request):
@action(detail=False, methods=["get"], url_path="list-nodes", url_name="list_nodes")
def list_nodes(self, request):
"""List all public nodes with active sensors."""
nodes = []
# Loop through the last active nodes
for last_active in LastActiveNodes.objects.iterator():
Expand Down Expand Up @@ -219,7 +223,17 @@ def list(self, request):

return Response(nodes)

def create(self, request):
@action(detail=False, methods=["get"], url_path="my-nodes", url_name="my_nodes")
def list_my_nodes(self, request):
"""List only the nodes owned by the authenticated user."""
if request.user.is_authenticated:
queryset = Node.objects.filter(owner=request.user)
serializer = NodeSerializer(queryset, many=True)
return Response(serializer.data)
return Response({"detail": "Authentication credentials were not provided."}, status=403)

@action(detail=False, methods=["post"], url_path="create-node", url_name="create_node")
def create_node(self, request):
serializer = NodeSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
Expand Down

0 comments on commit a2f06d8

Please sign in to comment.