-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix catalog inconsistency when creating a dossier from a dossiertemplate. #6952
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fix catalog inconsistency when creating a dossier from a dossiertemplate. [njohner] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,47 +4,38 @@ | |
from plone import api | ||
from Products.CMFCore.utils import _getAuthenticatedUser | ||
from zope.globalrequest import getRequest | ||
from zope.interface import alsoProvides | ||
from zope.interface import Interface | ||
from zope.interface import noLongerProvides | ||
from zope.i18n import translate | ||
|
||
|
||
class IDisableCatalogIndexing(Interface): | ||
"""Marker-interface to disable the catalog | ||
indexing functions. | ||
|
||
If this interface is provided by the request, all the | ||
catalog-index-methods will be disabled. | ||
|
||
Use the DeactivatedCatalogIndexing contextmanager to | ||
get in use of this functionality. | ||
""" | ||
|
||
|
||
class DeactivatedCatalogIndexing(object): | ||
"""Contextmanager: Deactivates catalog-indexing | ||
""" | ||
def __enter__(self): | ||
alsoProvides(getRequest(), IDisableCatalogIndexing) | ||
PatchCMFCatalogAware()() | ||
|
||
def __exit__(self, exc_type, exc_val, exc_tb): | ||
noLongerProvides(getRequest(), IDisableCatalogIndexing) | ||
PatchCMFCatalogAware().unpatch() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. then use the same instance with the thread-safe mapping to unpatch
|
||
|
||
|
||
class CatalogAlreadyPatched(Exception): | ||
"""Will be raised if we try to patch the catalog indexing methods | ||
more than once. | ||
""" | ||
|
||
|
||
class PatchCMFCatalogAware(MonkeyPatch): | ||
"""Patch the Products.CMFCore.CMFCatalogAware indexObject, reindexObject | ||
and unindexObject methods. | ||
|
||
This patch is deactivated by default and can be activated through | ||
the DeactivatedCatalogIndexing context manager: | ||
This patch is deactivated is not applied by default and can be activated | ||
through the DeactivatedCatalogIndexing context manager: | ||
|
||
>>> with DeactivatedCatalogIndexing(): | ||
... object.reindexObject # Does nothing | ||
... object.unindexObject # Does nothing | ||
... object.indexObject # Does nothing | ||
|
||
If the patch is activated, it skips the catalog index-methods. | ||
If the patch is activated, it skips the catalog index-methods. The patch | ||
gets removed when exiting the context manager. | ||
|
||
What's the motivation behind this patch? | ||
|
||
|
@@ -58,38 +49,44 @@ class PatchCMFCatalogAware(MonkeyPatch): | |
and do it manually at the end of your tasks. | ||
""" | ||
|
||
original_indexing_methods = {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm having a very bad feeling about this class attribute. It will be shared between threads. Please redesign this a bit, so that we use the same instances of
|
||
|
||
def __call__(self): | ||
|
||
def _is_indexing_disabled(): | ||
return IDisableCatalogIndexing.providedBy(getRequest()) | ||
if self.is_already_applied(): | ||
raise CatalogAlreadyPatched() | ||
|
||
def indexObject(self): | ||
if _is_indexing_disabled(): | ||
# do nothing if indexing is disabled | ||
return | ||
return original_indexObject(self) | ||
return | ||
|
||
def unindexObject(self): | ||
if _is_indexing_disabled(): | ||
# do nothing if indexing is disabled | ||
return | ||
return original_unindexObject(self) | ||
return | ||
|
||
def reindexObject(self, idxs=[]): | ||
if _is_indexing_disabled(): | ||
# do nothing if indexing is disabled | ||
return | ||
return original_reindexObject(self, idxs) | ||
return | ||
|
||
from Products.CMFCore.CMFCatalogAware import CMFCatalogAware | ||
locals()['__patch_refs__'] = False | ||
original_indexObject = CMFCatalogAware.indexObject | ||
original_unindexObject = CMFCatalogAware.unindexObject | ||
original_reindexObject = CMFCatalogAware.reindexObject | ||
self.original_indexing_methods['indexObject'] = CMFCatalogAware.indexObject | ||
self.original_indexing_methods['unindexObject'] = CMFCatalogAware.unindexObject | ||
self.original_indexing_methods['reindexObject'] = CMFCatalogAware.reindexObject | ||
self.patch_refs(CMFCatalogAware, 'indexObject', indexObject) | ||
self.patch_refs(CMFCatalogAware, 'unindexObject', unindexObject) | ||
self.patch_refs(CMFCatalogAware, 'reindexObject', reindexObject) | ||
|
||
def is_already_applied(self): | ||
return bool(self.original_indexing_methods) | ||
|
||
def unpatch(self): | ||
from Products.CMFCore.CMFCatalogAware import CMFCatalogAware | ||
locals()['__patch_refs__'] = False | ||
self.patch_refs(CMFCatalogAware, 'indexObject', | ||
self.original_indexing_methods.pop('indexObject')) | ||
self.patch_refs(CMFCatalogAware, 'unindexObject', | ||
self.original_indexing_methods.pop('unindexObject')) | ||
self.patch_refs(CMFCatalogAware, 'reindexObject', | ||
self.original_indexing_methods.pop('reindexObject')) | ||
|
||
|
||
class PatchCMFCatalogAwareHandlers(MonkeyPatch): | ||
"""Customize `handleDynamicTypeCopiedEvent` to use RoleAssignmentManager | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't create multiple instances of the monkey patch, instead do
self.catalogpatch = PatchCMFCatalogAware()
(and call it separately)