def update_rating(item_id, rank): if item_id is not None and 0 <= int(rank) <= 10: sum = Items.get_item(item_id).sum_of_rankings + int(rank) num = Items.get_item(item_id).num_of_reviews + 1 return Items.update_item(item_id, 'sum_of_rankings', sum) and \ Items.update_item(item_id, 'num_of_reviews', num) and \ Items.update_item(item_id, 'item_rating', sum / num)
def test_permissions(self): UsersLogic.register(RegisteredUser('ShaharShahar', '1212345678')) UsersLogic.register(RegisteredUser('TomerTomerLev', '65412321')) shop = Shop('myShop', 'Active') ShopLogic.create_shop(shop, 'ShaharShahar') UsersLogic.add_manager( 'ShaharShahar', StoreManager('TomerTomerLev', 'myShop', 1, 1, 1, 1, 1, 1, 1, 1)) ItemsLogic.add_item_to_shop( Item(None, 'myShop', 'doll', 'toys', 'toys:kids', 20, 300, 'regular', None, 0, 0, 0), 'TomerTomerLev') item = Items.get_item(1) self.assertEqual(item.shop_name, 'myShop') self.assertEqual(item.price, 20) self.assertEqual(item.quantity, 300) status = ItemsLogic.edit_shop_item('TomerTomerLev', 1, 'price', 40) self.assertTrue(status) status = ItemsLogic.edit_shop_item('TomerTomerLev', 1, 'name', 'doll_new') self.assertTrue(status) status = ItemsLogic.edit_shop_item('TomerTomerLev', 1, 'quantity', 40) self.assertTrue(status) item = Items.get_item(1) self.assertEqual(item.name, 'doll_new') self.assertEqual(item.quantity, 40) self.assertEqual(item.keyWords, 'toys:kids') status = ItemsLogic.remove_item_from_shop(1, 'TomerTomerLev') self.assertTrue(status)
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 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 check_in_stock(item_id, amount): if item_id is not None and amount is not None and amount > 0: item = Items.get_item(item_id) if item is not False: if item.quantity >= amount: return True return False
def search_by_keywords(item_keywords): if item_keywords is not None: matched_items = [] splitted_keywords = item_keywords.split() for keyword in splitted_keywords: matched_items.append(Items.search_items_by_keywords(keyword)) seen = set() set_to_return = [] for my_list in matched_items: for obj in my_list: if obj.id not in seen: set_to_return.append(obj) seen.add(obj.id) return set_to_return
def get_all_reviews_on_item(item_id): if item_id is not None: item = Items.get_item(item_id) if item is not False: return ReviewsOnItems.get_all_reviews_on_item(item_id)
def get_item_by_code(code): return Items.get_item_by_code(code)
def get_item(item_id): return Items.get_item(item_id)
def search_by_category(item_category): if item_category is not None: return Items.search_items_by_category(item_category)
def get_item_without_lottery(item_id): item = Items.get_item(item_id) if item is False or item.kind == "ticket": return False return item
def get_id_by_name(item_name): return Items.get_id_by_name(item_name)
def get_top_five_ranked_items(): return Items.get_top_five_ranked_items()
def search_by_name(item_name): if item_name is not None: return Items.search_items_by_name(item_name) else: return False
def get_shop_items(shop_name): return Items.get_shop_items(shop_name=shop_name)
def search_items_in_shop(shop_name): if shop_name is not None: return Items.search_items_in_shop(shop_name) else: return False