/plugins/sys_plugin/sys_plugin.py

http://micolog.googlecode.com/ · Python · 166 lines · 138 code · 24 blank · 4 comment · 19 complexity · 3c52c751e1c74480383155c0cf66133c MD5 · raw file

  1. # -*- coding: utf-8 -*-
  2. from micolog_plugin import *
  3. import logging,re
  4. from google.appengine.api import mail
  5. from model import *
  6. from google.appengine.api import users
  7. from base import BaseRequestHandler,urldecode
  8. from google.appengine.ext.webapp import template
  9. SBODY='''New comment on your post "%(title)s"
  10. Author : %(author)s
  11. E-mail : %(email)s
  12. URL : %(weburl)s
  13. Comment:
  14. %(content)s
  15. You can see all comments on this post here:
  16. %(commenturl)s
  17. '''
  18. BBODY='''Hi~ New reference on your comment for post "%(title)s"
  19. Author : %(author)s
  20. URL : %(weburl)s
  21. Comment:
  22. %(content)s
  23. You can see all comments on this post here:
  24. %(commenturl)s
  25. '''
  26. class NotifyHandler(BaseRequestHandler):
  27. def __init__(self):
  28. BaseRequestHandler.__init__(self)
  29. self.current="config"
  30. self.sbody=OptionSet.getValue('sys_plugin_sbody',SBODY)
  31. self.bbody=OptionSet.getValue('sys_plugin_bbody',BBODY)
  32. def get(self):
  33. self.template_vals.update({'self':self})
  34. content=template.render('plugins/sys_plugin/setup.html',self.template_vals)
  35. self.render2('views/admin/setup_base.html',{'m_id':'sysplugin_notify','content':content})
  36. #Also you can use:
  37. #self.render2('plugins/sys_plugin/setup2.html',{'m_id':'sysplugin_notify','self':self})
  38. def post(self):
  39. self.bbody=self.param('bbody')
  40. self.sbody=self.param('sbody')
  41. self.blog.comment_notify_mail=self.parambool('comment_notify_mail')
  42. self.blog.put()
  43. OptionSet.setValue('sys_plugin_sbody',self.sbody)
  44. OptionSet.setValue('sys_plugin_bbody',self.bbody)
  45. self.get()
  46. class sys_plugin(Plugin):
  47. def __init__(self):
  48. Plugin.__init__(self,__file__)
  49. self.author="xuming"
  50. self.authoruri="http://xuming.net"
  51. self.uri="http://xuming.net"
  52. self.description="System plugin for micolog"
  53. self.name="Sys Plugin"
  54. self.version="0.2"
  55. self.blocklist=OptionSet.getValue("sys_plugin_blocklist",default="")
  56. self.register_filter('head',self.head)
  57. self.register_filter('footer',self.footer)
  58. self.register_urlmap('sys_plugin/setup',self.setup)
  59. self.register_urlhandler('/admin/sys_plugin/notify',NotifyHandler)
  60. self.register_setupmenu('sysplugin_notify',_('Notify'),'/admin/sys_plugin/notify')
  61. self.register_action('pre_comment',self.pre_comment)
  62. self.register_action('save_comment',self.save_comment)
  63. self.sbody=OptionSet.getValue('sys_plugin_sbody',SBODY)
  64. self.bbody=OptionSet.getValue('sys_plugin_bbody',BBODY)
  65. def head(self,content,blog=None,*arg1,**arg2):
  66. content=content+'<meta name="generator" content="Micolog %s" />'%blog.version
  67. return content
  68. def footer(self,content,blog=None,*arg1,**arg2):
  69. return content+'<!--Powered by micolog %s-->'%blog.version
  70. def setup(self,page=None,*arg1,**arg2):
  71. if not page.is_login:
  72. page.redirect(users.create_login_url(page.request.uri))
  73. tempstr='''
  74. <p>blocklist:</p>
  75. <form action="" method="post">
  76. <p>
  77. <textarea name="ta_list" style="width:400px;height:300px">%s</textarea>
  78. </p>
  79. <input type="submit" value="submit">
  80. </form>'''
  81. if page.request.method=='GET':
  82. page.render2('views/admin/base.html',{'m_id':'sysplugin_block','content':tempstr%self.blocklist})
  83. else:
  84. self.blocklist=page.param("ta_list")
  85. OptionSet.setValue("sys_plugin_blocklist",self.blocklist)
  86. page.render2('views/admin/base.html',{'m_id':'sysplugin_block','content':tempstr%self.blocklist})
  87. def get(self,page):
  88. return '''<h3>Sys Plugin</h3>
  89. <p>This is a system plugin for micolog. <br>Also a demo for how to write plugin for micolog.</p>
  90. <h4>feature</h4>
  91. <p><ol>
  92. <li>Add Meta &lt;meta name="generator" content="Micolog x.x" /&gt;</li>
  93. <li>Add footer "&lt;!--Powered by micolog x.x--&gt;"</li>
  94. <li>Comments Filter with blocklist <a href="/e/sys_plugin/setup">Setup</a></li>
  95. <li>Comment Notify <a href="/admin/sys_plugin/notify">Setup</a></li>
  96. </ol></p>
  97. '''
  98. def pre_comment(self,comment,*arg1,**arg2):
  99. for s in self.blocklist.splitlines():
  100. if comment.content.find(s)>-1:
  101. raise Exception
  102. def save_comment(self,comment,*arg1,**arg2):
  103. if self.blog.comment_notify_mail:
  104. self.notify(comment)
  105. def notify(self,comment):
  106. try:
  107. sbody=self.sbody.decode('utf-8')
  108. except:
  109. sbody=self.sbody
  110. try:
  111. bbody=self.bbody.decode('utf-8')
  112. except:
  113. bbody=self.bbody
  114. if self.blog.comment_notify_mail and self.blog.owner and not users.is_current_user_admin() :
  115. sbody=sbody%{'title':comment.entry.title,
  116. 'author':comment.author,
  117. 'weburl':comment.weburl,
  118. 'email':comment.email,
  119. 'content':comment.content,
  120. 'commenturl':comment.entry.fullurl+"#comment-"+str(comment.key().id())
  121. }
  122. mail.send_mail_to_admins(self.blog.owner.email(),'Comments:'+comment.entry.title, sbody,reply_to=comment.email)
  123. #reply comment mail notify
  124. refers = re.findall(r'#comment-(\d+)', comment.content)
  125. if len(refers)!=0:
  126. replyIDs=[int(a) for a in refers]
  127. commentlist=comment.entry.comments()
  128. emaillist=[c.email for c in commentlist if c.reply_notify_mail and c.key().id() in replyIDs]
  129. emaillist = {}.fromkeys(emaillist).keys()
  130. for refer in emaillist:
  131. if self.blog.owner and mail.is_email_valid(refer):
  132. emailbody = bbody%{'title':comment.entry.title,
  133. 'author':comment.author,
  134. 'weburl':comment.weburl,
  135. 'email':comment.email,
  136. 'content':comment.content,
  137. 'commenturl':comment.entry.fullurl+"#comment-"+str(comment.key().id())
  138. }
  139. message = mail.EmailMessage(sender = self.blog.owner.email(),subject = 'Comments:'+comment.entry.title)
  140. message.to = refer
  141. message.body = emailbody
  142. message.send()