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
Naoiss Homework 3 (Bugra) #88
Open
Naoiss
wants to merge
2
commits into
upy:main
Choose a base branch
from
Naoiss:homework3
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
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| from django.utils.translation import gettext_lazy as _ | ||
| from django_filters import rest_framework as filters | ||
|
|
||
| from baskets.models import Basket, BasketItem | ||
| from customers.filters import CustomerFilter | ||
| from products.filters import ProductFilter | ||
|
|
||
|
|
||
| class BasketFilter(filters.FilterSet): | ||
| customer = CustomerFilter | ||
| status = filters.CharFilter(label=_("Status"), lookup_expr="icontains") | ||
|
|
||
| class Meta: | ||
| model = Basket | ||
| fields = ("customer", "status") | ||
|
|
||
|
|
||
| class BasketItemFilter(filters.FilterSet): | ||
| product = ProductFilter | ||
| basket = BasketFilter | ||
| price = filters.NumberFilter(label=_("Price"), lookup_expr="gte") | ||
|
|
||
| class Meta: | ||
| model = BasketItem | ||
| fields = ("product", "basket", "price") |
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,65 @@ | ||
| from django.db.transaction import atomic | ||
| from rest_framework import serializers | ||
|
|
||
| from baskets.models import Basket, BasketItem | ||
|
|
||
| from products.serializers import ProductSerializer, ProductDetailedSerializer | ||
| from customers.serializers import CustomerSerializer | ||
|
|
||
|
|
||
| class BasketSerializer(serializers.ModelSerializer): | ||
| class Meta: | ||
| model = Basket | ||
| read_only_fields = ['id', 'created_at', 'updated_at'] | ||
| fields = ("id", "customer", "status") | ||
|
|
||
|
|
||
| class BasketItemSerializer(serializers.ModelSerializer): | ||
| class Meta: | ||
| model = BasketItem | ||
| read_only_fields = ['id', 'created_at', 'updated_at'] | ||
| fields = ("id", "basket", "product", "quantity", "price") | ||
|
|
||
|
|
||
| class BasketDetailedSerializer(BasketSerializer): | ||
| customer = CustomerSerializer() | ||
| class Meta: | ||
| model = Basket | ||
| fields = ("customer", "status") | ||
|
|
||
| @atomic() | ||
| def create(self, validated_data): | ||
| customer = validated_data.pop("customer", None) | ||
| basket = super().create(validated_data) | ||
|
|
||
| if customer: | ||
| serializer = CustomerSerializer(data=customer) | ||
| serializer.is_valid(raise_exception=True) | ||
| serializer.save() | ||
| basket.customer.add(*serializer.instance) | ||
|
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. sepet eklerken customer eklenmesi cok kullanisli gozukmiyor sanki? |
||
|
|
||
| return basket | ||
|
|
||
|
|
||
| class BasketItemDetailedSerializer(BasketItemSerializer): | ||
| basket = BasketDetailedSerializer() | ||
| product = ProductDetailedSerializer() | ||
|
|
||
| @atomic() | ||
| def create(self, validated_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. genelde (ozel durumlar haric) detailed'lar ile create/update islemleri yapmamayi tercih ediyoruz. |
||
| basket = validated_data.pop("basket", None) | ||
| product = validated_data.pop("product", None) | ||
|
|
||
| basket_item = super().create(validated_data) | ||
|
|
||
| if basket: | ||
| serializer = BasketSerializer(data=basket) | ||
| serializer.is_valid(raise_exception=True) | ||
| serializer.save() | ||
| basket_item.basket.add(*serializer.instance) | ||
| if product: | ||
| serializer = ProductSerializer(data=product) | ||
| serializer.is_valid(raise_exception=True) | ||
| serializer.save() | ||
| basket_item.product.add(*serializer.instance) | ||
| return basket_item | ||
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,3 +1,25 @@ | ||
| from django.shortcuts import render | ||
| from rest_framework import viewsets | ||
| from baskets.filters import BasketFilter, BasketItemFilter | ||
| from baskets.models import Basket, BasketItem | ||
| from baskets.serializers import BasketSerializer, BasketDetailedSerializer, BasketItemSerializer, BasketItemDetailedSerializer | ||
| from core.mixins import DetailedViewSetMixin | ||
|
|
||
| # Create your views here. | ||
|
|
||
| class BasketViewSet(DetailedViewSetMixin, viewsets.ModelViewSet): | ||
| queryset = Basket.objects.all() | ||
| serializer_class = BasketSerializer | ||
| filterset_class = BasketFilter | ||
| serializer_action_classes = { | ||
| "detailed_list": BasketDetailedSerializer, | ||
| "detailed": BasketDetailedSerializer, | ||
| } | ||
|
|
||
|
|
||
| class BasketItemViewSet(DetailedViewSetMixin, viewsets.ModelViewSet): | ||
| queryset = BasketItem.objects.all() | ||
| serializer_class = BasketItemSerializer | ||
| filterset_class = BasketItemFilter | ||
| serializer_action_classes = { | ||
| "detailed_list": BasketItemDetailedSerializer, | ||
| "detailed": BasketItemDetailedSerializer, | ||
| } |
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,41 @@ | ||
| from django_filters import rest_framework as filters | ||
| from django.utils.translation import gettext_lazy as _ | ||
| from customers.models import Customer, City, Country, Address | ||
|
|
||
|
|
||
| class CustomerFilter(filters.FilterSet): | ||
| first_name = filters.CharFilter(label=_("First Name"), lookup_expr="icontains") | ||
| last_name = filters.CharFilter(label=_("Last Name"), lookup_expr="icontains") | ||
| email = filters.CharFilter(label=_("Email"), lookup_expr="icontains") | ||
| is_active = filters.BooleanFilter(label=_('Active'), field_name='is_active') | ||
|
|
||
| class Meta: | ||
| model = Customer | ||
| fields = ("first_name", "last_name", "email", "is_active") | ||
|
|
||
|
|
||
| class CountryFilter(filters.FilterSet): | ||
| name = filters.CharFilter(label=_("Country Name"), lookup_expr="icontains") | ||
|
|
||
| class Meta: | ||
| model = Country | ||
| fields = ("name",) | ||
|
|
||
|
|
||
| class CityFilter(filters.FilterSet): | ||
| country = CountryFilter | ||
| name = filters.CharFilter(label=_("City Name"), lookup_expr="icontains") | ||
|
|
||
| class Meta: | ||
| model = City | ||
| fields = ("name", "country",) | ||
|
|
||
|
|
||
| class AddressFilter(filters.FilterSet): | ||
| customer = CustomerFilter | ||
| name = filters.CharFilter(label=_("Address Name"), lookup_expr="icontains") | ||
| city = CityFilter | ||
|
|
||
| class Meta: | ||
| model = Address | ||
| fields = ("customer", "name", "city",) |
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,79 @@ | ||
| from django.db.transaction import atomic | ||
| from rest_framework import serializers | ||
| from customers.models import Customer, City, Address, Country | ||
|
|
||
| class CustomerSerializer(serializers.ModelSerializer): | ||
| class Meta: | ||
| model = Customer | ||
| fields = ("id", "first_name", "last_name", "email",) | ||
|
|
||
| class CitySerializer(serializers.ModelSerializer): | ||
| class Meta: | ||
| model = City | ||
| fields = ("id", "name", "country",) | ||
|
|
||
|
|
||
| class CityDetailedSerializer(serializers.ModelSerializer): | ||
| country = CountrySerializer() | ||
|
|
||
| class Meta: | ||
| model = City | ||
| fields = ("id", "name", "country",) | ||
|
|
||
| @atomic() | ||
| def create(self, validated_data): | ||
| country = validated_data.pop("country", None) | ||
| city = super().create(validated_data) | ||
| if country: | ||
| serializer = CountrySerializer(data=country) | ||
| serializer.is_valid(raise_exception=True) | ||
| serializer.save() | ||
| city.country.add(*serializer.instance) | ||
| return city | ||
|
|
||
|
|
||
| class AddressSerializer(serializers.ModelSerializer): | ||
| class Meta: | ||
| model = Address | ||
| fields = ("id", "customer", "name", "full_name", | ||
| "line_1", "line_2", "phone", "district", "zipcode", | ||
| "city", "is_default") | ||
|
|
||
|
|
||
| class AddressDetailedSerializer(serializers.ModelSerializer): | ||
| customer = CustomerSerializer() | ||
| city = CityDetailedSerializer() | ||
|
|
||
| class Meta: | ||
| model = Address | ||
| fields = ("id", "customer", "name", "full_name", | ||
| "line_1", "line_2", "phone", "district", "zipcode", | ||
| "city", "is_default") | ||
|
|
||
| @atomic() | ||
| def create(self, validated_data): | ||
| customer = validated_data.pop("customer", None) | ||
| city = validated_data.pop("city", None) | ||
|
|
||
| address = super().create(validated_data) | ||
|
|
||
| if customer: | ||
| serializer = CustomerSerializer(data=customer) | ||
| serializer.is_valid(raise_exception=True) | ||
| serializer.save() | ||
| address.customer.add(*serializer.instance) | ||
|
|
||
| address = super().create(validated_data) | ||
|
|
||
| if city: | ||
| serializer = CityDetailedSerializer(data=city) | ||
| serializer.is_valid(raise_exception=True) | ||
| serializer.save() | ||
| address.city.add(*serializer.instance) | ||
| return address | ||
|
|
||
|
|
||
| class CountrySerializer(serializers.ModelSerializer): | ||
| class Meta: | ||
| model = Country | ||
| fields = ("id", "name",) |
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,3 +1,38 @@ | ||
| from django.shortcuts import render | ||
| from rest_framework import viewsets | ||
|
|
||
| # Create your views here. | ||
| from core.mixins import DetailedViewSetMixin | ||
| from customers.filters import CustomerFilter, CityFilter, CountryFilter, AddressFilter | ||
| from customers.models import Customer, City, Country, Address | ||
| from customers.serializers import CustomerSerializer, CitySerializer, CountrySerializer, AddressSerializer, CityDetailedSerializer, AddressDetailedSerializer | ||
|
|
||
|
|
||
| class CustomerViewSet(viewsets.ModelViewSet): | ||
| queryset = Customer.objects.filter(is_active=True) | ||
| serializer_class = CustomerSerializer | ||
| filterset_class = CustomerFilter | ||
|
|
||
|
|
||
| class CityViewSet(DetailedViewSetMixin, viewsets.ModelViewSet): | ||
| queryset = City.objects.all() | ||
| serializer_class = CitySerializer | ||
| filterset_class = CityFilter | ||
| serializer_action_classes = { | ||
| "detailed_list": CityDetailedSerializer, | ||
| "detailed": CityDetailedSerializer, | ||
| } | ||
|
|
||
|
|
||
| class CountryViewSet(DetailedViewSetMixin, viewsets.ModelViewSet): | ||
| queryset = Country.objects.all() | ||
| serializer_class = CountrySerializer | ||
| filterset_class = CountryFilter | ||
|
|
||
|
|
||
| class AddressViewSet(DetailedViewSetMixin, viewsets.ModelViewSet): | ||
| queryset = Address.objects.all() | ||
| serializer_class = AddressSerializer | ||
| filterset_class = AddressFilter | ||
| serializer_action_classes = { | ||
| "detailed_list": AddressDetailedSerializer, | ||
| "detailed": AddressDetailedSerializer, | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| from django_filters import rest_framework as filters | ||
| from django.utils.translation import gettext_lazy as _ | ||
| from customers.filters import CustomerFilter, CityFilter | ||
| from baskets.filters import BasketFilter | ||
| from products.filters import ProductFilter | ||
| from orders.models import Order, OrderBankAccount, OrderItem, BillingAddress, ShippingAddress | ||
|
|
||
|
|
||
| class OrderFilter(filters.FilterSet): | ||
| customer = CustomerFilter | ||
| basket = BasketFilter | ||
| status = filters.CharFilter(label=_("Status"), lookup_expr="iexact") | ||
|
|
||
| class Meta: | ||
| model = Order | ||
| fields = ("customer", "basket", "status",) | ||
|
|
||
|
|
||
| class OrderItemFilter(filters.FilterSet): | ||
| order = OrderFilter | ||
| product = ProductFilter | ||
|
|
||
| class Meta: | ||
| model = OrderItem | ||
| fields = ("order", "product",) | ||
|
|
||
|
|
||
| class OrderBankAccountFilter(filters.FilterSet): | ||
| order = OrderFilter | ||
| name = filters.CharFilter(label=_("City Name"), lookup_expr="icontains") | ||
| iban = filters.CharFilter(label=_("IBAN"), lookup_expr="iexact") | ||
| bank_name = filters.CharFilter(label=_("Bank Name"), lookup_expr="icontains") | ||
|
|
||
| class Meta: | ||
| model = OrderBankAccount | ||
| fields = ("name", "iban", "bank_name", "order") | ||
|
|
||
|
|
||
| class BillingAddressFilter(filters.FilterSet): | ||
| full_name = filters.CharFilter(label=_("Full Name"), | ||
| lookup_expr="icontains") | ||
| phone = filters.CharFilter(label=_("Phone")) | ||
| district = filters.CharFilter(label=_("District"), lookup_expr="icontains") | ||
| zipcode = filters.CharFilter(label=_("Zipcode")) | ||
| city = CityFilter | ||
|
|
||
| class Meta: | ||
| model = BillingAddress | ||
| fields = ( | ||
| "full_name", "phone", "district", "zipcode", "city") | ||
|
|
||
|
|
||
| class ShippingAddressFilter(filters.FilterSet): | ||
| full_name = filters.CharFilter(label=_("Full Name"), | ||
| lookup_expr="icontains") | ||
| phone = filters.CharFilter(label=_("Phone")) | ||
| district = filters.CharFilter(label=_("District"), lookup_expr="icontains") | ||
| zipcode = filters.CharFilter(label=_("Zipcode")) | ||
| city = CityFilter | ||
|
|
||
| class Meta: | ||
| model = ShippingAddress | ||
| fields = ( | ||
| "full_name", "phone", "district", "zipcode", "city") |
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.
bu alanlar zaten read_only degil mi?