cart.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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, cart=get_cart(request))
  30. def remove_from_cart(request, cart_token):
  31. """ function that removes a single product instance from the
  32. current customer's shopping cart
  33. """
  34. cart_item = get_single_item(request, cart_token)
  35. if cart_item:
  36. gift_detail = cart_item.gift_detail
  37. if gift_detail:
  38. gift_detail.delete()
  39. cart_item.delete()
  40. def cart_subtotal(request):
  41. """ cart subtotal """
  42. cart = get_cart(request)
  43. return cart.cart_subtotal
  44. def cart_distinct_item_count(request):
  45. """ returns the total number of items in the user's cart """
  46. cart = get_cart(request)
  47. return cart.item_count
  48. def is_empty(request):
  49. """ Check if the cart is empty """
  50. return cart_distinct_item_count(request) == 0
  51. def update_cart(request, cart_item, quantity):
  52. """ function takes the quantity for single product instance in the
  53. current customer's shopping cart, and updates it.
  54. """
  55. if cart_item:
  56. if int(quantity) > 0:
  57. cart_item.quantity = int(quantity)
  58. cart_item.save()
  59. else:
  60. remove_from_cart(request, cart_item.cart_token)
  61. def add_to_cart(request, item, account_number=None, gift_detail=None, quantity=1):
  62. """ function that takes an item instance to the current
  63. customer's shopping cart
  64. :param detail: dictionary of additional cart information
  65. """
  66. # get cart
  67. cart = get_cart(request)
  68. if not cart:
  69. LOGGER.info("Cart is None, just returning. See this with search bots")
  70. return
  71. # get products in cart
  72. cart_products = cart.cart_items
  73. product_in_cart = False
  74. # check to see if item is already in cart
  75. for cart_item in cart_products:
  76. if cart_item.product.pk == item.pk:
  77. # update the quantity if found
  78. # only works if we can have one item and no options
  79. cart_item.quantity = cart_item.quantity + quantity
  80. product_in_cart = True
  81. cart_item.save()
  82. if not product_in_cart:
  83. # create and save a new cart item
  84. cart_item = CartItem()
  85. cart_item.cart = cart
  86. cart_item.product = item
  87. cart_item.quantity = quantity
  88. cart_item.account_number = account_number
  89. cart_item.save()
  90. if gift_detail:
  91. cart_item_gift_detail = CartItemGiftDetail(**gift_detail)
  92. cart_item_gift_detail.save()
  93. cart_item.gift_detail = cart_item_gift_detail
  94. cart_item.save()
  95. def remove_old_cart_items():
  96. """ 1. calculate date of 30 days ago,
  97. 2. find how many carts are older then date
  98. 3. delete those Cart instances
  99. """
  100. LOGGER.debug("Removing old carts")
  101. remove_before = datetime.now() + timedelta(days=-30)
  102. # TODO, keep carts related to checkout?
  103. old_items = Cart.objects.filter(last_update__lt=remove_before)
  104. old_item_count = old_items.count()
  105. if old_item_count > 0:
  106. old_items.delete()
  107. LOGGER.debug("{0} carts were removed".format(old_item_count))