PageRenderTime 154ms CodeModel.GetById 120ms app.highlight 18ms RepoModel.GetById 13ms app.codeStats 0ms

/plugins/sys_plugin/sys_plugin.py

http://micolog.googlecode.com/
Python | 166 lines | 164 code | 1 blank | 1 comment | 12 complexity | 3c52c751e1c74480383155c0cf66133c MD5 | raw file
  1# -*- coding: utf-8 -*-

  2from micolog_plugin import *
  3import logging,re
  4from google.appengine.api import mail
  5from model import *
  6from google.appengine.api import users
  7from base import BaseRequestHandler,urldecode
  8from google.appengine.ext.webapp import template
  9
 10SBODY='''New comment on your post "%(title)s"

 11Author : %(author)s

 12E-mail : %(email)s

 13URL	: %(weburl)s

 14Comment:

 15%(content)s

 16You can see all comments on this post here:

 17%(commenturl)s

 18'''
 19
 20BBODY='''Hi~ New reference on your comment for post "%(title)s"

 21Author : %(author)s

 22URL	: %(weburl)s

 23Comment:

 24%(content)s

 25You can see all comments on this post here:

 26%(commenturl)s

 27'''
 28
 29class NotifyHandler(BaseRequestHandler):
 30	def __init__(self):
 31		BaseRequestHandler.__init__(self)
 32		self.current="config"
 33		self.sbody=OptionSet.getValue('sys_plugin_sbody',SBODY)
 34		self.bbody=OptionSet.getValue('sys_plugin_bbody',BBODY)
 35
 36	def get(self):
 37		self.template_vals.update({'self':self})
 38		content=template.render('plugins/sys_plugin/setup.html',self.template_vals)
 39		self.render2('views/admin/setup_base.html',{'m_id':'sysplugin_notify','content':content})
 40		#Also you can use:

 41		#self.render2('plugins/sys_plugin/setup2.html',{'m_id':'sysplugin_notify','self':self})

 42
 43	def post(self):
 44		self.bbody=self.param('bbody')
 45		self.sbody=self.param('sbody')
 46		self.blog.comment_notify_mail=self.parambool('comment_notify_mail')
 47		self.blog.put()
 48		OptionSet.setValue('sys_plugin_sbody',self.sbody)
 49		OptionSet.setValue('sys_plugin_bbody',self.bbody)
 50
 51		self.get()
 52
 53
 54class sys_plugin(Plugin):
 55	def __init__(self):
 56		Plugin.__init__(self,__file__)
 57		self.author="xuming"
 58		self.authoruri="http://xuming.net"
 59		self.uri="http://xuming.net"
 60		self.description="System plugin for micolog"
 61		self.name="Sys Plugin"
 62		self.version="0.2"
 63		self.blocklist=OptionSet.getValue("sys_plugin_blocklist",default="")
 64		self.register_filter('head',self.head)
 65		self.register_filter('footer',self.footer)
 66
 67		self.register_urlmap('sys_plugin/setup',self.setup)
 68
 69		self.register_urlhandler('/admin/sys_plugin/notify',NotifyHandler)
 70		self.register_setupmenu('sysplugin_notify',_('Notify'),'/admin/sys_plugin/notify')
 71
 72		self.register_action('pre_comment',self.pre_comment)
 73		self.register_action('save_comment',self.save_comment)
 74		self.sbody=OptionSet.getValue('sys_plugin_sbody',SBODY)
 75		self.bbody=OptionSet.getValue('sys_plugin_bbody',BBODY)
 76
 77
 78
 79
 80	def head(self,content,blog=None,*arg1,**arg2):
 81		content=content+'<meta name="generator" content="Micolog %s" />'%blog.version
 82		return content
 83
 84	def footer(self,content,blog=None,*arg1,**arg2):
 85
 86		return content+'<!--Powered by micolog %s-->'%blog.version
 87
 88	def setup(self,page=None,*arg1,**arg2):
 89		if not page.is_login:
 90			page.redirect(users.create_login_url(page.request.uri))
 91		tempstr='''

 92		    <p>blocklist:</p>

 93			<form action="" method="post">

 94			<p>

 95			<textarea name="ta_list" style="width:400px;height:300px">%s</textarea>

 96			</p>

 97			<input type="submit" value="submit">

 98			</form>'''
 99		if page.request.method=='GET':
100			page.render2('views/admin/base.html',{'m_id':'sysplugin_block','content':tempstr%self.blocklist})
101		else:
102			self.blocklist=page.param("ta_list")
103			OptionSet.setValue("sys_plugin_blocklist",self.blocklist)
104			page.render2('views/admin/base.html',{'m_id':'sysplugin_block','content':tempstr%self.blocklist})
105
106	def get(self,page):
107		return '''<h3>Sys Plugin</h3>

108			   <p>This is a system plugin for micolog. <br>Also a demo for how to write plugin for micolog.</p>

109			   <h4>feature</h4>

110			   <p><ol>

111			   <li>Add Meta &lt;meta name="generator" content="Micolog x.x" /&gt;</li>

112			   <li>Add footer "&lt;!--Powered by micolog x.x--&gt;"</li>

113			   <li>Comments Filter with blocklist <a href="/e/sys_plugin/setup">Setup</a></li>

114			   <li>Comment Notify <a href="/admin/sys_plugin/notify">Setup</a></li>

115			   </ol></p>

116				'''
117
118	def pre_comment(self,comment,*arg1,**arg2):
119		for s in self.blocklist.splitlines():
120			if comment.content.find(s)>-1:
121				raise Exception
122	def save_comment(self,comment,*arg1,**arg2):
123		if self.blog.comment_notify_mail:
124			self.notify(comment)
125
126	def notify(self,comment):
127
128		try:
129						sbody=self.sbody.decode('utf-8')
130		except:
131						sbody=self.sbody
132		try:
133						bbody=self.bbody.decode('utf-8')
134		except:
135						bbody=self.bbody
136
137		if self.blog.comment_notify_mail and self.blog.owner and not users.is_current_user_admin() :
138			sbody=sbody%{'title':comment.entry.title,
139						   'author':comment.author,
140						   'weburl':comment.weburl,
141						   'email':comment.email,
142						   'content':comment.content,
143						   'commenturl':comment.entry.fullurl+"#comment-"+str(comment.key().id())
144						 }
145			mail.send_mail_to_admins(self.blog.owner.email(),'Comments:'+comment.entry.title, sbody,reply_to=comment.email)
146
147		#reply comment mail notify

148		refers = re.findall(r'#comment-(\d+)', comment.content)
149		if len(refers)!=0:
150			replyIDs=[int(a) for a in refers]
151			commentlist=comment.entry.comments()
152			emaillist=[c.email for c in commentlist if c.reply_notify_mail and c.key().id() in replyIDs]
153			emaillist = {}.fromkeys(emaillist).keys()
154			for refer in emaillist:
155				if self.blog.owner and mail.is_email_valid(refer):
156						emailbody = bbody%{'title':comment.entry.title,
157						   'author':comment.author,
158						   'weburl':comment.weburl,
159						   'email':comment.email,
160						   'content':comment.content,
161						   'commenturl':comment.entry.fullurl+"#comment-"+str(comment.key().id())
162						 }
163						message = mail.EmailMessage(sender = self.blog.owner.email(),subject = 'Comments:'+comment.entry.title)
164						message.to = refer
165						message.body = emailbody
166						message.send()