Parent: [30c45d] (diff)

Child: [61baed] (diff)

Download this file

blog.py    165 lines (137 with data), 5.3 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
from time import sleep
from datetime import datetime
from random import randint
import tg
from pylons import c, g
from pymongo.errors import OperationFailure, DuplicateKeyError
from ming import schema
from ming.orm import FieldProperty, MappedClass, session, state
from allura import model as M
from allura.lib import helpers as h
from allura.lib import patience
common_suffix = tg.config.get('forgemail.domain', '.sourceforge.net')
class BlogPostSnapshot(M.Snapshot):
class __mongometa__:
name='blog_post_snapshot'
type_s='Blog Post Snapshot'
def original(self):
return BlogPost.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 = super(BlogPostSnapshot, self).index()
result.update(
title_s='Version %d of %s' % (
self.version, self.original().shorthand_id()),
type_s=self.type_s,
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
class BlogPost(M.VersionedArtifact):
class __mongometa__:
name='blog_post'
history_class = BlogPostSnapshot
unique_indexes = [ ('project_id', 'app_config_id', 'slug') ]
type_s = 'Blog Post'
title = FieldProperty(str, if_missing='Untitled')
text = FieldProperty(str, if_missing='')
timestamp = FieldProperty(datetime, if_missing=datetime.utcnow)
slug = FieldProperty(str)
state = FieldProperty(schema.OneOf('draft', 'published'), if_missing='draft')
def _get_date(self):
return self.timestamp.date()
def _set_date(self, value):
self.timestamp = datetime.combine(value, self.time)
date = property(_get_date, _set_date)
def _get_time(self):
return self.timestamp.time()
def _set_time(self, value):
self.timestamp = datetime.combine(self.date, value)
time = property(_get_time, _set_time)
@property
def html_text(self):
return g.markdown.convert(self.text)
@property
def html_text_preview(self):
return g.markdown.convert(h.text.truncate(self.text, 200))
@property
def email_address(self):
domain = '.'.join(reversed(self.app.url[1:-1].split('/'))).replace('_', '-')
return '%s@%s%s' % (self.title.replace('/', '.'), domain, common_suffix)
def make_slug(self):
slugsafe = ''.join(
ch.lower()
for ch in self.title.replace(' ', '-')
if ch.isalnum() or ch == '-')
base = '%s/%s' % (
self.timestamp.strftime('%Y/%m'),
slugsafe)
self.slug = base
while True:
try:
session(self).insert_now(self, state(self))
return self.slug
except DuplicateKeyError:
self.slug = base + '-%.3d' % randint(0,999)
def url(self):
return self.app.url + self.slug + '/'
def shorthand_id(self):
return self.slug
def index(self):
result = super(BlogPost, self).index()
result.update(
title_s=self.slug,
type_s=self.type_s,
snippet_s='%s: %s' % (self.title, h.text.truncate(self.text, 200)),
text=self.text)
return result
def get_version(self, version):
HC = self.__mongometa__.history_class
return HC.query.find({'artifact_id':self._id, 'version':int(version)}).one()
def commit(self):
self.subscribe()
super(BlogPost, self).commit()
if self.version > 1:
v1 = self.get_version(self.version-1)
v2 = self
la = [ line + '\n' for line in v1.text.splitlines() ]
lb = [ line + '\n' for line in v2.text.splitlines() ]
diff = ''.join(patience.unified_diff(
la, lb,
'v%d' % v1.version,
'v%d' % v2.version))
description = diff
if v1.title != v2.title:
subject = '%s renamed page %s to %s' % (
c.user.username, v2.title, v1.title)
else:
subject = '%s modified page %s' % (
c.user.username, self.title)
else:
description = self.text
subject = '%s created page %s' % (
c.user.username, self.title)
M.Feed.post(self, description)
M.Notification.post(
artifact=self, topic='metadata', text=description, subject=subject)
class Attachment(M.BaseAttachment):
metadata=FieldProperty(dict(
artifact_id=schema.ObjectId,
app_config_id=schema.ObjectId,
type=str,
filename=str))
@property
def artifact(self):
return M.BlogPost.query.get(_id=self.metadata.artifact_id)
MappedClass.compile_all()