-
Notifications
You must be signed in to change notification settings - Fork 30
Gormezoglu homework 4 #107
base: main
Are you sure you want to change the base?
Changes from all commits
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,12 +1,15 @@ | ||
| from rest_framework import viewsets | ||
| from rest_framework import viewsets, status | ||
| from rest_framework.response import Response | ||
| from rest_framework.decorators import action | ||
|
|
||
| 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 | ||
|
|
||
|
|
||
| class BasketItemViewSet(DetailedViewSetMixin, viewsets.ModelViewSet): | ||
| class BasketItemViewSet(DetailedViewSetMixin,viewsets.ModelViewSet): | ||
| http_method_names = ["get"] | ||
| queryset = BasketItem.objects.all() | ||
| serializer_class = BasketItemSerializer | ||
| filterset_class = BasketItemFilter | ||
|
|
@@ -15,12 +18,34 @@ class BasketItemViewSet(DetailedViewSetMixin, viewsets.ModelViewSet): | |
| "detailed": BasketItemDetailedSerializer, | ||
| } | ||
|
|
||
| def get_queryset(self): | ||
| queryset = super().get_queryset() | ||
| user = self.request.user.id | ||
| return queryset.filter(basket__customer__id=user) | ||
|
|
||
|
|
||
| class BasketViewSet(DetailedViewSetMixin, viewsets.ModelViewSet): | ||
| permission_classes = () | ||
| http_method_names = ["get", "delete", "post"] | ||
|
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 degil sanki. |
||
| queryset = Basket.objects.all() | ||
| serializer_class = BasketSerializer | ||
| filterset_class = BasketFilter | ||
| serializer_action_classes = { | ||
| "detailed_list": BasketDetailedSerializer, | ||
| "detailed": BasketDetailedSerializer, | ||
| "create_product": BasketItemSerializer | ||
|
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. create_product seklinde bir action tanimlamamasiz. |
||
| } | ||
|
|
||
| def get_queryset(self): | ||
| # customer can only see own basket | ||
| 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. login olmamis bir customer varsa sanki problem var. |
||
|
|
||
| @action(detail=True, methods=['post']) | ||
| def add_product(self, request, pk=None): | ||
| serializer = BasketItemSerializer(data=request.data) | ||
|
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. price bilgisini kullanicidan almak guzel gozukmuyor. |
||
| if serializer.is_valid(): | ||
| serializer.save() | ||
| return Response(serializer.data, status=status.HTTP_201_CREATED) | ||
|
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. basketviewset baskete ait bir serializer verisi donse daha guzel olur. |
||
| return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) | ||
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 customer varsa sanki problem var.