Ignore:
Timestamp:
21 Dec 2014, 10:13:45 (10 years ago)
Author:
Henrik Bettermann
Message:

Export only customer documents and contracts which match the exporter's class_name attribute.

Reorganize exporters to ease customization. Let's always use baseclasses.

Location:
main/waeup.ikoba/trunk/src/waeup/ikoba/customers
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.ikoba/trunk/src/waeup/ikoba/customers/export.py

    r12275 r12279  
    2727from waeup.ikoba.customers.catalog import CustomersQuery
    2828from waeup.ikoba.customers.interfaces import (
    29     ICustomer, ICSVCustomerExporter, ICustomerDocument,
    30     ISampleContract)
     29    ICustomer, ICSVCustomerExporter,
     30    ICustomerSampleDocument, ISampleContract)
    3131from waeup.ikoba.utils.batching import ExporterBase
    3232from waeup.ikoba.utils.helpers import iface_names, to_timezone
     
    3434#: A tuple containing all exporter names referring to customers or
    3535#: subobjects thereof.
    36 EXPORTER_NAMES = ('customers', 'customerdocuments')
     36EXPORTER_NAMES = ('customers', 'customersampledocuments', 'samplecontracts')
    3737
    3838
     
    4242    return cust_filter.query()
    4343
    44 def get_documents(customers):
     44def get_documents(customers, class_name):
    4545    """Get all documents of `customers`.
    4646    """
     
    4848    for customer in customers:
    4949        for document in customer.get('documents', {}).values():
    50             documents.append(document)
     50            if document.class_name == class_name:
     51                documents.append(document)
    5152    return documents
    5253
    53 def get_contracts(customers):
     54def get_contracts(customers, class_name):
    5455    """Get all contracts of `customers`.
    5556    """
     
    5758    for customer in customers:
    5859        for contract in customer.get('contracts', {}).values():
    59             contracts.append(contract)
     60            if contract.class_name == class_name:
     61                contracts.append(contract)
    6062    return contracts
    6163
     
    151153
    152154
    153 class CustomerDocumentExporter(grok.GlobalUtility, CustomerExporterBase):
    154     """Exporter for CustomerDocument instances.
    155     """
    156     grok.name('customerdocuments')
    157 
    158     iface = ICustomerDocument
    159 
    160     #: The title under which this exporter will be displayed
    161     title = _(u'Customer Documents')
     155class CustomerDocumentExporterBase(grok.GlobalUtility, CustomerExporterBase):
     156    """Exporter for Customer Document instances.
     157
     158    This is a baseclass.
     159    """
     160    grok.baseclass()
     161    iface = None
     162    class_name = None
     163    title = None
    162164
    163165    #: Fieldnames considered by this exporter
     
    177179
    178180    def filter_func(self, x, **kw):
    179         return get_documents(x)
     181        return get_documents(x, self.class_name)
    180182
    181183    def mangle_value(self, value, name, context=None):
     
    184186            value = value.messages
    185187        return super(
    186             CustomerDocumentExporter, self).mangle_value(
     188            CustomerDocumentExporterBase, self).mangle_value(
    187189            value, name, context=context)
    188190
    189191
    190 class ContractExporter(grok.GlobalUtility, CustomerExporterBase):
     192class CustomerSampleDocumentExporter(CustomerDocumentExporterBase):
     193    """Exporter for CustomerSampleDocument instances.
     194    """
     195    grok.name('customersampledocuments')
     196
     197    iface = ICustomerSampleDocument
     198    class_name = 'CustomerSampleDocument'
     199    title = _(u'Customer Sample Documents')
     200
     201
     202class SampleContractExporterBase(grok.GlobalUtility, CustomerExporterBase):
    191203    """Exporter for Contract instances.
    192     """
    193     grok.name('contracts')
    194 
    195     iface = ISampleContract
    196 
    197     #: The title under which this exporter will be displayed
    198     title = _(u'Contracts')
     204
     205    This is a baseclass.
     206    """
     207    grok.baseclass()
     208    iface = None
     209    class_name = None
     210    title = None
    199211
    200212    #: Fieldnames considered by this exporter
     
    211223
    212224    def filter_func(self, x, **kw):
    213         return get_contracts(x)
     225        return get_contracts(x, self.class_name)
    214226
    215227    def mangle_value(self, value, name, context=None):
     
    225237                return mangled_value
    226238        return super(
    227             ContractExporter, self).mangle_value(
     239            SampleContractExporterBase, self).mangle_value(
    228240            value, name, context=context)
     241
     242
     243class SampleContractExporter(SampleContractExporterBase):
     244    """Exporter for sample contracts.
     245    """
     246    grok.name('samplecontracts')
     247    iface = ISampleContract
     248    class_name = 'SampleContract'
     249    title = _(u'Sample Contracts')
  • main/waeup.ikoba/trunk/src/waeup/ikoba/customers/interfaces.py

    r12260 r12279  
    251251
    252252
     253class ICustomerSampleDocument(ICustomerDocument):
     254    """A customer sample document object.
     255
     256    """
     257
     258
    253259class ICustomerPDFDocument(ICustomerDocument):
    254     """A customer document.
     260    """A customer pdf document object.
    255261
    256262    """
  • main/waeup.ikoba/trunk/src/waeup/ikoba/customers/tests/test_export.py

    r12259 r12279  
    3131from waeup.ikoba.customers.catalog import CustomersQuery
    3232from waeup.ikoba.customers.export import (
    33     CustomerExporter, CustomerDocumentExporter, ContractExporter,
     33    CustomerExporter, CustomerSampleDocumentExporter, SampleContractExporter,
    3434    get_customers)
    3535from waeup.ikoba.customers.interfaces import ICSVCustomerExporter
     
    131131        return
    132132
    133 class CustomerDocumentExporterTest(CustomerImportExportSetup):
     133class CustomerSampleDocumentExporterTest(CustomerImportExportSetup):
    134134
    135135    layer = FunctionalLayer
    136136
    137137    def setUp(self):
    138         super(CustomerDocumentExporterTest, self).setUp()
     138        super(CustomerSampleDocumentExporterTest, self).setUp()
    139139        self.setup_for_export()
    140140        return
     
    142142    def test_ifaces(self):
    143143        # make sure we fullfill interface contracts
    144         obj = CustomerDocumentExporter()
     144        obj = CustomerSampleDocumentExporter()
    145145        verifyObject(ICSVCustomerExporter, obj)
    146         verifyClass(ICSVCustomerExporter, CustomerDocumentExporter)
     146        verifyClass(ICSVCustomerExporter, CustomerSampleDocumentExporter)
    147147        return
    148148
    149149    def test_get_as_utility(self):
    150150        # we can get a documents exporter as utility
    151         result = queryUtility(ICSVExporter, name="customerdocuments")
     151        result = queryUtility(ICSVExporter, name="customersampledocuments")
    152152        self.assertTrue(result is not None)
    153153        return
     
    157157        document = CustomerSampleDocument()
    158158        document.document_id = u'DOC1'
    159         exporter = CustomerDocumentExporter()
     159        exporter = CustomerSampleDocumentExporter()
    160160        exporter.export([document], self.outfile)
    161161        result = open(self.outfile, 'rb').read()
     
    172172        self.setup_customer(self.customer)
    173173        document = self.customer['documents']['DOC1']
    174         exporter = CustomerDocumentExporter()
     174        exporter = CustomerSampleDocumentExporter()
    175175        exporter.export([document], self.outfile)
    176176        result = open(self.outfile, 'rb').read()
     
    191191        # set values we can expect in export file
    192192        self.setup_customer(self.customer)
    193         exporter = CustomerDocumentExporter()
     193        exporter = CustomerSampleDocumentExporter()
    194194        exporter.export_all(self.app, self.outfile)
    195195        result = open(self.outfile, 'rb').read()
     
    209209        # set values we can expect in export file
    210210        self.setup_customer(self.customer)
    211         exporter = CustomerDocumentExporter()
     211        exporter = CustomerSampleDocumentExporter()
    212212        exporter.export_customer(self.customer, self.outfile)
    213213        result = open(self.outfile, 'rb').read()
     
    224224
    225225
    226 class ContractExporterTest(CustomerImportExportSetup):
     226class SampleContractExporterTest(CustomerImportExportSetup):
    227227
    228228    layer = FunctionalLayer
    229229
    230230    def setUp(self):
    231         super(ContractExporterTest, self).setUp()
     231        super(SampleContractExporterTest, self).setUp()
    232232        self.setup_for_export()
    233233        return
     
    235235    def test_ifaces(self):
    236236        # make sure we fullfill interface contracts
    237         obj = ContractExporter()
     237        obj = SampleContractExporter()
    238238        verifyObject(ICSVCustomerExporter, obj)
    239         verifyClass(ICSVCustomerExporter, ContractExporter)
     239        verifyClass(ICSVCustomerExporter, SampleContractExporter)
    240240        return
    241241
    242242    def test_get_as_utility(self):
    243243        # we can get a contracts exporter as utility
    244         result = queryUtility(ICSVExporter, name="contracts")
     244        result = queryUtility(ICSVExporter, name="samplecontracts")
    245245        self.assertTrue(result is not None)
    246246        return
     
    250250        contract = SampleContract()
    251251        contract.contract_id = u'CON1'
    252         exporter = ContractExporter()
     252        exporter = SampleContractExporter()
    253253        exporter.export([contract], self.outfile)
    254254        result = open(self.outfile, 'rb').read()
     
    267267        # set values we can expect in export file
    268268        self.setup_customer(self.customer)
    269         exporter = ContractExporter()
     269        exporter = SampleContractExporter()
    270270        exporter.export([self.contract], self.outfile)
    271271        result = open(self.outfile, 'rb').read()
     
    286286        # set values we can expect in export file
    287287        self.setup_customer(self.customer)
    288         exporter = ContractExporter()
     288        exporter = SampleContractExporter()
    289289        exporter.export_all(self.app, self.outfile)
    290290        result = open(self.outfile, 'rb').read()
     
    305305        # set values we can expect in export file
    306306        self.setup_customer(self.customer)
    307         exporter = ContractExporter()
     307        exporter = SampleContractExporter()
    308308        exporter.export_customer(self.customer, self.outfile)
    309309        result = open(self.outfile, 'rb').read()
Note: See TracChangeset for help on using the changeset viewer.