给Micolog 添加评论回复邮件通知功能

九月 6th, 2009 发表评论 阅读评论

首先在相应主题的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 标签: micolog


    “给Micolog 添加评论回复邮件通知功能 ”共有 10 条留言

  1. Leyond 2010-05-14 at 23:49 |#1 F

    我用的是最新版的micolog, 哪些回复代码都是在一个sys_plugin.py文件里面。可以收到别人回复的通知,但是回复别人,别人就收不到了邮件通知了。这个为啥,我在界面添加了
    <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. 我勾选了也没有反应,那里其他还需要更改的吗
  2. 云在天边 2010-05-15 at 00:00 |#2 F

    @Leyond-419001
    最新版的代码里应该不用改就可以了,你到后台看看 Mail API Calls等的使用情况,看是否调用发送成功
  3. 云在天边 2010-05-15 at 00:16 |#3 F

    @Leyond-419001
    我知道了原因了!默认情况下,系统会寻找恢复格式为@username-id:,只有满足上述格式的,才会调用mail api
    replyComments = re.findall(r'@[\S]+[:]', self.content)
    所以你需要在模版中把回复格式修改成匹配format即可。
  4. Leyond 2010-05-15 at 11:05 |#4 F

    引用云在天边
    @Leyond-419001

    我知道了原因了!默认情况下,系统会寻找恢复格式为@username-id:,只有满足上述格式的,才会调用mail api

    replyComments = re.findall(r'@[\S]+[:]', self.content)

    所以你需要在模版中把回复格式修改成匹配format即可。

    It works~ thx
  5. Leyond 2010-05-15 at 11:44 |#5 F

    觉得不是很好用,只有在后台勾选了允许将评论发送到你的邮箱:。才可以发送。现在我的想法是 我自己不想收到别人的回复,但是别人留言若有回复他能收到。 我测试的时候只有勾选允许将评论发送到你的邮箱,留言者才能收到回复。
  6. Leyond 2010-05-15 at 11:49 |#6 F

    恩,我把:去掉 不好看
  7. 云在天边 2010-05-15 at 11:54 |#7 F

    @Leyond-424001
    这个很简单啊,你给你的应用添加一个专门负责发送接收评论e-mail的账户,在修改一下代码即可。
  8. houkai 2010-05-15 at 14:34 |#8 F

    感谢提醒 ie8下 ok了!
  9. 云在天边 2010-05-16 at 17:47 |#7 F

    @houkai-426001
    我的修改了半天,也终于好了,呵呵
  10. houkai 2010-05-16 at 19:59 |#10 F

    测试留言
  1. 本文目前尚无任何 trackbacks 和 pingbacks.
在这里留下您的脚印吧……

/static/smilies/icon_question.gif /static/smilies/icon_razz.gif /static/smilies/icon_sad.gif /static/smilies/icon_evil.gif /static/smilies/icon_exclaim.gif /static/smilies/icon_smile.gif /static/smilies/icon_redface.gif /static/smilies/icon_biggrin.gif /static/smilies/icon_surprised.gif /static/smilies/icon_mrgreen.gif /static/smilies/icon_eek.gif /static/smilies/icon_confused.gif /static/smilies/icon_cool.gif /static/smilies/icon_lol.gif /static/smilies/icon_mad.gif /static/smilies/icon_twisted.gif /static/smilies/icon_rolleyes.gif /static/smilies/icon_wink.gif /static/smilies/icon_idea.gif /static/smilies/icon_arrow.gif /static/smilies/icon_neutral.gif /static/smilies/icon_cry.gif

请输入验证码(不区分大小写)