1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
| #!/usr/bin/env python
# -*- coding: utf-8 -*-
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext import db
from google.appengine.api import users
import urllib, datetime, re
step = 10
title = "Python blog system"
class AuthHandler(webapp.RequestHandler):
def get(self, key = ""):
if users.get_current_user() == None:
self.write("<a href="%s">Sign in or register</a>." % users.create_login_url("/admin"))
elif users.is_current_user_admin() != True:
self.write('Your account %s is not admin. <a href="%s">Log out</a> and log in with an admin account.' % (users.get_current_user(), users.create_logout_url("/admin")))
else:
if key:
self.get2(key)
else:
self.get2()
def post(self, key = ""):
if users.is_current_user_admin():
if key:
self.post2(key)
else:
self.post2()
def write(self, str):
self.response.out.write(str)
class MainHandler(AuthHandler):
def get(self, pageStr):
try:
page = int(pageStr)
except ValueError:
page = 0
printHeader(self, title)
self.write('<h1><a href="/">%s</a></h1>' % title)
if users.is_current_user_admin():
self.write('<h2>[<a href="http://blog.elearning.co.jp?s=&search_404=1">New</a>] [<a href="%s">Log out</a>]</h2>' % users.create_logout_url("/"))
entries = Entry.all().order("-datetime").fetch(step + 1, page * step)
for entry in entries[:step]:
printEntry(self, entry)
if len(entries) > step:
self.write('[ <a href="/%d"> Next %d</a> ]' % (page + 1, step))
if page > 0:
self.write('[ <a href="/%d"> Prev %d</a> ]' % (page - 1, step))
printFooter(self)
class RSSHandler(AuthHandler):
def get(self, pageStr):
self.write(
u"""< ?xml version="1.0" encoding="utf-8"?>
<rdf:rdf xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns="http://purl.org/rss/1.0/">
<channel rdf:about="http://python-blog-system.appspot.com/">
<title>%(title)s</title>
<link>http://python-blog-system.appspot.com</link>
<description>%(title)s</description>
<dc:language>ja</dc:language>
<dc:creator>N/A</dc:creator>
<dc:date>%(now)s</dc:date>
</channel>""" % {'now' : '2010-12-31T16:57:12+09:00', 'title' : title})
for entry in Entry.all().order("-datetime").fetch(30):
self.write(""" <item rdf:about="http://python-blog-system.appspot.com/entry/%(key)s">
<title>%(title)s</title>
<link>http://python-blog-system.appspot.com/entry/%(key)s</link>
<description>%(body)s</description>
<dc:date>%(datetime)s</dc:date>
</item>
""" % {"key" : entry.key(), "title" : h(entry.title), "body" : h(entry.body), "datetime" : entry.formattedDatetimeInJST})
self.write("</rdf:rdf>")
class AdminHandler(AuthHandler):
def get2(self, key):
self.redirect("/")
class PostHandler(AuthHandler):
def get2(self,key = ""):
entry = Entry.get(key) if key != '' else Entry()
title = "Edit Entry" if key!= '' else "New Entry"
deleteButton = """<input type="button" value="delete" onclick="if(confirm('Are you sure to delete it?'))location.href='/delete/%s'"/>""" % key if key!= '' else ''
printHeader(self, title)
self.write("<h1>%s</h1>" % title)
self.write(u"""<form method="post" action="/post/%(key)s">
<h2>タイトル</h2><input type="text" name="title" value="%(title)s" style="width:400px"/>
<h2>本文</h2><textarea name="body" style="width:400px;height:300px;">%(body)s</textarea>
<h2>タグ</h2><input type="text" name="tags" value="%(tags)s" style="width:400px"/>
<h2>画像 [<a href="http://blog.elearning.co.jp?s=uploader&search_404=1" target="_blank">uploader</a>]</h2>
<div>%(deleteButton)s<input type="submit" value="submit"/></div></form>""" % {"key" : key, "title" : entry.title, "body" : entry.body, "tags" : entry.tagStr(), "deleteButton":deleteButton})
printFooter(self)
def post2(self, key = ""):
if self.request.get("title") != '' and self.request.get("body") != '':
entry = Entry.get(key) if key != '' else Entry()
entry.title = self.request.get("title")
entry.body = self.request.get("body")
entry.tags = []
for tagStr in self.request.get('tags').replace(u' ',' ').replace(' ',' ').replace(',',' ').split(' '):
tag = Tag.all().filter("tag =", tagStr).get()
if tag == None:
tag = Tag(tag = tagStr)
tag.put()
entry.tags.append(tag.key())
entry.put()
self.redirect('/')
class PostCommentHandler(AuthHandler):
def post(self, key):
if self.request.get("comment") != '':
Comment(
entry = Entry.get(key),
comment = self.request.get("comment"),
delpass = self.request.get("delpass"),
nickname = self.request.get("nickname")
).put()
self.redirect("/entry/%s" % key)
class DeleteHandler(AuthHandler):
def get2(self, key):
db.delete(Entry.get(key))
self.redirect('/')
class DeleteCommentHandler(AuthHandler):
def post(self, key):
comment = Comment.get(key)
entry_key = comment.entry.key()
self.write(self.request.get("delpass"))
self.write(comment.delpass)
if self.request.get("delpass") == comment.delpass:
db.delete(comment)
self.redirect('/entry/%s' % entry_key)
class TagHandler(AuthHandler):
def get(self, key):
tagStr = urllib.unquote(key).decode('utf-8')
title = "Python blog system / %s" % tagStr
printHeader(self, title);
tag = Tag.all().filter("tag =", tagStr).get()
self.write('<h1><a href="/">Python blog system</a> / %s</h1>' % h(tagStr))
if tag:
for entry in tag.entries:
printEntry(self, entry)
else:
self.write("<h2>Tag %s does not exist</h2>" % h(tagStr))
printFooter(self)
class EntryHandler(AuthHandler):
def get(self, key):
entry = Entry.get(key)
printHeader(self, "%s / %s" % (title, entry.title));
self.write('<h1><a href="/">%s</a></h1>' % title)
printEntry(self, entry, commentDetail = True)
printFooter(self)
class UploaderHandler(AuthHandler):
def get2(self, key):
printHeader(self, "Uploader")
self.write("<h1>Uploader</h1>")
for image in Image.all():
self.write('<h2><img src="/image/%(key)s"/><br /><input type="text" value="[img:%(key)s]" style="width:300px;font-size:x-small"/><input type="button" value="delete" onclick="location.href=\'/deleteImage/%(key)s\'"/></h2>' % {"key":image.key()})
self.write(u'<h2><form action="/uploader" enctype="multipart/form-data" method="post"><input type="file" name="file"/><input type="submit" value="Upload"/></form></h2>')
printFooter(self)
def post2(self, key):
if self.request.get('file'):
self.write("hoge")
image = Image()
image.image = self.request.POST.get('file').file.read()
image.contentType = self.request.body_file.vars['file'].headers['content-type']
image.put()
self.redirect('/uploader')
class DeleteImageHandler(AuthHandler):
def get2(self, key):
Image.get(key).delete()
self.redirect('/uploader')
class ImageHandler(AuthHandler):
def get(self, key):
image = Image.get(key)
self.response.headers['Content-Type'] = image.contentType.encode('utf-8')
self.response.out.write(image.image)
def printEntry(self, entry, commentDetail = False):
user = users.get_current_user()
if user:
editLink = '[<a href="/post/%s">edit</a>]' % entry.key()
else:
editLink = ''
self.write('<div class="entry">\n<div class="entryHeader">\n')
self.write('<h2 class="title"><a href="/entry/%(key)s">%(title)s</a> %(editLink)s</h2> <div class="entryDate">%(datetime)s</div>'
% {"key" : entry.key(), "title" : h(entry.title), "editLink" : editLink, "datetime" : entry.formattedDatetimeInJST})
self.write('</div>\n')#header
self.write(replaceImages(linkURLs(nl2br(h(entry.body)))));
self.write('\n<div class="entryFooter">tag:\n')
for tag in entry.tags:
tagObj = Tag.get(tag)
self.write('<a href="/tag/%s"><span class="tag">%s</span></a>\n' % (urllib.quote_plus(tagObj.tag.encode('utf-8')) ,h(tagObj.tag)))
if commentDetail:
self.write(u'<h2>コメント</h2><div class="comments"><a name="comments">\n')
for comment in entry.comments.order('datetime'):
if comment.nickname == None or comment.nickname == "":
comment.nickname = "Anonymous"
delbutton = u"""
<div style="float:right">
<form method="post" name="form" action="/deleteComment/%(key)s">
<input type="text" name="delpass" class="delcommentpassword"/>
<input type="button" onclick="if(confirm('本当に削除しますか?'))form.submit()" value="削除" class="delcommentbutton"/>
</form>
</div><br clear="all"/>""" % {"key":comment.key()}
self.write('<h3 class="comment">%s: %s %s</h3>' % (comment.nickname, comment.comment, delbutton))
self.write(u'<div style="width:84px;float:left;font-size:xx-small;position:relative;top:6px;">名前</div>')
self.write(u'<div style="width:184px;float:left;font-size:xx-small;position:relative;top:6px;">コメント</div>')
self.write(u'<div style="width:100px;float:left;font-size:xx-small;position:relative;top:6px;">削除パス</div>')
self.write(u'<br clear="all"/>')
self.write(u'<form action="/postComment/%s" method="post" style="padding:0">' % entry.key())
self.write(u'<input type="text" name="nickname" style="width:80px;"/>')
self.write(u'<input type="text" name="comment" style="width:180px;"/>')
self.write(u'<input type="text" name="delpass" style="width:50px;"/>')
self.write(u'<input type="submit" value="投稿" style="width:50px"/>')
self.write("</form></a></div>\n")
else:
self.write(u'<a href="/entry/%s#comments">コメント(%s)</a>\n' % (entry.key(), entry.comments.count()))
self.write("</div>\n")#footer
self.write("</div>\n")#entry
def urlReplacer(match, limit = 45):
return '<a href="%s" target="_blank">%s</a>' % (match.group(), match.group()[:limit] + ('...' if len(match.group()) > limit else ''))
def linkURLs(str):
return re.sub(r'([^"]|^)(https?|ftp)(://[\w:;/.?%#&=+-]+)', urlReplacer, str)
def replaceImages(str):
return re.sub(r'\[img:(.*)\]', r'<img src="http://blog.elearning.co.jp?s=\1&search_404=1" style="max-width:400px"/>', str)
def printHeader(self, title):
self.write("""< ?xml version="1.0" encoding="UTF-8"?>
< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
<head>
<link rel="alternate" type="application/rss+xml" title="RSS" href="rss"/>
<meta http-equiv="content-script-type" content="text/javascript"/>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>%s</title>
<link rel="stylesheet" type="text/css" href="/css/style.css"/>
<meta name = "viewport" content = "width=420"/>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-20245912-2']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>""" % (h(title)))
self.write("</head>\n<body>\n");
def printFooter(self):
self.write(u'<div>developed by <a href="http://php6.jp/python/">python練習帳</a></div>\n</body>\n</html>')
def nl2br(str):
return str.replace('\r\n','\n').replace('\n','<br />\n')
class Entry(db.Model):
title = db.StringProperty(default = "")
body = db.TextProperty(default = "")
tags = db.ListProperty(db.Key)
datetime = db.DateTimeProperty(auto_now_add = True)
@property
def formattedDatetimeInJST(self):
return (self.datetime + datetime.timedelta(hours=9)).strftime("%Y-%m-%d %H:%M:%S")
def tagStr(self):
return " ".join([Tag.get(x).tag for x in self.tags])
class Tag(db.Model):
tag = db.StringProperty()
@property
def entries(self):
return Entry.all().filter('tags', self.key()).order('-datetime')
class Comment(db.Model):
comment = db.TextProperty(default = "")
entry = db.ReferenceProperty(Entry, collection_name = 'comments')
user = db.UserProperty()
datetime = db.DateTimeProperty(auto_now_add = True)
delpass = db.TextProperty()
nickname = db.TextProperty()
class Image(db.Model):
image = db.BlobProperty()
contentType = db.StringProperty()
def main():
application = webapp.WSGIApplication([
('/tag/(.*)', TagHandler),
('/entry/(.*)', EntryHandler),
('/admin/?(.*)', AdminHandler),
('/postComment/?(.*)', PostCommentHandler),
('/post/?(.*)', PostHandler),
('/rss/?(.*)', RSSHandler),
('/deleteComment/?(.*)', DeleteCommentHandler),
('/deleteImage/(.*)', DeleteImageHandler),
('/delete/?(.*)', DeleteHandler),
('/uploader/?(.*)', UploaderHandler),
('/image/(.*)', ImageHandler),
('/(.*)', MainHandler)
], debug=True)
util.run_wsgi_app(application)
def h(html):
return html.replace('&','&').replace('< ','<').replace('>','>').replace('"','"')
if __name__ == '__main__':
main() |