-
Notifications
You must be signed in to change notification settings - Fork 30
Eylultuncel homework 4 #98
base: main
Are you sure you want to change the base?
Changes from all commits
3bff20f
851428d
b67c189
33168c9
39b5dc6
65d3c2d
7360d92
93cedc1
ef080f4
dcb27fd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,14 @@ | ||
| from django.core.exceptions import ObjectDoesNotExist | ||
| from django.db.transaction import atomic | ||
| from rest_framework import viewsets | ||
| from rest_framework.decorators import action | ||
| from rest_framework.response import Response | ||
|
|
||
| from baskets.filters import BasketItemFilter, BasketFilter | ||
| from baskets.models import BasketItem, Basket | ||
| from baskets.serializers import BasketItemSerializer, BasketSerializer, BasketItemDetailedSerializer, BasketDetailedSerializer | ||
| from core.mixins import DetailedViewSetMixin | ||
| from products.models import Product | ||
|
|
||
|
|
||
| class BasketItemViewSet(DetailedViewSetMixin, viewsets.ModelViewSet): | ||
|
|
@@ -15,12 +20,48 @@ class BasketItemViewSet(DetailedViewSetMixin, viewsets.ModelViewSet): | |
| "detailed": BasketItemDetailedSerializer, | ||
| } | ||
|
|
||
| def get_queryset(self): | ||
| queryset = super().get_queryset() | ||
| user_id = self.request.user.id | ||
| return queryset.filter(basket__customer__id=user_id) | ||
|
|
||
|
|
||
| class BasketViewSet(DetailedViewSetMixin, viewsets.ModelViewSet): | ||
| http_method_names = ["get", "delete"] | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. basketi kullaniciya sildirmek guzel bir fikir olmayabilir. |
||
| queryset = Basket.objects.all() | ||
| serializer_class = BasketSerializer | ||
| filterset_class = BasketFilter | ||
| serializer_action_classes = { | ||
| "detailed_list": BasketDetailedSerializer, | ||
| "detailed": BasketDetailedSerializer, | ||
| "add_to_basket": BasketItemSerializer, | ||
| } | ||
|
|
||
| def get_queryset(self): | ||
| queryset = super().get_queryset() | ||
| user = self.request.user | ||
| return queryset.filter(customer=user) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ayni sekilde login olmamis bir kullanici yoksa problem olusuyor sanki. |
||
|
|
||
| @atomic() | ||
| @action(detail=True, methods=['post'], http_method_names=['post']) | ||
| def add_to_basket(self, request, pk=None): | ||
| user_id = self.request.user.id | ||
| product = Product.objects.get(id=request.data["product"]) | ||
| quantity = request.data["quantity"] | ||
| price = product.price | ||
| try: | ||
| basket = Basket.objects.get(customer__id=user_id, status="open") | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. model objelerini kullanmak yerine serializerlari kullanmak daha iyi fikir. |
||
| except ObjectDoesNotExist: | ||
| basket = Basket.objects.create(customer_id=user_id, status="open") | ||
|
|
||
| try: | ||
| basket_item = BasketItem.objects.get(basket__customer__id=user_id, product=product) | ||
| basket_item.quantity += int(quantity) | ||
| except ObjectDoesNotExist: | ||
| basket_item = BasketItem.objects.create(basket=basket, product=product, quantity=int(quantity), price=price) | ||
|
|
||
| basket.save() | ||
| basket_item.save() | ||
|
|
||
| serializer = BasketItemSerializer(basket_item) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. basketviewseti basket'e ait bir serializer'i donmesi daha uyumlu olacaktir. |
||
| return Response(serializer.data) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
login olmamis bir user yoksa, burada bir problem olusuyor sanki?