PageRenderTime 47ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/blog/blog.py

https://github.com/peter-the-tea-drinker/tornado-base-example
Python | 88 lines | 65 code | 11 blank | 12 comment | 4 complexity | 7dc66706f21254bb0765b449bc7c9c04 MD5 | raw file
Possible License(s): GPL-2.0
  1. import config
  2. from tbone.user import get_user
  3. from tbone.appbase import orm
  4. import sqlalchemy as s
  5. class Blog(orm.Base):
  6. __tablename__ = 'blog'
  7. id = s.Column(s.Integer, primary_key=True)
  8. #html = Column(UnicodeText)
  9. title = s.Column(s.String(50))
  10. slug = s.Column(s.String(50),unique=True)
  11. html = s.Column(s.UnicodeText)
  12. def __init__(self, title, html, slug = None):
  13. import tornado.escape
  14. self.html = html
  15. self.title = title
  16. if slug is None:
  17. slug = tornado.escape.url_escape(title)
  18. self.slug = slug
  19. def __repr__(self):
  20. return self.html
  21. import tornado.web
  22. class BlogIndex(tornado.web.RequestHandler):
  23. def get(self):
  24. #self.write('hih')
  25. blogs = [b for b in orm.Session().query(Blog).all()]#['1','2']
  26. user = get_user(self,orm.Session())
  27. name = 'Anonymous' if user is None else user.name
  28. self.render("blogindex.html",blogs = blogs,
  29. name=name,path=self.request.path)
  30. def post(self):
  31. self.set_header("Content-Type", "text/plain")
  32. self.write("You wrote " + self.get_argument("title") + self.get_argument("message"))
  33. # page = ?
  34. class BlogHandler(tornado.web.RequestHandler):
  35. def get(self,entry):
  36. import tornado.escape
  37. #slug = tornado.escape.url_unescape(entry)# .decode('utf-8')
  38. slug = tornado.escape.url_escape(entry)
  39. post = orm.Session().query(Blog).filter_by(slug=slug).first()
  40. self.write('''<html><body>%s
  41. <form action="%s" method="post">
  42. <input type="text" name="message">
  43. <input type="submit" value="Submit">
  44. </form></html></body>'''%(post,self.request.path))
  45. def post(self,entry):
  46. self.set_header("Content-Type", "text/plain")
  47. self.write("You wrote " + self.get_argument("message"))
  48. urls = [
  49. (r"/",BlogIndex),
  50. (r"/(.+)", BlogHandler),
  51. ]
  52. def make_fixtures():
  53. # Warning, only call this once, or it will crash (due to duplicate entries)
  54. # Note, text.get_app() is called every unit test, so DON'T do this:
  55. # class TestBlog(AsyncHTTPTestCase):
  56. # def get_app(self):
  57. # blog.make_fixtures() # BAD!
  58. # return Application(blog.urls)
  59. nihao = u'\u4f60\u597d' # Chinese for "hello" to test unicode.
  60. # u.encode('ascii', 'xmlcharrefreplace')?
  61. orm.Base.metadata.create_all(orm.engine)
  62. session = orm.Session()
  63. first_post = Blog('First','I IZ IN UR DATABASE, DROPPNI UR TBALEZ!'+1000*' '+'KTHXBYE')
  64. session.add(first_post)
  65. second_post = Blog('Second','I IZ STILL IN UR DATABASE')
  66. session.add(second_post)
  67. uni = Blog(nihao,'unicode '*10+nihao*10)
  68. session.add(uni)
  69. session.commit()
  70. post = session.query(Blog).first()
  71. # how is unicode title handled?
  72. # are capitals unique?
  73. # blank title?