-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create api endpoints in blog, users app
- Loading branch information
1 parent
72bd958
commit 0f761f8
Showing
28 changed files
with
276 additions
and
8 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 |
---|---|---|
@@ -1,4 +1,16 @@ | ||
from rest_framework import routers | ||
|
||
from .blog import ( | ||
PostViewSet, | ||
PostCommentViewSet, | ||
PostCommentLikeViewSet, | ||
PostLikeViewSet, | ||
PostDislikeViewSet | ||
) | ||
|
||
router = routers.DefaultRouter() | ||
router.register("post", PostViewSet, basename="post") | ||
router.register("post_comment", PostCommentViewSet, basename="post-comment") | ||
router.register("post_comment_like", PostCommentLikeViewSet, basename="post-comment-like") | ||
router.register("post_like", PostLikeViewSet, basename="post-like") | ||
router.register("post_dislike", PostDislikeViewSet, basename="post-dislike") |
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 @@ | ||
from .views import * # noqa |
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,24 @@ | ||
from rest_framework import serializers | ||
|
||
from apps.blog.models import Post | ||
|
||
|
||
class PostSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Post | ||
fields = [ | ||
"id", | ||
"title", | ||
"get_absolute_url", | ||
"status", | ||
"description", | ||
"publisher_at", | ||
"is_active", | ||
"author", | ||
"like_count", | ||
"dislike_count", | ||
"comment_count", | ||
"watching", | ||
"created_at", | ||
"updated_at", | ||
] |
Empty file.
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,16 @@ | ||
from rest_framework import viewsets, permissions | ||
|
||
from apps.blog.models import Post | ||
from .serializer import PostSerializer | ||
|
||
|
||
class PostViewSet(viewsets.ModelViewSet): | ||
queryset = Post.published.all().order_by("-created_at") | ||
serializer_class = PostSerializer | ||
|
||
def get_permissions(self): | ||
if self.action in ['list', 'retrieve']: | ||
return [permissions.AllowAny()] | ||
return [permissions.IsAuthenticated()] | ||
|
||
__all__ = ("PostViewSet", ) |
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 @@ | ||
from .views import * # noqa |
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,25 @@ | ||
from rest_framework import serializers | ||
|
||
from apps.blog.models import PostComment, Post | ||
from apps.users.models import User | ||
|
||
|
||
class MiniPostCommentUser(serializers.ModelSerializer): | ||
class Meta: | ||
model = User | ||
fields = ["id", "username", "email", "created_at", "updated_at"] | ||
|
||
|
||
class MiniPostCommentPost(serializers.ModelSerializer): | ||
class Meta: | ||
model = Post | ||
fields = ["id", "title", "created_at", "updated_at"] | ||
|
||
|
||
class PostCommentSerializer(serializers.ModelSerializer): | ||
user = MiniPostCommentUser(read_only=True) | ||
comment = MiniPostCommentPost(read_only=True) | ||
|
||
class Meta: | ||
model = PostComment | ||
fields = ["id", "user", "comment", "created_at", "updated_at"] |
Empty file.
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,16 @@ | ||
from rest_framework import viewsets, permissions | ||
|
||
from apps.blog.models import PostComment | ||
from .serializers import PostCommentSerializer | ||
|
||
|
||
class PostCommentViewSet(viewsets.ModelViewSet): | ||
queryset = PostComment.objects.all().order_by("-created_at") | ||
serializer_class = PostCommentSerializer | ||
|
||
def get_permissions(self): | ||
if self.action in ['list', 'retrieve']: | ||
return [permissions.AllowAny()] | ||
return [permissions.IsAuthenticated()] | ||
|
||
__all__ = ("PostCommentViewSet", ) |
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 @@ | ||
from .views import * # noqa |
24 changes: 24 additions & 0 deletions
24
apps/blog/api_endpoints/blog/PostCommentLike/serializers.py
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,24 @@ | ||
from rest_framework import serializers | ||
|
||
from apps.blog.models import PostCommentLike | ||
from apps.users.models import User | ||
|
||
|
||
class MiniPostCommentLikeUserSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = User | ||
fields = ["id", "username", "created_at", "updated_at"] | ||
|
||
|
||
class MiniPostCommentLikePostCommentSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
fields = ["id", "post", "message", "created_at", "updated_at"] | ||
|
||
|
||
class PostCommentLikeSerializer(serializers.ModelSerializer): | ||
user = MiniPostCommentLikeUserSerializer(read_only=True) | ||
comment = MiniPostCommentLikePostCommentSerializer(read_only=True) | ||
|
||
class Meta: | ||
model = PostCommentLike | ||
fields = ["id", "user", "comment"] |
Empty file.
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,12 @@ | ||
from rest_framework import viewsets, permissions | ||
|
||
from apps.blog.models import PostCommentLike | ||
from .serializers import PostCommentLikeSerializer | ||
|
||
|
||
class PostCommentLikeViewSet(viewsets.ModelViewSet): | ||
queryset = PostCommentLike.objects.all().order_by("-id") | ||
serializer_class = PostCommentLikeSerializer | ||
permission_classes = [permissions.IsAuthenticated] | ||
|
||
__all__ = ("PostCommentLikeViewSet", ) |
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 @@ | ||
from .views import * # noqa |
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 serializers | ||
|
||
from apps.blog.models import PostDislike | ||
|
||
|
||
class PostDislikeSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = PostDislike | ||
fields = ["id", "user", "post"] |
Empty file.
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,32 @@ | ||
from django.shortcuts import get_object_or_404 | ||
|
||
from rest_framework import viewsets, response, status, permissions | ||
|
||
from apps.blog.models import PostDislike, PostLike, Post | ||
from .serializers import PostDislikeSerializer | ||
|
||
|
||
class PostDislikeViewSet(viewsets.ModelViewSet): | ||
queryset = PostDislike.objects.all() | ||
serializer_class = PostDislikeSerializer | ||
permission_classes = [permissions.IsAuthenticated] | ||
|
||
def create(self, request, *args, **kwargs): | ||
post_id = request.data.get("post") | ||
post = get_object_or_404(Post, id=post_id) | ||
user = request.user | ||
|
||
PostLike.objects.filter(post=post, user=user).delete() | ||
|
||
existing_dislike = PostDislike.objects.filter(post=post, user=user) | ||
if existing_dislike.exists(): | ||
existing_dislike.delete() | ||
return response.Response({"message": "Dislike removed"}, status=status.HTTP_200_OK) | ||
|
||
dislike = PostDislike.objects.create(post=post, user=user) | ||
serializer = self.get_serializer(dislike) | ||
|
||
return response.Response(serializer.data, status=status.HTTP_201_CREATED) | ||
|
||
|
||
__all__ = ("PostDislikeViewSet", ) |
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 @@ | ||
from .views import * # noqa |
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 serializers | ||
|
||
from apps.blog.models import PostLike | ||
|
||
|
||
class PostLikeSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = PostLike | ||
fields = ["id", "user", "post"] |
Empty file.
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,31 @@ | ||
from django.shortcuts import get_object_or_404 | ||
|
||
from rest_framework import viewsets, response, status, permissions | ||
|
||
from apps.blog.models import PostLike, PostDislike, Post | ||
from .serializers import PostLikeSerializer | ||
|
||
|
||
class PostLikeViewSet(viewsets.ModelViewSet): | ||
queryset = PostLike.objects.all() | ||
serializer_class = PostLikeSerializer | ||
permission_classes = [permissions.IsAuthenticated] | ||
|
||
def create(self, request, *args, **kwargs): | ||
post_id = request.data.get("post") | ||
post = get_object_or_404(Post, id=post_id) | ||
user = request.user | ||
|
||
PostDislike.objects.filter(post=post, user=user).delete() | ||
|
||
existing_like = PostLike.objects.filter(post=post, user=user) | ||
if existing_like.exists(): | ||
existing_like.delete() | ||
return response.Response({"message": "Like removed"}, status=status.HTTP_200_OK) | ||
|
||
like = PostLike.objects.create(post=post, user=user) | ||
serializer = self.get_serializer(like) | ||
|
||
return response.Response(serializer.data, status=status.HTTP_201_CREATED) | ||
|
||
__all__ = ("PostLikeViewSet", ) |
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,5 @@ | ||
from .Post import * # noqa | ||
from .PostComment import * # noqa | ||
from .PostCommentLike import * # noqa | ||
from .PostDislike import * # noqa | ||
from .PostLike import * # noqa |
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
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,10 @@ | ||
from rest_framework import permissions | ||
|
||
|
||
class IsOwnerPermission(permissions.BasePermission): | ||
""" | ||
If user is owner | ||
""" | ||
|
||
def has_object_permission(self, request, view, obj): | ||
return obj == request.user |
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
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 |
---|---|---|
@@ -1,11 +1,23 @@ | ||
from rest_framework import viewsets, permissions | ||
from rest_framework_simplejwt import authentication | ||
|
||
from apps.users.models import User | ||
from .serializers import UserSerializer | ||
|
||
from rest_framework import viewsets | ||
from .permissions import IsOwnerPermission | ||
|
||
|
||
class UserViewSet(viewsets.ModelViewSet): | ||
queryset = User.objects.all() | ||
serializer_class = UserSerializer | ||
authentication_classes = [authentication.JWTAuthentication] | ||
|
||
def get_permissions(self): | ||
if self.action == 'create': | ||
return [permissions.AllowAny()] | ||
|
||
if self.action in ['retrieve', 'update', 'partial_update', 'destroy']: | ||
return [permissions.IsAuthenticated(), permissions.IsAdminUser, IsOwnerPermission()] | ||
|
||
return [permissions.IsAuthenticated()] | ||
|
||
__all__ = ("UserViewSet", ) |
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
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