Skip to content

Commit

Permalink
update some actions
Browse files Browse the repository at this point in the history
  • Loading branch information
khasanjon-dev committed Nov 21, 2023
1 parent 1d92c69 commit 801b2e8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
12 changes: 12 additions & 0 deletions apps/users/serializers/addition.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from rest_framework.serializers import Serializer, ModelSerializer

from products.models import Product
from users.models import Favorite
from users.models.addition import Basket


Expand All @@ -20,3 +21,14 @@ class BasketModelSerializer(ModelSerializer):
class Meta:
model = Basket
fields = ('id', 'quantity', 'product')


class FavoriteModelSerializer(ModelSerializer):
product = ProductSerializer(many=True, read_only=True)

class Meta:
model = Favorite
fields = (
'id',
'product',
)
19 changes: 8 additions & 11 deletions apps/users/views/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
from rest_framework.response import Response
from rest_framework.viewsets import GenericViewSet

from products.models import Product
from users.models import Favorite
from users.models import User
from users.models.addition import Basket
from users.serializers import (CodeCheckSerializer, RegisterSerializer,
UserModelSerializer)
from users.serializers.addition import BasketModelSerializer, NoneSerializer, ProductSerializer
from users.serializers.addition import BasketModelSerializer, NoneSerializer, FavoriteModelSerializer
from users.serializers.register import PhoneSerializer
from users.serializers.user import UserProfileSerializer, UserSerializer
from utils.send_code import send_code_phone
Expand Down Expand Up @@ -195,7 +194,8 @@ def delete_favorite(self, request, pk):
detail = {'message': "Sevimlilardan o'chirishda xatolik!"}
return Response(detail, status.HTTP_400_BAD_REQUEST)

@action(methods=['get'], detail=False, permission_classes=(IsAuthenticated,), serializer_class=ProductSerializer,
@action(methods=['get'], detail=False, permission_classes=(IsAuthenticated,),
serializer_class=FavoriteModelSerializer,
url_path='favorites')
def favorites(self, request):
"""
Expand All @@ -204,14 +204,11 @@ def favorites(self, request):
```
"""
favorites = request.user.favorites
favorites_ids = favorites.values_list('product', flat=True)
query = Product.objects.all()
queryset = query.filter(id__in=favorites_ids)
page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True)
return self.get_paginated_response(serializer.data)
serializer = self.get_serializer(queryset, many=True)
# page = self.paginate_queryset(queryset)
# if page is not None:
# serializer = self.get_serializer(page, many=True)
# return self.get_paginated_response(serializer.data)
serializer = self.get_serializer(favorites, many=True)
return Response(serializer.data)

@action(methods=['get'], detail=True, permission_classes=(IsAuthenticated,), serializer_class=NoneSerializer)
Expand Down
Binary file modified oqtepa.sqlite3
Binary file not shown.

0 comments on commit 801b2e8

Please sign in to comment.