Creating Basic Zope Applications - Replies
(Page 4 of 4 )
We're now on the final part of our forum system: replies. As I said eariler, each reply will have its own file within the topic's folder. Create a Zope Page Template object named topic in our forum's main folder and insert this into it:
<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/getReplies'></span>
<b>New Reply</b><br />
<form method='POST' action='newReply'>
<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 Reply'>
</form>
</span>
</span>
This calls the file getReplies in the current context and then outputs a forum to add replies. The form sends its data to the file newReply. This is very similar to the cat file.
The next step is to create a Script ( Python ) object by the name of getReplies. Like the getTopics script, this handles the dirty work. It gathers the replies and formats them, quoting any HTML code and handling linebreaks.
from Products.PythonScripts.standard import html_quote, newline_to_br
for reply in context.objectValues():
print '<b>%s</b><br />' % ( reply.getProperty ( 'author' ) )
print '%s<br /><br />' % ( newline_to_br ( html_quote ( reply ) ) )
return printed
Finally, we need to create the files required to add new replies. Create a Zope Page Template object named newReply.
<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/addReply'></span>
</span>
</span>
The only thing the page does worth noting is that it calls the file addReply in the current context. Create a Script ( Python ) object named addReply. Fill it with the following code:
id = str ( len ( context.objectIds() ) + 1 )
context.manage_addProduct [ 'OFSP' ].manage_addFile ( id, file = context.REQUEST.message )
replyObject = getattr ( context, id )
replyObject.manage_addProperty ( 'author', context.REQUEST.author, 'string' )
return 'Your reply has been added.<br />'
The script creates a new file in the topic's folder, fills it with the reply and adds an author property, giving it the appropriate value.
Conclusion
Your forum is now fully functional! Go ahead and test it out. Notice how easy it was to create the forum. This illustrates how easy Zope makes the creation of Web applications.
Feel free to expand the forum for extra practice. Now that you understand the basics of creating applications in Zope, you should find it quite easy. Try adding a user database or moderation and administration functions.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |