Calculate Order total - lhmisho/django-eCommerce GitHub Wiki

In this section i am going to calculate the cart total

First i need to import post_save signal in our model

from django.db.models.signals import pre_save, post_save

Managing model

orders/model

from django.db import models
from django.db.models.signals import pre_save, post_save
from eshop.utils import unique_order_id_generator
from carts.models import Cart
# Create your models here.

ORDER_STATUS_CHOICE = (
    ('created', 'CREATED'),
    ('paid', 'PAID'),
    ('shipped', 'SHIPPING'),
    ('refounded', 'REFOUNDED')
)

class Order(models.Model):
    order_id = models.CharField(max_length=120, blank=True)
    #billing_profile     =
    #shipping_address    =
    #billing_address     =
    cart    = models.ForeignKey(Cart, on_delete=models.CASCADE)
    status  = models.CharField(max_length=120, default='created', choices=ORDER_STATUS_CHOICE)
    shipping_total = models.DecimalField(default=5.99, max_digits=100, decimal_places=2)
    total   = models.DecimalField(default=0.00, max_digits=100, decimal_places=2)

    def update_total(self):
        cart_total = self.cart.total
        shipping_total = self.shipping_total
        new_total = cart_total + shipping_total
        self.total = new_total
        self.save()
        return new_total

    def __str__(self):
        return self.order_id

# using pre_save singnals with unique_order_generator for saving the unique order id
def pre_save_order_id(sender, instance, *args, **kewarg):
    if not instance.order_id:
        instance.order_id = unique_order_id_generator(instance)

pre_save.connect(pre_save_order_id , sender=Order)


# using post_save for cart total ( it works if the cart is not created )
def post_save_cart_total(sender, instance, created, *args, **kewarg):
    if not created:
        cart_obj    = instance
        cart_total  = cart_obj.total
        cart_id     = cart_obj.id

        qs = Order.objects.filter(cart__id=cart_id)

        if qs.count() == 1:
            order_obj = qs.first()
            order_obj.update_total()

post_save.connect(post_save_cart_total, sender=Cart)

# if the cart is created than it's going to update the order total
# updating total in our order
def post_save_order(sender, instance, created, *args, **kwargs):
    print("running")
    if created:
        print("updating")
        instance.update_total()
        print("running")

post_save.connect(post_save_order, sender=Order)

Here i added this portion


# using post_save for cart total ( it works if the cart is not created )
def post_save_cart_total(sender, instance, created, *args, **kewarg):
    if not created:
        cart_obj    = instance
        cart_total  = cart_obj.total
        cart_id     = cart_obj.id

        qs = Order.objects.filter(cart__id=cart_id)

        if qs.count() == 1:
            order_obj = qs.first()
            order_obj.update_total()

post_save.connect(post_save_cart_total, sender=Cart)

# if the cart is created than it's going to update the order total
# updating total in our order
def post_save_order(sender, instance, created, *args, **kwargs):
    print("running")
    if created:
        print("updating")
        instance.update_total()
        print("running")

post_save.connect(post_save_order, sender=Order)