/blog/blog.py
Python | 88 lines | 65 code | 11 blank | 12 comment | 4 complexity | 7dc66706f21254bb0765b449bc7c9c04 MD5 | raw file
Possible License(s): GPL-2.0
- import config
- from tbone.user import get_user
- from tbone.appbase import orm
- import sqlalchemy as s
- class Blog(orm.Base):
- __tablename__ = 'blog'
- id = s.Column(s.Integer, primary_key=True)
- #html = Column(UnicodeText)
- title = s.Column(s.String(50))
- slug = s.Column(s.String(50),unique=True)
- html = s.Column(s.UnicodeText)
- def __init__(self, title, html, slug = None):
- import tornado.escape
- self.html = html
- self.title = title
- if slug is None:
- slug = tornado.escape.url_escape(title)
- self.slug = slug
- def __repr__(self):
- return self.html
- import tornado.web
- class BlogIndex(tornado.web.RequestHandler):
- def get(self):
- #self.write('hih')
- blogs = [b for b in orm.Session().query(Blog).all()]#['1','2']
- user = get_user(self,orm.Session())
- name = 'Anonymous' if user is None else user.name
- self.render("blogindex.html",blogs = blogs,
- name=name,path=self.request.path)
- def post(self):
- self.set_header("Content-Type", "text/plain")
- self.write("You wrote " + self.get_argument("title") + self.get_argument("message"))
- # page = ?
- class BlogHandler(tornado.web.RequestHandler):
- def get(self,entry):
- import tornado.escape
- #slug = tornado.escape.url_unescape(entry)# .decode('utf-8')
- slug = tornado.escape.url_escape(entry)
- post = orm.Session().query(Blog).filter_by(slug=slug).first()
- self.write('''<html><body>%s
- <form action="%s" method="post">
- <input type="text" name="message">
- <input type="submit" value="Submit">
- </form></html></body>'''%(post,self.request.path))
- def post(self,entry):
- self.set_header("Content-Type", "text/plain")
- self.write("You wrote " + self.get_argument("message"))
- urls = [
- (r"/",BlogIndex),
- (r"/(.+)", BlogHandler),
- ]
- def make_fixtures():
- # Warning, only call this once, or it will crash (due to duplicate entries)
- # Note, text.get_app() is called every unit test, so DON'T do this:
- # class TestBlog(AsyncHTTPTestCase):
- # def get_app(self):
- # blog.make_fixtures() # BAD!
- # return Application(blog.urls)
- nihao = u'\u4f60\u597d' # Chinese for "hello" to test unicode.
- # u.encode('ascii', 'xmlcharrefreplace')?
- orm.Base.metadata.create_all(orm.engine)
- session = orm.Session()
- first_post = Blog('First','I IZ IN UR DATABASE, DROPPNI UR TBALEZ!'+1000*' '+'KTHXBYE')
- session.add(first_post)
- second_post = Blog('Second','I IZ STILL IN UR DATABASE')
- session.add(second_post)
- uni = Blog(nihao,'unicode '*10+nihao*10)
- session.add(uni)
- session.commit()
- post = session.query(Blog).first()
- # how is unicode title handled?
- # are capitals unique?
- # blank title?