-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: introduce model for post reaction * feat: add post in PostReaction * feat: add missing related name * feat: post reactions retreive and admin registery * feat: post reaction create/update view
- Loading branch information
1 parent
6611cb6
commit 7523ce2
Showing
9 changed files
with
143 additions
and
10 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,7 +1,8 @@ | ||
from django.contrib import admin | ||
|
||
from .models import Comment, Follower, Post | ||
from .models import Comment, Follower, Post, PostReaction | ||
|
||
admin.site.register(Comment) | ||
admin.site.register(Follower) | ||
admin.site.register(Post) | ||
admin.site.register(PostReaction) |
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,51 @@ | ||
# Generated by Django 4.2.3 on 2024-08-18 13:48 | ||
|
||
import django.db.models.deletion | ||
from django.conf import settings | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
("social", "0005_follower_follower_unique_follower_following"), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="PostReaction", | ||
fields=[ | ||
( | ||
"id", | ||
models.BigAutoField( | ||
auto_created=True, | ||
primary_key=True, | ||
serialize=False, | ||
verbose_name="ID", | ||
), | ||
), | ||
("created_date", models.DateTimeField(auto_now_add=True)), | ||
("modified_date", models.DateTimeField(auto_now=True)), | ||
("reaction", models.CharField(max_length=10)), | ||
( | ||
"post", | ||
models.ForeignKey( | ||
on_delete=django.db.models.deletion.CASCADE, | ||
related_name="user_reactions", | ||
to="social.post", | ||
), | ||
), | ||
( | ||
"user", | ||
models.ForeignKey( | ||
on_delete=django.db.models.deletion.CASCADE, | ||
to=settings.AUTH_USER_MODEL, | ||
), | ||
), | ||
], | ||
options={ | ||
"abstract": False, | ||
}, | ||
), | ||
] |
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,3 +1,4 @@ | ||
from .comment import Comment # noqa: F401 | ||
from .follower import Follower # noqa: F401 | ||
from .post import Post # noqa: F401 | ||
from .post_reaction import PostReaction # noqa: F401 |
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,13 @@ | ||
from django.db import models | ||
|
||
from thenewboston.general.models import CreatedModified | ||
|
||
|
||
class PostReaction(CreatedModified): | ||
user = models.ForeignKey('users.User', on_delete=models.CASCADE) | ||
post = models.ForeignKey('social.Post', related_name='user_reactions', on_delete=models.CASCADE) | ||
|
||
reaction = models.CharField(max_length=10) | ||
|
||
def __str__(self): | ||
return f'{self.user}-{self.reaction}' |
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,32 @@ | ||
from rest_framework import serializers | ||
|
||
from thenewboston.users.serializers.user import UserReadSerializer | ||
|
||
from ..models import PostReaction | ||
|
||
|
||
class PostReactionsReadSerializer(serializers.ModelSerializer): | ||
user = UserReadSerializer(read_only=True) | ||
|
||
class Meta: | ||
model = PostReaction | ||
fields = ('reaction', 'user') | ||
read_only_fields = ( | ||
'user', | ||
'reaction', | ||
) | ||
|
||
|
||
class PostReactionCreateUpdateSerializer(serializers.ModelSerializer): | ||
|
||
class Meta: | ||
model = PostReaction | ||
fields = ['post', 'reaction'] | ||
|
||
def create(self, validated_data): | ||
post = validated_data.get('post') | ||
reaction = validated_data.get('reaction') | ||
user = self.context['request'].user | ||
|
||
post_reaction, _ = PostReaction.objects.update_or_create(user=user, post=post, defaults={'reaction': reaction}) | ||
return post_reaction |
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,12 +1,17 @@ | ||
from django.urls import include, path | ||
from rest_framework.routers import SimpleRouter | ||
|
||
from .views.comment import CommentViewSet | ||
from .views.follower import FollowerViewSet | ||
from .views.post import PostViewSet | ||
from .views.post_reaction import PostReactionCreateUpdateView | ||
|
||
router = SimpleRouter(trailing_slash=False) | ||
router.register('comments', CommentViewSet) | ||
router.register('followers', FollowerViewSet) | ||
router.register('posts', PostViewSet) | ||
|
||
urlpatterns = router.urls | ||
urlpatterns = [ | ||
path('', include(router.urls)), | ||
path('post_reaction/', PostReactionCreateUpdateView.as_view(), name='post-reaction-create-update'), | ||
] |
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,17 @@ | ||
from rest_framework import status | ||
from rest_framework.permissions import IsAuthenticated | ||
from rest_framework.response import Response | ||
from rest_framework.views import APIView | ||
|
||
from ..serializers.post_reaction import PostReactionCreateUpdateSerializer | ||
|
||
|
||
class PostReactionCreateUpdateView(APIView): | ||
permission_classes = [IsAuthenticated] | ||
|
||
def post(self, request, *args, **kwargs): | ||
serializer = PostReactionCreateUpdateSerializer(data=request.data, context={'request': request}) | ||
serializer.is_valid(raise_exception=True) | ||
serializer.save() | ||
|
||
return Response(status=status.HTTP_200_OK) |