def test_add_get_policy(self): status = ShoppingPolicyLogic.add_shopping_policy_on_shop('ShaharBenS2', 'eBay', "", "N", 0) self.assertTrue(StoB(status)) status = ShoppingPolicyLogic.add_shopping_policy_on_shop('ShaharBenS2', 'eBay', "", "UT", 0) self.assertTrue(StoB(status)) status = ShoppingPolicyLogic.add_shopping_policy_on_identity('Ultimate_ShaharShahar', "", "N", 0) self.assertTrue(StoB(status)) status = ShoppingPolicyLogic.add_shopping_policy_on_identity('Ultimate_ShaharShahar', "", "N", 0) self.assertTrue(StoB(status)) status = ShoppingPolicyLogic.add_shopping_policy_on_category('Ultimate_ShaharShahar', "", "", "N", 0) self.assertTrue(StoB(status)) status = ShoppingPolicyLogic.add_shopping_policy_on_category('Ultimate_ShaharShahar', "", "", "N", 0) self.assertTrue(StoB(status)) status = ShoppingPolicyLogic.add_shopping_policy_on_items('Ultimate_ShaharShahar', "", "", "N", 0) self.assertTrue(StoB(status)) status = ShoppingPolicyLogic.add_shopping_policy_on_items('Ultimate_ShaharShahar', "", "", "N", 0) self.assertTrue(StoB(status)) IP = ShoppingPolicyLogic.get_all_shopping_policy_on_identity() CP = ShoppingPolicyLogic.get_all_shopping_policy_on_category() ITP = ShoppingPolicyLogic.get_all_shopping_policy_on_items() SP = ShoppingPolicyLogic.get_all_shopping_policy_on_shop('eBay') self.assertEqual(len(IP), 2) self.assertEqual(len(CP), 2) self.assertEqual(len(ITP), 2) self.assertEqual(len(SP), 2)
def getGlobalShoppingPolicyConditions(request): if request.method == 'GET': policy_id = request.GET.get('policy_id') global_policies = ShoppingPolicyLogic.get_all_shopping_policy_on_identity() for GP in global_policies: if GP.policy_id == int(policy_id): if GP.conditions == "1=1": return HttpResponse("") return HttpResponse(GP.conditions.replace("'", "''")) return HttpResponse("FAILED: Can't find that policy")
def test_update_policy(self): status = True status &= StoB(ShoppingPolicyLogic.add_shopping_policy_on_shop('ShaharBenS2', 'eBay', "", "N", 0)) status &= StoB(ShoppingPolicyLogic.add_shopping_policy_on_identity('Ultimate_ShaharShahar', "", "N", 0)) status &= StoB(ShoppingPolicyLogic.add_shopping_policy_on_category('Ultimate_ShaharShahar', "", "", "N", 0)) status &= StoB(ShoppingPolicyLogic.add_shopping_policy_on_items('Ultimate_ShaharShahar', "", "", "N", 0)) status &= StoB(ShoppingPolicyLogic.update_shopping_policy_on_shop('ShaharBenS2', 1, "restriction", "N", 'eBay')) status &= StoB(ShoppingPolicyLogic.update_shopping_policy_on_shop('ShaharBenS2', 1, "quantity", 10, 'eBay')) status &= StoB( ShoppingPolicyLogic.update_shopping_policy_on_identity('Ultimate_ShaharShahar', 1, "restriction", "AL")) status &= StoB( ShoppingPolicyLogic.update_shopping_policy_on_identity('Ultimate_ShaharShahar', 1, "quantity", 4)) status &= StoB( ShoppingPolicyLogic.update_shopping_policy_on_category('Ultimate_ShaharShahar', 1, "restriction", "E")) status &= StoB( ShoppingPolicyLogic.update_shopping_policy_on_category('Ultimate_ShaharShahar', 1, "quantity", 3)) status &= StoB( ShoppingPolicyLogic.update_shopping_policy_on_category('Ultimate_ShaharShahar', 1, "category", "books")) status &= StoB( ShoppingPolicyLogic.update_shopping_policy_on_items('Ultimate_ShaharShahar', 1, "restriction", "UT")) status &= StoB(ShoppingPolicyLogic.update_shopping_policy_on_items('Ultimate_ShaharShahar', 1, "quantity", 10)) status &= StoB( ShoppingPolicyLogic.update_shopping_policy_on_items('Ultimate_ShaharShahar', 1, "item_name", "DP by GoF")) self.assertTrue(status) IP = ShoppingPolicyLogic.get_all_shopping_policy_on_identity() CP = ShoppingPolicyLogic.get_all_shopping_policy_on_category() ITP = ShoppingPolicyLogic.get_all_shopping_policy_on_items() SP = ShoppingPolicyLogic.get_all_shopping_policy_on_shop('eBay') self.assertEqual(IP[0].restriction, "AL") self.assertEqual(IP[0].quantity, 4) self.assertEqual(CP[0].restriction, "E") self.assertEqual(CP[0].quantity, 3) self.assertEqual(CP[0].category, "books") self.assertEqual(ITP[0].restriction, "UT") self.assertEqual(ITP[0].quantity, 10) self.assertEqual(ITP[0].item_name, "DP by GoF") self.assertEqual(SP[0].restriction, "N") self.assertEqual(SP[0].quantity, 10)
def check_identity_shopping_policies(username, cart_items): identity_policies = ShoppingPolicyLogic.get_all_shopping_policy_on_identity( ) for identity_policy in identity_policies: if username is not "guest": if is_meet_conditions(username, identity_policy.conditions) is False: continue if identity_policy.restriction is 'N': continue elif identity_policy.restriction is 'AL': if len(cart_items) < identity_policy.quantity: return "FAILED: Not enough items in cart; You allowed at least " + identity_policy.quantity elif identity_policy.restriction is 'E': if len(cart_items) != identity_policy.quantity: return "FAILED: Not exact num of items in cart; You allowed exactly " + identity_policy.quantity elif identity_policy.restriction is 'UT': if len(cart_items) > identity_policy.quantity: return "FAILED: Too much items in cart; You allowed at most " + identity_policy.quantity return True
def check_identity_shopping_policies(username, cart_items): identity_policies = ShoppingPolicyLogic.get_all_shopping_policy_on_identity() for identity_policy in identity_policies: if not (username == "guest"): if is_meet_conditions(username, identity_policy.conditions) is False: continue if identity_policy.restriction == 'N': continue num_of_items = 0 for cart_item in cart_items: num_of_items = num_of_items + cart_item.item_quantity if identity_policy.restriction == 'AL': if num_of_items < identity_policy.quantity: return "FAILED: Not enough items in cart; You allowed at least " + str(identity_policy.quantity) elif identity_policy.restriction == 'E': if num_of_items != identity_policy.quantity: return "FAILED: Not exact num of items in cart; You allowed exactly " + str(identity_policy.quantity) elif identity_policy.restriction == 'UT': if num_of_items > identity_policy.quantity: return "FAILED: Too much items in cart; You allowed at most " + str(identity_policy.quantity) return True
def get_all_shopping_policy_on_identity(request): if request.method == 'GET': return ShoppingPolicyLogic.get_all_shopping_policy_on_identity() return HttpResponse('FAILED')
def get_system_policies(request): if request.method == 'GET': login = request.COOKIES.get('login_hash') if login is not None: username = Consumer.loggedInUsers.get(login) if username is not None: if UsersLogic.is_system_manager(username): item_policies_html = "" item_policies = ShoppingPolicyLogic.get_all_shopping_policy_on_items( ) for item_policy in item_policies: is_none = "" selectors = {} if item_policy.restriction is 'N': is_none = "disabled" selectors['N'] = "" selectors['UT'] = "" selectors['AL'] = "" selectors['E'] = "" selectors[ item_policy.restriction] = 'selected="selected"' item_policies_html += loader.render_to_string( 'components/shopping_item_policy.html', context={ 'id': item_policy.policy_id, 'item_name': item_policy.item_name, 'selector_value': item_policy.restriction, 'quantity': item_policy.quantity, 'is_none': is_none, 'N_S': selectors.get('N'), 'UT_S': selectors.get('UT'), 'AL_S': selectors.get('AL'), 'E_S': selectors.get('E'), }) category_policies_html = "" category_policies = ShoppingPolicyLogic.get_all_shopping_policy_on_category( ) for category_policy in category_policies: is_none = "" selectors = {} if category_policy.restriction is 'N': is_none = "disabled" selectors['N'] = "" selectors['UT'] = "" selectors['AL'] = "" selectors['E'] = "" selectors[category_policy. restriction] = 'selected="selected"' category_policies_html += loader.render_to_string( 'components/shopping_category_policy.html', context={ 'id': category_policy.policy_id, 'category_name': category_policy.category, 'selector_value': category_policy.restriction, 'quantity': category_policy.quantity, 'is_none': is_none, 'N_S': selectors.get('N'), 'UT_S': selectors.get('UT'), 'AL_S': selectors.get('AL'), 'E_S': selectors.get('E'), }) global_policies_html = "" global_policies = ShoppingPolicyLogic.get_all_shopping_policy_on_identity( ) for global_policy in global_policies: is_none = "" selectors = {} if global_policy.restriction is 'N': is_none = "disabled" selectors['N'] = "" selectors['UT'] = "" selectors['AL'] = "" selectors['E'] = "" selectors[ global_policy.restriction] = 'selected="selected"' global_policies_html += loader.render_to_string( 'components/shopping_global_policy.html', context={ 'id': global_policy.policy_id, 'selector_value': global_policy.restriction, 'quantity': global_policy.quantity, 'is_none': is_none, 'N_S': selectors.get('N'), 'UT_S': selectors.get('UT'), 'AL_S': selectors.get('AL'), 'E_S': selectors.get('E'), }) topbar = loader.render_to_string( 'components/TopbarLoggedIn.html', context={'username': username}) cart_count = len(ShoppingLogic.get_cart_items(username)) navbar = loader.render_to_string( 'components/NavbarButtons.html', context={'cart_items': cart_count}) return render(request, 'system-policies.html', context={ 'topbar': topbar, 'navbar': navbar, 'item_policies': item_policies_html, 'category_policies': category_policies_html, 'global_policies': global_policies_html }) return HttpResponse("You don't have the privilege to be here")