This repository was archived by the owner on May 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
Emincan Yazgi Homework4 #100
Open
ecanyazgi
wants to merge
8
commits into
upy:main
Choose a base branch
from
ecanyazgi:EmincanYazgi-homework4
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7fa8e7b
'Migration-corrections-made'
ecanyazgi 65e9d61
Merge branch 'main' of https://github.com/ecanyazgi/bootcamp
ecanyazgi ca6bc62
migrations sorted
ecanyazgi ae4a645
Merge branch 'main' of https://github.com/ecanyazgi/bootcamp
ecanyazgi 6eff6cb
migrations file added because a pulling error
ecanyazgi 9efecdb
register endpoint created
ecanyazgi 1d71119
password_repeat field added
ecanyazgi d86ced9
add to basket and remove from basket actions are added
ecanyazgi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # Generated by Django 3.2.9 on 2021-12-14 19:34 | ||
|
|
||
| from django.conf import settings | ||
| from django.db import migrations, models | ||
| import django.db.models.deletion | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| initial = True | ||
|
|
||
| dependencies = [ | ||
| migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
| ('baskets', '0001_initial'), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AddField( | ||
| model_name='basket', | ||
| name='customer', | ||
| field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL, verbose_name='Customer'), | ||
| ), | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,12 @@ | ||
| 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 baskets.serializers import BasketItemSerializer, BasketSerializer, BasketItemDetailedSerializer, \ | ||
| BasketDetailedSerializer,BasketPostSerializer | ||
| from core.mixins import DetailedViewSetMixin | ||
| from products.models import Product | ||
|
|
||
|
|
||
| class BasketItemViewSet(DetailedViewSetMixin, viewsets.ModelViewSet): | ||
|
|
@@ -22,5 +25,88 @@ class BasketViewSet(DetailedViewSetMixin, viewsets.ModelViewSet): | |
| filterset_class = BasketFilter | ||
| serializer_action_classes = { | ||
| "detailed_list": BasketDetailedSerializer, | ||
| "detailed": BasketDetailedSerializer, | ||
| "detailed": BasketPostSerializer, | ||
| "add_to_basket": BasketPostSerializer, | ||
| "remove_from_basket": BasketPostSerializer | ||
| } | ||
|
|
||
| def get_queryset(self): | ||
| queryset = super().get_queryset() | ||
| user = self.request.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 kullanici durumu soz konusu ise burada problem olusuyor sanki? |
||
| return queryset.filter(customer=user) | ||
|
|
||
| @action(detail=True, methods=['post', 'put']) | ||
| def add_to_basket(self, request, pk=None): | ||
|
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. kayit ekleme/guncelleme, valitation islemleri serializer tarafinda yapilmasi daha iyi olacaktir. |
||
| """Add an item to a user's basket. | ||
| Adding to basket is disallowed if there is not enough inventory for the | ||
| product available. If there is, the quantity is increased on an existing | ||
| basket item or a new basket item is created with that quantity and added | ||
| to the basket. | ||
| """ | ||
| basket = self.get_object() | ||
|
|
||
| try: | ||
| product = Product.objects.get( | ||
| pk=request.data['items'][0]['product'] | ||
| ) | ||
| quantity = int(request.data['items'][0]['quantity']) | ||
| price = request.data['items'][0]['price'] | ||
| except Exception as ex: | ||
| print(ex) | ||
| return Response('Required fields must be filled') | ||
| """ | ||
| before adding the item to the basket, product's stock quantity is checked | ||
| """ | ||
|
|
||
| if product.stock.quantity <= 0 or product.stock.quantity - quantity < 0: | ||
| print("There is no more product available") | ||
| return Response('There is no more product available') | ||
| existing_basket_item = BasketItem.objects.filter(basket=basket, product=product).first() | ||
| """ | ||
| before creating a new basket item check if it is in the basket already | ||
| and if it is increase the quantity of that item | ||
| """ | ||
| if existing_basket_item: | ||
| existing_basket_item.quantity += quantity | ||
| existing_basket_item.save() | ||
| else: | ||
| new_basket_item = BasketItem(basket=basket, product=product, quantity=quantity, price=price) | ||
|
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. |
||
| new_basket_item.save() | ||
| serializer = BasketPostSerializer(basket) | ||
| return Response(serializer.data) | ||
|
|
||
| @action(detail=True, methods=['post', 'put']) | ||
| def remove_from_basket(self, request, pk=None): | ||
| """Remove an item from a user's basket. | ||
| Removing from the basket can be done with editing quantity for item. | ||
| Edited quantity is decreased from the existing quantity. | ||
| But after removing if items quantity is equal to zero item is deleted. | ||
| """ | ||
| basket = self.get_object() | ||
| try: | ||
| product = Product.objects.get( | ||
| pk=request.data['items'][0]['product'] | ||
| ) | ||
| quantity = int(request.data['items'][0]['quantity']) | ||
| price = request.data['items'][0]['price'] | ||
| except Exception as ex: | ||
| print(ex) | ||
| return Response('Required fields must be filled') | ||
|
|
||
| try: | ||
| basket_item = BasketItem.objects.get(basket=basket, product=product) | ||
| except Exception as ex: | ||
| print(ex) | ||
| return Response({'status': 'fail'}) | ||
|
|
||
| # if removing an item where the quantity of the item will become zero after process, remove the basket item | ||
| # completely otherwise decrease the quantity of the basket item | ||
| if basket_item.quantity - quantity <= 0: | ||
| basket_item.delete() | ||
| else: | ||
| basket_item.quantity -= quantity | ||
| basket_item.save() | ||
|
|
||
| # return the updated basket to indicate success | ||
| serializer = BasketPostSerializer(basket) | ||
| return Response(serializer.data) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
migration dosyalarina dokunmamaliyiz.