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

Anas Abdulwahid #33

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
72 changes: 64 additions & 8 deletions commerce/controllers.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
from typing import List

from uuid import uuid4
from typing import List
import string
import random
from django.contrib.auth.models import User
from django.db.models import Q
from django.shortcuts import get_object_or_404
from django.utils import tree
from ninja import Router
from pydantic import UUID4

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

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


@vendor_controller.get('', response=List[VendorOut])
Expand All @@ -31,7 +35,8 @@ def list_products(
price_to: int = None,
vendor=None,
):
products_qs = Product.objects.filter(is_active=True).select_related('merchant', 'vendor', 'category', 'label')
products_qs = Product.objects.filter(is_active=True).select_related(
'merchant', 'vendor', 'category', 'label')

if not products_qs:
return 404, {'detail': 'No products found'}
Expand Down Expand Up @@ -109,9 +114,9 @@ def list_products(
"""


@address_controller.get('')
@address_controller.get('', response=List[AddressOut])
def list_addresses(request):
pass
return Address.objects.all()


# @products_controller.get('categories', response=List[CategoryOut])
Expand Down Expand Up @@ -189,7 +194,8 @@ def view_cart(request):
})
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=User.objects.first())
item.item_qty += 1
item.save()
except Item.DoesNotExist:
Expand Down Expand Up @@ -220,3 +226,53 @@ def delete_item(request, id: UUID4):
item.delete()

return 204, {'detail': 'Item deleted!'}

@order_controller.post('item/{id}/increase_quantity', response={
200: MessageOut
})
@order_controller.post()
def increase_item_quantity(request, id: uuid4):
item = get_object_or_404(Item, id=id, User=User.objects.first())
if item.item_qty > 1:
item.update()
return 200, {'detail': 'Item added'}
item.item_qty += 1
item.save()
return 200, {'detail', 'Item auantity increase successfully'}


def generate_ref_code():
return ''.join(random.sample(string.ascii_letters+string.digits, 6))


@order_controller.post('create_order')
def create_order(request):
order_qs = Order(
user=User.objects.first(),
status=OrderStatus.objects.get(is_default=True),
ref_code=generate_ref_code(),
ordered=False,
)

user_items = Item.objects.filter(user=User.objects.first())
user_items.update(ordered=True)
order_qs.items.append(*user_items)
order_qs.total = order_qs.order_total
order_qs.save()
return {'detail': 'thanks the order created successfuly'}


@checkout_controller.post('checkout_note', response={
200: List[CheckoutSchema],
404: MessageOut
})
def List_checkout(
request, *,
q: str = None,
):
checkout_qs = Item.objects.filter(user=User.objects.first(), ordered=False)

if checkout_qs:
return checkout_qs

return 404, {'detail': 'checkout please '}
21 changes: 13 additions & 8 deletions commerce/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import uuid

from PIL import Image
from django.contrib.auth import get_user_model
from django.db import models
Expand All @@ -26,7 +25,8 @@ class Product(Entity):
qty = models.DecimalField('qty', max_digits=10, decimal_places=2)
cost = models.DecimalField('cost', max_digits=10, decimal_places=2)
price = models.DecimalField('price', max_digits=10, decimal_places=2)
discounted_price = models.DecimalField('discounted price', max_digits=10, decimal_places=2)
discounted_price = models.DecimalField(
'discounted price', max_digits=10, decimal_places=2)
vendor = models.ForeignKey('commerce.Vendor', verbose_name='vendor', related_name='products',
on_delete=models.SET_NULL,
null=True, blank=True)
Expand All @@ -52,13 +52,15 @@ class Order(Entity):
on_delete=models.CASCADE)
address = models.ForeignKey('commerce.Address', verbose_name='address', null=True, blank=True,
on_delete=models.CASCADE)
total = models.DecimalField('total', blank=True, null=True, max_digits=1000, decimal_places=0)
total = models.DecimalField(
'total', blank=True, null=True, max_digits=1000, decimal_places=0)
status = models.ForeignKey('commerce.OrderStatus', verbose_name='status', related_name='orders',
on_delete=models.CASCADE)
note = models.CharField('note', null=True, blank=True, max_length=255)
ref_code = models.CharField('ref code', max_length=255)
ordered = models.BooleanField('ordered')
items = models.ManyToManyField('commerce.Item', verbose_name='items', related_name='order')
items = models.ManyToManyField(
'commerce.Item', verbose_name='items', related_name='order')

def __str__(self):
return f'{self.user.first_name} + {self.total}'
Expand All @@ -75,7 +77,8 @@ class Item(Entity):
Product can live alone in the system, while
Item can only live within an order
"""
user = models.ForeignKey(User, verbose_name='user', related_name='items', on_delete=models.CASCADE)
user = models.ForeignKey(User, verbose_name='user',
related_name='items', on_delete=models.CASCADE)
product = models.ForeignKey('commerce.Product', verbose_name='product',
on_delete=models.CASCADE)
item_qty = models.IntegerField('item_qty')
Expand Down Expand Up @@ -121,7 +124,6 @@ class Category(Entity):
image = models.ImageField('image', upload_to='category/')
is_active = models.BooleanField('is active')


def __str__(self):
if self.parent:
return f'- {self.name}'
Expand All @@ -135,6 +137,7 @@ class Meta:
def children(self):
return self.children


class Merchant(Entity):
name = models.CharField('name', max_length=255)

Expand Down Expand Up @@ -209,8 +212,10 @@ class Address(Entity):
on_delete=models.CASCADE)
work_address = models.BooleanField('work address', null=True, blank=True)
address1 = models.CharField('address1', max_length=255)
address2 = models.CharField('address2', null=True, blank=True, max_length=255)
city = models.ForeignKey(City, related_name='addresses', on_delete=models.CASCADE)
address2 = models.CharField(
'address2', null=True, blank=True, max_length=255)
city = models.ForeignKey(
City, related_name='addresses', on_delete=models.CASCADE)
phone = models.CharField('phone', max_length=255)

def __str__(self):
Expand Down
15 changes: 12 additions & 3 deletions commerce/schemas.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from typing import List

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

from commerce.models import Product, Merchant
from pydantic.schema import model_schema
from commerce.models import City, Product, Merchant, Address


class MessageOut(Schema):
Expand Down Expand Up @@ -42,6 +41,13 @@ class CategoryOut(UUIDSchema):
CategoryOut.update_forward_refs()


class AddressOut(ModelSchema):
address1: str
address2: str
City: str
phone: str


class ProductOut(ModelSchema):
vendor: VendorOut
label: LabelOut
Expand All @@ -67,6 +73,9 @@ class Config:
# class ProductManualSchemaOut(Schema):
# pass

class CheckoutSchema(Schema):
note: str


class CitySchema(Schema):
name: str
Expand Down
3 changes: 2 additions & 1 deletion config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from django.urls import path
from ninja import NinjaAPI

from commerce.controllers import products_controller, address_controller, vendor_controller, order_controller
from commerce.controllers import products_controller, address_controller, vendor_controller, order_controller, checkout_controller
from config import settings

api = NinjaAPI()
Expand All @@ -27,6 +27,7 @@
api.add_router('addresses', address_controller)
api.add_router('vendors', vendor_controller)
api.add_router('orders', order_controller)
api.add_router('orders',checkout_controller)

urlpatterns = [
path('admin/', admin.site.urls),
Expand Down