simple_examples.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. def hello1():
  2. """ simple page without template """
  3. return 'Hello World'
  4. def hello2():
  5. """ simple page without template but with internationalization """
  6. return T('Hello World')
  7. def hello3():
  8. """ page rendered by template simple_examples/index3.html or generic.html"""
  9. return dict(message='Hello World')
  10. def hello4():
  11. """ page rendered by template simple_examples/index3.html or generic.html"""
  12. response.view = 'simple_examples/hello3.html'
  13. return dict(message=T('Hello World'))
  14. def hello5():
  15. """ generates full page in controller """
  16. return HTML(BODY(H1(T('Hello World'), _style='color: red;'))).xml() # .xml to serialize
  17. def hello6():
  18. """ page rendered with a flash"""
  19. response.flash = 'Hello World in a flash!'
  20. return dict(message=T('Hello World'))
  21. def redirectme():
  22. """ redirects to /{{=request.application}}/{{=request.controller}}/hello3 """
  23. redirect(URL('hello3'))
  24. def raisehttp():
  25. """ returns an HTTP 400 ERROR page """
  26. raise HTTP(400, 'internal error')
  27. def servejs():
  28. """ serves a js document """
  29. import gluon.contenttype
  30. response.headers['Content-Type'] = \
  31. gluon.contenttype.contenttype('.js')
  32. return 'alert("This is a Javascript document, it is not supposed to run!");'
  33. def makejson():
  34. return response.json(['foo', {'bar': ('baz', None, 1.0, 2)}])
  35. def makertf():
  36. import gluon.contrib.pyrtf as q
  37. doc = q.Document()
  38. section = q.Section()
  39. doc.Sections.append(section)
  40. section.append('Section Title')
  41. section.append('web2py is great. ' * 100)
  42. response.headers['Content-Type'] = 'text/rtf'
  43. return q.dumps(doc)
  44. def rss_aggregator():
  45. import datetime
  46. import gluon.contrib.rss2 as rss2
  47. import gluon.contrib.feedparser as feedparser
  48. d = feedparser.parse('http://rss.slashdot.org/Slashdot/slashdot/to')
  49. rss = rss2.RSS2(title=d.channel.title, link=d.channel.link,
  50. description=d.channel.description,
  51. lastBuildDate=datetime.datetime.now(),
  52. items=[rss2.RSSItem(title=entry.title,
  53. link=entry.link, description=entry.description,
  54. pubDate=datetime.datetime.now()) for entry in
  55. d.entries])
  56. response.headers['Content-Type'] = 'application/rss+xml'
  57. return rss.to_xml(encoding='utf-8')
  58. def ajaxwiki():
  59. default = """
  60. # section
  61. ## subsection
  62. ### sub subsection
  63. - **bold** text
  64. - ''italic''
  65. - [[link http://google.com]]
  66. ``
  67. def index: return 'hello world'
  68. ``
  69. -----------
  70. Quoted text
  71. -----------
  72. ---------
  73. 0 | 0 | 1
  74. 0 | 2 | 0
  75. 3 | 0 | 0
  76. ---------
  77. """
  78. form = FORM(TEXTAREA(_id='text', _name='text', value=default),
  79. INPUT(_type='button',
  80. _value='markmin',
  81. _onclick="ajax('ajaxwiki_onclick',['text'],'html')"))
  82. return dict(form=form, html=DIV(_id='html'))
  83. def ajaxwiki_onclick():
  84. return MARKMIN(request.vars.text).xml()