From 7ed835d572d25cb84109616f3e609ea02b6852f6 Mon Sep 17 00:00:00 2001 From: Daniel Muremwa Mburu Date: Sun, 18 Dec 2022 23:51:59 +0300 Subject: [PATCH] comment actions update --- api/urls.py | 7 +++++-- api/views.py | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/api/urls.py b/api/urls.py index be45951..098c05e 100644 --- a/api/urls.py +++ b/api/urls.py @@ -21,16 +21,19 @@ # api/create_note/ path('create_note/', views.NoteCreationApi.as_view(), name="create-note"), - # TODO: DEPRACATE + # TODO: DEPRECATE # api/comments/ path('/comments/', views.AllComments.as_view(), name='comments'), # api/v2/33/comments/ path('v2//comments/', views.AllCommentsV2.as_view(), name='comments-v2'), - # api/comments/4/actions/ + # api/comment/4/actions/ path('comment//actions/', views.comment_actions, name='comment-actions'), + # api/comment/4/actions/v2/ + path('comment//actions/v2/', views.comment_actions_v2, name='comment-actions-v2'), + # user/get/ path('user/get/', views.get_user, name='get-user'), diff --git a/api/views.py b/api/views.py index 4109bf4..7ee98e9 100644 --- a/api/views.py +++ b/api/views.py @@ -8,6 +8,7 @@ from rest_framework import views, generics from rest_framework import status from django.http import Http404 +from django.utils import timezone from .serializers import NotesSerializer, NoteSerializer, UserSerializer, ApiUserSerializer, ApiNoteSerializer, \ ApiCommentSerializer @@ -68,6 +69,7 @@ def post(request): }) +# TODO: DEPRECATE class AllComments(views.APIView, CommentProcessor): @staticmethod def shape(comment, user_id, host): @@ -135,6 +137,7 @@ def post(self, *args, **kwargs): }) +# TODO: DEPRECATE @api_view(['POST']) def comment_actions(request, pk): comment = get_object_or_404(Comment, pk=pk) @@ -242,3 +245,55 @@ def post(self, *args, **kwargs): }) return Response(response, status=res_status) + + +@api_view(['DELETE', 'PATCH']) +def comment_actions_v2(request, comment_pk): + response = {'success': False} + res_status = status.HTTP_403_FORBIDDEN + comment = get_object_or_404(Comment, pk=comment_pk) + + # update a comment + if request.method == 'PATCH': + if request.user == comment.user: + posted_comment = request.data.get('comment') + + if posted_comment: + processor = CommentProcessor() + processed_comment = processor.mark(posted_comment) + comment.original_comment = posted_comment + comment.comment_text = processed_comment.get('processed_comment') + comment.modified = timezone.now() + comment.save() + notify = [] + + # add mentioned users + for mentioned in processed_comment.get('mentioned'): + if mentioned.profile not in comment.mentioned.all(): + comment.mentioned.add(mentioned.profile) + notify.append(mentioned) + + # notify new mentioned users + if notify: + notes_signal.send(comment_actions, comment=comment, mentioned=notify) + + response.update({ + 'success': True, + 'comment': ApiCommentSerializer(comment).data + }) + + else: + res_status = status.HTTP_400_BAD_REQUEST + response.update({ + 'success': False, + 'message': 'Missing data \'comment\'.' + }) + + # delete a comment + elif request.method == 'DELETE': + if request.user == comment.user or request.user == comment.note.user: + comment.delete() + res_status = status.HTTP_200_OK + response.update({'success': True}) + + return Response(response, status=res_status)