Skip to content

Commit

Permalink
refactor: change serializers class names and fix quiblet query_set
Browse files Browse the repository at this point in the history
  • Loading branch information
moonlitgrace committed Dec 16, 2024
1 parent 426d531 commit cc05bdf
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 39 deletions.
2 changes: 1 addition & 1 deletion backend/apps/comment/api/v1/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from ...models import CommentModel


class CommentSerializer(serializers.ModelSerializer):
class CommentModelSerializer(serializers.ModelSerializer):
path = serializers.CharField(required=False)

class Meta:
Expand Down
4 changes: 2 additions & 2 deletions backend/apps/comment/api/v1/viewsets.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from common.api.bases.viewsets import UpdateRetrieveDestroyViewSet

from ...models import CommentModel
from .serializers import CommentSerializer
from .serializers import CommentModelSerializer


class CommentModelViewSet(UpdateRetrieveDestroyViewSet):
queryset = CommentModel.objects.all()
serializer_class = CommentSerializer
serializer_class = CommentModelSerializer

def perform_destroy(self, instance):
CommentModel.objects.soft_delete(instance) # pyright: ignore
Expand Down
13 changes: 8 additions & 5 deletions backend/apps/quib/api/v1/serializers.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
from rest_framework import serializers

from apps.quiblet.api.v1.serializers import QuibletSerializer, QuibletSlimSerializer
from apps.quiblet.api.v1.serializers import (
QuibletModelSerializer,
QuibletSlimModelSerializer,
)

from ...models import QuibModel


class QuibSerializer(serializers.ModelSerializer):
quiblet = QuibletSerializer(read_only=True)
class QuibModelSerializer(serializers.ModelSerializer):
quiblet = QuibletModelSerializer(read_only=True)

class Meta:
model = QuibModel
fields = '__all__'


class QuibSlimSerializer(serializers.ModelSerializer):
quiblet = QuibletSlimSerializer(read_only=True)
class QuibSlimModelSerializer(serializers.ModelSerializer):
quiblet = QuibletSlimModelSerializer(read_only=True)

class Meta:
model = QuibModel
Expand Down
14 changes: 7 additions & 7 deletions backend/apps/quib/api/v1/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,34 @@
from rest_framework import response, status, viewsets
from rest_framework.decorators import action

from apps.comment.api.v1.serializers import CommentSerializer
from apps.comment.api.v1.serializers import CommentModelSerializer

from ...models import QuibModel
from .serializers import QuibSerializer, QuibSlimSerializer
from .serializers import QuibModelSerializer, QuibSlimModelSerializer


class QuibModelViewSet(viewsets.ModelViewSet):
queryset = QuibModel.objects.all()

def get_serializer_class(self): # pyright: ignore
if self.action == 'list':
return QuibSlimSerializer
return QuibSlimModelSerializer
# if custom action: 'comment'
if self.action == 'comments':
return CommentSerializer
return QuibSerializer
return CommentModelSerializer
return QuibModelSerializer

@action(detail=True, methods=[HTTPMethod.GET, HTTPMethod.POST])
def comments(self, request, pk=None):
quib_instance = get_object_or_404(QuibModel, pk=pk)

if request.method == HTTPMethod.GET:
comments = quib_instance.comments.all()
serializer = CommentSerializer(comments, many=True)
serializer = CommentModelSerializer(comments, many=True)

return response.Response(serializer.data, status=status.HTTP_200_OK)

serializer = CommentSerializer(data=request.data)
serializer = CommentModelSerializer(data=request.data)
serializer.is_valid(raise_exception=True)

comment_instance = serializer.save()
Expand Down
4 changes: 2 additions & 2 deletions backend/apps/quiblet/api/v1/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from ...models import QuibletModel


class QuibletSerializer(serializers.ModelSerializer):
class QuibletModelSerializer(serializers.ModelSerializer):
class Meta:
model = QuibletModel
fields = '__all__'
Expand All @@ -17,7 +17,7 @@ def validate_name(self, name):
return name


class QuibletSlimSerializer(serializers.ModelSerializer):
class QuibletSlimModelSerializer(serializers.ModelSerializer):
class Meta:
model = QuibletModel
fields = ('name', 'avatar')
Expand Down
6 changes: 3 additions & 3 deletions backend/apps/quiblet/api/v1/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@
from common.patches.request import PatchedHttpRequest

from ...models import QuibletModel
from .serializers import QuibletExistsSerializer, QuibletSerializer
from .serializers import QuibletExistsSerializer, QuibletModelSerializer


class QuibletModelViewSet(viewsets.ModelViewSet):
queryset = QuibletModel.objects.all()
serializer_class = QuibletSerializer
serializer_class = QuibletModelSerializer
lookup_field = 'name'

def get_queryset(self) -> QuerySet[QuibletModel]: # pyright: ignore
return super().get_queryset()

def get_object(self) -> QuibletModel: # pyright: ignore
qs = self.get_queryset()
filter_kwargs = {f'{self.lookup_field}': self.kwargs[self.lookup_field]}
filter_kwargs = {f'{self.lookup_field}__iexact': self.kwargs[self.lookup_field]}
obj = qs.filter(**filter_kwargs).first()

if not obj:
Expand Down
22 changes: 11 additions & 11 deletions backend/apps/user/api/v1/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from ...models import ProfileModel, User


class UserSerializer(serializers.ModelSerializer):
class UserModelSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('id', 'email', 'password', 'date_joined')
Expand All @@ -15,19 +15,19 @@ def create(self, validated_data):
return user


class ProfileSerializer(serializers.ModelSerializer):
user = UserSerializer(read_only=True)

class Meta:
model = ProfileModel
fields = '__all__'


class AuthSerializer(serializers.ModelSerializer):
class UserAuthModelSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('email', 'password')


class AuthTokenResponseSerializer(serializers.Serializer):
class UserAuthTokenSerializer(serializers.Serializer):
token = serializers.CharField()


class ProfileModelSerializer(serializers.ModelSerializer):
user = UserModelSerializer(read_only=True)

class Meta:
model = ProfileModel
fields = '__all__'
14 changes: 9 additions & 5 deletions backend/apps/user/api/v1/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
from common.api.exceptions import ServerError
from common.api.serializers import DetailResponseSerializer

from .serializers import AuthSerializer, AuthTokenResponseSerializer, ProfileSerializer
from .serializers import (
ProfileModelSerializer,
UserAuthModelSerializer,
UserAuthTokenSerializer,
)


class LoginAPIView(views.APIView):
Expand All @@ -18,9 +22,9 @@ class LoginAPIView(views.APIView):
and issues a token upon successful login.
"""

serializer_class = AuthSerializer
serializer_class = UserAuthModelSerializer

@extend_schema(responses=AuthTokenResponseSerializer)
@extend_schema(responses=UserAuthTokenSerializer)
def post(self, request, format=None):
user = authenticate(
email=request.data.get('email'), password=request.data.get('password')
Expand Down Expand Up @@ -54,7 +58,7 @@ class RegisterAPIView(generics.CreateAPIView):
View to handle registering of new users.
"""

serializer_class = AuthSerializer
serializer_class = UserAuthModelSerializer


class MeAPIView(views.APIView):
Expand All @@ -68,7 +72,7 @@ class MeAPIView(views.APIView):
"""

permission_classes = (permissions.IsAuthenticated,)
serializer_class = ProfileSerializer
serializer_class = ProfileModelSerializer

def get(self, request):
if request.user_profile:
Expand Down
6 changes: 3 additions & 3 deletions backend/apps/user/api/v1/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from rest_framework import exceptions, filters, permissions, viewsets

from ...models import ProfileModel
from .serializers import ProfileSerializer
from .serializers import ProfileModelSerializer


class ProfileModelReadOnlyViewSet(viewsets.ReadOnlyModelViewSet):
Expand All @@ -14,7 +14,7 @@ class ProfileModelReadOnlyViewSet(viewsets.ReadOnlyModelViewSet):
"""

queryset = ProfileModel.objects.all()
serializer_class = ProfileSerializer
serializer_class = ProfileModelSerializer
filter_backends = (filters.SearchFilter,)
search_fields = ('username',)

Expand All @@ -28,7 +28,7 @@ class MyProfilesModelViewSet(viewsets.ModelViewSet):
"""

permission_classes = (permissions.IsAuthenticated,)
serializer_class = ProfileSerializer
serializer_class = ProfileModelSerializer

def get_queryset(self): # pyright: ignore
"""
Expand Down

0 comments on commit cc05bdf

Please sign in to comment.