# Path: simplestore/cart/models.py
# class Cart(models.Model):
#     user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True)
#     session_key = models.CharField(max_length=255, null=True)
#     created = models.DateTimeField(auto_now_add=True, auto_now=False)
#     updated = models.DateTimeField(auto_now_add=False, auto_now=True)
#     active = models.BooleanField(default=True)
#     price_subtotal = models.DecimalField(max_digits=10, decimal_places=2, default=0, null=True)
#     price_total = models.DecimalField(max_digits=10, decimal_places=2, default=0)
# 
#     class Meta:
#         unique_together = ('user', 'session_key')
# 
#     def update_subtotal(self):
#         subtotal = self.items.all().aggregate(sum=models.Sum('total_price'))
#         self.price_subtotal = subtotal['sum']
#         self.save()
# 
#     def get_total_quantity_of_items(self):
#         qty = self.items.all().aggregate(sum=models.Sum('quantity'))
#         return qty['sum']
# 
#     def __str__(self):
#         return "Cart id: {id}".format(id=self.pk)


from .models import Cart


def get_cart(request, create=False):
    """
    Return current Cart in session or create new Cart object if doesn't exists.
    :return: Cart object
    """
    if not request.session.session_key:
        request.session.create()

    if request.user.is_authenticated() and request.session.get('user_cart'):
        kwargs = {
            'session_key': request.session['user_cart'],
            'user': request.user
        }
    else:
        kwargs = {'session_key': request.session.session_key}

    try:
        return Cart.objects.get(**kwargs)
    except Cart.DoesNotExist:
        if create:

# Path: simplestore/cart/models.py
# class Cart(models.Model):
#     user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True)
#     session_key = models.CharField(max_length=255, null=True)
#     created = models.DateTimeField(auto_now_add=True, auto_now=False)
#     updated = models.DateTimeField(auto_now_add=False, auto_now=True)
#     active = models.BooleanField(default=True)
#     price_subtotal = models.DecimalField(max_digits=10, decimal_places=2, default=0, null=True)
#     price_total = models.DecimalField(max_digits=10, decimal_places=2, default=0)
# 
#     class Meta:
#         unique_together = ('user', 'session_key')
# 
#     def update_subtotal(self):
#         subtotal = self.items.all().aggregate(sum=models.Sum('total_price'))
#         self.price_subtotal = subtotal['sum']
#         self.save()
# 
#     def get_total_quantity_of_items(self):
#         qty = self.items.all().aggregate(sum=models.Sum('quantity'))
#         return qty['sum']
# 
#     def __str__(self):
#         return "Cart id: {id}".format(id=self.pk)


# Path: simplestore/cart/utils.py
from .models import Cart



def get_cart(request, create=False):
    """
    Return current Cart in session or create new Cart object if doesn't exists.
    :return: Cart object
    """
    if not request.session.session_key:
        request.session.create()

    if request.user.is_authenticated() and request.session.get('user_cart'):
        kwargs = {
            'session_key': request.session['user_cart'],
            'user': request.user
        }
    else:
        kwargs = {'session_key': request.session.session_key}

    try:
        return Cart.objects.get(**kwargs)
    except Cart.DoesNotExist:
        if create:

            return Cart.objects.create(**kwargs)