Parent: [a248b1] (diff)

Child: [87bf23] (diff)

Download this file

wiki.py    224 lines (184 with data), 6.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
from time import sleep
from datetime import datetime
import tg
from pylons import g #g is a namespace for globally accessable app helpers
from pylons import c as context
from pymongo.errors import OperationFailure
from ming import schema
from ming.orm.base import state, session
from ming.orm.mapped_class import MappedClass
from ming.orm.property import FieldProperty, ForeignIdProperty, RelationProperty
from pyforge.model import VersionedArtifact, Snapshot, Message, File, Feed, Thread, Post
from pyforge.lib import helpers as h
common_suffix = tg.config.get('forgemail.domain', '.sourceforge.net')
class PageHistory(Snapshot):
class __mongometa__:
name='page_history'
def original(self):
return Page.query.get(_id=self.artifact_id)
def shorthand_id(self):
return '%s#%s' % (self.original().shorthand_id(), self.version)
def url(self):
return self.original().url() + '?version=%d' % self.version
def index(self):
result = Snapshot.index(self)
result.update(
title_s='Version %d of %s' % (
self.version,self.original().title),
type_s='WikiPage Snapshot',
text=self.data.text)
return result
@property
def html_text(self):
"""A markdown processed version of the page text"""
return g.markdown_wiki.convert(self.data.text)
@property
def attachments(self):
return self.original().attachments
@property
def email_address(self):
return self.original().email_address
def root_comments(self):
if '_id' in self:
return Comment.query.find(dict(page_id=self.artifact_id, parent_id=None))
else:
return []
class Page(VersionedArtifact):
class __mongometa__:
name='page'
history_class = PageHistory
title=FieldProperty(str)
text=FieldProperty(schema.String, if_missing='')
viewable_by=FieldProperty([str])
def commit(self):
VersionedArtifact.commit(self)
if self.version > 1:
t1 = self.upsert(self.title, self.version-1).text
t2 = self.text
description = h.diff_text(t1, t2)
else:
description = self.text
Feed.post(self, description)
@property
def email_address(self):
domain = '.'.join(reversed(self.app.url[1:-1].split('/')))
return '%s@%s%s' % (self.title.replace('/', '.'), domain, common_suffix)
def url(self):
return self.app_config.url() + self.title + '/'
def shorthand_id(self):
return self.title
def index(self):
result = VersionedArtifact.index(self)
result.update(
title_s='WikiPage %s' % self.title,
version_i=self.version,
type_s='WikiPage',
text=self.text)
return result
@property
def attachments(self):
return Attachment.by_metadata(page_id=self._id,type='attachment')
@classmethod
def upsert(cls, title, version=None):
"""Update page with `title` or insert new page with that name"""
if version is None:
q = dict(
project_id=context.project._id,
title=title)
#Check for existing page object
obj = cls.query.get(
app_config_id=context.app.config._id,
title=title)
if obj is None:
obj = cls(
title=title,
app_config_id=context.app.config._id,
)
t = Thread(discussion_id=obj.app_config.discussion_id,
artifact_id=obj._id,
subject='Discussion for %s page' % title)
return obj
else:
pg = cls.upsert(title)
HC = cls.__mongometa__.history_class
ss = HC.query.find({'artifact_id':pg._id, 'version':int(version)}).one()
return ss
def reply(self, text):
Feed.post(self, text)
# Get thread
thread = Thread.query.get(artifact_id=self._id)
return Post(
discussion_id=thread.discussion_id,
thread_id=thread._id,
text=text)
# return Comment(page_id=self._id, text=text)
@property
def html_text(self):
"""A markdown processed version of the page text"""
return g.markdown_wiki.convert(self.text)
def root_comments(self):
if '_id' in self:
return Comment.query.find(dict(page_id=self._id, parent_id=None))
else:
return []
class Attachment(File):
class __mongometa__:
name = 'attachment.files'
indexes = [
'metadata.filename',
'metadata.page_id' ]
# Override the metadata schema here
metadata=FieldProperty(dict(
page_id=schema.ObjectId,
app_config_id=schema.ObjectId,
type=str,
filename=str))
@property
def page(self):
return Page.query.get(_id=self.metadata.page_id)
def url(self):
return self.page.url() + 'attachment/' + self.filename
class Comment(Message):
class __mongometa__:
name='comment'
page_id=ForeignIdProperty(Page)
page = RelationProperty(Page)
def index(self):
result = Message.index(self)
author = self.author()
result.update(
title_s='Comment on page %s by %s' % (
self.page.title, author.display_name),
type_s='Comment on WikiPage',
page_title_t=self.page.title)
return result
def reply(self, text):
Feed.post(self.page, text)
r = Message.reply(self)
r.text = text
return r
@property
def page(self):
"""The page this comment connects too"""
return Page.query.get(_id=self.page_id)
@property
def posted_ago(self):
comment_td = (datetime.utcnow() - self.timestamp)
if comment_td.seconds < 3600 and comment_td.days < 1:
return "%s minutes ago" % (comment_td.seconds / 60)
elif comment_td.seconds >= 3600 and comment_td.days < 1:
return "%s hours ago" % (comment_td.seconds / 3600)
elif comment_td.days >= 1 and comment_td.days < 7:
return "%s days ago" % comment_td.days
elif comment_td.days >= 7 and comment_td.days < 30:
return "%s weeks ago" % (comment_td.days / 7)
elif comment_td.days >= 30 and comment_td.days < 365:
return "%s months ago" % (comment_td.days / 30)
else:
return "%s years ago" % (comment_td.days / 365)
def url(self):
"""The URL for the page for this comment"""
return self.page.url() + '#comment-' + self._id
def shorthand_id(self):
return '%s-%s' % (self.page.title, self._id)
MappedClass.compile_all()