From 0562418c0ae199463b5845f6afc107f2162e865e Mon Sep 17 00:00:00 2001 From: Daniel Muremwa Mburu Date: Sun, 18 Dec 2022 21:36:12 +0300 Subject: [PATCH] Comments v2 get --- api/serializers.py | 5 +++-- api/urls.py | 18 +++++++++++------- api/views.py | 14 ++++++++++++-- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/api/serializers.py b/api/serializers.py index b5a7fd2..ea37263 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -76,10 +76,11 @@ class Meta: class ApiProfileSerializer(serializers.ModelSerializer): username = serializers.CharField(source='user.username') user_id = serializers.IntegerField(source='pk') + profile_url = serializers.URLField(source='get_absolute_url') class Meta: model = Profile - fields = ('username', 'user_id') + fields = ('username', 'user_id', 'profile_url') class ApiCommentSerializer(serializers.ModelSerializer): @@ -104,7 +105,7 @@ class Meta: class ApiNoteSerializer(serializers.ModelSerializer): note = serializers.CharField(source='title') user = ApiUserSerializer(many=False) - comments = ApiCommentSerializer(many=True) + comments = ApiCommentSerializer(many=True, source='comment_set') class Meta: model = Note diff --git a/api/urls.py b/api/urls.py index 2a3ad4c..be45951 100644 --- a/api/urls.py +++ b/api/urls.py @@ -6,25 +6,29 @@ app_name = "api" urlpatterns = [ - # notes/ + # api/notes/ path('notes/', views.NotesApi.as_view(), name="notes"), - # notes/note34/ + # api/notes/note34/ path('notes/note/', views.NoteAPi.as_view(), name="note"), - # create_user/ + # api/create_user/ path('create_user/', views.UserCreateApi.as_view(), name="create-user"), - # login/ + # api/login/ path('login/', obtain_auth_token, name="api-login"), - # create_note/ + # api/create_note/ path('create_note/', views.NoteCreationApi.as_view(), name="create-note"), - # comments/ + # TODO: DEPRACATE + # api/comments/ path('/comments/', views.AllComments.as_view(), name='comments'), - # comments/4/actions/ + # api/v2/33/comments/ + path('v2//comments/', views.AllCommentsV2.as_view(), name='comments-v2'), + + # api/comments/4/actions/ path('comment//actions/', views.comment_actions, name='comment-actions'), # user/get/ diff --git a/api/views.py b/api/views.py index 51a5951..2e67618 100644 --- a/api/views.py +++ b/api/views.py @@ -9,7 +9,7 @@ from rest_framework import status from django.http import Http404 -from .serializers import NotesSerializer, NoteSerializer, UserSerializer +from .serializers import NotesSerializer, NoteSerializer, UserSerializer, ApiUserSerializer, ApiNoteSerializer from notes.views import CommentProcessor, notes_signal from notes.models import Note, Comment @@ -136,7 +136,7 @@ def post(self, *args, **kwargs): @api_view(['POST']) def comment_actions(request, pk): - comment = comment = get_object_or_404(Comment, pk=pk) + comment = get_object_or_404(Comment, pk=pk) response = Response(status=status.HTTP_403_FORBIDDEN) if 'HTTP_X_HTTP_METHOD_OVERRIDE' in request.META: @@ -195,3 +195,13 @@ def get_user(request): return render(request, 'notes/user_not_found.html', {'failed_user': username_query}) return redirect(reverse('base_account:foreign-user', kwargs={'user_id': str(ava_users[0].pk)})) + + +class AllCommentsV2(views.APIView, CommentProcessor): + + def get(self, *args, **kwargs): + note = Note.objects.get(pk=kwargs.get('note_pk')) + return Response({ + 'current_user': ApiUserSerializer(self.request.user).data, + 'notes_info': ApiNoteSerializer(note).data + })