Created
December 3, 2009 07:16
-
-
Save amcgregor/247954 to your computer and use it in GitHub Desktop.
Compact MongoDB Wiki example.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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