Changeset 12194


Ignore:
Timestamp:
11 Dec 2014, 08:12:31 (10 years ago)
Author:
Henrik Bettermann
Message:

Fix handle_product_remove. The attribute is meanwhile called product_object.

Add handle_document_removed with test.

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

Legend:

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

    r12169 r12194  
    2525from zope.component.interfaces import IFactory
    2626from zope.interface import implementedBy
     27from zope.event import notify
    2728
    2829from waeup.ikoba.image import IkobaImageFile
     
    321322            file_obj.filename, file_obj.data)
    322323
     324
     325@grok.subscribe(ICustomerDocument, grok.IObjectRemovedEvent)
     326def handle_document_removed(document, event):
     327    """If a document is deleted, we make sure that also referrers to
     328    customer contract objects are removed.
     329    """
     330    docid = document.document_id
     331
     332    # Find all customer contracts that refer to given document...
     333    try:
     334        contracts = document.customer['contracts'].values()
     335    except AttributeError:
     336        # customer not available. This might happen during tests.
     337        return
     338    for contract in contracts:
     339        # Remove that referrer...
     340        #contract.product = None
     341        for key, value in contract.__dict__.items():
     342            if key.endswith('_object') and \
     343                getattr(value, 'document_id', None) == docid:
     344                setattr(contract, key, None)
     345                notify(grok.ObjectModifiedEvent(contract))
     346                contract.customer.__parent__.logger.info(
     347                    'ObjectRemovedEvent - %s - %s - removed: %s' % (
     348                        contract.customer.customer_id,
     349                        contract.contract_id,
     350                        document.document_id))
     351    return
     352
  • main/waeup.ikoba/trunk/src/waeup/ikoba/customers/tests/test_catalog.py

    r12098 r12194  
    2424from zope.component import queryUtility, createObject
    2525from zope.component.hooks import setSite
     26from hurry.workflow.interfaces import IWorkflowState
    2627from waeup.ikoba.app import Company
     28from waeup.ikoba.interfaces import VERIFIED
    2729from waeup.ikoba.testing import FunctionalLayer, FunctionalTestCase
    2830from waeup.ikoba.customers.customer import Customer
     
    6668        self.product = self.app['products'][self.product_id]
    6769
     70        document = createObject('waeup.CustomerSampleDocument')
     71        document.title = u'My Samle Document'
     72        IWorkflowState(document).setState(VERIFIED)
     73        self.customer['documents'].addDocument(document)
     74        self.document_id = document.document_id
     75        self.document = self.customer['documents'][self.document_id]
     76
    6877        contract = createObject('waeup.SampleContract')
    6978        contract.title = u'My Samle Contract'
    70         contract.product = product
     79        contract.product_object = product
     80        contract.document_object = document
    7181        contract.last_product_id = product.product_id
    7282        self.customer['contracts'].addContract(contract)
    7383        self.contract_id = contract.contract_id
    7484        self.contract = self.customer['contracts'][self.contract_id]
    75 
    76         document = createObject('waeup.CustomerSampleDocument')
    77         document.title = u'My Samle Document'
    78         self.customer['documents'].addDocument(document)
    79         self.document_id = document.document_id
    80         self.document = self.customer['documents'][self.document_id]
    8185
    8286        return
     
    167171        assert len(results) == 1
    168172        assert results[0] is self.customer['contracts'][self.contract_id]
    169         assert results[0].product is self.product
     173        assert results[0].product_object is self.product
    170174
    171175    def test_product_removal(self):
    172176        # This test does not only test catalog components, it also ensures
    173         # that the product attribute of an contract is cleared
     177        # that the product object attribute of a contract is cleared
    174178        # but the last_product_id is not.
    175179        del self.app['products'][self.product_id]
    176         self.assertTrue(self.contract.product is None)
     180        self.assertTrue(self.contract.product_object is None)
    177181        self.assertEqual(self.contract.last_product_id, 'SAM')
    178182        cat = queryUtility(ICatalog, name='contracts_catalog')
     
    181185        assert len(results) == 1
    182186        assert results[0] is self.customer['contracts'][self.contract_id]
    183         assert results[0].product is None
     187        assert results[0].product_object is None
    184188        # Product removal is being logged in customers.log
    185189        logfile = os.path.join(
     
    190194            in logcontent)
    191195
     196    def test_document_removal(self):
     197        # Actually, this test does not test catalog components at all.
     198        # Tt only ensures that the document object attributes of a contract
     199        # are cleared, like in the test_product_removal above.
     200        del self.customer['documents'][self.document_id]
     201        self.assertTrue(self.contract.document_object is None)
     202        # Document removal is being logged in customers.log
     203        logfile = os.path.join(
     204            self.app['datacenter'].storage, 'logs', 'customers.log')
     205        logcontent = open(logfile).read()
     206        self.assertTrue(
     207            'INFO - system - ObjectRemovedEvent - K1000000 - c101 - removed: d101\n'
     208            in logcontent)
     209
  • main/waeup.ikoba/trunk/src/waeup/ikoba/products/product.py

    r12097 r12194  
    7979    for contract in results:
    8080        # Remove that referrer...
    81         contract.product = None
     81        contract.product_object = None
    8282        notify(grok.ObjectModifiedEvent(contract))
    8383        contract.customer.__parent__.logger.info(
Note: See TracChangeset for help on using the changeset viewer.