Skip to content
Open

v_1 #41

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 83 additions & 60 deletions commerce/controllers.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import random
import string
from typing import List
from uuid import uuid4

from django.contrib.auth.models import User
from django.db.models import Q
from django.shortcuts import get_object_or_404
from ninja import Router
from pydantic import UUID4

from commerce.models import Product, Category, City, Vendor, Item, Order, OrderStatus
from commerce.schemas import MessageOut, ProductOut, CitiesOut, CitySchema, VendorOut, ItemOut, ItemSchema, ItemCreate
from commerce.models import Address, Product, Category, City, Vendor, Item, Order, OrderStatus
from commerce.schemas import AddressOut, MessageOut, OrderOut, ProductOut, CitiesOut, CitySchema, VendorOut, ItemOut, ItemSchema, ItemCreate

products_controller = Router(tags=['products'])
address_controller = Router(tags=['addresses'])
Expand Down Expand Up @@ -55,70 +56,43 @@ def list_products(
return products_qs


"""
# product = Product.objects.all().select_related('merchant', 'category', 'vendor', 'label')
# print(product)
#
# order = Product.objects.all().select_related('address', 'user').prefetch_related('items')

# try:
# one_product = Product.objects.get(id='8d3dd0f1-2910-457c-89e3-1b0ed6aa720a')
# except Product.DoesNotExist:
# return {"detail": "Not found"}
# print(one_product)
#
# shortcut_function = get_object_or_404(Product, id='8d3dd0f1-2910-457c-89e3-1b0ed6aa720a')
# print(shortcut_function)

# print(type(product))
# print(product.merchant.name)
# print(type(product.merchant))
# print(type(product.category))


Product <- Merchant, Label, Category, Vendor

Retrieve 1000 Products form DB
@address_controller.get('address', response={
200: List[AddressOut],
404 : MessageOut
})
def list_addresses(request):
add_qs= Address.objects.all()
if add_qs:
return add_qs
return 404,{'details': 'no address'}

products = Product.objects.all()[:1000] (select * from product limit 1000)
@address_controller.get('addaddress', response={
200: MessageOut,
404 : MessageOut
})
def create_addresse(request, add_in: AddressOut):
Address.objects.create(**add_in.dict(), user= User.objects.first())
return 200, {'ddetails',"address added!"}

for p in products:
print(p)

for every product, we retrieve (Merchant, Label, Category, Vendor) records

Merchant.objects.get(id=p.merchant_id) (select * from merchant where id = 'p.merchant_id')
Label.objects.get(id=p.label_id) (select * from merchant where id = 'p.label_id')
Category.objects.get(id=p.category_id) (select * from merchant where id = 'p.category_id')
Vendor.objects.get(id=p.vendor_id) (select * from merchant where id = 'p.vendor_id')

4*1000+1

Solution: Eager loading

products = (select * from product limit 1000)

mids = [p1.merchant_id, p2.merchant_id, ...]
[p1.label_id, p2.label_id, ...]
.
.
.

select * from merchant where id in (mids) * 4 for (label, category and vendor)

4+1

"""


@address_controller.get('')
def list_addresses(request):
pass

@address_controller.get('address/{id}', response={
200: AddressOut,
404: MessageOut
})
def retrieve_address(request, id: UUID4):
return get_object_or_404(Address, id=id)

# @products_controller.get('categories', response=List[CategoryOut])
# def list_categories(request):
# return Category.objects.all()
@address_controller.put('address/{id}', response={
200: AddressOut,
400: MessageOut
})
def update_address(request, id: UUID4, add_in: AddressOut):
address = get_object_or_404(Address, id=id)
for key, value in add_in.items():
setattr(address, key, value)
return 200, address


@address_controller.get('cities', response={
Expand All @@ -133,11 +107,24 @@ def list_cities(request):

return 404, {'detail': 'No cities found'}

@address_controller.get('Items',response={
200: List[ItemCreate],
404: MessageOut,
})
def list_item(request):
item_qs = Item.objects.all()

if item_qs:
return item_qs

return 404, {'detail': 'No cities found'}


@address_controller.get('cities/{id}', response={
200: CitiesOut,
404: MessageOut
})

def retrieve_city(request, id: UUID4):
return get_object_or_404(City, id=id)

Expand Down Expand Up @@ -214,6 +201,40 @@ def reduce_item_quantity(request, id: UUID4):
return 200, {'detail': 'Item quantity reduced successfully!'}


@order_controller.post('item/{id}/increase-qty', response={
200: MessageOut
})
def increase_qty(request, id: UUID4):
item = get_object_or_404(Item, id=id, user=User.objects.first())
item.item_qty +=1
item.save()
return 200 , {'detail': 'Item quantity icreased successfully!'}


@order_controller.post('create', response={
200: OrderOut ,
400: MessageOut
})
def create_order():
# set order specification
orderquery = Order.objects.create(
user = User.objects.first(),
status = OrderStatus.objects.get(is_defualt=True),
ref_code= generate_ref_code(),
Order=False,
)
# get itemes from item tb - to specific user- oserderd is false
item_list = Item.objects.filter(user=User.objects.first()).filter(ordered=False)

#links items to order
orderquery.items.add(*item_list)
item_list.update(ordered=True)
orderquery.total =orderquery.order_total
orderquery.save()
return {'detail': 'successfull creation'}



@order_controller.delete('item/{id}', response={
204: MessageOut
})
Expand Down Expand Up @@ -252,3 +273,5 @@ def create_order(request):
order_qs.save()

return {'detail': 'order created successfully'}


19 changes: 18 additions & 1 deletion commerce/schemas.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import List

from ninja import ModelSchema, Schema
from ninja import ModelSchema, Schema, schema
from ninja.orm import create_schema
from pydantic import UUID4

Expand Down Expand Up @@ -86,9 +86,26 @@ class ItemSchema(Schema):
class ItemCreate(Schema):
product_id: UUID4
item_qty: int
id: UUID4


class ItemOut(UUIDSchema, ItemSchema):
pass

class OrderOut(Schema):
id: UUID4
Status: str
ref_code: UUID4
total: float
user_id : UUID4
address_id: UUID4


class AddressOut(UUIDSchema, Schema):

city_id:UUID4
address1:str
address2:str
phone:str
work_address:bool

1 change: 1 addition & 0 deletions config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
urlpatterns = [
path('admin/', admin.site.urls),
path('api/', api.urls),


]

Expand Down