Parent: [35852b] (diff)

Child: [3390fb] (diff)

Download this file

wiki.py    86 lines (72 with data), 2.4 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
from datetime import datetime
from time import sleep
from pylons import c
from docutils.core import publish_parts
import re
import pymongo
from pymongo.errors import OperationFailure
from ming import schema as S
from ming import Field
from pyforge.model import Artifact, Message
wikiwords = [
(r'\b([A-Z]\w+[A-Z]+\w+)', r'<a href="../\1/">\1</a>'),
(r'([^\\])\[(.*)\]', r'\1<a href="../\2/">\2</a>'),
(r'\\\[(.*)\]', r'[\1]'),
(r'^\[(.*)\]', r'<a href="../\1/">\1</a>'),
]
wikiwords = [
(re.compile(pattern), replacement)
for pattern, replacement in wikiwords ]
class Page(Artifact):
class __mongometa__:
name='page'
title=Field(str)
version=Field(int, if_missing=0)
author_id=Field(S.ObjectId, if_missing=lambda:c.user._id)
timestamp=Field(S.DateTime, if_missing=datetime.utcnow)
text=Field(S.String, if_missing='')
@classmethod
def upsert(cls, title, version=None):
q = dict(
project_id=c.project._id,
title=title)
if version is not None:
q['version'] = version
versions = cls.m.find(q)
if not versions.count():
return cls.make(dict(
project_id=c.project._id,
title=title,
text='',
version=0))
latest = max(versions, key=lambda v:v.version)
new_obj=dict(latest, version=latest.version + 1)
del new_obj['_id']
return cls.make(new_obj)
@classmethod
def history(cls, title):
history = cls.m.find(
dict(project_id=c.project._id, title=title))
history = history.sort('version', pymongo.DESCENDING)
return history
@property
def html_text(self):
content = publish_parts(self.text, writer_name='html')['html_body']
for pattern, replacement in wikiwords:
content = pattern.sub(replacement, content)
return content
def reply(self):
while True:
try:
c = Comment.make(dict(page_title=self.title))
c.m.insert()
return c
except OperationFailure:
sleep(0.1)
continue
def root_comments(self):
return Comment.m.find(dict(page_title=self.title, parent_id=None))
class Comment(Message):
class __mongometa__:
name='comment'
page_title=Field(str)