Skip to content

Commit

Permalink
feat: [admin] added dashboard and integrated with backend
Browse files Browse the repository at this point in the history
  • Loading branch information
aabidsofi19 committed Feb 17, 2022
1 parent 44835fa commit 86dc0bc
Show file tree
Hide file tree
Showing 57 changed files with 2,480 additions and 9,944 deletions.
43 changes: 43 additions & 0 deletions apps/backend/Orders/migrations/0012_auto_20220212_0445.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Generated by Django 3.1.13 on 2022-02-12 04:45

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('Users', '0008_auto_20220206_1710'),
('Orders', '0011_order_fulfillment_status'),
]

operations = [
migrations.RemoveField(
model_name='orderitem',
name='Address',
),
migrations.RemoveField(
model_name='orderitem',
name='fullfillment_status',
),
migrations.AddField(
model_name='order',
name='Address',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.DO_NOTHING, to='Users.address'),
),
migrations.AddField(
model_name='order',
name='discount_percent',
field=models.IntegerField(default=0),
),
migrations.AddField(
model_name='order',
name='tax_percent',
field=models.IntegerField(default=0),
),
migrations.AddField(
model_name='orderitem',
name='fulfillment_status',
field=models.CharField(choices=[('Unfulfilled', 'Unfulfilled'), ('PartiallyFulfilled', 'PartiallyFulfilled'), ('Fulfilled', 'Fulfilled')], default='Unfulfilled', max_length=50),
),
]
38 changes: 38 additions & 0 deletions apps/backend/Orders/migrations/0013_auto_20220212_1244.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Generated by Django 3.1.13 on 2022-02-12 12:44

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('Orders', '0012_auto_20220212_0445'),
]

operations = [
migrations.RenameField(
model_name='order',
old_name='Customer',
new_name='customer',
),
migrations.RenameField(
model_name='order',
old_name='extra_charges',
new_name='shipping_charges',
),
migrations.RenameField(
model_name='orderitem',
old_name='Amount',
new_name='amount',
),
migrations.RenameField(
model_name='orderitem',
old_name='Customer',
new_name='customer',
),
migrations.RenameField(
model_name='orderitem',
old_name='Quantity',
new_name='quantity',
),
]
71 changes: 57 additions & 14 deletions apps/backend/Orders/models.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import operator
from functools import reduce
from django.db import models
from Users.models import Customer, Address
from cart.models import document_exists
from shop.models import Product


# Create your models here.
class Order(models.Model):
Customer = models.ForeignKey(Customer, on_delete=models.DO_NOTHING)
customer = models.ForeignKey(Customer, on_delete=models.DO_NOTHING)
paid = models.BooleanField(default=False)
payment_methods = (("0", "stripe_checkout"), ("1", "PaymentIntent"))

Expand Down Expand Up @@ -36,41 +39,81 @@ class Order(models.Model):
choices=Fullfilment_Statuses, default="Unfulfilled", max_length=50
)

address = models.ForeignKey(Address, on_delete=models.DO_NOTHING, null=True)
tax_percent = models.IntegerField(default=0)
discount_percent = models.IntegerField(default=0)
shipping_charges = models.IntegerField(default=0)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
extra_charges = models.IntegerField(default=0)

def __str__(self):
return f"{self.Customer} {self.id}"
return f"{self.customer} {self.id}"

@property
def total_amount(self):
items = OrderItem.objects.filter(order=self)
total_amount = self.extra_charges
for item in items:
total_amount += item.total_amount()
return total_amount
subtotal = sum((x.total_amount for x in items))
subtotal = subtotal + self.shipping_charges
subtotal = subtotal - (subtotal * self.discount_percent / 100)
return subtotal + (subtotal * self.tax_percent / 100)

@property
def total_items(self):

items = OrderItem.objects.filter(order=self)
return len(items)

@property
def total_quantity(self):

items = OrderItem.objects.filter(order=self)
return reduce(sum, map(lambda x: x.quantity, items))

@property
def total_weight(self):

items = OrderItem.objects.filter(order=self)
return reduce(sum, map(lambda x: x.product.weight * x.quantity, items))

@property
def total_weight_in_grams(self):
return self.total_weight() * 1000


# TODO : lowercase the quantity , weight , amount , customer in client
class OrderItem(models.Model):
order = models.ForeignKey(Order, on_delete=models.CASCADE)
product_id = models.CharField(max_length=100, null=False)
variation_id = models.CharField(null=False, max_length=50)
Amount = models.IntegerField(null=False) # single item price
Customer = models.ForeignKey(Customer, on_delete=models.DO_NOTHING)
Address = models.ForeignKey(Address, on_delete=models.DO_NOTHING)
Quantity = models.IntegerField(null=False)
amount = models.IntegerField(null=False) # single item price
customer = models.ForeignKey(Customer, on_delete=models.DO_NOTHING)

quantity = models.IntegerField(null=False)
Order_date = models.DateTimeField(auto_now_add=True, blank=True)

details = models.TextField(max_length=500, null=True)
paid = models.BooleanField(default=False)
fullfillment_status = models.CharField(max_length=200, default="Unpaid")

Fullfilment_Statuses = (
("Unfulfilled", "Unfulfilled"),
("PartiallyFulfilled", "PartiallyFulfilled"),
("Fulfilled", "Fulfilled"),
)
fulfillment_status = models.CharField(
choices=Fullfilment_Statuses, default="Unfulfilled", max_length=50
)

@property
def total_amount(self):
return self.Amount * self.Quantity
return self.amount * self.quantity

@property
def product(self):
return Product.objects.get(id=self.product_id)

if document_exists(lambda: Product.objects.get(id=self.product_id)):
return Product.objects.get(id=self.product_id)
else:
return Product(name="does not exist")

def __str__(self):
return str(self.id)
63 changes: 42 additions & 21 deletions apps/backend/Orders/schema.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
from ast import Str
import base64
from shop.types import ProductType, VariationType
from django.core.paginator import Paginator
import graphene
from graphene import relay

from graphene import Int, relay
import json
from graphene_django import DjangoObjectType, DjangoConnectionField
from graphene_django.filter import DjangoFilterConnectionField

from .models import Order, OrderItem
from Users.models import User, Customer, Address
from cart.cart import Cart
from cart.models import PersistentCart
from graphql_jwt.decorators import login_required
from graphql_jwt.decorators import login_required, superuser_required


from django_filters import FilterSet, OrderingFilter


def object_id_from_global_id(global_id: Str) -> Int:
global_id_decoded = base64.decodebytes(bytes(global_id, "utf-8")).decode("utf-8")
return int(global_id_decoded.split(":")[1])


class OrderFilter(FilterSet):
class Meta:
model = Order
fields = {
"id": ["exact"],
"Customer": ["exact"],
"customer": ["exact"],
"paid": ["exact"],
"payment_method": ["exact"],
"payment_status": ["exact"],
"fulfillment_status": ["exact"],
"created_at": ["exact"],
"created_at": ["exact", "gt", "gte", "lt", "lte", "range", "date"],
"updated_at": ["exact"],
"extra_charges": ["exact"],
# "extra_charges": ["exact"],
}

order_by = OrderingFilter(fields=(("updated_at", "created_at"),))


class PaginationType(graphene.ObjectType):

page_no = graphene.Int()
total_pages = graphene.Int()
total_count = graphene.Int()


class CountableConnectionBase(relay.Connection):
class Meta:
abstract = True
Expand All @@ -61,7 +61,7 @@ class Meta:

def resolve_totalAmount(self, info):

return self.total_amount()
return self.total_amount


class OrderItemType(DjangoObjectType):
Expand All @@ -73,14 +73,16 @@ class Meta:
totalAmount = graphene.Int()

def resolve_totalAmount(self, info):
return self.total_amount()
return self.total_amount

def resolve_product(self, info):

return self.product

def resolve_variation(self, info):

for v in self.product.variations:

if str(v._id) == self.variation_id:
return v

Expand All @@ -98,6 +100,8 @@ class Query(graphene.ObjectType):
@login_required
def resolve_order(self, info, id=None, get_current=None):
user = info.context.user
if id is not None:
id = object_id_from_global_id(id)

if get_current:
session = info.context.session
Expand Down Expand Up @@ -162,13 +166,14 @@ def mutate(root, info, input):

customer = Customer.objects.get(user=user)

order = Order(Customer=customer)
order.save()
# address=Address.objects.get(pk=input.address_id)
if input.is_primary:
address = customer.default_address
else:
address = customer.Addresses.get(pk=int(input.address_id))
order = Order(customer=customer, address=address)
order.save()

order_items = []

for item in cart.products():
Expand All @@ -182,10 +187,9 @@ def mutate(root, info, input):
order_item = OrderItem(
order=order,
product_id=str(product_id),
Amount=price,
Address=address,
Customer=customer,
Quantity=item.quantity,
amount=price,
customer=customer,
quantity=item.quantity,
variation_id=str(variation_id),
)

Expand All @@ -197,5 +201,22 @@ def mutate(root, info, input):
return CreateOrderMutation(order=order, order_items=order_items)


class UpdateOrderMutation(graphene.Mutation):

order = graphene.Field(OrderNode)

class Arguments:
order_id = graphene.String()
fulfillment_status = graphene.String()

def mutate(self, info, order_id, fulfillment_status, **kwargs):
id = object_id_from_global_id(order_id)
order = Order.objects.get(pk=id)
order.fulfillment_status = fulfillment_status
order.save()
return UpdateOrderMutation(order=order)


class Mutation(graphene.ObjectType):
create_order = CreateOrderMutation.Field()
update_order = UpdateOrderMutation.Field()
Loading

0 comments on commit 86dc0bc

Please sign in to comment.