Changeset 12143 for main/waeup.ikoba/trunk/src/waeup/ikoba
- Timestamp:
- 4 Dec 2014, 12:22:03 (10 years ago)
- Location:
- main/waeup.ikoba/trunk/src/waeup/ikoba/customers
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.ikoba/trunk/src/waeup/ikoba/customers/export.py
r12130 r12143 27 27 from waeup.ikoba.customers.catalog import CustomersQuery 28 28 from waeup.ikoba.customers.interfaces import ( 29 ICustomer, ICSVCustomerExporter, ICustomerDocument) 29 ICustomer, ICSVCustomerExporter, ICustomerDocument, 30 ISampleContract) 30 31 from waeup.ikoba.utils.batching import ExporterBase 31 32 from waeup.ikoba.utils.helpers import iface_names, to_timezone … … 50 51 return documents 51 52 53 def get_contracts(customers): 54 """Get all contracts of `customers`. 55 """ 56 contracts = [] 57 for customer in customers: 58 for contract in customer.get('contracts', {}).values(): 59 contracts.append(contract) 60 return contracts 52 61 53 62 class CustomerExporterBase(ExporterBase): … … 137 146 value, name, context=context) 138 147 148 139 149 class CustomerDocumentExporter(grok.GlobalUtility, CustomerExporterBase): 140 150 """Exporter for CustomerDocument instances. … … 159 169 160 170 def mangle_value(self, value, name, context=None): 161 """Treat location values special. 162 """ 171 163 172 if name == 'history': 164 173 value = value.messages … … 166 175 CustomerDocumentExporter, self).mangle_value( 167 176 value, name, context=context) 177 178 179 class ContractExporter(grok.GlobalUtility, CustomerExporterBase): 180 """Exporter for Contract instances. 181 """ 182 grok.name('contracts') 183 184 #: Fieldnames considered by this exporter 185 fields = tuple( 186 sorted(iface_names( 187 ISampleContract, 188 exclude_attribs=False, 189 omit=['translated_state', 190 'formatted_transition_date', 191 'translated_class_name']))) 192 193 #: The title under which this exporter will be displayed 194 title = _(u'Contracts') 195 196 def filter_func(self, x, **kw): 197 return get_contracts(x, **kw) 198 199 def mangle_value(self, value, name, context=None): 200 201 if name == 'history': 202 value = value.messages 203 if name.endswith('_object'): 204 mangled_value = getattr(value, 'document_id', None) 205 if mangled_value: 206 return mangled_value 207 mangled_value = getattr(value, 'product_id', None) 208 if mangled_value: 209 return mangled_value 210 return super( 211 ContractExporter, self).mangle_value( 212 value, name, context=context) -
main/waeup.ikoba/trunk/src/waeup/ikoba/customers/interfaces.py
r12119 r12143 276 276 last_product_id = Attribute( 277 277 'Id of product recently stored with the contract') 278 is_editable = Attribute('Document editable by customer') 279 translated_class_name = Attribute('Translatable class name') 278 280 279 281 title = schema.TextLine( -
main/waeup.ikoba/trunk/src/waeup/ikoba/customers/tests/test_batching.py
r12131 r12143 138 138 contract = createObject('waeup.SampleContract') 139 139 contract.title = u'My Contract' 140 contract.product_object = self.product 141 contract.document_object = self.document 140 142 customer['contracts'].addContract(contract) 141 143 self.contract = contract -
main/waeup.ikoba/trunk/src/waeup/ikoba/customers/tests/test_export.py
r12131 r12143 31 31 from waeup.ikoba.customers.catalog import CustomersQuery 32 32 from waeup.ikoba.customers.export import ( 33 CustomerExporter, CustomerDocumentExporter, get_customers) 33 CustomerExporter, CustomerDocumentExporter, ContractExporter, 34 get_customers) 34 35 from waeup.ikoba.customers.interfaces import ICSVCustomerExporter 35 36 from waeup.ikoba.customers.customer import Customer 36 37 from waeup.ikoba.customers.documents import CustomerSampleDocument 38 from waeup.ikoba.customers.contracts import SampleContract 37 39 from waeup.ikoba.customers.tests.test_batching import CustomerImportExportSetup 38 40 from waeup.ikoba.testing import FunctionalLayer … … 88 90 exporter.export([self.customer], self.outfile) 89 91 result = open(self.outfile, 'rb').read() 90 self.assert True(91 'customer_id,email,firstname,lastname,middlename,phone, '92 ' reg_number,sex,suspended,suspended_comment,password,state,'93 ' history\r\n'94 ' A111111,anna@sample.com,Anna,Tester,M.,+234-123-12345#,'95 ' 123,,0,,,created'96 inresult92 self.assertMatches( 93 'customer_id,email,firstname,lastname,middlename,phone,reg_number,' 94 'sex,suspended,suspended_comment,password,state,history\r\n' 95 'A111111,anna@sample.com,Anna,Tester,M.,+234-123-12345#,123,,0,,,' 96 'created,[u\'2014-12-04 11:25:35 UTC - Customer record created ' 97 'by system\']\r\n', 98 result 97 99 ) 98 100 return … … 202 204 return 203 205 204 def test_export_customer (self):206 def test_export_customer_document(self): 205 207 # we can really export all documents of a certain customer 206 208 # set values we can expect in export file … … 219 221 ) 220 222 return 223 224 225 class ContractExporterTest(CustomerImportExportSetup): 226 227 layer = FunctionalLayer 228 229 def setUp(self): 230 super(ContractExporterTest, self).setUp() 231 self.setup_for_export() 232 return 233 234 def test_ifaces(self): 235 # make sure we fullfill interface contracts 236 obj = ContractExporter() 237 verifyObject(ICSVCustomerExporter, obj) 238 verifyClass(ICSVCustomerExporter, ContractExporter) 239 return 240 241 def test_get_as_utility(self): 242 # we can get a contracts exporter as utility 243 result = queryUtility(ICSVExporter, name="contracts") 244 self.assertTrue(result is not None) 245 return 246 247 def test_export_empty(self): 248 # we can export a nearly empty contract 249 contract = SampleContract() 250 exporter = ContractExporter() 251 exporter.export([contract], self.outfile) 252 result = open(self.outfile, 'rb').read() 253 self.assertEqual( 254 result, 255 'class_name,contract_category,contract_id,document_object,' 256 'history,is_editable,last_product_id,last_transition_date,' 257 'product_object,state,title\r\n' 258 259 'SampleContract,sample,c101,,[],1,,,,,\r\n' 260 ) 261 return 262 263 def test_export(self): 264 # we can really export customer contracts. 265 # set values we can expect in export file 266 self.setup_customer(self.customer) 267 exporter = ContractExporter() 268 exporter.export([self.contract], self.outfile) 269 result = open(self.outfile, 'rb').read() 270 self.assertMatches( 271 'class_name,contract_category,contract_id,document_object,' 272 'history,is_editable,last_product_id,last_transition_date,' 273 'product_object,state,title\r\n' 274 275 'SampleContract,sample,c101,d101,[u\'2014-12-04 12:10:46 UTC - ' 276 'Contract record created by system\'],0,,' 277 '2014-12-04 12:10:46.333445#,SAM,created,My Contract\r\n', 278 result 279 ) 280 return 281 282 def test_export_all(self): 283 # we can really export all contracts 284 # set values we can expect in export file 285 self.setup_customer(self.customer) 286 exporter = ContractExporter() 287 exporter.export_all(self.app, self.outfile) 288 result = open(self.outfile, 'rb').read() 289 self.assertMatches( 290 'class_name,contract_category,contract_id,document_object,' 291 'history,is_editable,last_product_id,last_transition_date,' 292 'product_object,state,title\r\n' 293 294 'SampleContract,sample,c101,d101,[u\'2014-12-04 12:10:46 UTC - ' 295 'Contract record created by system\'],0,,' 296 '2014-12-04 12:10:46.333445#,SAM,created,My Contract\r\n', 297 result 298 ) 299 return 300 301 def test_export_contract(self): 302 # we can really export all contracts of a certain customer 303 # set values we can expect in export file 304 self.setup_customer(self.customer) 305 exporter = ContractExporter() 306 exporter.export_customer(self.customer, self.outfile) 307 result = open(self.outfile, 'rb').read() 308 self.assertMatches( 309 'class_name,contract_category,contract_id,document_object,' 310 'history,is_editable,last_product_id,last_transition_date,' 311 'product_object,state,title\r\n' 312 313 'SampleContract,sample,c101,d101,[u\'2014-12-04 12:10:46 UTC - ' 314 'Contract record created by system\'],0,,' 315 '2014-12-04 12:10:46.333445#,SAM,created,My Contract\r\n', 316 result 317 ) 318 return 319
Note: See TracChangeset for help on using the changeset viewer.