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 6ab9649 commit 85ebde9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 21 deletions.
10 changes: 4 additions & 6 deletions apps/users/serializers/addition.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from products.models import Product
from rest_framework.serializers import ModelSerializer, Serializer

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

Expand All @@ -23,11 +24,8 @@ class Meta:


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

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

from products.models import Product
from users.models import Favorite, User
from users.models.addition import Basket
from users.serializers import (CodeCheckSerializer, RegisterSerializer,
UserModelSerializer)
from users.serializers.addition import (BasketModelSerializer,
FavoriteModelSerializer,
NoneSerializer)
NoneSerializer, ProductSerializer)
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 @@ -164,14 +166,13 @@ def add_favorite(self, request, pk):
"""
try:
favorite, created = Favorite.objects.get_or_create(customer=request.user, product_id=pk)
detail = {'success': True}
if created:
return Response(detail, status.HTTP_201_CREATED)
detail['message'] = 'Already added!'
return Response(detail)
serializer = FavoriteModelSerializer(favorite)
return Response(serializer.data)
except Exception as e:
print(e)
detail = {'message': "Sevimlilarga qo'shishda xatolik!"}
detail = {
'message': "Sevimlilarga qo'shishda xatolik!",
'error': f'{e}'
}
return Response(detail, status.HTTP_400_BAD_REQUEST)

@action(methods=['delete'], detail=True, permission_classes=(IsAuthenticated,), serializer_class=NoneSerializer,
Expand All @@ -191,20 +192,18 @@ def delete_favorite(self, request, pk):
return Response(detail, status.HTTP_400_BAD_REQUEST)

@action(methods=['get'], detail=False, permission_classes=(IsAuthenticated,),
serializer_class=FavoriteModelSerializer,
url_path='favorites')
serializer_class=ProductSerializer)
def favorites(self, request):
"""
favorite lar listini olish uchun
```
"""
favorites = request.user.favorites
# 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)
product_ids = favorites.values_list('product', flat=True)
query = Product.objects.all()
queryset = query.filter(id__in=product_ids)
serializer = self.get_serializer(queryset, 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 85ebde9

Please sign in to comment.