Parent: [79f02d] (diff)

Child: [c75016] (diff)

Download this file

main.py    141 lines (110 with data), 3.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import difflib
from pprint import pformat
from pylons import c, request
from tg import expose, redirect
from pyforge.app import Application
from pyforge.lib.dispatch import _dispatch
from helloforge import model as M
from helloforge import version
class HelloForgeApp(Application):
'''This is the HelloWorld application for PyForge, showing
all the rich, creamy goodness that is installable apps.
'''
__version__ = version.__version__
default_config = dict(project_name='NoProject',
message='Custom message goes here')
def __init__(self, config):
self.config = config
self.root = RootController()
def install(self, project):
self.config.config['project_name'] = project._id
self.uninstall(project)
p = M.Page.upsert('Root')
p.text = 'This is the root page.'
p.m.save()
def uninstall(self, project):
M.Page.m.remove(dict(project_id=c.project._id))
M.Comment.m.remove(dict(project_id=c.project._id))
class RootController(object):
@expose('helloforge.templates.index')
def index(self):
return dict(message=c.app.config.config['message'])
def _dispatch(self, state, remainder):
return _dispatch(self, state, remainder)
def _lookup(self, pname, *remainder):
return PageController(pname), remainder
class PageController(object):
def __init__(self, title):
self.title = title
self.comments = CommentController(self.title)
def page(self, version=None):
if version is None:
return M.Page.upsert(self.title)
else:
return M.Page.upsert(self.title, version=int(version))
@expose('helloforge.templates.page_view')
def index(self, version=None):
return dict(page=self.page(version))
@expose('helloforge.templates.page_edit')
def edit(self):
return dict(page=self.page())
@expose('helloforge.templates.page_history')
def history(self):
pages = M.Page.history(self.title)
return dict(title=self.title, pages=pages)
@expose('helloforge.templates.page_diff')
def diff(self, v1, v2):
p1 = self.page(int(v1))
p2 = self.page(int(v2))
diff=difflib.unified_diff(
p1.text.split('\n'),
p2.text.split('\n'))
return dict(p1=p1, p2=p2, diff=diff)
@expose(content_type='text/plain')
def raw(self):
return pformat(self.page())
@expose()
def revert(self, version):
orig = self.page(version)
current = self.page()
current.text = orig.text
current.m.save()
redirect('.')
@expose()
def update(self, text):
page = self.page()
page.text = text
page.m.save()
redirect('.')
class CommentController(object):
def __init__(self, page_title, comment_id=None):
self.page_title = page_title
self.comment_id = comment_id
def page(self):
return M.Page.upsert(self.page_title)
def comment(self):
return M.Comment.m.get(_id=self.comment_id)
@expose()
def reply(self, text):
if self.comment_id:
c = self.comment().reply()
c.text = text
else:
c = self.page().reply()
c.text = text
c.m.save()
redirect(request.referer)
@expose()
def delete(self):
self.comment().m.delete()
redirect(request.referer)
def _dispatch(self, state, remainder):
return _dispatch(self, state, remainder)
def _lookup(self, next, *remainder):
if self.comment_id:
return CommentController(
self.page_title,
self.comment_id + '/' + next), remainder
else:
return CommentController(
self.page_title, next), remainder