class TestCustomerManagement(EasyShopTestCase): """ """ def afterSetUp(self): """ """ super(TestCustomerManagement, self).afterSetUp() self.cm = ICustomerManagement(self.shop) def testAddCustomers(self): """ """ result = self.cm.addCustomer("c1") self.failIf(result == False) result = self.cm.addCustomer("c2") self.failIf(result == False) result = self.cm.addCustomer("c3") self.failIf(result == False) result = self.cm.addCustomer("c4") self.failIf(result == False) ids = [c.getId for c in self.cm.getCustomers()] self.assertEqual(ids, ["c1", "c2", "c3", "c4"]) def testGetAuthenticatedCustomer_1(self): """As anonymous, returns standard customer """ self.logout() customer = self.cm.getAuthenticatedCustomer() self.assertEqual(customer.getId(), "123") def testGetAuthenticatedCustomer_2(self): """As member """ self.login("newmember") customer = self.cm.getAuthenticatedCustomer() self.failUnless(ICustomer.providedBy(customer)) self.assertEqual(customer.getId(), "newmember") def testGetCustomerById_1(self): """Existing customer """ result = self.cm.addCustomer("c1") self.failIf(result == False) customer = self.cm.getCustomerById("c1") self.assertEqual(customer.getId(), "c1") self.failUnless(ICustomer.providedBy(customer)) def testGetCustomerById_2(self): """Non-existing customer """ customer = self.cm.getCustomerById("doe") self.assertEqual(customer, None)
def getCreditCards(self): """ """ if self._isValid("credit-card") == False: return [] cm = ICustomerManagement(self.context) customer = cm.getAuthenticatedCustomer() result = [] pm = IPaymentInformationManagement(customer) for credit_card in pm.getPaymentInformations( interface=ICreditCard, check_validity=True): selected_payment_information = \ pm.getSelectedPaymentInformation(check_validity=True) if selected_payment_information and \ selected_payment_information.getId() == credit_card.getId(): checked = True else: checked = False exp_date = "%s/%s" % (credit_card.card_expiration_date_month, credit_card.card_expiration_date_year) result.append({ "id" : credit_card.getId(), "type" : credit_card.card_type, "owner" : credit_card.card_owner, "number" : credit_card.card_number, "expiration_date" : exp_date, "checked" : checked, }) return result
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 getBankAccounts(self): """ """ if self._isValid("direct-debit") == False: return [] cm = ICustomerManagement(self.context) customer = cm.getAuthenticatedCustomer() result = [] pm = IPaymentInformationManagement(customer) for bank_account in pm.getPaymentInformations( interface=IBankAccount, check_validity=True): selected_payment_information = \ pm.getSelectedPaymentInformation(check_validity=True) if selected_payment_information and \ selected_payment_information.getId() == bank_account.getId(): checked = True else: checked = False result.append({ "id" : bank_account.getId(), "bic" : bank_account.bank_identification_code, "account_no" : bank_account.account_number, "depositor" : bank_account.depositor, "bank_name" : bank_account.bank_name, "checked" : checked, }) return result
def isCustomerComplete(self): """ """ cm = ICustomerManagement(self.context) customer = cm.getAuthenticatedCustomer() return ICompleteness(customer).isComplete()
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 getBankAccounts(self): """ """ if self._isValid("direct-debit") == False: return [] cm = ICustomerManagement(self.context) customer = cm.getAuthenticatedCustomer() result = [] pm = IPaymentInformationManagement(customer) for bank_account in pm.getPaymentInformations(interface=IBankAccount, check_validity=True): selected_payment_information = \ pm.getSelectedPaymentInformation(check_validity=True) if selected_payment_information and \ selected_payment_information.getId() == bank_account.getId(): checked = True else: checked = False result.append({ "id": bank_account.getId(), "bic": bank_account.bank_identification_code, "account_no": bank_account.account_number, "depositor": bank_account.depositor, "bank_name": bank_account.bank_name, "checked": checked, }) return result
def addOrder(self, customer=None, cart=None): """ """ cartmanager = ICartManagement(self.context) if customer is None: cm = ICustomerManagement(self.context) customer = cm.getAuthenticatedCustomer() if cart is None: cart = cartmanager.getCart() portal = getToolByName(self.context, 'portal_url').getPortalObject() ## The current user may not be allowed to create an order, so we ## temporarily change the security context to use a temporary ## user with manager role. old_sm = getSecurityManager() tmp_user = UnrestrictedUser(old_sm.getUser().getId(), '', ['Manager'], '') tmp_user = tmp_user.__of__(portal.acl_users) newSecurityManager(None, tmp_user) # Add a new order new_id = self._createOrderId() self.orders.invokeFactory("Order", id=new_id) new_order = getattr(self.orders, new_id) # Copy Customer to Order customer = ICustomerManagement(self.context).getAuthenticatedCustomer() cm = ICopyManagement(customer) cm.copyTo(new_order) # Add cart items to order IItemManagement(new_order).addItemsFromCart(cart) # Add total tax new_order.setTax(ITaxes(cart).getTaxForCustomer()) # Add shipping values to order sm = IShippingPriceManagement(self.context) new_order.setShippingPriceNet(sm.getPriceNet()) new_order.setShippingPriceGross(sm.getPriceForCustomer()) new_order.setShippingTax(sm.getTaxForCustomer()) new_order.setShippingTaxRate(sm.getTaxRateForCustomer()) # Add payment price values to order pp = IPaymentPriceManagement(self.context) new_order.setPaymentPriceGross(pp.getPriceForCustomer()) new_order.setPaymentPriceNet(pp.getPriceNet()) new_order.setPaymentTax(pp.getTaxForCustomer()) new_order.setPaymentTaxRate(pp.getTaxRateForCustomer()) ## Reset security manager setSecurityManager(old_sm) # Index with customer again new_order.reindexObject() return new_order
def getCreditCards(self): """ """ if self._isValid("credit-card") == False: return [] cm = ICustomerManagement(self.context) customer = cm.getAuthenticatedCustomer() result = [] pm = IPaymentInformationManagement(customer) for credit_card in pm.getPaymentInformations(interface=ICreditCard, check_validity=True): selected_payment_information = \ pm.getSelectedPaymentInformation(check_validity=True) if selected_payment_information and \ selected_payment_information.getId() == credit_card.getId(): checked = True else: checked = False exp_date = "%s/%s" % (credit_card.card_expiration_date_month, credit_card.card_expiration_date_year) result.append({ "id": credit_card.getId(), "type": credit_card.card_type, "owner": credit_card.card_owner, "number": credit_card.card_number, "expiration_date": exp_date, "checked": checked, }) return result
def getAddressInfo(self): """ """ id = self.request.get("id") cm = ICustomerManagement(self.context) customer = cm.getAuthenticatedCustomer() am = IAddressManagement(customer) address = am.getAddress(id) result = {} result["id"] = id if self.request.get("firstname", "") != "": result["firstname"] = self.request.get("firstname") else: result["firstname"] = address.getFirstname() if self.request.get("lastname", "") != "": result["lastname"] = self.request.get("lastname") else: result["lastname"] = address.getLastname() if self.request.get("companyname", "") != "": result["companyname"] = self.request.get("companyname") else: result["companyname"] = address.getCompanyName() if self.request.get("address1", "") != "": result["address1"] = self.request.get("address1") else: result["address1"] = address.getAddress1() if self.request.get("address2", "") != "": result["address2"] = self.request.get("address2") else: result["address2"] = address.getAddress2() if self.request.get("zipcode", "") != "": result["zipcode"] = self.request.get("zipcode") else: result["zipcode"] = address.getZipCode() if self.request.get("city", "") != "": result["city"] = self.request.get("city") else: result["city"] = address.getCity() if self.request.get("country", "") != "": result["country"] = self.request.get("country") else: result["country"] = address.getCountry() if self.request.get("phone", "") != "": result["phone"] = self.request.get("phone") else: result["phone"] = address.getPhone() return result
def getSelectedShippingMethod(self): """ """ cm = ICustomerManagement(IShopManagement(self.context).getShop()) customer = cm.getAuthenticatedCustomer() shipping_method_id = customer.selected_shipping_method return self.getShippingMethod(shipping_method_id)
def getShippingAddress(self): """ """ cm = ICustomerManagement(self.context) customer = cm.getAuthenticatedCustomer() am = IAddressManagement(customer) address = am.getShippingAddress() return addressToDict(address)
def getInvoiceAddress(self): """Returns invoice address of the current customer. """ cm = ICustomerManagement(self.context) customer = cm.getAuthenticatedCustomer() am = IAddressManagement(customer) address = am.getInvoiceAddress() return addressToDict(address)
def afterSetUp(self): """ """ super(TestDirectDebit, self).afterSetUp() self.login("newmember") cm = ICustomerManagement(self.shop) self.customer = cm.getAuthenticatedCustomer() self.customer.invokeFactory( "BankAccount", id = "bank-account", )
def getMailInfo(self): """ """ cm = ICustomerManagement(self.context) customer = cm.getAuthenticatedCustomer() am = IAddressManagement(customer) shipping_address = am.getShippingAddress() mtool = getToolByName(self.context, "portal_membership") member = mtool.getAuthenticatedMember() name = shipping_address.getFirstname() + " " name += shipping_address.getLastname() return { "email" : member.getProperty("email"), "name" : name, }
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 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 getVATRegistration(self): """Returns the VAT registration (if any) of the current customer. """ result = [] shop = IShopManagement(self.context).getShop() if shop.__dict__.has_key('VATCountry') and shop.VATCountry != "None" and len(ITaxManagement(self.context).getCustomerTaxes()): customermgmt = ICustomerManagement(self.context) customer = customermgmt.getAuthenticatedCustomer() vatreg = customer.getVATRegistration() if not vatreg: vatreg = "" vatcountries = VAT_COUNTRIES.keys() vatcountries.sort() result.append({ "country" : vatreg[:2], "number" : vatreg[2:], "countries" : vatcountries, }) return result
def getVATRegistration(self): """Returns the VAT registration (if any) of the current customer. """ result = [] shop = IShopManagement(self.context).getShop() if shop.__dict__.has_key( 'VATCountry') and shop.VATCountry != "None" and len( ITaxManagement(self.context).getCustomerTaxes()): customermgmt = ICustomerManagement(self.context) customer = customermgmt.getAuthenticatedCustomer() vatreg = customer.getVATRegistration() if not vatreg: vatreg = "" vatcountries = VAT_COUNTRIES.keys() vatcountries.sort() result.append({ "country": vatreg[:2], "number": vatreg[2:], "countries": vatcountries, }) return result
def addOrder(self, customer=None, cart=None): """ """ cartmanager = ICartManagement(self.context) if customer is None: cm = ICustomerManagement(self.context) customer = cm.getAuthenticatedCustomer() if cart is None: cart = cartmanager.getCart() portal = getToolByName(self.context, 'portal_url').getPortalObject() ## The current user may not be allowed to create an order, so we ## temporarily change the security context to use a temporary ## user with manager role. old_sm = getSecurityManager() tmp_user = UnrestrictedUser( old_sm.getUser().getId(), '', ['Manager'], '' ) tmp_user = tmp_user.__of__(portal.acl_users) newSecurityManager(None, tmp_user) # Add a new order new_id = self._createOrderId() self.orders.invokeFactory("Order", id=new_id) new_order = getattr(self.orders, new_id) # Copy Customer to Order customer = ICustomerManagement(self.context).getAuthenticatedCustomer() cm = ICopyManagement(customer) cm.copyTo(new_order) # Add cart items to order IItemManagement(new_order).addItemsFromCart(cart) # Add total tax new_order.setTax(ITaxes(cart).getTaxForCustomer()) # Add shipping values to order sm = IShippingPriceManagement(self.context) new_order.setShippingPriceNet(sm.getPriceNet()) new_order.setShippingPriceGross(sm.getPriceForCustomer()) new_order.setShippingTax(sm.getTaxForCustomer()) new_order.setShippingTaxRate(sm.getTaxRateForCustomer()) # Add payment price values to order pp = IPaymentPriceManagement(self.context) new_order.setPaymentPriceGross(pp.getPriceForCustomer()) new_order.setPaymentPriceNet(pp.getPriceNet()) new_order.setPaymentTax(pp.getTaxForCustomer()) new_order.setPaymentTaxRate(pp.getTaxRateForCustomer()) ## Reset security manager setSecurityManager(old_sm) # Index with customer again new_order.reindexObject() return new_order