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.

File:
1 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
Note: See TracChangeset for help on using the changeset viewer.