Skip to content

Commit 04fae27

Browse files
committed
Add ability to add Request comment in REST API #345
Signed-off-by: tdruez <tdruez@nexb.com>
1 parent 1d8a62a commit 04fae27

4 files changed

Lines changed: 67 additions & 0 deletions

File tree

docs/integrations-rest-api.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,25 @@ Allows updating only specific fields. For example, to close a request::
204204
response = requests.patch(api_url, headers=headers, json=data)
205205
print(response.json())
206206

207+
Add comment
208+
-----------
209+
210+
``POST /api/v2/requests/{uuid}/add_comment/``
211+
212+
This endpoint allows you to attach a new comment to an existing request.
213+
A successful call will store the comment and return a confirmation message.
214+
215+
**Payload example**:
216+
217+
.. code-block:: json
218+
219+
{
220+
"text": "Comment content"
221+
}
222+
223+
**Notes**:
224+
- The ``uuid`` in the URL must correspond to the target request.
225+
- The ``text`` field is required and should contain the full comment content.
226+
- Comments are attributed to the authenticated user making the request.
227+
- A successful request returns HTTP 201 with a status message.
228+
- Invalid or missing fields will return HTTP 400 along with error details.

workflow/api.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515
import django_filters
1616
from rest_framework import serializers
17+
from rest_framework import status
18+
from rest_framework.decorators import action
19+
from rest_framework.response import Response
1720
from rest_framework.viewsets import ReadOnlyModelViewSet
1821

1922
from dje.api import CreateRetrieveUpdateListViewSet
@@ -407,3 +410,19 @@ def perform_update(self, serializer):
407410
event_type=RequestEvent.EDIT,
408411
dataspace=self.request.user.dataspace,
409412
)
413+
414+
@action(
415+
detail=True,
416+
methods=["post"],
417+
serializer_class=RequestCommentSerializer,
418+
)
419+
def add_comment(self, request, *args, **kwargs):
420+
"""Add a comment to this request."""
421+
request_instance = self.get_object()
422+
423+
serializer = RequestCommentSerializer(data=request.data)
424+
if serializer.is_valid():
425+
request_instance.add_comment(self.request.user, **serializer.validated_data)
426+
return Response({"status": "Comment added."}, status=status.HTTP_201_CREATED)
427+
428+
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

workflow/models.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,14 @@ def serialize_hook(self, hook):
602602
"data": serializer.data,
603603
}
604604

605+
def add_comment(self, user, text):
606+
"""Create and return a RequestComment for this Request."""
607+
return self.comments.create(
608+
user=user,
609+
text=text,
610+
dataspace=self.dataspace,
611+
)
612+
605613
def close(self, user, reason):
606614
"""
607615
Set the Request status to CLOSED.

workflow/tests/test_api.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,24 @@ def test_api_request_endpoint_edit_serialized_data(self):
786786
expected = {"serialized_data": ['"Organization" is required.']}
787787
self.assertEqual(expected, response.data)
788788

789+
def test_api_request_endpoint_add_comment_action(self):
790+
self.client.login(username="super_user", password="secret")
791+
add_comment_url = reverse("api_v2:request-add-comment", args=[self.request1.uuid])
792+
793+
data = {}
794+
response = self.client.post(add_comment_url, data=data)
795+
self.assertEqual(status.HTTP_400_BAD_REQUEST, response.status_code)
796+
expected = {
797+
"text": ["This field is required."],
798+
}
799+
self.assertEqual(expected, response.json())
800+
801+
data = {"text": "Comment content."}
802+
response = self.client.post(add_comment_url, data=data)
803+
self.assertEqual(status.HTTP_201_CREATED, response.status_code)
804+
expected = {"status": "Comment added."}
805+
self.assertEqual(expected, response.data)
806+
789807
def test_api_request_and_request_template_endpoints_tab_permission(self):
790808
self.assertEqual((TabPermission,), RequestViewSet.extra_permissions)
791809
self.assertEqual((TabPermission,), RequestTemplateViewSet.extra_permissions)

0 commit comments

Comments
 (0)