Creating Basic Zope Applications - Topics
(Page 3 of 4 )
Let's now create a list of topics in a given category. As you saw in the previous script, we need to create a file named cat. Go ahead and create a Zope Page Template named cat. Insert the following text into it:
<span metal:use-macro='container/base/macros/base'>
<h1 metal:fill-slot='title' tal:content='here/title'>Title</h1>
<span metal:fill-slot='content'>
<span tal:condition='here/objectValues'>
<span tal:replace='structure context/getTopics'></span>
</span>
<span tal:condition='not:here/objectValues'>
Sorry, this category contains no topics.<br />
</span>
<br />
<b>New Topic</b><br />
<form method='POST' action='newTopic'>
<label>Subject:</label><br />
<input type='text' name='subject'><br />
<label>Author:</label><br />
<input type='text' name='author'><br />
<label>Message:</label><br />
<textarea name='message' rows='10' cols='40'></textarea><br />
<input type='submit' value='Add Topic'>
</form>
</span>
</span>
Again, we use the macro to make our page's layout match the layouts of the other files. Feel free to edit the macro and experiment with different styles.
We use a conditional expression to determine whether the category contains any topics. If it does, we call the file getTopics in the current context. If the folder contains no objects, we display a message which explains that there are no topics available. Finally, we create a form that allows us to add new topics. The form sends its data to a file called newTopic.
Create a Script ( Python ) object with the name of getTopics. This script will create a list of topics and format them. Insert the following script into it:
for topic in context.objectValues():
if topic.meta_type == 'Folder':
print '<a href=\'%s/topic\'>%s</a>' % ( topic.getId(), topic.getProperty ( 'title' ) )
print '<br />By %s<br />' % ( topic.getProperty ( 'author' ) )
return printed
This script is very similar to the last script. Notice how we retrieve an author property, however.
We'll now make the file newTopic. Create a Zope Page Template named newTopic and insert the following code:
<span metal:use-macro='container/base/macros/base'>
<h1 metal:fill-slot='title' tal:content='context/title'>Title</h1>
<span metal:fill-slot='content'>
<span tal:replace='structure context/addTopic'></span>
</span>
</span>
We don't do anything amazing, as you can see. We only call the file addTopic in the current context. Create a Script ( Python ) named addTopic. It will do the real work. Insert the following text:
id = str ( len ( context.objectIds() ) + 1 )
context.manage_addProduct [ 'OFSP' ].manage_addFolder ( id, title = context.REQUEST.subject )
replyFolder = getattr ( context, id )
replyFolder.manage_addProperty ( 'author', context.REQUEST.author, 'string' )
replyFolder.manage_addProduct [ 'OFSP' ].manage_addFile ( '1', file = context.REQUEST.message )
replyObject = getattr ( replyFolder, '1' )
replyObject.manage_addProperty ( 'author', context.REQUEST.author, 'string' )
return 'Your topic has been added.<br />'
Although the code may look complicated, its function is pretty simple. First, it counts the number of topics currently in the category. For simplicity's sake, the topic folder Ids will just be numbers. The first topic will be 1, the second 2, the third 3 and so on.
We then create a folder for the topic with the appropriate title. Next, we add an author property, and, finally, we create the first reply and add its author property.
Next: Replies >>
More Zope Articles
More By Peyton McCullough