# Path: simplestore/cart/models.py
# class CartItem(models.Model):
#     cart = models.ForeignKey(Cart, null=True, related_name='items', blank=True)
#     product = models.ForeignKey(Product, related_name='products', on_delete=models.DO_NOTHING)
#     date_added = models.DateTimeField(auto_now_add=True)
#     quantity = models.PositiveIntegerField(default=1)
#     total_price = models.DecimalField(max_digits=10, decimal_places=2, null=True)
# 
#     class Meta:
#         ordering = ['date_added']
# 
#     def get_absolute_url(self):
#         return self.product.get_absolute_url()
# 
#     def __str__(self):
#         return self.product.name


from decimal import Decimal
from django.db.models.signals import pre_save, post_save, post_delete
from django.dispatch import receiver
from .models import CartItem




@receiver(pre_save, sender=CartItem)
def cart_item_pre_save_receiver(sender, instance, **kwargs):
    if int(instance.quantity) >= 1:
        cart_item_total = Decimal(instance.quantity) * Decimal(instance.product.price)
        instance.total_price = cart_item_total


@receiver(post_save, sender=CartItem)
def cart_item_post_save_receiver(sender, instance, **kwargs):
    instance.cart.update_subtotal()



# Path: simplestore/cart/models.py
# class CartItem(models.Model):
#     cart = models.ForeignKey(Cart, null=True, related_name='items', blank=True)
#     product = models.ForeignKey(Product, related_name='products', on_delete=models.DO_NOTHING)
#     date_added = models.DateTimeField(auto_now_add=True)
#     quantity = models.PositiveIntegerField(default=1)
#     total_price = models.DecimalField(max_digits=10, decimal_places=2, null=True)
# 
#     class Meta:
#         ordering = ['date_added']
# 
#     def get_absolute_url(self):
#         return self.product.get_absolute_url()
# 
#     def __str__(self):
#         return self.product.name


# Path: simplestore/cart/signals.py
from decimal import Decimal
from django.db.models.signals import pre_save, post_save, post_delete
from django.dispatch import receiver
from .models import CartItem





@receiver(pre_save, sender=CartItem)
def cart_item_pre_save_receiver(sender, instance, **kwargs):
    if int(instance.quantity) >= 1:
        cart_item_total = Decimal(instance.quantity) * Decimal(instance.product.price)
        instance.total_price = cart_item_total


@receiver(post_save, sender=CartItem)
def cart_item_post_save_receiver(sender, instance, **kwargs):
    instance.cart.update_subtotal()



@receiver(post_delete, sender=CartItem)