Entries For: April 2007
2007-04-19
Passing data to viewlets from the parent template
To insert a rendered viewlet manager into a page, one uses something like "tal:replace="structure provider: ILeftSide". Of course, viewlet managers are providers, they implement the IContentProvider interface and are looked up with this interface in the 'provider' TALES handler. One particular trick that content providers do is that they allow insertion of TAL data from the template, to make it accessible inside the content provider class. A sort of "parameters", as it is detailed in the README.txt file, chapter "Additional data from TAL". Things are a bit more complicated for the viewlet managers, because the viewlet manager object is created with a new type at runtime, a type which inherits only the minimum information from the new viewlet manager interface. The only way to make the created viewlet manager provide a new interface (as required by the TALESProviderExpression to insert data from the context) is to subclass ViewletManagerBase and use this class as the factory class for the viewlet manager. I'm giving a simplified example, mostly inspired by the zope.contentprovider documentation:
The viewlet manager interface,
class ILeftSide(IViewletManager):
"""The left side"""
which is registered with:
<viewletManager name="ILeftSide" provides=".ILeftSide"
class=".MessagePassingViewletManager" permission="zope.View" />
The viewlet manager is implemented with this new class:
class MessagePassingViewletManager(ViewletManagerBase):
implements(IMessage)
The class is, as I have found, a key component, because you need it to declare to implement a new interface, the IMessage:
class IMessage(Interface):
message = TextLine(u'Some message')
And the IMessage interface is declared as having a type, zope.contentprovider.interfaces.ITALNamespaceData
<zope:interface interface=".interfaces.IMessage"
type="zope.contentprovider.interfaces.ITALNamespaceData" />
The idea is that all attributes specified in the IMessage interface can then be specified as attributes for the element that renders the viewlet manager in the template:
<div tal:define="message string:Hello World"
tal:replace="structure provider:ILeftSide" />
Then, in any viewlet subscribed to the ILeftSide manager, you' can have:
<div tal:content="view/manager/message" />
which will render of course the 'Hello world' string. You can actually pass any object this way, there's no constraint imposed by the type defined in the interface schema, as I could tell from my tests.
2007-04-13
Traversing from Python a Zope 3 page that uses the traverse subpath
Getting the traverse subpath in Zope 2 is relatively easy and involves almost no tricks. Doing the same thing in Zope3 is a bit more involved, but nevertheless still relatively easy. What might not be obvious at first is how to traverse to the page from Python code. Call me slow, but it took me about 2 minutes to think of the solution.
The idea is: I have a page along these lines: http://localhost/site/tips/page_context/next. 'tips' is the page that is published, 'page_context' is a string that contains a unique page context key for which I get the tips and 'next' is an operation I want to do, based on which I get the tips content. I'm doing this because I'm storing in the session a history of 'seen tips', so that I can navigate them forward/backward.
So how does one get that page from python code? Like this:
page_context = 'my_page'
tips_page = getMultiAdapter((self.context, self.request), name='tips')
tip_content = tips_page.publishTraverse(self.request, page_context).publishTraverse(self.request, 'next')()
Starting the fans for an HP Compaq NX6125 with Ubuntu Feisty after suspend
In the saga to make my laptop behave as best as I can I've encountered a new obstacle: using Ubuntu Feisty Fawn (the beta) with kubuntu-desktop installed and using KDE, I can put the laptop in suspend mode, but the CPU fan won't turn on when I wake it. After a bit of searching I've found out that I need to do:
root@ubuntu:~# echo 0 > /proc/acpi/fan/C262/state
This starts the fan. Writing 3 in that fan ACPI state will turn off the fan.
References:
- http://gentoo-wiki.com/HARDWARE_HP_Compaq_nx6125_with_Turion64
- http://www.novell.com/documentation/suse91/suselinux-adminguide/html/ch09s03.html
The last update of Ubuntu broke the suspend, as shaky as it was before. The HAL package was updated and now Kubuntu won't wake up from sleep anymore. Also, I've updated my desktop from Edgy to Feisty and the update process wasn't without pains: the update-manager software would freeze and after updating with aptitude distr-upgrade, Gnome won't start anymore, the apparent reason being some problem with alsa. Funny enough, after I've installed kubuntu-desktop I can run KDE and play music with amarok without problems.