Changeset 12738 for main/waeup.ikoba
- Timestamp:
- 11 Mar 2015, 22:44:17 (10 years ago)
- Location:
- main/waeup.ikoba/branches/uli-payments/src/waeup/ikoba/customers
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.ikoba/branches/uli-payments/src/waeup/ikoba/customers/customer.py
r12553 r12738 25 25 from hurry.workflow.interfaces import IWorkflowState, IWorkflowInfo 26 26 from zope.password.interfaces import IPasswordManager 27 from zope.component import getUtility, createObject 27 from zope.catalog.interfaces import ICatalog 28 from zope.component import getUtility, createObject, queryUtility 28 29 from zope.component.interfaces import IFactory 29 30 from zope.interface import implementedBy … … 43 44 from waeup.ikoba.customers.documents import CustomerDocumentsContainer 44 45 from waeup.ikoba.customers.contracts import ContractsContainer 46 from waeup.ikoba.payments.interfaces import IPayer, IPayerFinder 45 47 from waeup.ikoba.utils.helpers import attrs_to_fields, now, copy_filesystem_tree 46 48 … … 399 401 return file, path, IkobaImageFile( 400 402 file_obj.filename, file_obj.data) 403 404 405 class CustomerPayer(grok.Adapter): 406 """Adapter to turn customers into IPayers. 407 """ 408 grok.implements(IPayer) 409 grok.context(ICustomer) 410 411 @property 412 def first_name(self): 413 return getattr(self.context, 'firstname', None) 414 415 @property 416 def last_name(self): 417 return getattr(self.context, 'lastname', None) 418 419 @property 420 def payer_id(self): 421 return getattr(self.context, 'customer_id', None) 422 423 424 class CustomerFinder(grok.GlobalUtility): 425 """Find customers. 426 """ 427 grok.name('customer_finder') 428 grok.implements(IPayerFinder) 429 430 def get_payer_by_id(self, customer_id): 431 catalog = queryUtility(ICatalog, 'customers_catalog') 432 if catalog is None: 433 return None 434 result = catalog.searchResults( 435 customer_id=(customer_id, customer_id)) 436 result = [x for x in result] 437 if not result: 438 return None 439 # there should not be more than one result really. 440 return result[0] -
main/waeup.ikoba/branches/uli-payments/src/waeup/ikoba/customers/tests/test_customer.py
r12297 r12738 24 24 from cStringIO import StringIO 25 25 from datetime import tzinfo 26 from zope.component import getUtility, queryUtility, createObject 26 from zope.component import ( 27 getUtility, queryUtility, createObject, getUtilitiesFor, 28 ) 29 from zope.component.hooks import setSite 27 30 from zope.catalog.interfaces import ICatalog 28 31 from zope.component.interfaces import IFactory … … 31 34 from zope.schema.interfaces import RequiredMissing 32 35 from waeup.ikoba.interfaces import IExtFileStore, IFileStoreNameChooser 36 from waeup.ikoba.app import Company 33 37 from waeup.ikoba.customers.customer import ( 34 Customer, CustomerFactory, handle_customer_removed, path_from_custid) 38 Customer, CustomerFactory, handle_customer_removed, path_from_custid, 39 CustomerPayer, CustomerFinder, 40 ) 35 41 from waeup.ikoba.customers.interfaces import ( 36 42 ICustomer, ICustomerNavigation, ICustomersUtils) 37 43 from waeup.ikoba.customers.tests.test_batching import CustomerImportExportSetup 44 from waeup.ikoba.payments.interfaces import IPayer, IPayerFinder 38 45 from waeup.ikoba.testing import FunctionalLayer, FunctionalTestCase 39 46 … … 92 99 93 100 101 class TestCustomerHelpers(FunctionalTestCase): 102 103 layer = FunctionalLayer 104 105 def test_payer_adapter(self): 106 # we can adapt ICustomer to IPayer (i.e. create a payer) 107 customer = Customer() 108 customer.firstname, customer.lastname = u'Anna', u'Tester' 109 result = IPayer(customer) 110 self.assertTrue(isinstance(result, CustomerPayer)) 111 verify.verifyObject(IPayer, result) 112 self.assertEqual(result.first_name, u'Anna') 113 self.assertEqual(result.last_name, u'Tester') 114 self.assertEqual(result.payer_id, customer.customer_id) 115 116 def test_customer_finder_iface(self): 117 # we have a customer finder that returns IPayableFinder data. 118 verify.verifyClass(IPayerFinder, CustomerFinder) 119 120 def test_customer_finder_registered(self): 121 # the customer finder is a utility registered on startup 122 util = getUtility(IPayerFinder, name='customer_finder') 123 self.assertTrue(isinstance(util, CustomerFinder)) 124 utils = [util for name, util in getUtilitiesFor(IPayerFinder) 125 if isinstance(util, CustomerFinder)] 126 self.assertEqual(len(utils), 1) 127 128 def create_customer_and_site(self): 129 customer = Customer() 130 customer.customer_id = u'CUST1' 131 self.getRootFolder()['app'] = Company() 132 app = self.getRootFolder()['app'] 133 setSite(app) 134 return customer, app 135 136 def test_customer_finder(self): 137 # the customer finder can really find customers 138 customer, app = self.create_customer_and_site() 139 app['mycustomer'] = customer # trigger cataloging 140 finder = CustomerFinder() 141 result = finder.get_payer_by_id('CUST1') 142 self.assertTrue(result is customer) 143 144 def test_customer_finder_not_stored(self): 145 # we get none if an id is not stored 146 customer, app = self.create_customer_and_site() 147 app['mycustomer'] = customer # trigger cataloging 148 finder = CustomerFinder() 149 result = finder.get_payer_by_id('Not-a-valid-id') 150 self.assertTrue(result is None) 151 152 def test_customer_finder_no_catalog(self): 153 # customer finder does not complain about missing catalog 154 finder = CustomerFinder() 155 result = finder.get_payer_by_id('CUST1') 156 self.assertTrue(result is None) 157 158 94 159 class CustomerRemovalTests(CustomerImportExportSetup): 95 160 # Test handle_customer_removed
Note: See TracChangeset for help on using the changeset viewer.