def add_all_shopping_cart_to_user(shopping_cart): for cart_item in shopping_cart: status = ShoppingCartDB.add_item_shopping_cart(cart_item) if status is False: return False if cart_item.code is not None: status = ShoppingCartDB.update_code_shopping_cart(cart_item.username, cart_item.item_id, cart_item.code) if status is False: return False return True
def test_add_item_to_cart(self): username = '******' item_id = 1 quantity = 20 ShoppingCartDB.add_item_shopping_cart( ShoppingCartDB.ShoppingCartItem(username, item_id, quantity, "")) cart_items = ShoppingCartDB.get_cart_items(username) self.assertEqual(len(cart_items), 1) self.assertEqual(cart_items[0].username, username) self.assertEqual(cart_items[0].item_id, item_id) self.assertEqual(cart_items[0].item_quantity, quantity)
def add_item_shopping_cart(shop_cart_item): if shop_cart_item.username is not None and shop_cart_item.item_id is not None and shop_cart_item.item_quantity > 0: if ItemsLogic.check_in_stock(shop_cart_item.item_id, shop_cart_item.item_quantity) is False: return False existing = ShoppingCartDB.get_shopping_cart_item(shop_cart_item) if existing is not False: return update_item_shopping_cart( shop_cart_item.username, shop_cart_item.item_id, shop_cart_item.item_quantity + existing.item_quantity) else: return ShoppingCartDB.add_item_shopping_cart(shop_cart_item) return False
def update_code_shopping_cart(username, item_id, code): if username is not None and item_id is not None and code is not None: user = RegisteredUsers.get_user(username) if user is not False: if len(code) == 15 and isinstance(code, str): return ShoppingCartDB.update_code_shopping_cart( username, item_id, code) return False
def update_item_shopping_cart(username, item_id, new_quantity): if username is not None and item_id is not None and new_quantity >= 0: if new_quantity is 0: return remove_item_shopping_cart(username, item_id) if ItemsLogic.check_in_stock(item_id, new_quantity) is False: return False user = RegisteredUsers.get_user(username) if user is not False: return ShoppingCartDB.update_item_shopping_cart( username, item_id, new_quantity) return False
def test_remove_item_from_cart(self): username = '******' item_id = 1 quantity = 20 ShoppingCartDB.add_item_shopping_cart( ShoppingCartDB.ShoppingCartItem(username, item_id, quantity, "")) ShoppingCartDB.remove_item_shopping_cart(username, item_id) cart_items = ShoppingCartDB.get_cart_items(username) self.assertEqual(len(cart_items), 0)
def test_get_cart_items(self): username = '******' item_id1 = 1 quantity1 = 20 item_id2 = 2 quantity2 = 2 ShoppingCartDB.add_item_shopping_cart( ShoppingCartDB.ShoppingCartItem(username, item_id1, quantity1, "")) ShoppingCartDB.add_item_shopping_cart( ShoppingCartDB.ShoppingCartItem(username, item_id2, quantity2, "")) cart_items = ShoppingCartDB.get_cart_items(username) self.assertEqual(len(cart_items), 2) self.assertEqual(username, cart_items[0].username) self.assertEqual(item_id1, cart_items[0].item_id) self.assertEqual(quantity1, cart_items[0].item_quantity) self.assertEqual(username, cart_items[1].username) self.assertEqual(item_id2, cart_items[1].item_id) self.assertEqual(quantity2, cart_items[1].item_quantity)
def remove_item_shopping_cart(username, item_id): if username is not None and item_id is not None: user = RegisteredUsers.get_user(username) if user is not False: return ShoppingCartDB.remove_item_shopping_cart(username, item_id)
def remove_shopping_cart(username): if username is not None: return ShoppingCartDB.remove_shopping_cart(username)
def check_empty_cart_user(username): return ShoppingCartDB.check_empty(username)
def get_cart_items(username): if username is not None: return ShoppingCartDB.get_cart_items(username)
def remove_shopping_cart_db(username): return ShoppingCartDB.remove_shopping_cart(username)
def pay_all(login_token): lotteries = [] if login_token is not None: # check if cart has items empty = check_empty_cart_user(login_token) if empty is not True: username = Consumer.loggedInUsers[login_token] # if so, check foreach item if the requested amount exist cart_items = get_cart_items(login_token) shopping_policy_status = shopping_policy_check(username, cart_items) if shopping_policy_status is not True: return shopping_policy_status # cart_items is a array consist of shopping_cart objects message = check_stock_for_shopping_cart(cart_items) if message is not True: return message # if so, sum all items costs, get from costumer his credentials total_cost = 0 # for each item, calculate visible_discount purchase_id = Purchases.add_purchase_and_return_id(datetime.now(), username, 0) if purchase_id is False: return 'Something went wrong with the purchase' for shopping_cart_item in cart_items: item = get_item(shopping_cart_item.item_id) new_price = get_new_price_for_item(item, shopping_cart_item) lottery_message = check_lottery_ticket(item, shopping_cart_item, username) if lottery_message is not True: return lottery_message total_cost = total_cost + shopping_cart_item.item_quantity * new_price status = PurchasedItems.add_purchased_item(purchase_id, shopping_cart_item.item_id, shopping_cart_item.item_quantity, shopping_cart_item.item_quantity * new_price) if status is False: return 'Something went wrong with the purchase' status = update_purchase_total_price(purchase_id, total_cost) if status is False: return 'Something went wrong with the purchase' new_quantity = item.quantity - shopping_cart_item.item_quantity if item.kind == 'ticket': if new_quantity == 0: lotteries.append(item.id) status = ItemsLogic.update_stock(item.id, new_quantity) if status is False: return 'Something went wrong with the purchase' # live alerts owners = Owners.get_owners_by_shop(item.shop_name) owners_name = [] for owner in owners: if owner.should_notify > 0: owners_name.append(owner.username) PurchasesAlerts.notify_purchasing_alerts(owners_name, '<strong>' + username + '</strong> has bought item <a href="http://localhost:8000/app/item/?item_id=' + str( item.id) + '"># <strong>' + str( item.id) + '</strong></a> from your shop') pay_confirmation = ExternalSystems.payment.pay(total_cost, username) if pay_confirmation is False: return 'Payment System Denied.' sup_confirmation = ExternalSystems.supply.supply_a_purchase(username, purchase_id) if sup_confirmation is False: return 'Supply System Denied.' remove_shopping_cart(login_token) status = ShoppingCartDB.remove_shopping_cart(username) lottery_ending_check(lotteries) if status: LoggerLogic.add_event_log(username, "PAY ALL") return [purchase_id, total_cost] return 'Shopping cart is empty'