def remove_user(username, registered_user): if username is not None and registered_user is not None: if SystemManagers.is_system_manager(username) is not False: sys_manager = SystemManagers.is_system_manager( registered_user.username) is_store_manager = StoreManagers.is_store_manager( registered_user.username) is_owner = Owners.is_owner(registered_user.username) if sys_manager is False: user = RegisteredUsers.get_user(registered_user.username) if user is not False: result_delete = True if is_store_manager is not False: result_delete = StoreManagers.remove_manager( registered_user.username) else: if is_owner is not False: result_delete = Owners.remove_owner( registered_user.username) final_result = result_delete and RegisteredUsers.remove_user( registered_user.username) if final_result: LoggerLogic.add_event_log(username, "DELETE USER") return final_result return False return False
def add_owner(username, owner): if username is not None: if Owners.get_owner(username, owner.shop_name) is not False: if RegisteredUsers.get_user(owner.username) is not False: if Owners.get_owner(owner.username, owner.shop_name) is not False: to_return = 'FAILED! ' + owner.username + ' is already an owner' return to_return result = Owners.add_owner(owner) if result: result = HistoryAppointings.add_history_appointing( username, owner.username, 'Owner', owner.shop_name, strftime("%d-%m-%Y %H:%M:%S", gmtime()), 'Owner') else: return "FAILED" if result and is_manager_of_shop(owner.username, owner.shop_name): result = remove_store_manager(username, owner.shop_name, owner.username) if result: return "SUCCESS" else: return "FAILED" elif result: return "SUCCESS" else: return "FAILED" else: return "FAILED: Username does not exists" else: return "FAILED: You are not the owner of this shop" else: return "FAILED: Missing Parameters"
def test_modify_notifications(self): ShopLogic.create_shop(SHOP, USERNAME) UsersLogic.modify_notifications(USERNAME, 0, SHOP.name) owner = Owners.get_owner(USERNAME, SHOP_NAME) self.assertEqual(0, owner.should_notify) UsersLogic.modify_notifications(USERNAME, 1, SHOP.name) owner = Owners.get_owner(USERNAME, SHOP_NAME) self.assertEqual(1, owner.should_notify)
def update_shopping_policy_on_shop(username, policy_id, field_name, new_value, shop_name): if policy_id is not None and field_name is not None and new_value is not None: if int(policy_id) < 0: return "FAILED: Invalid id of Policy" if field_name not in ['shop_name', 'conditions', 'restriction', 'quantity']: return "FAILED: Invalid field name" if Owners.get_owner(username, shop_name) is not False: if field_name in ['conditions']: status = checkConditionsSyntax(new_value) if new_value == "": new_value = "1=1" if status is not True: return status if not ShoppingPolicies.update_shopping_policy_on_shop(policy_id, field_name, new_value): return "FAILED: DB error." LoggerLogic.add_event_log(username, "POLICY: UPDATE SHOP SHOPPING POLICY") return True manager = StoreManagers.get_store_manager(username, shop_name) if manager is not False: if manager.permission_set_policy > 0: if field_name in ['conditions']: status = checkConditionsSyntax(new_value) if new_value == "": new_value = "1=1" if status is not True: return status if not ShoppingPolicies.update_shopping_policy_on_shop(policy_id, field_name, new_value): return "FAILED: DB error." LoggerLogic.add_event_log(username, "POLICY: UPDATE SHOP SHOPPING POLICY") return True return 'FAILED: no permissions!' return 'FAILED: you are not a the Owner of the shop' return "FAILED: One (or more) of the parameters is None"
def send_message_from_shop(username, message): output = False manager = StoreManagers.get_store_manager(username, message.from_username) if Shops.search_shop( message.to_username) is False and RegisteredUsers.is_user_exists( message.to_username) is False: return "FAILED: Target does not exists" if manager is not False: if manager.permission_reply_messages > 0: output = Messages.send_message_from_shop(message) else: return "FAILED: You don't have the permissions" if Owners.get_owner(username, message.from_username) is not False: output = Messages.send_message_from_shop(message) else: return "FAILED: You are not authorized" if output: users = [message.to_username] if message.to_username == 'System': SMs = SystemManagers.get_all_system_managers() SM_names = [] for sm in SMs: SM_names.append(sm.username) users = SM_names MessagingAlerts.notify_messaging_alerts( users, '<a href = "../app/home/messages/?content=received" >' ' You Have a new message from <strong>Shop</strong>' + message.from_username + '</a>') return "SUCCESS" else: return "FAILED"
def add_invisible_discount_category(disc, username): if disc is not None and username is not None and 0 <= disc.percentage <= 100: is_owner = Owners.get_owner(username, disc.shop_name) is_manager = StoreManagers.get_store_manager(username, disc.shop_name) if is_owner is not False or (is_manager is not False and is_manager.discount_permission == 1): return Discount.add_invisible_discount_category(disc) return False
def remove_shopping_policy_on_shop(username, policy_id, shop_name): if policy_id is not None and policy_id > 0: if Owners.get_owner(username, shop_name) is not False: if not ShoppingPolicies.remove_shopping_policy_on_shop(policy_id): return "FAILED: DB error." LoggerLogic.add_event_log(username, "POLICY: REMOVE SHOP SHOPPING POLICY") return True return 'FAILED: you are not a the Owner of the shop' return "FAILED: Invalid id of Policy"
def get_all_sent_shop_messages(username, shop_name): manager = StoreManagers.get_store_manager(username, shop_name) if manager is not False: if manager.permission_get_all_messages > 0: if Shops.search_shop(shop_name) is not False: return Messages.get_all_sent_shop_messages(shop_name) if Owners.get_owner(username, shop_name) is not False: if Shops.search_shop(shop_name) is not False: return Messages.get_all_sent_shop_messages(shop_name) return False
def get_shop_purchase_history(username, shop_name): manager = StoreManagers.get_store_manager(username, shop_name) owner = Owners.get_owner(username, shop_name) if manager is not False: if manager.permission_get_purchased_history > 0: return PurchasedItems.get_purchased_items_by_shop(shop_name) else: if owner is not False: return PurchasedItems.get_purchased_items_by_shop(shop_name) return False
def re_open_shop(username, shop_name): owner_of_shop = Owners.get_owner(username, shop_name) if owner_of_shop is not False: result = Shops.re_open_shop(shop_name) if result: LoggerLogic.add_event_log(username, "SHOP STATUS CHANGED - RE-OPEN") return result else: return False
def edit_shop_item(username, item_id, field_name, new_value): item = Items.get_item(item_id) result = StoreManagers.get_store_manager(username, item.shop_name) if result is not False: edit_item_permission = result.permission_edit_item if edit_item_permission > 0: return Items.update_item(item_id, field_name, new_value) else: if Owners.get_owner(username, item.shop_name) is not False: return Items.update_item(item_id, field_name, new_value) return False
def add_item_to_shop(item, username): if item is not None and item.shop_name is not None and username is not None and item.quantity >= 0 \ and item.price >= 0: manager = StoreManagers.get_store_manager(username, item.shop_name) if manager is not False: add_item_permission = manager.permission_add_item if add_item_permission > 0: return Items.add_item_to_shop(item) if Owners.get_owner(username, item.shop_name) is not False: return Items.add_item_to_shop(item) return False
def create_shop(shop, username): if shop is not None and username is not None: if Shops.search_shop(shop.name) is False: if Shops.create_shop(shop): if Owners.add_owner(Owner(username, shop.name, None)): LoggerLogic.add_event_log(username, "OPEN SHOP") return "SUCCESS" return "FAILED: Adding Owner" return "FAILED: Adding Shop" return "FAILED: Shop name is taken" return "FAILED: Missing parameters"
def close_shop(username, shop_name): owner_of_shop = Owners.get_owner(username, shop_name) if owner_of_shop is not False: result = Shops.close_shop(shop_name) if result: lotteries = get_lotteries_by_shop(shop_name) for lottery in lotteries: lottery_timer(lottery.id) LoggerLogic.add_event_log(username, "SHOP STATUS CHANGED - CLOSE") return result else: return False
def remove_item_from_shop(item_id, username): if item_id is not None: item = Items.get_item(item_id) if item is not False: manager = StoreManagers.get_store_manager(username, item.shop_name) if manager is not False: remove_item_permission = manager.permission_remove_item if remove_item_permission > 0: return Items.remove_item_from_shop(item_id) elif Owners.is_owner(username): return Items.remove_item_from_shop(item_id) return False
def update_permissions(username, store_manager): if Owners.is_owner_on_shop(username, store_manager.store_name) is not False: previous_store_manager = StoreManagers.get_store_manager( store_manager.username, store_manager.store_name) status = StoreManagers.update_permissions(store_manager) if status: if isEmptyPermissions(previous_store_manager): status = HistoryAppointings.update_history_appointing( username, store_manager.username, store_manager.store_name, getPermissionsString(store_manager)) return status return False
def pay_all_guest(guest): if guest is not None: # check if cart has items empty = check_empty_cart_guest(guest) if empty is not True: purchase_id = 0 # if so, check foreach item if the requested amount exist cart_items = Consumer.guestShoppingCart[guest] # cart_items is a array consist of shopping_cart objects shopping_policy_status = UserShoppingCartLogic.shopping_policy_check( "guest", cart_items) if shopping_policy_status is not True: return shopping_policy_status 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 for shopping_cart_item in cart_items: item = get_item(shopping_cart_item.item_id) new_price = UserShoppingCartLogic.get_new_price_for_item( item, shopping_cart_item) total_cost = total_cost + shopping_cart_item.item_quantity * new_price new_quantity = item.quantity - shopping_cart_item.item_quantity 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: owners_name.append(owner.username) PurchasesAlerts.notify_purchasing_alerts( owners_name, '<strong>' + guest + '</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, guest) if pay_confirmation is False: return 'Payment System Denied.' sup_confirmation = ExternalSystems.supply.supply_a_purchase( guest, purchase_id) if sup_confirmation is False: return 'Supply System Denied.' status = remove_shopping_cart_guest(guest) if status is False: return 'Something went wrong with the purchase' LoggerLogic.add_event_log("GUEST", "PAY ALL") return [purchase_id, total_cost] return 'Shopping cart is empty'
def add_manager(username, store_manager): if username is not None: if Owners.get_owner(username, store_manager.store_name) is not False: if RegisteredUsers.get_user(store_manager.username) is not False: if store_manager.store_name is not None: if Owners.get_owner(store_manager.username, store_manager.store_name): return "FAILED! An owner can't be store manager" if StoreManagers.add_manager(store_manager): if HistoryAppointings.add_history_appointing( username, store_manager.username, 'Store Manager', store_manager.store_name, strftime("%d-%m-%Y %H:%M:%S", gmtime()), ''): return 'SUCCESS' return "FAILED" return "FAILED" else: return "FAILED: Shop does not exists" else: return "FAILED: User does not exists" else: return "FAILED: You are not the owner of this shop" else: return "FAILED: Missing Parameters"
def add_shopping_policy_on_shop(username, shop_name, conditions, restriction, quantity): if shop_name is not None and conditions is not None: if restriction is not None and quantity is not None: if restriction not in ['N', 'AL', 'E', 'UT']: return "FAILED: Invalid value of restriction." if int(quantity) < 0: return "FAILED: Negative quantity is invalid." if Owners.get_owner(username, shop_name) is not False: if not ShoppingPolicies.add_shopping_policy_on_shop(shop_name, conditions, restriction, quantity): return "FAILED: DB error." LoggerLogic.add_event_log(username, "POLICY: ADD SHOP SHOPPING POLICY") return True manager = StoreManagers.get_store_manager(username,shop_name) if manager is not False: if manager.permission_set_policy > 0: if not ShoppingPolicies.add_shopping_policy_on_shop(shop_name, conditions, restriction, quantity): return "FAILED: DB error." LoggerLogic.add_event_log(username, "POLICY: ADD SHOP SHOPPING POLICY") return True return 'FAILED: no permissions!' return 'FAILED: you are not a the Owner of the shop' return "FAILED: One (or more) of the parameters is None" return "FAILED: One (or more) of the parameters is None"
def get_owned_shops(username): return Owners.get_shops_by_owner(username)
def get_store_owners(shop_name): return Owners.get_owners_by_shop(shop_name)
def is_owner(username, shop_name): return Owners.get_owner(username, shop_name) is not False
def test_add_owner(self): ShopLogic.create_shop(SHOP, USERNAME) owner = Owners.get_owner(USERNAME, SHOP.name) self.assertEqual(USERNAME, owner.username) self.assertEqual(SHOP.name, owner.shop_name)
def test_torture3(self): # Adding Users status = UsersLogic.register( RegisteredUser('u1ser1u1ser1', 'wxde12exd12')) self.assertTrue(status) status = UsersLogic.register(RegisteredUser('u2ser2u2ser2', '34c124c1')) self.assertTrue(status) status = UsersLogic.register( RegisteredUser('u3ser3u3ser3', '1c241c24c1')) self.assertTrue(status) status = UsersLogic.register( RegisteredUser('u4ser4u4ser4', '3214v132v4132')) self.assertTrue(status) status = UsersLogic.register(RegisteredUser('u5seru5ser', '12121212')) self.assertTrue(status) # Adding System Managers status = UsersLogic.add_system_manager( SystemManager('sys1sys1', 'POWER123')) self.assertTrue(status) # Creating Shops status = ShopLogic.create_shop(Shop('myShop1', 'Active'), 'u1ser1u1ser1') self.assertTrue(status) status = ShopLogic.create_shop(Shop('myShop2', 'Active'), 'u2ser2u2ser2') self.assertTrue(status) status = UsersLogic.add_owner('u1ser1u1ser1', Owner('u3ser3u3ser3', 'myShop1', 0)) self.assertTrue(status) owner = Owners.get_owner('u1ser1u1ser1', 'myShop1') status = UsersLogic.add_manager( owner.username, StoreManager('u4ser4u4ser4', 'myShop1', 1, 1, 1, 1, 1, 1, 1, 1)) status = UsersLogic.add_manager( 'u2ser2u2ser2', StoreManager('u4ser4u4ser4', 'myShop2', 1, 1, 1, 1, 1, 1, 1, 1)) ItemsLogic.add_item_to_shop( Item(None, 'myShop1', 'banana', 'fruits', 'fruit;healthy;yellow', 4.90, 300, 'regular', None, 0, 0, 0), 'u4ser4u4ser4') ItemsLogic.add_item_to_shop( Item(None, 'myShop2', 'doll', 'toys', 'fun', 30, 10, 'regular', None, 0, 0, 0), 'u2ser2u2ser2') ItemsLogic.add_item_to_shop( Item(None, 'myShop1', 'soda', 'drinks', 'good', 4.90, 20, 'regular', None, 0, 0, 0), 'u1ser1u1ser1') ItemsLogic.add_item_to_shop( Item(None, 'myShop2', 'cucumber', 'vegetables', 'fun', 4.90, 300, 'regular', None, 0, 0, 0), 'u4ser4u4ser4') ItemsLogic.add_item_to_shop( Item(None, 'myShop1', 'vodka', 'drinks', 'bad;for;your;health', 70, 2, 'regular', None, 0, 0, 0), 'u3ser3u3ser3') username1 = 'u4ser4u4ser4' username2 = 'u2ser2u2ser2' username3 = 'u1ser1u1ser1' username4 = 'u3ser3u3ser3' username5 = 'u5seru5ser' access_token1 = hashlib.md5(username1.encode()).hexdigest() Consumer.loggedInUsers[access_token1] = username1 Consumer.loggedInUsersShoppingCart[access_token1] = [] access_token2 = hashlib.md5(username2.encode()).hexdigest() Consumer.loggedInUsers[access_token2] = username2 Consumer.loggedInUsersShoppingCart[access_token2] = [] access_token3 = hashlib.md5(username3.encode()).hexdigest() Consumer.loggedInUsers[access_token3] = username3 Consumer.loggedInUsersShoppingCart[access_token3] = [] access_token4 = hashlib.md5(username4.encode()).hexdigest() Consumer.loggedInUsers[access_token4] = username4 Consumer.loggedInUsersShoppingCart[access_token4] = [] access_token5 = hashlib.md5(username5.encode()).hexdigest() Consumer.loggedInUsers[access_token5] = username5 Consumer.loggedInUsersShoppingCart[access_token5] = [] UserShoppingCartLogic.add_item_shopping_cart( access_token5, ShoppingCartItem('u5seru5ser', 1, 10, None)) UserShoppingCartLogic.add_item_shopping_cart( access_token5, ShoppingCartItem('u5seru5ser', 2, 5, None)) UserShoppingCartLogic.add_item_shopping_cart( access_token5, ShoppingCartItem('u5seru5ser', 3, 15, None)) items = UserShoppingCartLogic.get_cart_items(access_token5) self.assertEqual(len(items), 3) self.assertEqual(items[0].code, None) UserShoppingCartLogic.remove_item_shopping_cart(access_token5, 1) items = UserShoppingCartLogic.get_cart_items(access_token5) self.assertEqual(len(items), 2) UserShoppingCartLogic.remove_item_shopping_cart(access_token5, 2) items = UserShoppingCartLogic.get_cart_items(access_token5) self.assertEqual(len(items), 1) # Only item id 3 left UserShoppingCartLogic.pay_all(access_token5) items1 = UsersLogic.get_purchase_history('u5seru5ser') items2 = ItemsLogic.get_all_purchased_items('sys1sys1') items3 = ShopLogic.get_shop_purchase_history('u4ser4u4ser4', 'myShop1') self.assertEqual(items1[0].item_id, items2[0].item_id) self.assertEqual(items2[0].quantity, items3[0].quantity) self.assertEqual(items1[0].price, items3[0].price) self.assertTrue('Nadav Ha Gever')
def test_torture2(self): # Adding Users status = UsersLogic.register( RegisteredUser('u1ser1u1ser1', 'wxde12exd12')) self.assertTrue(status) status = UsersLogic.register(RegisteredUser('u2ser2u2ser2', '34c124c1')) self.assertTrue(status) status = UsersLogic.register( RegisteredUser('u3ser3u3ser3', '1c241c24c1')) self.assertTrue(status) status = UsersLogic.register( RegisteredUser('u4ser4u4ser4', '3214v132v4132')) self.assertTrue(status) status = UsersLogic.register(RegisteredUser('u5seru5ser', '12121212')) self.assertTrue(status) # Adding System Managers status = UsersLogic.add_system_manager( SystemManager('sys1sys1', 'POWER123')) self.assertTrue(status) # Creating Shops status = ShopLogic.create_shop(Shop('myShop1', 'Active'), 'u1ser1u1ser1') self.assertTrue(status) status = ShopLogic.create_shop(Shop('myShop2', 'Active'), 'u2ser2u2ser2') self.assertTrue(status) status = UsersLogic.add_owner('u1ser1u1ser1', Owner('u3ser3u3ser3', 'myShop1', 0)) self.assertTrue(status) owner = Owners.get_owner('u1ser1u1ser1', 'myShop1') status = UsersLogic.add_manager( owner.username, StoreManager('u4ser4u4ser4', 'myShop1', 1, 1, 1, 1, 1, 1, 1, 1)) status = UsersLogic.add_manager( 'u2ser2u2ser2', StoreManager('u4ser4u4ser4', 'myShop2', 1, 1, 1, 1, 1, 1, 1, 1)) manager = StoreManagers.get_store_manager('u4ser4u4ser4', 'myShop1') self.assertEqual(manager.permission_reply_messages, 1) ItemsLogic.add_item_to_shop( Item(None, 'myShop1', 'banana', 'fruits', 'fruit;healthy;yellow', 4.90, 300, 'regular', None, 0, 0, 0), 'u4ser4u4ser4') ItemsLogic.add_item_to_shop( Item(None, 'myShop2', 'doll', 'toys', 'fun', 30, 10, 'regular', None, 0, 0, 0), 'u2ser2u2ser2') ItemsLogic.add_item_to_shop( Item(None, 'myShop1', 'soda', 'drinks', 'good', 4.90, 20, 'regular', None, 0, 0, 0), 'u1ser1u1ser1') ItemsLogic.add_item_to_shop( Item(None, 'myShop2', 'cucumber', 'vegetables', 'fun', 4.90, 300, 'regular', None, 0, 0, 0), 'u4ser4u4ser4') ItemsLogic.add_item_to_shop( Item(None, 'myShop1', 'vodka', 'drinks', 'bad;for;your;health', 70, 2, 'regular', None, 0, 0, 0), 'u3ser3u3ser3') items = SearchLogic.search_by_name('banana') self.assertEqual(items[0].quantity, 300) self.assertEqual(items[0].price, 4.90) self.assertEqual(len(items), 1) items = SearchLogic.search_by_category('drinks') self.assertEqual(items[0].quantity, 20) self.assertEqual(items[1].price, 70) self.assertEqual(len(items), 2) items = SearchLogic.search_by_keywords('fun') self.assertEqual(items[0].quantity, 10) self.assertEqual(items[1].price, 4.90) self.assertEqual(len(items), 2) items = SearchLogic.search_items_in_shop('myShop2') self.assertEqual(items[0].name, 'doll') self.assertEqual(items[1].name, 'cucumber') self.assertEqual(len(items), 2) MessagingLogic.send_message_from_shop( 'u4ser4u4ser4', Message(None, 'myShop1', 'u5seru5ser', 'Nadav is our lord and savior')) messages = MessagingLogic.get_all_messages('u5seru5ser') self.assertEqual(len(messages), 1) self.assertEqual(messages[0].content, 'Nadav is our lord and savior') MessagingLogic.send_message( Message(None, 'u5seru5ser', 'myShop1', 'Hello Shop')) messages = MessagingLogic.get_all_shop_messages( 'u4ser4u4ser4', 'myShop1') self.assertEqual(len(messages), 1) self.assertEqual(messages[0].content, 'Hello Shop') MessagingLogic.send_message_from_shop( 'u1ser1u1ser1', Message(None, 'myShop1', 'myShop2', 'Hello Shop2')) messages = MessagingLogic.get_all_shop_messages( 'u2ser2u2ser2', 'myShop2') self.assertEqual(len(messages), 1) self.assertEqual(messages[0].content, 'Hello Shop2') MessagingLogic.send_message( Message(None, 'u1ser1u1ser1', 'u3ser3u3ser3', 'Shop2 Sucks!')) messages = MessagingLogic.get_all_messages('u3ser3u3ser3') self.assertEqual(messages[0].content, 'Shop2 Sucks!') UsersLogic.close_shop('u1ser1u1ser1', 'myShop1') items = SearchLogic.search_by_name('banana') self.assertEqual(len(items), 0)
def modify_notifications(owner_username, should_notify, shop_name): return Owners.modify_notifications(owner_username, should_notify, shop_name)
def test_add_owner_bad_owner(self): ShopLogic.create_shop(SHOP, USERNAME) is_owner = Owners.get_owner(OTHER_USERNAME, SHOP.name) self.assertFalse(is_owner)
def test_add_owner_bad_shop(self): ShopLogic.create_shop(SHOP, USERNAME) is_owner = Owners.get_owner(USERNAME, OTHER_SHOP_NAME) self.assertFalse(is_owner)
def is_owner_on_shop(username, shop_name): return Owners.is_owner_on_shop(username, shop_name)
def remove_store_manager(username, shop_name, target_id): if Owners.is_owner_on_shop(username, shop_name) is not False: return StoreManagers.remove_manager_from_shop(target_id, shop_name) return False