cart.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. """ Code to handle the website shopping cart """
  2. from datetime import datetime, timedelta
  3. import logging
  4. from django.shortcuts import get_object_or_404
  5. from .models import Cart, CartItem, CartItemGiftDetail
  6. LOGGER = logging.getLogger('cart')
  7. def get_cart_from_key(cart_key):
  8. """ Given a cart_id, get the cart """
  9. return Cart.objects.for_session(cart_key)
  10. def get_cart(request):
  11. """ get the cart for the given request """
  12. return Cart.objects.for_request(request)
  13. def get_cart_items(request):
  14. """ return all items from the current user's cart """
  15. cart = get_cart(request)
  16. return cart.cart_items
  17. def empty_cart(request):
  18. """ empties the shopping cart of the current customer """
  19. user_cart = get_cart_items(request)
  20. user_cart.delete()
  21. def remove_subscriptions(request):
  22. """ removes any subscriptions from the cart """
  23. user_cart = get_cart_items(request)
  24. for item in user_cart:
  25. if item.product.is_subscription:
  26. item.delete()
  27. def get_single_item(request, cart_token):
  28. """ get the one item from the cart """
  29. return get_object_or_404(CartItem, cart_token=cart_token,
  30. cart=get_cart(request))
  31. def remove_from_cart(request, cart_token):
  32. """ function that removes a single product instance from the
  33. current customer's shopping cart
  34. """
  35. cart_item = get_single_item(request, cart_token)
  36. if cart_item:
  37. gift_detail = cart_item.gift_detail
  38. if gift_detail:
  39. gift_detail.delete()
  40. cart_item.delete()
  41. def cart_subtotal(request):
  42. """ cart subtotal """
  43. cart = get_cart(request)
  44. return cart.cart_subtotal
  45. def cart_distinct_item_count(request):
  46. """ returns the total number of items in the user's cart """
  47. cart = get_cart(request)
  48. return cart.item_count
  49. def is_empty(request):
  50. """ Check if the cart is empty """
  51. return cart_distinct_item_count(request) == 0
  52. def update_cart(request, cart_item, quantity):
  53. """ function takes the quantity for single product instance in the
  54. current customer's shopping cart, and updates it.
  55. """
  56. if cart_item:
  57. if int(quantity) > 0:
  58. cart_item.quantity = int(quantity)
  59. cart_item.save()
  60. else:
  61. remove_from_cart(request, cart_item.cart_token)
  62. def add_to_cart(request, item, account_number=None, gift_detail=None,
  63. quantity=1):
  64. """ function that takes an item instance to the current
  65. customer's shopping cart
  66. :param detail: dictionary of additional cart information
  67. """
  68. # get cart
  69. cart = get_cart(request)
  70. if not cart:
  71. LOGGER.info("Cart is None, just returning. See this with search bots")
  72. return
  73. #get products in cart
  74. cart_products = cart.cart_items
  75. product_in_cart = False
  76. # check to see if item is already in cart
  77. for cart_item in cart_products:
  78. if cart_item.product.pk == item.pk:
  79. # update the quantity if found
  80. # only works if we can have one item and no options
  81. cart_item.quantity = cart_item.quantity + quantity
  82. product_in_cart = True
  83. cart_item.save()
  84. if not product_in_cart:
  85. # create and save a new cart item
  86. cart_item = CartItem()
  87. cart_item.cart = cart
  88. cart_item.product = item
  89. cart_item.quantity = quantity
  90. cart_item.account_number = account_number
  91. cart_item.save()
  92. if gift_detail:
  93. cart_item_gift_detail = CartItemGiftDetail(**gift_detail)
  94. cart_item_gift_detail.save()
  95. cart_item.gift_detail = cart_item_gift_detail
  96. cart_item.save()
  97. def remove_old_cart_items():
  98. """ 1. calculate date of 30 days ago,
  99. 2. find how many carts are older then date
  100. 3. delete those Cart instances
  101. """
  102. LOGGER.debug("Removing old carts")
  103. remove_before = datetime.now() + timedelta(days=-30)
  104. # TODO, keep carts related to checkout?
  105. old_items = Cart.objects.filter(last_update__lt=remove_before)
  106. old_item_count = old_items.count()
  107. if old_item_count > 0:
  108. old_items.delete()
  109. LOGGER.debug("{0} carts were removed".format(old_item_count))