-
Notifications
You must be signed in to change notification settings - Fork 462
Expand file tree
/
Copy pathviews.py
More file actions
94 lines (86 loc) · 3.36 KB
/
Copy pathviews.py
File metadata and controls
94 lines (86 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# SPDX-License-Identifier: Apache-2.0
from typing import Optional
from drf_yasg.utils import swagger_auto_schema
from rest_framework import viewsets, status
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from api.common import ok
from api.common.response import make_response_serializer
from api.utils.common import with_common_response
from common.responses import err
from common.serializers import PageQuerySerializer
from node.models import Node
from node.serializers import NodeList, NodeCreateBody, NodeID, NodeResponse
class NodeViewSet(viewsets.ViewSet):
permission_classes = [
IsAuthenticated,
]
@swagger_auto_schema(
operation_summary="List all nodes of the current organization",
query_serializer=PageQuerySerializer(),
responses=with_common_response(
{status.HTTP_200_OK: make_response_serializer(NodeList)}
),
)
def list(self, request):
serializer = PageQuerySerializer(data=request.GET)
p = serializer.get_paginator(Node.objects.filter(organization=request.user.organization))
return Response(
status=status.HTTP_200_OK,
data=ok(NodeList(
{
"total": p.count,
"data": NodeResponse(
p.page(serializer.data['page']).object_list,
context={"organization": request.user.organization},
many=True).data
},
).data),
)
@swagger_auto_schema(
operation_summary="Create a new node of the current organization",
request_body=NodeCreateBody,
responses=with_common_response(
{status.HTTP_201_CREATED: make_response_serializer(NodeID)}
),
)
def create(self, request):
serializer = NodeCreateBody(data=request.data, context={"organization": request.user.organization})
serializer.is_valid(raise_exception=True)
return Response(
status=status.HTTP_201_CREATED,
data=ok(NodeID(serializer.save().__dict__).data),
)
@swagger_auto_schema(
operation_summary="Retrieve a node by ID",
responses=with_common_response(
{status.HTTP_200_OK: make_response_serializer(NodeResponse)}
),
)
def retrieve(self, request, pk: Optional[str] = None):
try:
node = Node.objects.get(pk=pk, organization=request.user.organization)
except Node.DoesNotExist:
return Response(
status=status.HTTP_404_NOT_FOUND,
data=err("Node not found"),
)
return Response(
status=status.HTTP_200_OK,
data=ok(NodeResponse(node, context={"organization": request.user.organization}).data),
)
@swagger_auto_schema(
operation_summary="Delete a node by ID",
responses=with_common_response(
{status.HTTP_204_NO_CONTENT: "No Content"}
),
)
def destroy(self, request, pk: Optional[str] = None):
try:
Node.objects.get(pk=pk, organization=request.user.organization).delete()
except Node.DoesNotExist:
return Response(
status=status.HTTP_404_NOT_FOUND,
data=err("Node not found"),
)
return Response(status=status.HTTP_204_NO_CONTENT)