给Micolog 添加评论回复邮件通知功能
首先在相应主题的comments.html文件中在添加评论邮件通知的checkbox,位置可以随便定,感觉放在submit按钮之后会比较好:
<p>
<input name="submit" type="submit" id="submit" tabindex="6" value="Submit Comment" />
<input name="reply_notify_mail" id="reply_notify_mail" tabindex="5" checked="checked"
style="width: auto;" type="checkbox" />
<label for="reply_notify_mail">Notify me if there is a reply</label>
</p>
接着在blog.py中Post_comment类中添加处理客户端POST数据中的邮件定制信息:
# 大概在248行的位置,添加reply_notify_mail的几行
key=self.param('key')
content=self.param('comment')
replynotify=self.param('reply_notify_mail')
reply_notify_mail=True
if replynotify != 'on':
reply_notify_mail=False
# 大概在295行的位置,添加reply_notify_mail的一行
else:
comment=Comment(author=name,
content=content.replace('^~',
"<img src="../static/images/emotions/icon_" alt="" />'),
email=email,
reply_notify_mail=reply_notify_mail,
entry=Entry.get(key))
然后在model.py中添加评论回复通知功能:
if g_blog.comment_notify_mail and g_blog.owner:#and not users.is_current_user_admin()
sbody=sbody%(self.entry.title,self.author,self.email,self.weburl,self.content,
g_blog.baseurl+"/"+self.entry.link+"#comment-"+str(self.key().id()))
mail.send_mail_to_admins(g_blog.owner.email(),'Comments on:'
+self.entry.title, sbody,reply_to=self.email)
logging.info('send %s . entry: %s'%(g_blog.owner.email(),self.entry.title))
# 大概在515行的位置,从这里开始添加功能
replyComments = re.findall(r'@[\S]+[:]', self.content)
if len(replyComments)!=0:
originAuthor=[a[1:-1] for a in replyComments]
commentQuery=Comment.all().filter('entry =', self.entry)
.filter('author =', originAuthor[0]).order('-date')
commentGet=commentQuery.get()
emailInfo=commentGet.email
notifyEnable=commentGet.reply_notify_mail
if notifyEnable and g_blog.comment_notify_mail
and g_blog.owner and mail.is_email_valid(emailInfo):
emailBody=ebody%(originAuthor[0],self.entry.title,self.author,self.weburl,
self.content,g_blog.baseurl+"/"
+self.entry.link+"#comment-"+str(self.key().id()))
message=mail.EmailMessage(sender=g_blog.owner.email(),
subject="Reply on your post:"+self.entry.title)
message.to=emailInfo
message.body=emailBody
message.send()
# 大概在460行的位置添加数据库中表示是否定制邮件通知的数据元:
author=db.StringProperty()
email=db.EmailProperty()
reply_notify_mail=db.BooleanProperty(default=True) <---添加这一句
weburl=db.URLProperty()
之后在index.yaml添加查询用的index,这个比较关键,当时前面order('-date') gae总是报错,就是没有index的缘故:
# 重新添加一个Comment的查询index
- kind: Comment
properties:
- name: entry
- name: author
- name: date
direction: desc
添加完毕后,博客程序便会根据被回复人在当前文章最新的回复状态来判断是否发送邮件通知。
转载自Stingrey
转载请注明来自 云在天边看世界 http://www.tangblog.info本文永久链接 http://www.tangblog.info/2009/09/5/micolog_reply.html
“给Micolog 添加评论回复邮件通知功能 ”共有 10 条留言
<input name="reply_notify_mail" id="reply_notify_mail" tabindex="5" checked="checked"
style="width: auto;" type="checkbox" />
Notify me if there is a reply
里面默认的reply_notify_mail默认是False. 我勾选了也没有反应,那里其他还需要更改的吗
最新版的代码里应该不用改就可以了,你到后台看看 Mail API Calls等的使用情况,看是否调用发送成功
我知道了原因了!默认情况下,系统会寻找恢复格式为@username-id:,只有满足上述格式的,才会调用mail api
replyComments = re.findall(r'@[\S]+[:]', self.content)所以你需要在模版中把回复格式修改成匹配format即可。
It works~ thx
这个很简单啊,你给你的应用添加一个专门负责发送接收评论e-mail的账户,在修改一下代码即可。
我的修改了半天,也终于好了,呵呵
在这里留下您的脚印吧……