Skip to content

Commit

Permalink
Comments v2 get
Browse files Browse the repository at this point in the history
  • Loading branch information
muremwa committed Dec 18, 2022
1 parent e47bb99 commit 0562418
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
5 changes: 3 additions & 2 deletions api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
Expand Down
18 changes: 11 additions & 7 deletions api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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<int:note_id>/', 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('<int:pk>/comments/', views.AllComments.as_view(), name='comments'),

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

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

# user/get/
Expand Down
14 changes: 12 additions & 2 deletions api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
})

0 comments on commit 0562418

Please # to comment.