1
1
from chat .models import ChatGroup , GroupMessage
2
- from chat .serializers import ChatSerializers , GroupSerializers
3
- from core .permissions import CanEditOwnOrAdmin
2
+ from chat .serializers import (
3
+ ChatSerializers ,
4
+ FileAttachmentSerializers ,
5
+ GroupSerializers ,
6
+ ImageAttachmentSerializer ,
7
+ )
4
8
from rest_framework import authentication , generics , permissions , status
9
+ from rest_framework .decorators import APIView
5
10
from rest_framework .response import Response
6
11
7
12
8
13
class ChatMessageView (generics .ListAPIView ):
14
+ """
15
+ API endpoint to retrieve chat groups and their messages.
16
+
17
+ Request Method: GET /chat/messages/
18
+
19
+ Responses:
20
+ - 200 OK: Returns a list of chat groups and their messages.
21
+ - 401 Unauthorized: Authentication failed.
22
+ """
23
+
9
24
queryset = ChatGroup .objects .all ()
10
25
permission_classes = [permissions .IsAuthenticated ]
11
26
authentication_classes = [authentication .SessionAuthentication ]
@@ -28,6 +43,17 @@ def list(self, request, *args, **kwargs):
28
43
29
44
30
45
class ChatCreateView (generics .CreateAPIView ):
46
+ """
47
+ API endpoint to create a group message.
48
+
49
+ Request Method: POST /chat/messages/create
50
+
51
+ Responses:
52
+ - 201 Created: Message has been created.
53
+ - 400 Bad Request: Unable to create message.
54
+ - 401 Unauthorized: Authentication failed.
55
+ """
56
+
31
57
permission_classes = [permissions .IsAuthenticated ]
32
58
serializer_class = GroupSerializers
33
59
authentication_classes = [authentication .SessionAuthentication ]
@@ -37,7 +63,19 @@ class ChatCreateView(generics.CreateAPIView):
37
63
38
64
39
65
class ChatDeleteView (generics .DestroyAPIView ):
40
- permission_classes = [permissions .IsAdminUser | CanEditOwnOrAdmin ]
66
+ """
67
+ API endpoint to delete a group message by its ID.
68
+
69
+ Request Method: DELETE /chat/messsages/int:id/delete/
70
+
71
+ Responses:
72
+ - 200 OK: Message has been deleted.
73
+ - 401 Unauthorized: Authentication failed.
74
+ - 403 Forbidden: User does not have permission to delete the message.
75
+ - 404 Not Found: Message with the specified ID does not exist.
76
+ """
77
+
78
+ permission_classes = [permissions .IsAdminUser ]
41
79
serializer_class = GroupSerializers
42
80
queryset = GroupMessage .objects .all ()
43
81
lookup_field = "id"
@@ -53,28 +91,142 @@ def destroy(self, request, *args, **kwargs):
53
91
54
92
55
93
class ChatUpdateView (generics .UpdateAPIView ):
94
+ """
95
+ API endpoint to update a group message by its ID.
96
+
97
+ Request Method: PUT chat/messages/<int:id>/update/
98
+
99
+ Responses:
100
+ - 200 OK: Message has been updated.
101
+ - 400 Bad Request: Unable to update message.
102
+ - 401 Unauthorized: Authentication failed.
103
+ - 404 Not Found: Message with the specified ID does not exist.
104
+ """
105
+
56
106
serializer_class = GroupSerializers
57
- authentication_classes = [authentication .SessionAuthentication | CanEditOwnOrAdmin ]
107
+ authentication_classes = [authentication .SessionAuthentication ]
108
+ queryset = GroupMessage .objects .all ()
58
109
lookup_field = "id"
59
110
60
111
61
112
msg_update = ChatUpdateView .as_view ()
62
113
63
114
64
115
class GroupNameCreate (generics .CreateAPIView ):
116
+ """
117
+ API endpoint to create a new chat group.
118
+
119
+ Request Method: POST /chat/groups/create/
120
+
121
+ Responses:
122
+ - 201 Created: Chat group created successfully.
123
+ - 400 Bad Request: Invalid data provided.
124
+ - 401 Unauthorized: Authentication credentials were not provided or are invalid.
125
+ """
126
+
65
127
serializer_class = ChatSerializers
66
128
queryset = ChatGroup .objects .all ()
67
- permission_classes = [permissions .IsAuthenticatedOrReadOnly | CanEditOwnOrAdmin ]
129
+ permission_classes = [permissions .IsAuthenticatedOrReadOnly ]
68
130
69
131
70
132
group_create = GroupNameCreate .as_view ()
71
133
72
134
73
135
class GroupNameUpdate (generics .UpdateAPIView ):
136
+ """
137
+ API endpoint to update the group name of a chat group.
138
+
139
+ Request Method: PUT/PATCH /chat/groups/str:<group_name>/update/
140
+
141
+ Path Parameters:
142
+ - group_name (str): The current name of the chat group to be updated.
143
+
144
+ Request Body:
145
+ - group_name (str): The new name for the chat group.
146
+
147
+ Responses:
148
+ - 200 OK: Group name updated successfully.
149
+ - 400 Bad Request: Invalid data provided.
150
+ - 401 Unauthorized: Authentication credentials were not provided or are invalid.
151
+ - 404 Not Found: Chat group with the specified group_name does not exist.
152
+ """
153
+
74
154
serializer_class = ChatSerializers
75
155
queryset = ChatGroup .objects .all ()
76
- permission_classes = [permissions .IsAuthenticatedOrReadOnly | CanEditOwnOrAdmin ]
156
+ permission_classes = [permissions .IsAuthenticatedOrReadOnly ]
77
157
lookup_field = "group_name"
78
158
79
159
80
160
group_update = GroupNameUpdate .as_view ()
161
+
162
+
163
+ class FileAttachment (APIView ):
164
+ """
165
+ API endpoint to upload the file such as `.pdf`.
166
+
167
+ Request Method: POST /chat/upload_file/
168
+
169
+ Responses:
170
+ - 200 OK: File uploaded
171
+ - 400 Bad Request: Unable to upload file.
172
+ - 401 Unauthorized: Authentication failed.
173
+ """
174
+
175
+ permission_classes = [permissions .IsAuthenticated ]
176
+
177
+ def post (self , request , format = None ):
178
+ client = request .user
179
+ if not client :
180
+ return Response (
181
+ {"error" : "Unauthorized to upload file " },
182
+ status = status .HTTP_401_UNAUTHORIZED ,
183
+ )
184
+
185
+ serializer = FileAttachmentSerializers (data = request .data )
186
+ if serializer .is_valid ():
187
+ serializer .save ()
188
+ return Response ({"success" : "File uploaded" }, status = status .HTTP_200_OK )
189
+ print ("Serializer errors:" , serializer .errors )
190
+ return Response (
191
+ {"failed" : "Unable to upload file" , "detail_error" : serializer .errors },
192
+ status = status .HTTP_400_BAD_REQUEST ,
193
+ )
194
+
195
+
196
+ upload_file = FileAttachment .as_view ()
197
+
198
+
199
+ class ImageAttachment (APIView ):
200
+ """
201
+ API endpoint to upload the image file.
202
+
203
+ Request Method: POST /chat/upload_image/
204
+
205
+ Responses:
206
+ - 200 OK:Image uploaded
207
+ - 400 Bad Request: Unable to upload image.
208
+ - 401 Unauthorized: Authentication failed.
209
+ """
210
+
211
+ permission_classes = [permissions .IsAuthenticated ]
212
+
213
+ def post (self , request , format = None ):
214
+ client = request .user
215
+ if not client :
216
+ return Response (
217
+ {"error" : "Unauthorized to upload images" },
218
+ status = status .HTTP_401_UNAUTHORIZED ,
219
+ )
220
+
221
+ serializer = ImageAttachmentSerializer (data = request .data )
222
+ if serializer .is_valid ():
223
+ serializer .save ()
224
+ return Response ({"success" : "Image uploaded" }, status = status .HTTP_200_OK )
225
+ print ("Serializer errors:" , serializer .errors )
226
+ return Response (
227
+ {"failed" : "Unable to upload image" , "detail_error" : serializer .errors },
228
+ status = status .HTTP_400_BAD_REQUEST ,
229
+ )
230
+
231
+
232
+ upload_image = ImageAttachment .as_view ()
0 commit comments