Skip to content

Instantly share code, notes, and snippets.

@amcgregor
Created December 3, 2009 07:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amcgregor/247954 to your computer and use it in GitHub Desktop.
Save amcgregor/247954 to your computer and use it in GitHub Desktop.
Compact MongoDB Wiki example.
# encoding: utf8
"""Web application root controller."""
import textile
import pymongo
import web.core
pages = pymongo.Connection().wiki['pages']
class Page(web.core.RESTMethod):
def __init__(self, name):
self.name = name
self.record = pages.find_one({'name': name})
super(Page, self).__init__()
def get(self, raw=False):
if raw:
web.core.response.content_type = 'text/plain'
return self.record['content'] if self.record else ""
return './wiki.html', dict(
name = self.name,
record = self.record,
content = textile.textile(self.record['content']) if self.record else "<i>No content.</i>"
)
def post(self, content, button=None, raw=False):
if not self.record:
self.record = dict(name=self.name)
self.record['content'] = content
pages.save(self.record, safe=True)
return self.get(raw)
class RootController(web.core.Controller):
def index(self):
raise web.core.http.HTTPSeeOther(location='/home')
def __lookup__(self, name, *args, **kw):
return Page(name), args
if __name__ == '__main__':
from paste import httpserver
app = web.core.Application.factory(root=RootController, debug=True, **{
'web.templating.engine': 'mako'
})
httpserver.serve(app, host='127.0.0.1', port='8080')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment