Skip to content

Commit

Permalink
comment actions update
Browse files Browse the repository at this point in the history
  • Loading branch information
muremwa committed Dec 18, 2022
1 parent 47c3f97 commit 7ed835d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
7 changes: 5 additions & 2 deletions api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,19 @@
# api/create_note/
path('create_note/', views.NoteCreationApi.as_view(), name="create-note"),

# TODO: DEPRACATE
# TODO: DEPRECATE
# api/comments/
path('<int:pk>/comments/', views.AllComments.as_view(), name='comments'),

# api/v2/33/comments/
path('v2/<int:note_pk>/comments/', views.AllCommentsV2.as_view(), name='comments-v2'),

# api/comments/4/actions/
# api/comment/4/actions/
path('comment/<int:pk>/actions/', views.comment_actions, name='comment-actions'),

# api/comment/4/actions/v2/
path('comment/<int:comment_pk>/actions/v2/', views.comment_actions_v2, name='comment-actions-v2'),

# user/get/
path('user/get/', views.get_user, name='get-user'),

Expand Down
55 changes: 55 additions & 0 deletions api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -68,6 +69,7 @@ def post(request):
})


# TODO: DEPRECATE
class AllComments(views.APIView, CommentProcessor):
@staticmethod
def shape(comment, user_id, host):
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

0 comments on commit 7ed835d

Please # to comment.