def testWeight(self): """ """ self.shop.manage_addProduct["easyshop.core"].addWeightCriteria("c") v = IValidity(self.shop.c) self.assertEqual(v.isValid(), False) view = getMultiAdapter(( self.shop.products.product_1, self.shop.products.product_1.REQUEST), name="addToCart") view.addToCart() view = getMultiAdapter(( self.shop.products.product_2, self.shop.products.product_2.REQUEST), name="addToCart") view.addToCart() self.assertEqual(v.isValid(), True) self.shop.c.setWeight(29.0) self.assertEqual(v.isValid(), True) self.shop.c.setWeight(31.0) self.assertEqual(v.isValid(), False)
def testIsValid_2(self): """With one invalid criterion. """ self.default_tax.invokeFactory("DateCriteria", id="date_criterion") start = end = DateTime() + 1 self.default_tax.date_criterion.setStart(start) self.default_tax.date_criterion.setEnd(end) v = IValidity(self.default_tax) self.assertEqual(v.isValid(self.product_1), False)
def testCategory(self): """ """ c1_path = "/".join( self.portal.shop.categories.category_1.getPhysicalPath()) c2_path = "/".join( self.portal.shop.categories.category_2.getPhysicalPath()) c11_path = "/".join(self.portal.shop.categories.category_1.category_11. getPhysicalPath()) # Criteria has one catogory, products have this too self.folder.manage_addProduct["easyshop.core"].addCategoryCriteria("c") self.folder.c.setCategories((c11_path, )) v = IValidity(self.folder.c) self.assertEqual(v.isValid(self.product_1), True) self.assertEqual(v.isValid(self.product_2), True) # Criteria has one catogory, products haven't this self.folder.c.setCategories((c2_path, )) v = IValidity(self.folder.c) self.assertEqual(v.isValid(self.product_1), False) self.assertEqual(v.isValid(self.product_2), False) # Criteria has two catogories; products have just one of these self.folder.c.setCategories((c11_path, c1_path)) v = IValidity(self.folder.c) self.assertEqual(v.isValid(self.product_1), True) self.assertEqual(v.isValid(self.product_2), True)
def testValidity(self): """ """ self.shop.setPayPalId("") paypal = self.shop.paymentmethods.paypal result = IValidity(paypal).isValid() self.failUnless(result == False) self.shop.setPayPalId("*****@*****.**") result = IValidity(paypal).isValid() self.failUnless(result == True)
def testIsValid_3(self): """With one valid criterion. """ # Note product_1 costs 22.00 # price criterion is true if cart price > criterion price self.shipping_price.invokeFactory("PriceCriteria", id="price_criterion") self.shipping_price.price_criterion.setPrice(21.0) view = getMultiAdapter((self.shop.products.product_1, self.shop.products.product_1.REQUEST), name="addToCart") view.addToCart() v = IValidity(self.shop.shippingprices.shipping_price) self.assertEqual(v.isValid(), True)
def testPrice(self): """ """ self.shop.manage_addProduct["easyshop.core"].addPriceCriteria("c") v = IValidity(self.shop.c) self.login("newmember") self.assertEqual(v.isValid(), True) view = getMultiAdapter((self.shop.products.product_1, self.shop.products.product_1.REQUEST), name="addToCart") view.addToCart() self.assertEqual(v.isValid(), True) self.shop.c.setPrice(23.00) self.assertEqual(v.isValid(), False) self.shop.c.setPrice(22.00) self.assertEqual(v.isValid(), True) view = getMultiAdapter((self.shop.products.product_2, self.shop.products.product_2.REQUEST), name="addToCart") view.addToCart() self.shop.c.setPrice(42.00) self.assertEqual(v.isValid(), False) self.shop.c.setPrice(41.00) self.assertEqual(v.isValid(), True)
def testIsValid_4(self): """With one invalid and one valid criterion. """ # invalid self.default_tax.invokeFactory("DateCriteria", id="date_criterion") start = end = DateTime() + 1 self.default_tax.date_criterion.setStart(start) self.default_tax.date_criterion.setEnd(end) # valid (product_1 is in group_1; see utils.createTestEnvironment) self.default_tax.invokeFactory("GroupCriteria", id="group_criterion") self.default_tax.group_criterion.setGroups(["group_1"]) v = IValidity(self.default_tax) self.assertEqual(v.isValid(self.product_1), False)
def testProduct(self): """ """ self.shop.manage_addProduct["easyshop.core"].addProductCriteria("c") v = IValidity(self.shop.c) self.assertEqual(v.isValid(self.product_1), False) self.shop.c.setProducts(("product_1",)) self.assertEqual(v.isValid(self.product_1), True) self.assertEqual(v.isValid(self.product_2), False) self.shop.c.setProducts(("product_1", "product_2")) self.assertEqual(v.isValid(self.product_1), True) self.assertEqual(v.isValid(self.product_2), True)
def testProduct(self): """ """ self.shop.manage_addProduct["easyshop.core"].addProductCriteria("c") v = IValidity(self.shop.c) self.assertEqual(v.isValid(self.product_1), False) self.shop.c.setProducts(("product_1", )) self.assertEqual(v.isValid(self.product_1), True) self.assertEqual(v.isValid(self.product_2), False) self.shop.c.setProducts(("product_1", "product_2")) self.assertEqual(v.isValid(self.product_1), True) self.assertEqual(v.isValid(self.product_2), True)
def isValid(self, product=None): """Returns True if the selected shipping method of the current customer is within selected shipping methods of this criterion. """ shop = IShopManagement(self.context).getShop() sm = IShippingMethodManagement(shop) selected_method = sm.getSelectedShippingMethod() if self.context.getOperator() == "current": if selected_method is not None and \ selected_method.getId() in self.context.getShippingMethods(): return True else: return False else: for id in self.context.getShippingMethods(): # Never check validity for itself (context) if id == self.context.getId(): continue shipping_method = sm.getShippingMethod(id) if IValidity(shipping_method).isValid() == True: return False return True
def _calcTaxRateForCustomer(self): """Calculates the special tax for a given product and customer. """ # If the customer has a VAT registration and the shop has a VAT # registration, and his country ID is different to the shop's # country ID, then don't apply customer taxes (default taxes # still apply) customer = ICustomerManagement( self.shop).getAuthenticatedCustomer(createIfNotExist=False) if customer is not None: vatreg = customer.getVATRegistration() else: vatreg = None if not self.shop.__dict__.has_key( 'VATCountry' ) or self.shop.VATCountry == "None" or not vatreg or vatreg[: 2] == self.shop.VATCountry: # 1. Try to find a Tax for actual Customer tm = ITaxManagement(self.shop) for tax in tm.getCustomerTaxes(): if IValidity(tax).isValid(self.context) == True: return tax.getRate() # 2. If nothing is found, returns the default tax for the product. return self._calcTaxRateForProduct()
def getPriceGross(self): """Returns the first valid price. """ for price in self.getPaymentPrices(): if IValidity(price).isValid() == True: return price.getPrice() return 0
def testDate(self): """ """ self.folder.manage_addProduct["easyshop.core"].addDateCriteria("c") v = IValidity(self.folder.c) self.folder.c.setStart(DateTime() - 2) self.folder.c.setEnd(DateTime() - 1) self.assertEqual(v.isValid(), False) self.folder.c.setStart(DateTime() - 2) self.folder.c.setEnd(DateTime() + 1) self.assertEqual(v.isValid(), True) self.folder.c.setStart(DateTime() + 1) self.folder.c.setEnd(DateTime() + 2) self.assertEqual(v.isValid(), False)
def getPriceGross(self): """ """ for price in self.getShippingPrices(): if IValidity(price).isValid() == True: return price.getPrice() return 0
def getStockInformationFor(self, product): """ """ for information in self.stock_information.objectValues(): if IValidity(information).isValid(product) == True: return information return None
def testDate(self): """ """ self.folder.manage_addProduct["easyshop.core"].addDateCriteria("c") v = IValidity(self.folder.c) self.folder.c.setStart(DateTime()-2) self.folder.c.setEnd(DateTime()-1) self.assertEqual(v.isValid(), False) self.folder.c.setStart(DateTime()-2) self.folder.c.setEnd(DateTime()+1) self.assertEqual(v.isValid(), True) self.folder.c.setStart(DateTime()+1) self.folder.c.setEnd(DateTime()+2) self.assertEqual(v.isValid(), False)
def testCustomer(self): """ """ # customer criteria needs context self.folder.manage_addProduct["easyshop.core"].addCustomerCriteria("c") v = IValidity(self.folder.c) self.assertEqual(v.isValid(), False) self.folder.c.setCustomers(("newmember",)) self.assertEqual(v.isValid(), False) self.login("newmember") self.assertEqual(v.isValid(), True) self.folder.c.setCustomers(("newmember", "anothermember")) self.assertEqual(v.isValid(), True)
def testPrice(self): """ """ self.shop.manage_addProduct["easyshop.core"].addPriceCriteria("c") v = IValidity(self.shop.c) self.login("newmember") self.assertEqual(v.isValid(), True) view = getMultiAdapter(( self.shop.products.product_1, self.shop.products.product_1.REQUEST), name="addToCart") view.addToCart() self.assertEqual(v.isValid(), True) self.shop.c.setPrice(23.00) self.assertEqual(v.isValid(), False) self.shop.c.setPrice(22.00) self.assertEqual(v.isValid(), True) view = getMultiAdapter(( self.shop.products.product_2, self.shop.products.product_2.REQUEST), name="addToCart") view.addToCart() self.shop.c.setPrice(42.00) self.assertEqual(v.isValid(), False) self.shop.c.setPrice(41.00) self.assertEqual(v.isValid(), True)
def testPaymentMethod(self): """ """ self.shop.manage_addProduct["easyshop.core"].addPaymentMethodCriteria( "c") v = IValidity(self.shop.c) self.login("newmember") cm = ICustomerManagement(self.shop) customer = cm.getAuthenticatedCustomer() customer.selected_payment_method = u"cash-on-delivery" self.assertEqual(v.isValid(), False) customer.selected_payment_method = u"prepayment" self.assertEqual(v.isValid(), False) self.shop.c.setPaymentMethods(["direct-debit"]) self.assertEqual(v.isValid(), False) self.shop.c.setPaymentMethods(["prepayment"]) self.assertEqual(v.isValid(), True) self.shop.c.setPaymentMethods(["prepayment", "direct-debit"]) self.assertEqual(v.isValid(), True)
def testCountry(self): """ """ self.shop.manage_addProduct["easyshop.core"].addCountryCriteria("c") self.shop.c.setCountries((u"USA",)) v = IValidity(self.shop.c) self.login("newmember") cm = ICustomerManagement(self.shop) customer = cm.getAuthenticatedCustomer() customer.invokeFactory("Address", "address_1") customer.address_1.country = u"USA" self.assertEqual(v.isValid(), True) customer.address_1.country = u"Germany" self.assertEqual(v.isValid(), False)
def getPriceGross(self): """ """ amount_of_shops = ICartManagement(self.context).getAmountOfShops() for price in self.getShippingPrices(): if IValidity(price).isValid() == True: return price.getPrice() * amount_of_shops return 0
def _isValid(self, name): """Returns true if the payment method with given name is valid. """ spm = IPaymentMethodManagement(self.context) dd = spm.getPaymentMethod(name) if dd is None or IValidity(dd).isValid() == False: return False else: return True
def testCountry(self): """ """ self.shop.manage_addProduct["easyshop.core"].addCountryCriteria("c") self.shop.c.setCountries((u"USA", )) v = IValidity(self.shop.c) self.login("newmember") cm = ICustomerManagement(self.shop) customer = cm.getAuthenticatedCustomer() customer.invokeFactory("Address", "address_1") customer.address_1.country = u"USA" self.assertEqual(v.isValid(), True) customer.address_1.country = u"Germany" self.assertEqual(v.isValid(), False)
def testGroup(self): """ """ self.folder.manage_addProduct["easyshop.core"].addGroupCriteria("c") v = IValidity(self.folder.c) self.assertEqual(v.isValid(self.product_1), False) self.assertEqual(v.isValid(self.product_2), False) self.folder.c.setGroups(("group_1"),) self.assertEqual(v.isValid(self.product_1), True) self.assertEqual(v.isValid(self.product_2), True) self.folder.c.setGroups(("group_2"),) self.assertEqual(v.isValid(self.product_1), True) self.assertEqual(v.isValid(self.product_2), False) self.folder.c.setGroups(("group_1", "group_2"),) self.assertEqual(v.isValid(self.product_1), True) self.assertEqual(v.isValid(self.product_2), True)
def _calcTaxRateForProduct(self): """Calculates the default tax for a given product. """ # Returns the first tax rate, which is true. Taxes are sorted by # position which is also the priority tm = ITaxManagement(self.shop) for tax in tm.getDefaultTaxes(): if IValidity(tax).isValid(self.context) == True: return tax.getRate() return 0
def testIsValid_4(self): """With one invalid and one valid criterion. """ # Note product_1 costs 22.00 # price criterion is true if cart price > criterion price # valid self.shipping_price.invokeFactory("PriceCriteria", id="price_criterion") self.shipping_price.price_criterion.setPrice(23.0) view = getMultiAdapter((self.shop.products.product_1, self.shop.products.product_1.REQUEST), name="addToCart") view.addToCart() # invalid self.shipping_price.invokeFactory("DateCriteria", id="date_criterion") start = end = DateTime() + 1 self.shipping_price.date_criterion.setStart(start) self.shipping_price.date_criterion.setStart(end) v = IValidity(self.shop.shippingprices.shipping_price) self.assertEqual(v.isValid(), False)
def testPaymentMethod(self): """ """ self.shop.manage_addProduct["easyshop.core"].addPaymentMethodCriteria("c") v = IValidity(self.shop.c) self.login("newmember") cm = ICustomerManagement(self.shop) customer = cm.getAuthenticatedCustomer() customer.selected_payment_method = u"cash-on-delivery" self.assertEqual(v.isValid(), False) customer.selected_payment_method = u"prepayment" self.assertEqual(v.isValid(), False) self.shop.c.setPaymentMethods(["direct-debit"]) self.assertEqual(v.isValid(), False) self.shop.c.setPaymentMethods(["prepayment"]) self.assertEqual(v.isValid(), True) self.shop.c.setPaymentMethods(["prepayment", "direct-debit"]) self.assertEqual(v.isValid(), True)
def testCategory(self): """ """ c1_path = "/".join(self.portal.shop.categories.category_1.getPhysicalPath()) c2_path = "/".join(self.portal.shop.categories.category_2.getPhysicalPath()) c11_path = "/".join(self.portal.shop.categories.category_1.category_11.getPhysicalPath()) # Criteria has one catogory, products have this too self.folder.manage_addProduct["easyshop.core"].addCategoryCriteria("c") self.folder.c.setCategories((c11_path,)) v = IValidity(self.folder.c) self.assertEqual(v.isValid(self.product_1), True) self.assertEqual(v.isValid(self.product_2), True) # Criteria has one catogory, products haven't this self.folder.c.setCategories((c2_path,)) v = IValidity(self.folder.c) self.assertEqual(v.isValid(self.product_1), False) self.assertEqual(v.isValid(self.product_2), False) # Criteria has two catogories; products have just one of these self.folder.c.setCategories((c11_path, c1_path)) v = IValidity(self.folder.c) self.assertEqual(v.isValid(self.product_1), True) self.assertEqual(v.isValid(self.product_2), True)
def testWeight(self): """ """ self.shop.manage_addProduct["easyshop.core"].addWeightCriteria("c") v = IValidity(self.shop.c) self.assertEqual(v.isValid(), False) view = getMultiAdapter((self.shop.products.product_1, self.shop.products.product_1.REQUEST), name="addToCart") view.addToCart() view = getMultiAdapter((self.shop.products.product_2, self.shop.products.product_2.REQUEST), name="addToCart") view.addToCart() self.assertEqual(v.isValid(), True) self.shop.c.setWeight(29.0) self.assertEqual(v.isValid(), True) self.shop.c.setWeight(31.0) self.assertEqual(v.isValid(), False)
def getInformationPagesFor(self, product): """ """ mtool = getToolByName(self.context, "portal_membership") result = [] for information in self.information.objectValues(): if IValidity(information).isValid(product) == False: continue if IValidity(information).isValid(product) == False: continue if mtool.checkPermission("View", information) != True: continue result.append({ "title" : information.Title(), "id" : information.getId(), }) return result
def getSelectedPaymentInformation(self, check_validity=False): """ """ try: selected_payment_information = \ self.context[self.context.selected_payment_information] except (AttributeError, KeyError): return None # If selected payment method is not valid return None if check_validity == False or \ IValidity(selected_payment_information).isValid() == True: return selected_payment_information else: return None
def getDiscount(self): """Returns the first valid discount or None. """ # NOTE: Using the product to get the shop is due to EasyMall. The # product lives in the several shops and can used to get the mall and/or # the Shop, whereas the item lives in the all and there would be no # way back to the origin shop. shop = IShopManagement(self.context.getProduct()).getShop() for discount in IDiscountsManagement(shop).getDiscounts(): if IValidity(discount).isValid(self.context) == True: return discount return None
def getShippingMethods(self, check_validity=False): """ """ mtool = getToolByName(self.context, "portal_membership") shipping_methods = [] for shipping_method in self.shipping_methods.objectValues(): if check_validity and \ IValidity(shipping_method).isValid() == False: continue if mtool.checkPermission("View", shipping_method) is not None: shipping_methods.append(shipping_method) return shipping_methods
def isValid(self, product=None): """Returns true if all contained criteria are true. """ # To be true all criteria must be true (AND) for criteria in self.context.objectValues(): # First try "isValid" on the criterion. This maybe makes it easier # to write own 3rd party criteria without the need to write an # adapter. try: if criteria.isValid(product) == False: return False except AttributeError: # Then take the adapter. This makes it OTOH easier to overwrite the # default behaviour of the default criterias if IValidity(criteria).isValid(product) == False: return False return True
def getSelectablePaymentMethods(self, check_validity=False): """Returns payment method which are selectable by a customer. """ mtool = getToolByName(self.context, "portal_membership") result = [] for object in self.paymentmethods.objectValues(): if ISelectablePaymentMethod.providedBy(object) == False: continue if check_validity and \ IValidity(object).isValid(object) == False: continue if mtool.checkPermission("View", object) is not None: result.append(object) return result
def getPaymentInformations(self, interface=IPaymentInformation, check_validity=False): """Returns the payment information of a customer. """ mtool = getToolByName(self.context, "portal_membership") result = [] for object in self.context.objectValues(): if interface.providedBy(object) == False: continue if check_validity == True and \ IValidity(object).isValid(object) == False: continue if mtool.checkPermission("View", object) is not None: result.append(object) return result
def testCustomer(self): """ """ # customer criteria needs context self.folder.manage_addProduct["easyshop.core"].addCustomerCriteria("c") v = IValidity(self.folder.c) self.assertEqual(v.isValid(), False) self.folder.c.setCustomers(("newmember", )) self.assertEqual(v.isValid(), False) self.login("newmember") self.assertEqual(v.isValid(), True) self.folder.c.setCustomers(("newmember", "anothermember")) self.assertEqual(v.isValid(), True)
def getSelectedPaymentMethod(self, check_validity=False): """ """ try: selected_payment_method = \ self.context.paymentmethods[self.context.selected_payment_method] except (AttributeError, KeyError): # Try prepayment as fallback if self.context.paymentmethods.has_key("prepayment"): return self.context.paymentmethods["prepayment"] elif len(self.context.paymentmethods): # Any will do return self.context.paymentmethods.values()[0] else: return None # Check vor validity if check_validity == False or \ IValidity(selected_payment_method).isValid() == True: return selected_payment_method else: # Return prepayment as fallback return self.context.paymentmethods["prepayment"]
def testAdapters(self): """ """ adapter = IValidity(BankAccount("dummy")) self.assertEqual(str(adapter.__class__), "<class 'easyshop.shop.adapters.validity.Validity'>") adapter = IValidity(GenericPaymentMethod("dummy")) self.assertEqual(str(adapter.__class__), "<class 'easyshop.shop.adapters.validity.Validity'>") adapter = IValidity(PaymentPrice("dummy")) self.assertEqual(str(adapter.__class__), "<class 'easyshop.shop.adapters.validity.Validity'>") adapter = IValidity(CustomerTax("dummy")) self.assertEqual(str(adapter.__class__), "<class 'easyshop.shop.adapters.validity.Validity'>") adapter = IValidity(DefaultTax("dummy")) self.assertEqual(str(adapter.__class__), "<class 'easyshop.shop.adapters.validity.Validity'>") adapter = IValidity(ShippingPrice("dummy")) self.assertEqual(str(adapter.__class__), "<class 'easyshop.shop.adapters.validity.Validity'>")
def testIsValid_1(self): """Without criteria """ v = IValidity(self.shipping_price) self.assertEqual(v.isValid(), True)