-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1e29110
commit 7d73fdd
Showing
4 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from django.shortcuts import get_object_or_404 | ||
from rest_framework import serializers | ||
|
||
from ...models import Comment | ||
|
||
|
||
class CommentSerializer(serializers.ModelSerializer): | ||
path = serializers.CharField(required=False) | ||
|
||
class Meta: | ||
model = Comment | ||
fields = '__all__' | ||
|
||
def create(self, validated_data): | ||
data = { | ||
'quibbler': validated_data.get('quibbler') | ||
or self.context['request'].user_profile, | ||
'content': validated_data['content'], | ||
} | ||
|
||
if path := validated_data.get('path'): | ||
parent_instance = get_object_or_404(Comment, path__match=path) | ||
comment_instance: Comment = Comment.objects.create_child( | ||
parent=parent_instance, **data | ||
) | ||
else: | ||
comment_instance: Comment = Comment.objects.create_child(**data) | ||
|
||
comment_instance.save() | ||
return comment_instance |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from rest_framework import routers | ||
|
||
from .viewsets import CommentViewSet | ||
|
||
router = routers.DefaultRouter() | ||
router.register(r'', CommentViewSet) | ||
|
||
urlpatterns = router.urls |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from rest_framework import viewsets | ||
|
||
from ...models import Comment | ||
from .serializers import CommentSerializer | ||
|
||
|
||
class CommentViewSet(viewsets.ModelViewSet): | ||
queryset = Comment.objects.all() | ||
serializer_class = CommentSerializer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters