Ignore:
Timestamp:
30 Nov 2014, 09:12:44 (10 years ago)
Author:
Henrik Bettermann
Message:

Add event subscriber handle_product_removed which ensures that also referrers to customer application objects are removed when a product is deleted.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.ikoba/trunk/src/waeup/ikoba/products/product.py

    r12068 r12095  
    2424from hurry.workflow.interfaces import IWorkflowInfo, IWorkflowState
    2525from zope.event import notify
     26from zope.catalog.interfaces import ICatalog
    2627from zope.component import getUtility
    27 from zope.component.interfaces import IFactory
     28from zope.component.interfaces import IFactory, ComponentLookupError
    2829from zope.interface import implementedBy
    2930from zope.i18n import translate
     
    6162    def getInterfaces(self):
    6263        return implementedBy(Product)
     64
     65@grok.subscribe(IProduct, grok.IObjectRemovedEvent)
     66def handle_product_removed(product, event):
     67    """If a product is deleted, we make sure that also referrers to
     68    customer application objects are removed.
     69    """
     70    prodid = product.product_id
     71
     72    # Find all customer applications that refer to given product...
     73    try:
     74        cat = getUtility(ICatalog, name='applications_catalog')
     75    except ComponentLookupError:
     76        # catalog not available. This might happen during tests.
     77        return
     78    results = cat.searchResults(last_product_id=(prodid, prodid))
     79    for application in results:
     80        # Remove that referrer...
     81        application.product = None
     82        notify(grok.ObjectModifiedEvent(application))
     83        application.customer.__parent__.logger.info(
     84            'ObjectRemovedEvent - %s - %s - removed: product' % (
     85                application.customer.customer_id, application.application_id))
     86    return
     87
Note: See TracChangeset for help on using the changeset viewer.