-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
simplify products and add orders model (#134)
* simplify product model * simplify products and add orders
- Loading branch information
1 parent
155d0e0
commit 392618e
Showing
13 changed files
with
131 additions
and
28 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -47,6 +47,7 @@ | |
'products', | ||
'events', | ||
'tickets', | ||
'orders' | ||
] | ||
|
||
REST_FRAMEWORK = { | ||
|
This file contains 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 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,5 @@ | ||
from django.contrib import admin | ||
|
||
from .models import Order | ||
|
||
admin.site.register(Order) |
This file contains 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,14 @@ | ||
from django.db import models | ||
from products.models import Product | ||
from accounts.models import Account | ||
|
||
class Order(models.Model): | ||
product = models.ForeignKey(Product, on_delete=models.CASCADE) | ||
quantity = models.IntegerField() | ||
buyer = models.ForeignKey(Account, on_delete=models.PROTECT) | ||
total = models.DecimalField(max_digits=10, decimal_places=2) | ||
created_at = models.DateTimeField(auto_now_add=True) | ||
updated_at = models.DateTimeField(auto_now=True) | ||
|
||
def __str__(self): | ||
return f"Order #{self.id} - {self.product.name}" |
This file contains 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,5 @@ | ||
from rest_framework.permissions import BasePermission | ||
|
||
class IsBuyerOrAdmin(BasePermission): | ||
def has_object_permission(self, request, view, obj): | ||
return obj.buyer == request.user or request.user.is_staff |
This file contains 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,14 @@ | ||
from rest_framework.serializers import ModelSerializer | ||
|
||
from .models import Order | ||
|
||
class OrderSerializer(ModelSerializer): | ||
class Meta: | ||
model = Order | ||
fields = ('id', | ||
'product', | ||
'buyer', | ||
'quantity', | ||
'total', | ||
'created_at', | ||
'updated_at') |
This file contains 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,8 @@ | ||
from django.urls import path | ||
|
||
from . import views | ||
|
||
urlpatterns = [ | ||
path('', views.ListCreateOrderView.as_view()), | ||
path('<int:pk>/', views.RetrieveUpdateDestroyOrderView.as_view()), | ||
] |
This file contains 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,19 @@ | ||
from rest_framework import generics | ||
from rest_framework.permissions import IsAuthenticated | ||
|
||
from .models import Order | ||
from .serializers import OrderSerializer | ||
from .permissions import IsBuyerOrAdmin | ||
|
||
class ListCreateOrderView(generics.ListCreateAPIView): | ||
serializer_class = OrderSerializer | ||
permission_classes = (IsAuthenticated,) | ||
|
||
def get_queryset(self): | ||
user = self.request.user | ||
return Order.objects.filter(buyer=user) | ||
|
||
class RetrieveUpdateDestroyOrderView(generics.RetrieveUpdateDestroyAPIView): | ||
queryset = Order.objects.all() | ||
serializer_class = OrderSerializer | ||
permission_classes = (IsBuyerOrAdmin,) |
This file contains 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 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 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 rest_framework.serializers import ModelSerializer | ||
|
||
from .models import Product, ProductImage | ||
|
||
class ProductImageSerializer(ModelSerializer): | ||
class Meta: | ||
model = ProductImage | ||
fields = ( | ||
'id', | ||
'image', | ||
) | ||
|
||
class ProductSerializer(ModelSerializer): | ||
product_images = ProductImageSerializer(many=True, read_only=True) | ||
class Meta: | ||
model = Product | ||
fields = ( | ||
'id', | ||
'name', | ||
'description', | ||
'price', | ||
'color', | ||
'size', | ||
'created_at', | ||
'updated_at', | ||
'product_images', | ||
) | ||
|
||
def to_representation(self, instance): | ||
representation = super().to_representation(instance) | ||
filtered_images = instance.product_images.filter(product=instance) | ||
representation['product_images'] = ProductImageSerializer(filtered_images, many=True).data | ||
return representation |
This file contains 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,8 @@ | ||
from django.urls import path | ||
|
||
from . import views | ||
|
||
urlpatterns = [ | ||
path('', views.ListProductView.as_view()), | ||
path('<int:pk>/', views.RetrieveProductView.as_view()), | ||
] |
This file contains 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,12 @@ | ||
from django.shortcuts import render | ||
from rest_framework.generics import ListAPIView, RetrieveAPIView | ||
|
||
# Create your views here. | ||
from .models import Product | ||
from .serializers import ProductSerializer | ||
|
||
class ListProductView(ListAPIView): | ||
queryset = Product.objects.all() | ||
serializer_class = ProductSerializer | ||
|
||
class RetrieveProductView(RetrieveAPIView): | ||
queryset = Product.objects.all() | ||
serializer_class = ProductSerializer |