dolmen.app.container is a collection of tools to work with containers in Dolmen applications.
Getting started
We import the Grok, request and authentication tools, in order to use them in our tests:
>>> from grok import testing >>> from zope.component import getMultiAdapter >>> from zope.publisher.browser import TestRequest >>> from zope.security.testing import Principal, Participation >>> from zope.security.management import newInteraction, endInteraction
Namechooser
In order to get a consistent policy concerning the container keys, dolmen.app.container provides a INameChooser adapter, for the dolmen.content.IContainer objects.
We create our container type:
>>> import dolmen.content
>>> class Folder(dolmen.content.Container):
... dolmen.content.name('a folderish content')
... dolmen.content.require('dolmen.content.Add')
>>> testing.grok_component('folder', Folder)
True
>>> root = getSite()
>>> folder = Folder()
>>> root['folder'] = folder
We now create a simple content type:
>>> from zope.interface import Interface, implements
>>> class IDocument(Interface):
... pass
>>> class Document(dolmen.content.Content):
... dolmen.content.name('a document')
... implements(IDocument)
>>> testing.grok_component('doc', Document)
True
>>> manfred = Document()
>>> manfred.__content_type__
'a document'
>>> manfred.title = u"Manfred"
To use the namechooser, we simply adapt our container to the INameChooser interfaces:
>>> from zope.container.interfaces import INameChooser >>> chooser = INameChooser(folder) >>> chooser <dolmen.app.container.namechoosers.NormalizingNameChooser object at ...>
If no name is provided, the component uses the object’s title to compute an id:
>>> chooser.chooseName(name='', object=manfred) 'manfred'
If a name is provided, it is used if possible:
>>> chooser.chooseName(name='furry elephant', object=manfred) 'furry elephant'
If the name already exists, it adds a number at the end of the id. To do that, the name chooser will try all the values from 0 to 100 and keep the first successful combination:
>>> folder['manfred'] = object() >>> chooser.chooseName(name='', object=manfred) 'manfred_1'
If there is no solution, an error is raised:
>>> for i in range(0, 101): ... folder['manfred_%d' % i] = object() >>> chooser.chooseName(name='', object=manfred) Traceback (most recent call last): ... ValueError: Cannot find a unique name based on `manfred` after 100 attemps.
Listing
To complete the container’s tools, dolmen.app.container registers a view in charge of displaying the content of a container as a table.
First, we reset the container:
>>> del root['folder'] >>> folder = root['folder'] = Folder()
We add all kind of contents:
>>> folder['manfred'] = Document() >>> folder['judith'] = Document() >>> folder['subfolder'] = Folder() >>> folder['not_dolmen.content.IBaseContent'] = object()
Then, we can query the listing view:
>>> listing = getMultiAdapter((folder, request), name="folderlisting") >>> listing.update()
The table lines are a list of the container values:
>>> list(listing.values) [<dolmen.app.container.ftests.Document object at ...>, <dolmen.app.container.ftests.Document object at ...>, <object object at ...>, <dolmen.app.container.ftests.Folder object at ...>]
The rendering displays links with icons (if existing):
>>> print listing.content()
<div class="folder-listing">
<h1>Content of the folder</h1>
<div><table class="listing sortable">
<thead>
<tr>
<th>Title</th>
<th>Modification date</th>
</tr>
</thead>
<tbody>
<tr class="even">
<td><a href="http://127.0.0.1/folder/judith">judith</a></td>
<td>None</td>
</tr>
<tr class="odd">
<td><a href="http://127.0.0.1/folder/manfred">manfred</a></td>
<td>None</td>
</tr>
<tr class="even">
<td><a href="http://127.0.0.1/folder/not_dolmen.content.IBaseContent">not_dolmen.content.IBaseContent</a></td>
<td></td>
</tr>
<tr class="odd">
<td><a href="http://127.0.0.1/folder/subfolder">subfolder</a></td>
<td>None</td>
</tr>
</tbody>
</table></div>
</div>
Credits
All Dolmen packages are sponsorised by NPAI (http://www.npai.fr)
Changelog
0.4 (2010-05-31)
Added translations (FR).
Removed icons from the folder listing.
0.3 (2010-01-26)
Cleaned up the dependencies : zope.app.testing is no longer used.
0.2 (2009-12-26)
Corrected imports. Now all the dependencies are declared.
Updated imports to use the latest ZTK.
0.1 (2009-11-03)
Initial release
Release files for dolmen.app.container 0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| dolmen.app.container-0.4.tar.gz | 10.8 kB | Details |
Release files / dolmen.app.container-0.4.tar.gz
| Download URL | dolmen.app.container-0.4.tar.gz |
|---|---|
| Size | 10.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
622d8f7bdddb08e18a947bc46b2f8dc74514e63a7f43d07431220125b8e894ba
|
|
BLAKE2b-256 checksum How to use checksums |
b9541b3ccf5171988c00a87f740a54835db53538a17517f3237f5a170c5c8b8f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |