Sun Mar 01 22:26:57 2009 +0900
Initial Commit
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/myblog.py Sun Mar 01 22:26:57 2009 +0900 1.3 @@ -0,0 +1,163 @@ 1.4 +""" 1.5 + simple blog engine by web.py 1.6 + 1.7 + require web.py ver0.3 1.8 +""" 1.9 +__version__ = '0.0.3' 1.10 +__author__ = 'syobosyobo@gmail.com' 1.11 + 1.12 +import time 1.13 +import os 1.14 +import sqlite3 1.15 +import web 1.16 + 1.17 +URLROOT = 'webpy' 1.18 +TEMPLATE_PATH = '/path/to/templates/' 1.19 +DBPATH = '/path/to/sqlite' 1.20 +render = web.template.render(TEMPLATE_PATH) 1.21 + 1.22 +urls = ( 1.23 + '/login', 'login', 1.24 + '/logout', 'logout', 1.25 + '/blog/(.*)/([0-9][0-9][0-9][0-9])/([0-1][0-9])/([0-3][0-9])/(.*)/', 'blog', 1.26 + '/blog/(.*)/([0-9][0-9][0-9][0-9])/([0-1][0-9])/([0-3][0-9])/', 'blogday', 1.27 + '/blog/(.*)/([0-9][0-9][0-9][0-9])/([0-1][0-9])/', 'blogmonth', 1.28 + '/blog/(.*)/', 'bloguser', 1.29 + '/blogpost(.*)', 'blogpost', 1.30 + '/useradd', 'useradd', 1.31 + '/error/(.*)', 'error', 1.32 + '/', 'index' 1.33 +) 1.34 + 1.35 +application = web.application(urls, globals()).wsgifunc() 1.36 +db = web.database(dbn='sqlite', db=DBPATH) 1.37 + 1.38 +class error: 1.39 + def GET(self, code): 1.40 + return render.error(URLROOT, code) 1.41 + 1.42 +class useradd: 1.43 + def POST(self): 1.44 + i = web.input() 1.45 + if i['name'] == '' or i['password'] == '' or i['title'] == '': 1.46 + return web.seeother('/error/invalid-data') 1.47 + users = db.select('user') 1.48 + for user in users: 1.49 + if user['name'] == i.name: 1.50 + return web.seeother('/error/already-exists-username') 1.51 + sqlquery = u"INSERT INTO user (password, name, title) VALUES \ 1.52 + ('%s', '%s', '%s')" % ( i.password, i.name, i.title) 1.53 + n = db.query(sqlquery) 1.54 + web.webapi.setcookie(name='username', value=i.name, expires="") 1.55 + web.seeother('/blog/%s/' % i.name) 1.56 + 1.57 +class login: 1.58 + def POST(self): 1.59 + i = web.input() 1.60 + if i['name'] == '' or i['password'] == '': 1.61 + web.seeother('/error/invalid-data') 1.62 + return 1.63 + users = db.select('user') 1.64 + for user in users: 1.65 + if user['name'] == i.name and user['password'] == i.password: 1.66 + web.webapi.setcookie(name='username', value=i.name, expires='') 1.67 + return render.login(URLROOT, i.name) 1.68 + web.seeother('/error/invalid-login') 1.69 + 1.70 + def GET(self, name): 1.71 + return render.login(URLROOT, name) 1.72 + 1.73 +class logout: 1.74 + def GET(self): 1.75 + username = web.cookies()['username'] 1.76 + web.webapi.setcookie(name='username', value='', expires="") 1.77 + return render.logout(URLROOT, username) 1.78 + 1.79 +class index: 1.80 + def GET(self): 1.81 + users = db.select('user') 1.82 + cookie = web.cookies() 1.83 + if False == cookie.has_key('username'): 1.84 + web.webapi.setcookie(name='username', value='', expires="") 1.85 + cookie = None 1.86 + return render.index(URLROOT, users, cookie) 1.87 + 1.88 +class blogday: 1.89 + def GET(self, user, year, month, day): 1.90 + userinfo = db.select('user', where="user.name = '%s'" % user) 1.91 + userid = userinfo[0]['userid'] 1.92 + wheresql = "page.userid = '%s' AND page.year = '%s' " \ 1.93 + "AND page.month = '%s' AND page.day = '%s'" % ( 1.94 + userid, year, month, day) 1.95 + pages = db.select('page', where=wheresql, 1.96 + limit=5, order="page.created DESC") 1.97 + cookie = web.cookies() 1.98 + return render.bloguser(URLROOT, user, pages, cookie) 1.99 + 1.100 +class blogmonth: 1.101 + def GET(self, user, year, month): 1.102 + userinfo = db.select('user', where="user.name = '%s'" % user) 1.103 + userid = userinfo[0]['userid'] 1.104 + wheresql = "page.userid = '%s' AND page.year = '%s' " \ 1.105 + "AND page.month = '%s'" % (userid, year, month) 1.106 + pages = db.select('page', where=wheresql, 1.107 + limit=5, order="page.created DESC") 1.108 + cookie = web.cookies() 1.109 + return render.bloguser(URLROOT, user, pages, cookie) 1.110 + 1.111 +class bloguser: 1.112 + def GET(self, user): 1.113 + userinfo = db.select('user', where="user.name = '%s'" % user) 1.114 + try: 1.115 + userid = userinfo[0]['userid'] 1.116 + except IndexError: 1.117 + return web.seeother('/error/invalid-data') 1.118 + pages = db.select('page', where="page.userid = '%s'" % userid, 1.119 + limit=5, order="page.created DESC") 1.120 + cookie = web.cookies() 1.121 + return render.bloguser(URLROOT, user, pages, cookie) 1.122 + 1.123 +class blog: 1.124 + def GET(self, user, year, month, day, title): 1.125 + userinfo = db.select(['user', 'page'], 1.126 + where="page.title = '%s' and user.name = '%s'" % ( 1.127 + title, user)) 1.128 + try: 1.129 + userpage = userinfo[0]['pagetext'] 1.130 + except IndexError: 1.131 + return web.seeother('/error/invalid-data') 1.132 + return render.blog(URLROOT, user, year, month, day, title, userpage) 1.133 + 1.134 +class blogpost: 1.135 + def POST(self, user): 1.136 + i = web.input() 1.137 + username = user.split("=")[1] 1.138 + users = db.select('user', where="user.name = '%s'" % username) 1.139 + try: 1.140 + userid = users[0]['userid'] 1.141 + except IndexError: 1.142 + return web.seeother('/error/invalid-data') 1.143 + timestr = time.strftime("%Y-%m-%d %H:%M:%S") 1.144 + year = timestr[0:4] 1.145 + month = timestr[5:7] 1.146 + day = timestr[8:10] 1.147 + sqlquery = u"INSERT INTO page (title, pagetext, created, year, month, \ 1.148 + day, userid) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', %d)" % ( 1.149 + i.title, i.pagetext, timestr, year, month, day, userid) 1.150 + n = db.query(sqlquery) 1.151 + path = "/blog/%s/%s/%s/%s/%s/" % ( 1.152 + username, year, month, day, i.title) 1.153 + web.redirect(path) 1.154 + 1.155 +if __name__ == '__main__': 1.156 + if not os.path.exists(DBPATH): 1.157 + d = sqlite3.connect(DBPATH) 1.158 + c = d.cursor() 1.159 + c.execute("create table user (userid integer primary key, name text, \ 1.160 + password text, title text)") 1.161 + c.execute("create table page (pageid integer primary key, title text, \ 1.162 + pagetext text, created text, year text, month text, \ 1.163 + day text, userid integer)") 1.164 + application = web.application(urls, globals()) 1.165 + application.run() 1.166 +
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/templates/blog.html Sun Mar 01 22:26:57 2009 +0900 2.3 @@ -0,0 +1,10 @@ 2.4 +$def with (urlroot, user, year, month, day, title, userpage) 2.5 +<html><body> 2.6 +$if year and month and day and title: 2.7 + <p>This page is $year/$month/$day/$title/</p> 2.8 + <p>$userpage</p> 2.9 +$else: 2.10 + login ok?? 2.11 +<a href="/$urlroot/blog/$user/">user top</a> 2.12 +<a href="/$urlroot/">top</a> 2.13 +</body></html>
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/templates/bloguser.html Sun Mar 01 22:26:57 2009 +0900 3.3 @@ -0,0 +1,23 @@ 3.4 +$def with (urlroot, user, pages, cookie) 3.5 +<html><body> 3.6 +<a href="/$urlroot/">top</a> 3.7 +$if user: 3.8 + <p>This page is $user's blog</p> 3.9 +$if cookie.username == user: 3.10 + <form method="post" action="/$urlroot/blogpost&user=$user"> 3.11 + <p>new<br/> 3.12 + title : <input type="text" name="title" /><br/> 3.13 + text : <textarea name="pagetext" rows="12" cols="50" /></textarea><br/> 3.14 + <input type="submit" value="新しい記事を投稿" /> 3.15 + </p> 3.16 + </form> 3.17 +$for page in pages: 3.18 + <p style="margin:2em;"> 3.19 + $page.year/<a href="/$urlroot/blog/$user/$page.year/$page.month/"> 3.20 + $page.month</a>/ 3.21 + <a href="/$urlroot/blog/$user/$page.year/$page.month/$page.day/">$page.day</a> 3.22 + <a href="/$urlroot/blog/$user/$page.year/$page.month/$page.day/$page.title/"> 3.23 + $page.title</a><br/> 3.24 + $page.pagetext 3.25 + </p> 3.26 +</body></html>
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/templates/error.html Sun Mar 01 22:26:57 2009 +0900 4.3 @@ -0,0 +1,12 @@ 4.4 +$def with (urlroot, code) 4.5 +<html><body> 4.6 +<p> 4.7 + 4.8 +$if code == 'invalid-login': 4.9 + user $code 4.10 +$else: 4.11 + 500 Internal Server Error 4.12 + 4.13 +</p> 4.14 +<p><a href="/$urlroot/">top page</a></p> 4.15 +</body></html>
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/templates/index.html Sun Mar 01 22:26:57 2009 +0900 5.3 @@ -0,0 +1,56 @@ 5.4 +$def with (urlroot, users, cookie) 5.5 +<html><body> 5.6 +$if cookie == None: 5.7 + <p>ようこそ guest さん</p> 5.8 + <form method="post" action="/$urlroot/useradd"> 5.9 + <p> 5.10 + create new a acount<br/> 5.11 + user : <input type="text" name="name" /> 5.12 + title : <input type="text" name="title" /> 5.13 + password : <input type="password" name="password" /> 5.14 + <input type="submit" value="Create" /> 5.15 + </p> 5.16 + </form> 5.17 + <form method="post" action="/$urlroot/login"> 5.18 + <p>Login<br/> 5.19 + username : <input type="text" name="name" /> 5.20 + password : <input type="text" name="password" /> 5.21 + <input type="submit" value="Login" /> 5.22 + </p> 5.23 + </form> 5.24 +$elif cookie.username == '': 5.25 + <p>ようこそ guest さん</p> 5.26 + <form method="post" action="/$urlroot/useradd"> 5.27 + <p> 5.28 + create new a acount<br/> 5.29 + user : <input type="text" name="name" /> 5.30 + title : <input type="text" name="title" /> 5.31 + password : <input type="password" name="password" /> 5.32 + <input type="submit" value="Create" /> 5.33 + </p> 5.34 + </form> 5.35 + <form method="post" action="/$urlroot/login"> 5.36 + <p>Login<br/> 5.37 + username : <input type="text" name="name" /> 5.38 + password : <input type="text" name="password" /> 5.39 + <input type="submit" value="Login" /> 5.40 + </p> 5.41 + </form> 5.42 +$else: 5.43 + <p>ようこそ $cookie.username さん</p> 5.44 + <p><a href="/$urlroot/logout">logout</a></p> 5.45 + 5.46 +<p> 5.47 +セキュリティとかあまり考慮していないので、ご使用の際は注意してください。 5.48 +</p> 5.49 + 5.50 +<p> 5.51 +<br/><br/> 5.52 +User List<br/> 5.53 +人気のblogランキング、みたいな~ 5.54 +</p> 5.55 +<ul> 5.56 +$for user in users: 5.57 + <li id="t$user.userid">$user.name : <a href="./blog/$user.name/">$user.title</a></li> 5.58 +</ul> 5.59 +</body></html>
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 6.2 +++ b/templates/login.html Sun Mar 01 22:26:57 2009 +0900 6.3 @@ -0,0 +1,10 @@ 6.4 +$def with (urlroot, user) 6.5 +<html><body> 6.6 +<p> 6.7 +$if user == None: 6.8 + username : guest 6.9 +$else: 6.10 + username : $user ; <a href="./logout">logout</a> 6.11 +</p> 6.12 +<p><a href="/$urlroot/blog/$user/">$user</a></p> 6.13 +</body></html>
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 7.2 +++ b/templates/logout.html Sun Mar 01 22:26:57 2009 +0900 7.3 @@ -0,0 +1,5 @@ 7.4 +$def with (urlroot, user) 7.5 +<html><body> 7.6 +<p>$user さん、またおこしください。</p> 7.7 +<p><a href="/$urlroot/">top</a></p> 7.8 +</body></html>