Changeset 12143


Ignore:
Timestamp:
4 Dec 2014, 12:22:03 (10 years ago)
Author:
Henrik Bettermann
Message:

Add contract exporter.

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  
    2727from waeup.ikoba.customers.catalog import CustomersQuery
    2828from waeup.ikoba.customers.interfaces import (
    29     ICustomer, ICSVCustomerExporter, ICustomerDocument)
     29    ICustomer, ICSVCustomerExporter, ICustomerDocument,
     30    ISampleContract)
    3031from waeup.ikoba.utils.batching import ExporterBase
    3132from waeup.ikoba.utils.helpers import iface_names, to_timezone
     
    5051    return documents
    5152
     53def 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
    5261
    5362class CustomerExporterBase(ExporterBase):
     
    137146            value, name, context=context)
    138147
     148
    139149class CustomerDocumentExporter(grok.GlobalUtility, CustomerExporterBase):
    140150    """Exporter for CustomerDocument instances.
     
    159169
    160170    def mangle_value(self, value, name, context=None):
    161         """Treat location values special.
    162         """
     171
    163172        if name == 'history':
    164173            value = value.messages
     
    166175            CustomerDocumentExporter, self).mangle_value(
    167176            value, name, context=context)
     177
     178
     179class 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  
    276276    last_product_id = Attribute(
    277277        'Id of product recently stored with the contract')
     278    is_editable = Attribute('Document editable by customer')
     279    translated_class_name = Attribute('Translatable class name')
    278280
    279281    title = schema.TextLine(
  • main/waeup.ikoba/trunk/src/waeup/ikoba/customers/tests/test_batching.py

    r12131 r12143  
    138138        contract = createObject('waeup.SampleContract')
    139139        contract.title = u'My Contract'
     140        contract.product_object = self.product
     141        contract.document_object = self.document
    140142        customer['contracts'].addContract(contract)
    141143        self.contract = contract
  • main/waeup.ikoba/trunk/src/waeup/ikoba/customers/tests/test_export.py

    r12131 r12143  
    3131from waeup.ikoba.customers.catalog import CustomersQuery
    3232from waeup.ikoba.customers.export import (
    33     CustomerExporter, CustomerDocumentExporter, get_customers)
     33    CustomerExporter, CustomerDocumentExporter, ContractExporter,
     34    get_customers)
    3435from waeup.ikoba.customers.interfaces import ICSVCustomerExporter
    3536from waeup.ikoba.customers.customer import Customer
    3637from waeup.ikoba.customers.documents import CustomerSampleDocument
     38from waeup.ikoba.customers.contracts import SampleContract
    3739from waeup.ikoba.customers.tests.test_batching import CustomerImportExportSetup
    3840from waeup.ikoba.testing import FunctionalLayer
     
    8890        exporter.export([self.customer], self.outfile)
    8991        result = open(self.outfile, 'rb').read()
    90         self.assertTrue(
    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             in result
     92        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
    9799            )
    98100        return
     
    202204        return
    203205
    204     def test_export_customer(self):
     206    def test_export_customer_document(self):
    205207        # we can really export all documents of a certain customer
    206208        # set values we can expect in export file
     
    219221            )
    220222        return
     223
     224
     225class 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.