forked from plone/plone.app.tiles
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement tile book-keeping. See tests.py for some examples of how to…
… use it. svn path=/plone.app.tiles/trunk/; revision=32549
- Loading branch information
Showing
8 changed files
with
233 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,3 @@ | ||
plone.app.tiles todo | ||
==================== | ||
|
||
[ ] Book-keeping for which tiles have been persisted in annotations, to allow | ||
a "delete all" or selection of tile ids to delete. This may be done using | ||
events. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
"""Event handlers to keep a list of tiles | ||
""" | ||
|
||
from BTrees.OOBTree import OOBTree | ||
|
||
from zope.component import adapts | ||
from zope.interface import implements | ||
from zope.component import adapter | ||
|
||
from zope.annotation.interfaces import IAnnotatable, IAnnotations | ||
|
||
from zope.lifecycleevent.interfaces import IObjectAddedEvent | ||
from zope.lifecycleevent.interfaces import IObjectRemovedEvent | ||
|
||
from plone.tiles.interfaces import ITile | ||
|
||
from plone.app.tiles.interfaces import ITileBookkeeping | ||
|
||
ANNOTATIONS_KEY = "plone.app.tiles.bookkeeping" | ||
|
||
@adapter(ITile, IObjectAddedEvent) | ||
def recordTileAdded(tile, event): | ||
"""Inform the ITileBookkeeping adapter for the tile's new parent that | ||
it has just been added. | ||
""" | ||
|
||
bookkeeping = ITileBookkeeping(event.newParent, None) | ||
if bookkeeping is not None: | ||
bookkeeping.added(tile.__name__, tile.id) | ||
|
||
@adapter(ITile, IObjectRemovedEvent) | ||
def recordTileRemoved(tile, event): | ||
"""Inform the ITileBookkeeping adapter for the tile's old parent that | ||
it has just been removed. | ||
""" | ||
|
||
bookkeeping = ITileBookkeeping(event.oldParent, None) | ||
if bookkeeping is not None: | ||
bookkeeping.removed(tile.id) | ||
|
||
class AnnotationsTileBookkeeping(object): | ||
"""Default adapter for tile bookkeeping. | ||
This stores a btree in annotations on the content object, with tile ids | ||
as keys and tile types as values. | ||
""" | ||
|
||
implements(ITileBookkeeping) | ||
adapts(IAnnotatable) | ||
|
||
def __init__(self, context): | ||
self.context = context | ||
self.annotations = IAnnotations(context) | ||
|
||
def added(self, tileType, tileId): | ||
tree = self.annotations.setdefault(ANNOTATIONS_KEY, OOBTree()) | ||
tree[tileId] = tileType | ||
|
||
def removed(self, tileId): | ||
tree = self.annotations.get(ANNOTATIONS_KEY, {}) | ||
if tileId in tree: | ||
del tree[tileId] | ||
return True | ||
return False | ||
|
||
def typeOf(self, tileId): | ||
tree = self.annotations.get(ANNOTATIONS_KEY, {}) | ||
return tree.get(tileId, None) | ||
|
||
def enumerate(self, tileType=None): | ||
tree = self.annotations.setdefault(ANNOTATIONS_KEY, {}) | ||
for tileId, tileTypeStored in tree.iteritems(): | ||
if tileType is None or tileTypeStored == tileType: | ||
yield (tileId, tileTypeStored,) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters