Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

douha #25

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
10 changes: 5 additions & 5 deletions account/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
account_controller = Router(tags=['auth'])


@account_controller.post('signup', response={
@account_controller.post('signup',auth=None, response={
400: MessageOut,
201: AuthOut,
})
Expand Down Expand Up @@ -39,7 +39,7 @@ def signup(request, account_in: AccountCreate):
return 400, {'detail': 'User already registered!'}


@account_controller.post('signin', response={
@account_controller.post('signin',auth=None, response={
200: AuthOut,
404: MessageOut,
})
Expand All @@ -57,12 +57,12 @@ def signin(request, signin_in: SigninSchema):
}


@account_controller.get('', auth=GlobalAuth(), response=AccountOut)
@account_controller.get('', response=AccountOut)
def me(request):
return get_object_or_404(User, id=request.auth['pk'])


@account_controller.put('', auth=GlobalAuth(), response={
@account_controller.put('', response={
200: AccountOut,

})
Expand All @@ -71,7 +71,7 @@ def update_account(request, update_in: AccountUpdate):
return get_object_or_404(User, id=request.auth['pk'])


@account_controller.post('change-password', auth=GlobalAuth(), response={
@account_controller.post('change-password', response={
200: MessageOut,
400: MessageOut
})
Expand Down
17 changes: 17 additions & 0 deletions account/migrations/0002_alter_user_options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 3.2.8 on 2021-11-06 01:28

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('account', '0001_initial'),
]

operations = [
migrations.AlterModelOptions(
name='user',
options={'verbose_name': 'user', 'verbose_name_plural': 'users'},
),
]
161 changes: 140 additions & 21 deletions commerce/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,34 @@
from django.shortcuts import get_object_or_404
from ninja import Router
from pydantic import UUID4

from ninja import NinjaAPI
from account.authorization import GlobalAuth
from commerce.models import Product, Category, City, Vendor, Item, Order, OrderStatus
from commerce.schemas import ProductOut, CitiesOut, CitySchema, VendorOut, ItemOut, ItemSchema, ItemCreate
from commerce.models import Product, Category, City, Vendor, Item, Order, OrderStatus,Address
from commerce.schemas import (MessageOut, ProductOut, CitiesOut,
CitySchema, VendorOut, ItemOut,
ItemSchema, ItemCreate, Add_address, AddressSchema,AddressOut,OrderSchema)
from config.utils.schemas import MessageOut



api = NinjaAPI(auth=GlobalAuth())

products_controller = Router(tags=['products'])
address_controller = Router(tags=['addresses'])
vendor_controller = Router(tags=['vendors'])
order_controller = Router(tags=['orders'])
city_controller = Router(tags=['City'])
checkout_controller = Router(tags=['checkout'])


User = get_user_model()

@vendor_controller.get('', response=List[VendorOut])
@vendor_controller.get('',auth=None, response=List[VendorOut])
def list_vendors(request):
return Vendor.objects.all()


@products_controller.get('', response={
@products_controller.get('',auth=None, response={
200: List[ProductOut],
404: MessageOut
})
Expand Down Expand Up @@ -114,7 +123,7 @@ def list_products(
"""


@address_controller.get('')
@city_controller.get('',auth=None)
def list_addresses(request):
pass

Expand All @@ -124,7 +133,7 @@ def list_addresses(request):
# return Category.objects.all()


@address_controller.get('cities', response={
@city_controller.get('cities',auth=None, response={
200: List[CitiesOut],
404: MessageOut
})
Expand All @@ -137,15 +146,17 @@ def list_cities(request):
return 404, {'detail': 'No cities found'}


@address_controller.get('cities/{id}', response={

@city_controller.get('cities/{id}',auth=None, response={
200: CitiesOut,
404: MessageOut
})
def retrieve_city(request, id: UUID4):
return get_object_or_404(City, id=id)


@address_controller.post('cities', response={

@city_controller.post('cities', response={
201: CitiesOut,
400: MessageOut
})
Expand All @@ -155,7 +166,7 @@ def create_city(request, city_in: CitySchema):
return 201, city


@address_controller.put('cities/{id}', response={
@city_controller.put('cities/{id}',response={
200: CitiesOut,
400: MessageOut
})
Expand All @@ -166,7 +177,7 @@ def update_city(request, id: UUID4, city_in: CitySchema):
return 200, city


@address_controller.delete('cities/{id}', response={
@city_controller.delete('cities/{id}', response={
204: MessageOut
})
def delete_city(request, id: UUID4):
Expand All @@ -175,39 +186,47 @@ def delete_city(request, id: UUID4):
return 204, {'detail': ''}



#--------------------------------- Order --------------------------------
#------------------------------------------------------------------------


@order_controller.get('cart', response={
200: List[ItemOut],
404: MessageOut
})
def view_cart(request):
cart_items = Item.objects.filter(user=User.objects.first(), ordered=False)
cart_items = Item.objects.filter(user=request.auth['pk'], ordered=False)

if cart_items:
return cart_items

return 404, {'detail': 'Your cart is empty, go shop like crazy!'}



@order_controller.post('add-to-cart', response={
200: MessageOut,
# 400: MessageOut
})
def add_update_cart(request, item_in: ItemCreate):
try:
item = Item.objects.get(product_id=item_in.product_id, user=User.objects.first())
item = Item.objects.get(product_id=item_in.product_id, user=request.auth['pk'])
item.item_qty += 1
item.save()
except Item.DoesNotExist:
Item.objects.create(**item_in.dict(), user=User.objects.first())
Item.objects.create(**item_in.dict(), user=request.auth['pk'])

return 200, {'detail': 'Added to cart successfully'}




@order_controller.post('item/{id}/reduce-quantity', response={
200: MessageOut,
})
def reduce_item_quantity(request, id: UUID4):
item = get_object_or_404(Item, id=id, user=User.objects.first())
item = get_object_or_404(Item, id=id, user=request.auth['pk'])
if item.item_qty <= 1:
item.delete()
return 200, {'detail': 'Item deleted!'}
Expand All @@ -221,7 +240,7 @@ def reduce_item_quantity(request, id: UUID4):
204: MessageOut
})
def delete_item(request, id: UUID4):
item = get_object_or_404(Item, id=id, user=User.objects.first())
item = get_object_or_404(Item, id=id, user=request.auth['pk'])
item.delete()

return 204, {'detail': 'Item deleted!'}
Expand All @@ -231,27 +250,127 @@ def generate_ref_code():
return ''.join(random.sample(string.ascii_letters + string.digits, 6))


@order_controller.post('create-order', auth=GlobalAuth(), response=MessageOut)
@order_controller.post('create-order',auth=None, response=MessageOut)
def create_order(request):
'''
* add items and mark (ordered) field as True
* add ref_number
* add NEW status
* calculate the total
'''

order_qs = Order.objects.create(
user=User.objects.first(),
order_qs = Order(
user=request.auth['pk'],
status=OrderStatus.objects.get(is_default=True),
ref_code=generate_ref_code(),
ordered=False,
)

user_items = Item.objects.filter(user=User.objects.first()).filter(ordered=False)
user_items = Item.objects.filter(user=request.auth['pk'], ordered=False)
user_items.update(ordered=True)



order_qs.items.add(*user_items)
order_qs.total = order_qs.order_total
user_items.update(ordered=True)
order_qs.save()

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


#--------------------------- Addresses -----------------------------
#-------------------------------------------------------------------


@address_controller.get('address',auth=None, response={
200: List[AddressOut],
404: MessageOut
})
def list_Address(request):
Address_qs = Address.objects.all()

if Address_qs:
return Address_qs

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



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


@address_controller.post('', response={
201: AddressOut,
400: MessageOut
})
def create_address(request, address_in: Add_address):
city_instance = City.objects.get(id=address_in.city)
del address_in.city
address = Address.objects.create(**address_in.dict(), city=city_instance, user=request.auth['pk'])
address.save()
return 201, address


@address_controller.put('/{id}', response={
200: AddressOut,
404: MessageOut
})
def update_address(request, id: UUID4, new_data:Add_address):
address = get_object_or_404(Address, id=id)
city_instance = City.objects.get(id=new_data.city)
new_data.city = city_instance
for attr, value in new_data.dict().items():
setattr(address, attr, value)
address.save()
return 200, address


@address_controller.delete('addresses/{id}', response={
204: MessageOut
})
def delete_address(request, id: UUID4):
city = get_object_or_404(Address, id=id)
city.delete()
return 204, {'detail': ''}


##--------------------------- checkOut --------------------------------
##---------------------------------------------------------------------


@checkout_controller.post('create-checkout', response=
{ 200: MessageOut})

def create_checkout(request,order_in:OrderSchema,note:str=None):
'''
* create the checkout endpoint
* you should be able to add an optional note
* you should be able to add an address to the order
* set (ordered field) to True, thus the order becomes sealed
* change order status accordingly

'''

if get_object_or_404(City):
check_order=Order(
user=request.auth['pk'],
status=OrderStatus.objects.get(title='SHIPPED'),


)

check_order.note=note
check_order.ordered=True
check_order.address=Address.objects.get(id=order_in.address)
check_order.save()
return {'detail': ' checkout created successfully'}
else:
return MessageOut


Loading