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
Homework3 #82
Open
mataliksamil
wants to merge
4
commits into
upy:main
Choose a base branch
from
mataliksamil: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
Homework3 #82
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a85bccb
Serializers, Viewsets and Filters for the models in Customers App
mataliksamil c5118b0
Serializers, Viewsets and Filters added for the models in Baskets App
mataliksamil e381cdb
Serializers, Viewsets and Filters added for Orders Appp, Pagination A…
mataliksamil 75947d5
minor changes after feedback
mataliksamil 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 |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| SECRET_KEY=g)8v11ythdn-p_^4+@z23_1&x=qzp^(-#py*8v$&4bnjb&=$a% | ||
| DEBUG=True | ||
| SECRET_KEY=secret_key | ||
| DATABASE_URL=psql://superuser_username:superuser_password@server_ip:server_port/db_name | ||
| DATABASE_URL=psql://bootcamp:123456@Localhost:5432/bootcampecommerce | ||
| ALLOWED_HOSTS=127.0.0.1,0.0.0.0 | ||
| TIME_ZONE=Europe/Istanbul | ||
| TIME_ZONE=Europe/Istanbul | ||
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,35 @@ | ||
| from django.db.models import Q | ||
| from django_filters import rest_framework as filters | ||
| from django.utils.translation import gettext_lazy as _ | ||
| from baskets.models import Basket, BasketItem | ||
| from products.models import Product | ||
|
|
||
|
|
||
| class BasketFilter(filters.FilterSet): | ||
| """ | ||
| Basket Filter | ||
| """ | ||
| customer = filters.CharFilter(label=_("Customer"), method="filter_name") | ||
|
|
||
| class Meta: | ||
| model = Basket | ||
| fields = ("customer", "status") | ||
|
|
||
| def filter_name(self, qs, name, value): | ||
| replaced_value = value.replace("Ş", "ş") | ||
| return qs.filter(Q(customer__first_name__icontains=replaced_value) | Q(customer__first_name__icontains=value)) | ||
|
|
||
| class BasketItemFilter(filters.FilterSet): | ||
| """ | ||
| Basket Item Filter | ||
| """ | ||
| product = filters.CharFilter(label=_("Product-Name"), method="filter_name") | ||
|
|
||
| class Meta: | ||
| model = BasketItem | ||
| fields = ("basket", "product", "quantity", "price") | ||
|
|
||
| def filter_name(self, qs, name, value): | ||
| replaced_value = value.replace("Ş", "ş") | ||
| return qs.filter(Q(product__name__icontains=replaced_value) | Q(product__name__icontains=value)) | ||
|
|
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,38 @@ | ||
| from rest_framework import serializers | ||
| from django.db.transaction import atomic | ||
| from baskets.models import Basket, BasketItem | ||
| from customers.serializers import CustomerSerializer | ||
| from products.serializers import ProductSerializer | ||
|
|
||
|
|
||
| class BasketSerializer(serializers.ModelSerializer): | ||
| """ | ||
| Basket Serializer | ||
| """ | ||
|
|
||
| class Meta: | ||
| model = Basket | ||
| fields = ("id", "customer", "status", "created_at", "modified_at") | ||
|
|
||
|
|
||
| class BasketItemSerializer(serializers.ModelSerializer): | ||
| product = ProductSerializer | ||
|
|
||
| class Meta: | ||
| model = BasketItem | ||
| fields = ("id", "basket", "product", "quantity", "price") | ||
|
|
||
|
|
||
| class BasketItemDetailedSerializer(BasketItemSerializer): | ||
| """ | ||
| Basket Item Detailed Serializer | ||
| """ | ||
| basket = BasketSerializer(many=False) | ||
|
|
||
|
|
||
| class BasketDetailedSerializer(BasketSerializer): | ||
| """ | ||
| Basket Detailed Serializer | ||
| """ | ||
| customer = CustomerSerializer(many=False) | ||
|
|
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,26 @@ | ||
| from django.shortcuts import render | ||
|
|
||
| from rest_framework import viewsets | ||
| from baskets.serializers import BasketSerializer, BasketItemSerializer, BasketItemDetailedSerializer, BasketDetailedSerializer | ||
| from baskets.models import Basket, BasketItem | ||
| from core.mixins import DetailedViewSetMixin | ||
| from baskets.filters import BasketFilter, BasketItemFilter | ||
| # 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,33 @@ | ||
| from django.db.models import Q | ||
| from django_filters import rest_framework as filters | ||
| from django.utils.translation import gettext_lazy as _ | ||
|
|
||
| from customers.models import Address, Customer | ||
|
|
||
|
|
||
| class AddressFilter(filters.FilterSet): | ||
| customer = filters.CharFilter(label=_("Customer"), method="filter_name") | ||
|
|
||
| class Meta: | ||
| model = Address | ||
| fields = ("customer", "name", "full_name", "phone", "zipcode", "city", | ||
| "district") | ||
|
|
||
| def filter_name(self, qs, name, value): | ||
| replaced_value = value.replace("Ş", "ş") | ||
| return qs.filter(Q(customer__first_name__icontains=replaced_value) | Q(customer__first_name__icontains=value)) | ||
|
|
||
| class CustomerFilter(filters.FilterSet): | ||
| """ | ||
| Customer Filter | ||
| Filters customers by their names | ||
| """ | ||
| first_name = filters.CharFilter(label=_("Name"), method="filter_name") | ||
|
|
||
| class Meta: | ||
| model = Customer | ||
| fields = ("id", "first_name", "last_name", "email") | ||
|
|
||
| def filter_name(self, qs, first_name, value): | ||
| replaced_value = value.replace("Ş", "ş") | ||
| return qs.filter(Q(first_name__icontains=replaced_value) | Q(first_name__icontains=value)) |
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,71 @@ | ||
| from rest_framework import serializers | ||
| from customers.models import Customer, City, Country, Address | ||
| from django.db.transaction import atomic | ||
|
|
||
|
|
||
|
|
||
| class CitySerializer(serializers.ModelSerializer): | ||
|
|
||
| class Meta: | ||
| fields = ("id", "name", "country",) | ||
| model = City | ||
|
|
||
| class CountrySerializer(serializers.ModelSerializer): | ||
|
|
||
| class Meta: | ||
| model = Country | ||
| fields = ("id", "name", ) | ||
|
|
||
| class CityDetailedSerializer(CitySerializer): | ||
| country = CountrySerializer(many= False) | ||
|
|
||
| class Meta: | ||
| model = City | ||
| fields = ("id", "name", "country",) | ||
|
|
||
| class CustomerSerializer(serializers.ModelSerializer): | ||
|
|
||
| class Meta: | ||
| model = Customer | ||
| fields = ("id", "first_name", "last_name", "email") | ||
|
|
||
| class AddressSerializer(serializers.ModelSerializer): | ||
|
|
||
| class Meta: | ||
| model = Address | ||
| fields = ("id", "customer", "name", "full_name", "line_1", "line_2", | ||
| "phone", "district", "zipcode", "city") | ||
|
|
||
| class AddressDetailedSerializer(AddressSerializer): | ||
| """ | ||
| Address Detailed Serializer | ||
| includes Customer and DetailedCity Serializers | ||
| """ | ||
| customer = CustomerSerializer(many=False) | ||
| city = CityDetailedSerializer(many= False) | ||
|
|
||
| class Meta: | ||
| model = Address | ||
| fields = ("id", "customer", "name", "full_name", "line_1", "line_2", | ||
| "phone", "district", "zipcode", "city") | ||
| @atomic() | ||
| def create(self, validated_data): | ||
| customer = validated_data.pop("customer", None) | ||
| address = super().create(validated_data) | ||
| if customer: | ||
| serializer = CustomerSerializer(data=customer, many=False) | ||
| serializer.is_valid(raise_exception=True) | ||
| serializer.save() | ||
| address.customer.add(*serializer.instance) | ||
|
|
||
| city = validated_data.pop("city", None) | ||
| address = super().create(validated_data) | ||
|
|
||
| if city: | ||
| serializer = CityDetailedSerializer(data=city, many=False) | ||
| serializer.is_valid(raise_exception=True) | ||
| serializer.save() | ||
| address.city.add(*serializer.instance) | ||
|
|
||
| return address | ||
|
|
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,36 @@ | ||
| from django.shortcuts import render | ||
|
|
||
| from rest_framework import viewsets | ||
| from customers.serializers import CustomerSerializer, CitySerializer, CountrySerializer,\ | ||
| AddressSerializer, AddressDetailedSerializer, CityDetailedSerializer | ||
| from customers.models import Customer, City, Country, Address | ||
| from customers.filters import AddressFilter, CustomerFilter | ||
| from core.mixins import DetailedViewSetMixin | ||
| # Create your views here. | ||
|
|
||
| class CustomerViewSet(viewsets.ModelViewSet): | ||
| queryset = Customer.objects.all() | ||
| serializer_class = CustomerSerializer | ||
| filterset_class = CustomerFilter | ||
|
|
||
| class CityViewSet(viewsets.ModelViewSet): | ||
| queryset = City.objects.all() | ||
| serializer_class = CitySerializer | ||
| serializer_action_classes = { | ||
| "detailed_list": CityDetailedSerializer, | ||
| "detailed": CityDetailedSerializer | ||
| } | ||
|
|
||
| class CountryViewSet(viewsets.ModelViewSet): | ||
| queryset = Country.objects.all() | ||
| serializer_class = CountrySerializer | ||
|
|
||
|
|
||
| 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,20 @@ | ||
| from django.db.models import Q | ||
| from django_filters import rest_framework as filters | ||
| from django.utils.translation import gettext_lazy as _ | ||
| from orders.models import Order, OrderItem | ||
|
|
||
|
|
||
|
|
||
| class OrderFilter(filters.FilterSet): | ||
| """ | ||
| Order Filter | ||
| """ | ||
| customer = filters.CharFilter(label=_("Customer"), method="filter_name") | ||
|
|
||
| class Meta: | ||
| model = Order | ||
| fields = ("customer", "basket", "status") | ||
|
|
||
| def filter_name(self, qs, name, value): | ||
| replaced_value = value.replace("Ş", "ş") | ||
| return qs.filter(Q(customer__first_name__icontains=replaced_value) | Q(customer__first_name__icontains=value)) |
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,71 @@ | ||
| from django.db.transaction import atomic | ||
| from rest_framework import serializers | ||
| from customers.serializers import CustomerSerializer | ||
| from orders.models import Order, OrderItem, OrderBankAccount, BillingAddress, \ | ||
| ShippingAddress | ||
| from products.serializers import ProductSerializer | ||
|
|
||
| class BillingAddressSerializer(serializers.ModelSerializer): | ||
|
|
||
| class Meta: | ||
| model = BillingAddress | ||
| fields = ("id", "full_name", "line_1", "line_2", "phone", | ||
| "district", "zipcode", "city") | ||
|
|
||
| class ShippingAddressSerializer(serializers.ModelSerializer): | ||
|
|
||
| class Meta: | ||
| model = ShippingAddress | ||
| fields = ("id", "full_name", "line_1", "line_2", "phone", | ||
| "district", "zipcode", "city") | ||
|
|
||
| class OrderBankAccountSerializer(serializers.ModelSerializer): | ||
|
|
||
| class Meta: | ||
| model = OrderBankAccount | ||
| fields = ("id", "name", "iban", "bank_name", "order") | ||
|
|
||
| class OrderSerializer(serializers.ModelSerializer): | ||
|
|
||
| class Meta: | ||
| model = Order | ||
| fields = ("customer", "basket", "status", "billing_address", | ||
| "shipping_address", "total_price","created_at","modified_at") | ||
|
|
||
| class OrderItemSerializer(serializers.ModelSerializer): | ||
|
|
||
| class Meta: | ||
| model = OrderItem | ||
| fields = ("order", "product", "price") | ||
|
|
||
| class OrderItemDetailedSerializer(serializers.ModelSerializer): | ||
| """ | ||
| Order Item Detailed Serializer | ||
| Order and Product Informations Included | ||
| """ | ||
| order = OrderSerializer(many=False) | ||
| product = ProductSerializer(many=True) | ||
|
|
||
| class Meta: | ||
| model = OrderItem | ||
| fields = ("order", "product", "price") | ||
|
|
||
| @atomic() | ||
| def create(self, validated_data): | ||
| order = validated_data.pop("order", None) | ||
| product = validated_data.pop("product", None) | ||
| order_item = super().create(validated_data) | ||
|
|
||
| if order: | ||
| serializer = OrderSerializer(data=order, many=True) | ||
| serializer.is_valid(raise_exception=True) | ||
| serializer.save() | ||
| order_item.order.add(*serializer.instance) | ||
|
|
||
| if product: | ||
| serializer = ProductSerializer(data=product, many=True) | ||
| serializer.is_valid(raise_exception=True) | ||
| serializer.save() | ||
| order_item.order.add(*serializer.instance) | ||
|
|
||
| return order |
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 dosyaya gerce bilgileri eklememeliyiz.