accessing custom properties of file objects
I've been following the Zopebook and I'm trying to customize the file library. I have divided the files up into sections by adding a section property to each file. Here's a portion of the code: <div tal:condition="python:item.section == 'c'" tal:omit-tag> <td> <a href="http://sample" tal:attributes="href item/getId" tal:content="item/getId">File_link</a> </td> <td tal:content="item/file_size">Type</td> <td tal:content="item/title">Description</td> <td tal:content="item/date_mod" align="center">Last modified</td> </div> What's really strange is that this works: <div tal:condition="python:item.title == 'an_actual_file.zpt'" tal:omit-tag> I've found some links on "pre-condition" but from what I've read (http://www.zope.org/Members/dgeorgieff/howto_precondition) this seems to apply to dtml. How do I acess properties which I have created but aren't normally a part of an object? tia, Jesse
Jesse Goerz writes:
I've been following the Zopebook and I'm trying to customize the file library. I have divided the files up into sections by adding a section property to each file. Here's a portion of the code:
<div tal:condition="python:item.section == 'c'" tal:omit-tag> <td> <a href="http://sample" tal:attributes="href item/getId" tal:content="item/getId">File_link</a> </td> <td tal:content="item/file_size">Type</td> <td tal:content="item/title">Description</td> <td tal:content="item/date_mod" align="center">Last modified</td> </div>
What's really strange is that this works: <div tal:condition="python:item.title == 'an_actual_file.zpt'" tal:omit-tag> Hmm, you do not tell us, it what way the other code does not work...
exception? (which one, with Error Type/Value and tracebach) wrong result? something else? Dieter
On Thu, 2002-06-20 at 15:40, Dieter Maurer wrote:
Hmm, you do not tell us, it what way the other code does not work...
exception? (which one, with Error Type/Value and tracebach) wrong result? something else?
Sorry about that. I posted prematurely. I seem to be able to test if the property I created is there but I'd really like to base my conditions on the value of the property. Below is my amended code as well as the tracebacks. <table border="1" width="95%" bgcolor="#000000" cellspacing="2" cellpadding="2" align="center" tal:define="objects here/files; sort_on python:(('id', 'nocase', 'asc'), ('bobobase_modification_time', 'cmp', 'desc')); sorted_objects python:sequence.sort(objects, sort_on)"> <tbody> <tr> <th>Filename</th> <th>Size</th> <th>Description</th> <th>Last modified</th> </tr> <tr tal:repeat="item sorted_objects"> <!-- This one displays all the file objects with a property of "section", but I want to display based on the value of "section", i.e. there will be multiple different sections throughout the html page, each in its own table. <div tal:condition="python:exists('item/section')" --> <div tal:condition="python:'item/section'=='c'" tal:omit-tag> <td> <a href="http://jesse.homelinux.net/sample" tal:attributes="href item/getId" tal:content="item/getId">File_link</a> </td> <td tal:content="item/file_size">Type</td> <td tal:content="item/title">Description</td> <td tal:content="item/date_mod" align="center">Last modified</td> </div> </tr> </tbody> </table> The statement I'm working on here is: <div tal:condition="python:'item/section'=='c'" tal:omit-tag> That above gives no errors but outputs only one blank row. This: <div tal:condition="python:item.section=='c'" tal:omit-tag> outputs these errors: Traceback (innermost last): File /usr/lib/zope/lib/python/ZPublisher/Publish.py, line 150, in publish_module File /usr/lib/zope/lib/python/ZPublisher/Publish.py, line 114, in publish File /usr/lib/zope/lib/python/Zope/__init__.py, line 159, in zpublisher_exception_hook (Object: file_library) File /usr/lib/zope/lib/python/ZPublisher/Publish.py, line 98, in publish File /usr/lib/zope/lib/python/ZPublisher/mapply.py, line 88, in mapply (Object: index_html) File /usr/lib/zope/lib/python/ZPublisher/Publish.py, line 39, in call_object (Object: index_html) File /usr/lib/zope/lib/python/Shared/DC/Scripts/Bindings.py, line 252, in __call__ (Object: index_html) File /usr/lib/zope/lib/python/Shared/DC/Scripts/Bindings.py, line 283, in _bindAndExec (Object: index_html) File /usr/lib/zope/lib/python/Products/PageTemplates/ZopePageTemplate.py, line 210, in _exec (Object: index_html) File /usr/lib/zope/lib/python/Products/PageTemplates/PageTemplate.py, line 86, in pt_render (Object: index_html) File /usr/lib/zope/lib/python/TAL/TALInterpreter.py, line 158, in __call__ File /usr/lib/zope/lib/python/TAL/TALInterpreter.py, line 191, in interpret File /usr/lib/zope/lib/python/TAL/TALInterpreter.py, line 476, in do_loop_tal File /usr/lib/zope/lib/python/TAL/TALInterpreter.py, line 191, in interpret File /usr/lib/zope/lib/python/TAL/TALInterpreter.py, line 490, in do_condition File /usr/lib/zope/lib/python/Products/PageTemplates/TALES.py, line 262, in evaluateBoolean File /usr/lib/zope/lib/python/Products/PageTemplates/TALES.py, line 247, in evaluate File /usr/lib/zope/lib/python/Products/PageTemplates/ZRPythonExpr.py, line 49, in __call__ (Info: item.section=='c') File Python expression "item.section=='c'", line 2, in f TALESError: (see above) I've tried as many variations of the item/section as I could think of but obviously not the one I need. tia, Jesse
On Fri, 2002-06-21 at 00:10, Jesse Goerz wrote:
On Thu, 2002-06-20 at 15:40, Dieter Maurer wrote:
Hmm, you do not tell us, it what way the other code does not work...
exception? (which one, with Error Type/Value and tracebach) wrong result? something else?
Sorry about that. I posted prematurely. I seem to be able to test if the property I created is there but I'd really like to base my conditions on the value of the property. Below is my amended code as well as the tracebacks.
<table border="1" width="95%" bgcolor="#000000" cellspacing="2" cellpadding="2" align="center" tal:define="objects here/files; sort_on python:(('id', 'nocase', 'asc'), ('bobobase_modification_time', 'cmp', 'desc')); sorted_objects python:sequence.sort(objects, sort_on)">
<tbody> <tr> <th>Filename</th> <th>Size</th> <th>Description</th> <th>Last modified</th> </tr>
<tr tal:repeat="item sorted_objects"> <!-- This one displays all the file objects with a property of "section", but I want to display based on the value of "section", i.e. there will be multiple different sections throughout the html page, each in its own table. <div tal:condition="python:exists('item/section')" --> <div tal:condition="python:'item/section'=='c'" tal:omit-tag>
<td> <a href="http://jesse.homelinux.net/sample" tal:attributes="href item/getId" tal:content="item/getId">File_link</a> </td> <td tal:content="item/file_size">Type</td> <td tal:content="item/title">Description</td> <td tal:content="item/date_mod" align="center">Last modified</td>
</div> </tr> </tbody> </table>
The statement I'm working on here is: <div tal:condition="python:'item/section'=='c'" tal:omit-tag> That above gives no errors but outputs only one blank row.
I was having a hard time with the expressions so I tried a python script instead which seems to work. Here's what I did: """ returns all 'File' objects with property 'section' set to 'c' """ all_files = context.objectValues('File') c_files = [] for i in all_files: try: if getattr(i, 'section') == 'c': c_files.append(i) except: pass sequence.sort(c_files) return c_files What is the exact syntax of the sort method? I was trying to sort like I did above:
sort_on python:(('id', 'nocase', 'asc'), ('bobobase_modification_time', 'cmp', 'desc')); sorted_objects python:sequence.sort(objects, sort_on)">
The docs state that sequence.sort(seq, sort_schema) where sort schema is a tuple (key, function, direction). I used the exact same values ('id', 'nocase', 'asc') and I get Error Type: TALESError Error Value: exceptions.AttributeError on 'None' object has no attribute 'getitem' in "standard:'here/c_section'", at line 22, column 1
participants (2)
-
Dieter Maurer -
Jesse Goerz