Skip to content

Commit

Permalink
simplify products and add orders model (#134)
Browse files Browse the repository at this point in the history
* simplify product model

* simplify products and add orders
  • Loading branch information
arminpatel authored Mar 7, 2024
1 parent 155d0e0 commit 392618e
Show file tree
Hide file tree
Showing 13 changed files with 131 additions and 28 deletions.
1 change: 1 addition & 0 deletions backend/core/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
'products',
'events',
'tickets',
'orders'
]

REST_FRAMEWORK = {
Expand Down
2 changes: 2 additions & 0 deletions backend/core/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
path('api/token/', include('auth.urls')),
path('api/accounts/', include('accounts.urls')),
path('api/events/', include('events.urls')),
path('api/orders/', include('orders.urls')),
path('api/products/', include('products.urls')),
path('api/tickets/', include('tickets.urls')),
path('api/stripe/webhook/', stripe_webhook, name='stripe-webhook'),
path('swagger/', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
Expand Down
5 changes: 5 additions & 0 deletions backend/orders/admin.py
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)
14 changes: 14 additions & 0 deletions backend/orders/models.py
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}"
5 changes: 5 additions & 0 deletions backend/orders/permissions.py
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
14 changes: 14 additions & 0 deletions backend/orders/serializers.py
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')
8 changes: 8 additions & 0 deletions backend/orders/urls.py
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()),
]
19 changes: 19 additions & 0 deletions backend/orders/views.py
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,)
10 changes: 3 additions & 7 deletions backend/products/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@
# Register your models here.

class ProductAdmin(admin.ModelAdmin) :
list_display = ("name","description","category",'primary_variant',"created_at","updated_at")
fields = ("name","description","category","primary_variant",)

class ProductVariationAdmin(admin.ModelAdmin) :
list_display = ("Product","ProductSize","ProductColor","price","quantity")
list_display = ("name","description","category",'color', 'size', 'price',"created_at","updated_at")
fields = ("name","description","category", "color", "size", "price")

class ProductColorAdmin(admin.ModelAdmin) :
list_display = ("color",)
Expand All @@ -17,11 +14,10 @@ class ProductSizeAdmin(admin.ModelAdmin) :
list_display = ("size",)

class ProductImageAdmin(admin.ModelAdmin) :
list_display = ("product_variation_id","image")
list_display = ("product","image")


admin.site.register(Product,ProductAdmin)
admin.site.register(ProductVariation,ProductVariationAdmin)
admin.site.register(ProductColor,ProductColorAdmin)
admin.site.register(ProductSize,ProductSizeAdmin)
admin.site.register(ProductImage,ProductImageAdmin)
Expand Down
27 changes: 8 additions & 19 deletions backend/products/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from django.db import models

# Create your models here.

category_choices = [
('clothing','Clothing'),
('rsvp','RSVP'),
Expand All @@ -13,25 +11,19 @@ class Product(models.Model):
blank=True)
category = models.CharField(max_length=100,
choices=category_choices)
primary_variant = models.OneToOneField(
'ProductVariation',
on_delete=models.CASCADE,
default=None,
null=True,
blank=True,)

# Need to add Primary Variant and seller Id
color = models.CharField(max_length=100, blank = True)
size = models.CharField(max_length=100, blank = True)
price = models.IntegerField(default=0)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)

def __str__(self):
return self.name
return f'{self.name} {self.color} {self.size}'


class ProductVariation(models.Model):
Product = models.ForeignKey(Product,
on_delete=models.CASCADE,
related_name='product_variations')
# Product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name='product_variations')
ProductColor = models.ForeignKey('ProductColor',
on_delete=models.CASCADE)
ProductSize = models.ForeignKey('ProductSize',
Expand Down Expand Up @@ -62,12 +54,9 @@ def __str__(self):


class ProductImage(models.Model):
product_variation_id = models.ForeignKey(ProductVariation,
on_delete=models.CASCADE,
related_name='product_images')
product = models.ForeignKey(Product,
on_delete=models.CASCADE,
related_name='product_images')
image = models.ImageField(upload_to='product_images')

required = ['image']

def __str__(self):
return f'{self.product_variation_id.Product.name} {self.product_variation_id.ProductColor.color} {self.product_variation_id.ProductSize.size}'
33 changes: 33 additions & 0 deletions backend/products/serializers.py
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
8 changes: 8 additions & 0 deletions backend/products/urls.py
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()),
]
13 changes: 11 additions & 2 deletions backend/products/views.py
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

0 comments on commit 392618e

Please sign in to comment.