Changeset 12095


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.

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

Legend:

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

    r12094 r12095  
    2929from waeup.ikoba.customers.interfaces import (
    3030    IApplicationsContainer, ICustomerNavigation,
    31     IApplication, ICustomersUtils,
     31    IApplication, IApplicationEdit, ICustomersUtils,
    3232    )
    3333from waeup.ikoba.customers.utils import generate_application_id
     
    5959    """This is a customer application baseclass.
    6060    """
    61     grok.implements(IApplication, ICustomerNavigation)
     61    grok.implements(IApplication, IApplicationEdit, ICustomerNavigation)
    6262    grok.provides(IApplication)
    6363    grok.baseclass()
  • main/waeup.ikoba/trunk/src/waeup/ikoba/customers/browser.py

    r12094 r12095  
    5151    ICustomer, ICustomersContainer, ICustomerRequestPW, ICustomersUtils,
    5252    ICustomerDocument, ICustomerDocumentsContainer, ICustomerCreate,
    53     ICustomerPDFDocument, IApplicationsContainer, IApplication
     53    ICustomerPDFDocument, IApplicationsContainer, IApplication, IApplicationEdit
    5454    )
    5555from waeup.ikoba.customers.catalog import search
     
    11831183    grok.name('edit')
    11841184    grok.require('waeup.handleCustomer')
     1185    form_fields = grok.AutoFields(IApplicationEdit).omit('last_transition_date')
    11851186
    11861187    def update(self):
  • main/waeup.ikoba/trunk/src/waeup/ikoba/customers/interfaces.py

    r12094 r12095  
    290290        title = _(u'Product'),
    291291        source = AppCatProductSource(),
    292         required = True,
    293         )
     292        required = False,
     293        )
     294
     295class IApplicationEdit(IApplication):
     296    """Interface for editing application data by customers.
     297
     298    """
     299
     300    product = schema.Choice(
     301        title = _(u'Product'),
     302        source = AppCatProductSource(),
     303        required = True,
     304        )
  • main/waeup.ikoba/trunk/src/waeup/ikoba/customers/tests/test_catalog.py

    r12094 r12095  
    1616## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
    1717##
     18import os
    1819import grok
    1920import shutil
     
    167168        assert results[0] is self.customer['applications'][self.application_id]
    168169        assert results[0].product is self.product
     170
     171    def test_product_removal(self):
     172        # This test does not only test catalog components, it also ensures
     173        # that the product attribute of an application is cleared
     174        # but the last_product_id is not.
     175        del self.app['products'][self.product_id]
     176        self.assertTrue(self.application.product is None)
     177        self.assertEqual(self.application.last_product_id, 'SAM')
     178        cat = queryUtility(ICatalog, name='applications_catalog')
     179        results = cat.searchResults(last_product_id=('SAM', 'SAM'))
     180        results = [x for x in results]
     181        assert len(results) == 1
     182        assert results[0] is self.customer['applications'][self.application_id]
     183        assert results[0].product is None
     184        # Product removal is being logged in customers.log
     185        logfile = os.path.join(
     186            self.app['datacenter'].storage, 'logs', 'customers.log')
     187        logcontent = open(logfile).read()
     188        self.assertTrue(
     189            'INFO - system - ObjectRemovedEvent - K1000000 - a101 - removed: product\n'
     190            in logcontent)
     191
  • 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.