Skip to content

Commit

Permalink
Merge pull request #478 from bounswe/feature/BE-post-return-val
Browse files Browse the repository at this point in the history
Refactor post return values
  • Loading branch information
m-erkam authored Dec 13, 2024
2 parents 70fef85 + cbe7407 commit d82e6b8
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -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
),
),
]
4 changes: 2 additions & 2 deletions backend/marketfeed/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
33 changes: 23 additions & 10 deletions backend/marketfeed/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -325,9 +328,19 @@ def destroy(self, request, *args, **kwargs):

@action(detail=False, methods=['get'], url_path='posts-by-user/(?P<user_id>[^/.]+)')
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):
Expand Down

0 comments on commit d82e6b8

Please sign in to comment.