Personal tools
You are here: Home 博客群 zhangbingkai 让你的Ploneboard匿名用户填写姓名可发贴
Log in


Forgot your password?
Recent Comments
 gfghgfh (Anonymous User) 2008-09-05
 efer (Anonymous User) 2008-08-29
 sfdg (Anonymous User) 2008-08-29
 asddsaads (ruicong100) 2007-10-19
 sdsd (ruicong100) 2007-10-19
 
Document Actions

让你的Ploneboard匿名用户填写姓名可发贴 让你的Ploneboard匿名用户填写姓名可发贴

Submitted by zhangbingkai. on 2007-09-14 12:05.
Everydo.com的论坛使用Ploneboard实现,匿名浏览用户无需注册登录,填写姓名就可发贴;而再次访问Everydo.com时无需再填写姓名,将使用存在Cookie中的姓名来发贴。 因为有社区里朋友问到,我将实现过程写在这里。

1. 开启匿名用户可发贴

在添加了论坛board后,再来添加子论坛forum后,改变forum状态为Free of all,如下图:


2. 了解Ploneboard发贴、发表评论的过程

发贴时,页面打开的是这样地址:

.../add_conversation_form?

我们可以在Portal_skins中找到add_conversation_form的页面,它是一个CPT页面,你若不了解什么叫控制页面模 板,简易先到CZUG的Plone中文书籍项目中了解第十三章的内容,项目地址:http://czug.everydo.com 登录用户名/密码用 guset/guest 。

我们在add_conversation_form页面的Actions标签页中看到Post提交后会提交给 add_conversation_script,很显然,add_conversation_script是处理发贴表单的逻辑代码。我们也可以在 Portal_skins中找到add_conversation_script。

我们再来了解发表评论的过程。

发表评论有两种方式,使用回复或快速回复,我们看到点击“回复”时,链接到了

../add_comment_form

在快速回复的HTML代码中,看到form提交也提交到add_comment_form

很显然,add_comment_form是表单,我们在Portal_skins中找到了它,它也是一个CPT页面,在 add_comment_form的Actions标签页中看到Post后提交给了add_comment_script,那么 add_comment_script就是发表评注表单的处理逻辑代码。

3. 定制“显示”部分代码

我们在add_conversation_form的代码中看到:

<div metal:use-macro="here/add_comment_form/macros/comment_body_title" tal:omit-tag="" />

<div metal:use-macro="here/add_comment_form/macros/comment_body_text" tal:omit-tag="" />

<div metal:use-macro="here/add_comment_form/macros/comment_body_attachments" tal:omit-tag="" />

<div metal:use-macro="here/add_comment_form/macros/comment_body_buttons" tal:omit-tag="" />

显然add_conversation_form使用的是add_comment_form的宏。

我们再来在add_comment_form中定制添加姓名的宏:

 <div metal:define-macro="comment_body_creator" tal:omit-tag="">
<div class="field">
<label for="creator">您的名字</label>

<div class="formHelp">
</div>
<input id="creator" type="text" size="40" name="creator"
tal:attributes="value python:request.get('creator','');"/>
</div>

提示:姓名的表单中有这样的代码:

tal:attributes="value python:request.get('creator','');"

这是后文我们将从表单中的creator和email存放在Cookie中,让用户第一次发贴后,后面的发贴表单中有前面填写的值。

我们再将姓名的宏加在add_conversation_form中:

<div metal:use-macro="here/add_comment_form/macros/comment_body_creator" tal:omit-tag="" />

显示部分就定制完了。

4. 定制“逻辑”部分代码

在add_conversation_script中:

它的参数有:

title, text='', files=None

我们需要加上creator的参数,将 Parameter List 填写为

title, text='', files=None, creator=''

下面这部分代码是用户名的处理:

if pm.isAnonymousUser():
creator = 'Anonymous'
else:
creator = pm.getAuthenticatedMember().getUserName()

我们将它定制为:

if not pm.isAnonymousUser():
creator = pm.getAuthenticatedMember().getUserName()

if creator == '':
creator = '匿名用户'

可以将creator存放在在根目录的Cookie中,保存为30天,这部分代码可写在后面:

cookie_path = '/'
length = 30
expires = (DateTime() + length).toZone('GMT').rfc822()

context.REQUEST.RESPONSE.setCookie('creator',creator, path=cookie_path, expires=expires)

那么,add_comment_script中也类似的定制这样的代码。

然后,你就发现可以类似于Everydo.com中的论坛一样用户不登录可用自己的姓名发贴了。

Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: