junoblog.py

Wed Jul 22 02:46:23 2009 +0900

author
hattori@www.hexacosa.net
date
Wed Jul 22 02:46:23 2009 +0900
changeset 3
0ff9f24a8369
parent 2
296b919d523f
permissions
-rw-r--r--

fixed db column type

hattori@2 1 # -*- coding:utf-8 -*-
hattori@0 2 from juno import get, post, find, run, template, init, model, redirect
hattori@0 3 import time
hattori@0 4
hattori@2 5 __version__ = '0.0.2'
hattori@0 6 __author__ = 'Hideo Hattori'
hattori@0 7
hattori@0 8 CODEC = 'utf-8'
hattori@0 9
hattori@0 10 init({'dev_port': 8080,
hattori@0 11 'template_lib': 'jinja2',
hattori@0 12 'db_location': 'juno-blog.db',
hattori@2 13 'use_sessions': True,
hattori@0 14 'use_debugger': True})
hattori@0 15
hattori@0 16 User = model('User', name = 'string',
hattori@3 17 title = 'unicode',
hattori@0 18 password = 'string')
hattori@0 19
hattori@0 20 Blog = model('Blog', name = 'string',
hattori@3 21 title = 'unicode',
hattori@0 22 date = 'string',
hattori@3 23 text = 'unicodetext')
hattori@0 24
hattori@0 25 @get('/')
hattori@0 26 def home(web):
hattori@0 27 users = find(User).all()
hattori@2 28 template('index.html', {'users': [ u for u in users ],
hattori@2 29 'login': web.session.has_key('name')})
hattori@0 30
hattori@0 31 @post('/login/')
hattori@0 32 def login(web):
hattori@0 33 name = web.input('name')
hattori@0 34 password = web.input('password')
hattori@2 35 exist = User.find().filter(User.name==name).all()
hattori@2 36 if 1 == len(exist) and exist[0].password == password:
hattori@2 37 web.session['name'] = name
hattori@2 38 web.session.save()
hattori@2 39 template('login.html', {'user': name, 'exist': len(exist)})
hattori@0 40
hattori@0 41 @post('/useradd/')
hattori@0 42 def useradd(web):
hattori@0 43 name = web.input('name')
hattori@0 44 password = web.input('password')
hattori@0 45 title = web.input('title').decode(CODEC)
hattori@0 46 exist = User.find().filter(User.name==name)
hattori@0 47 if exist.count():
hattori@0 48 template('index.html', {})
hattori@0 49 return
hattori@0 50 user = User(name=name, title=title, password=password)
hattori@0 51 user.add().save()
hattori@2 52 web.session['name'] = name
hattori@2 53 web.session.save()
hattori@0 54 redirect('/blog/%s' % name)
hattori@0 55
hattori@0 56 @get('/logout/')
hattori@2 57 def logout(web):
hattori@2 58 if web.session.has_key('name') == True:
hattori@2 59 name = web.session['name']
hattori@2 60 web.session.delete()
hattori@2 61 else:
hattori@2 62 redirect('/')
hattori@2 63 return
hattori@2 64 template('logout.html', {'user': {'name': name}})
hattori@0 65
hattori@0 66 @get('/blog/:name/')
hattori@0 67 def blog(web, name):
hattori@0 68 user = User.find().filter(User.name==name).all()[0]
hattori@0 69 blogs = Blog.find().filter(Blog.name==name).all()
hattori@0 70 blogs = [ b for b in blogs if b.name == name ]
hattori@2 71 login = False
hattori@2 72 if web.session.has_key('name') == True:
hattori@2 73 if web.session['name'] == name:
hattori@2 74 login = True
hattori@2 75 template('blog.html', {'user': user, 'blogs': blogs, 'login': login})
hattori@0 76
hattori@0 77 @post('/blogpost/:name/')
hattori@0 78 def postblog(web, name):
hattori@0 79 title = web.input('title').decode(CODEC)
hattori@0 80 text = web.input('pagetext').decode(CODEC)
hattori@0 81 date = time.ctime()
hattori@0 82 b = Blog(name=name, title=title, text=text, date=date)
hattori@0 83 b.add().save()
hattori@0 84 blogs = find(Blog).all()
hattori@0 85 blogs = [ b for b in blogs if b.name == name ]
hattori@0 86 template('blog.html', {'user': {'name': name, 'title': title},
hattori@0 87 'blogs': blogs})
hattori@0 88
hattori@0 89 if __name__ == '__main__':
hattori@0 90 run()
hattori@0 91

mercurial