diff --git a/backend/marketfeed/migrations/0008_post_disliked_by_alter_post_liked_by.py b/backend/marketfeed/migrations/0008_post_disliked_by_alter_post_liked_by.py new file mode 100644 index 00000000..c5654804 --- /dev/null +++ b/backend/marketfeed/migrations/0008_post_disliked_by_alter_post_liked_by.py @@ -0,0 +1,29 @@ +# Generated by Django 4.2 on 2024-12-13 18:40 + +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ("marketfeed", "0007_portfoliostock_quantity"), + ] + + operations = [ + migrations.AddField( + model_name="post", + name="disliked_by", + field=models.ManyToManyField( + blank=True, related_name="disliked_posts", to=settings.AUTH_USER_MODEL + ), + ), + migrations.AlterField( + model_name="post", + name="liked_by", + field=models.ManyToManyField( + blank=True, related_name="liked_posts", to=settings.AUTH_USER_MODEL + ), + ), + ] diff --git a/backend/marketfeed/serializers.py b/backend/marketfeed/serializers.py index 2de4bfe7..28763e94 100644 --- a/backend/marketfeed/serializers.py +++ b/backend/marketfeed/serializers.py @@ -75,7 +75,7 @@ class StockPatternSearchSerializer(serializers.Serializer): limit = serializers.IntegerField(required=False, default=10) class TagSerializer(serializers.ModelSerializer): - user_id = serializers.PrimaryKeyRelatedField(queryset=User.objects.all()) + user_id = serializers.PrimaryKeyRelatedField(read_only=True) class Meta: model = Tag @@ -152,7 +152,7 @@ def update(self, instance, validated_data): class CommentSerializer(serializers.ModelSerializer): - user_id = serializers.PrimaryKeyRelatedField(queryset=User.objects.all()) + user_id = serializers.PrimaryKeyRelatedField(read_only=True) post_id = serializers.PrimaryKeyRelatedField(queryset=Post.objects.all()) class Meta: diff --git a/backend/marketfeed/views.py b/backend/marketfeed/views.py index 9996c226..a4b4bfa2 100644 --- a/backend/marketfeed/views.py +++ b/backend/marketfeed/views.py @@ -164,7 +164,7 @@ def retrieve(self, request, pk=None): def create(self, request): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) - serializer.save() + serializer.save(user_id=request.user) return Response(serializer.data, status=status.HTTP_201_CREATED) def update(self, request, pk=None): @@ -222,9 +222,6 @@ def user_portfolios(self, request, user_id=None): class PortfolioStockViewSet(ViewSet): - """ - A viewset for adding and removing stocks from a portfolio. - """ permission_classes = [permissions.IsAuthenticated] serializer_class = PortfolioStockActionSerializer @@ -288,9 +285,15 @@ def get_permissions(self): return super().get_permissions() def list(self, request): - posts = self.get_queryset() - serializer = self.get_serializer(posts, many=True) - return Response(serializer.data) + queryset = self.paginate_queryset(self.get_queryset()) + data = [] + for post in queryset: + serializer = self.get_serializer(post) + serialized_data = serializer.data + serialized_data['tags'] = [{'id': tag.id, 'name': tag.name} for tag in post.tags.all()] + data.append(serialized_data) + + return self.get_paginated_response(data) def create(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) @@ -325,9 +328,19 @@ def destroy(self, request, *args, **kwargs): @action(detail=False, methods=['get'], url_path='posts-by-user/(?P[^/.]+)') def user_posts(self, request, user_id=None): - posts = self.queryset.filter(author=user_id) - serializer = self.get_serializer(posts, many=True) - return Response(serializer.data) + queryset = self.queryset.filter(author_id=user_id) + if not queryset.exists(): + return Response({'detail': 'No posts found for the specified user.'}, status=status.HTTP_404_NOT_FOUND) + + paginated_queryset = self.paginate_queryset(queryset) + data = [] + for post in paginated_queryset: + serializer = self.get_serializer(post) + serialized_data = serializer.data + serialized_data['tags'] = [{'id': tag.id, 'name': tag.name} for tag in post.tags.all()] + data.append(serialized_data) + + return self.get_paginated_response(data) class CommentViewSet(viewsets.ModelViewSet):