Parent: [14bbc5] (diff)

Download this file

artifacts.py    105 lines (84 with data), 3.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
from time import sleep
from datetime import datetime
from pylons import c
from pymongo.errors import OperationFailure
from ming import schema
from ming.orm.property import FieldProperty
from pyforge.model import VersionedArtifact, Snapshot, Message
class MyArtifactHistory(Snapshot):
class __mongometa__:
name='my_artifact_history'
type_s='MyArtifact Snapshot'
def original(self):
return MyArtifact.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().shorthand_id()),
type_s=self.type_s,
text=self.data.text)
return result
class MyArtifact(VersionedArtifact):
class __mongometa__:
name='my_artifact'
history_class = MyArtifactHistory
type_s = 'MyArtifact'
text = FieldProperty(str, if_missing='')
def url(self):
return c.app.script_name + '/' + str(self._id) + '/'
def shorthand_id(self):
return '%s/%s' % (self.type_s, str(self._id))
def index(self):
result = VersionedArtifact.index(self)
result.update(type_s=self.type_s, text=self.text)
return result
def root_comments(self):
return MyArtifactComment.query.find(dict(artifact_id=self._id, parent_id=None))
def reply(self):
while True:
try:
c = MyArtifactComment(artifact_id=self._id)
return c
except OperationFailure:
sleep(0.1)
continue
class MyArtifactComment(Message):
class __mongometa__:
name='my_artifact_comment'
type_s = 'MyArtifact Comment'
artifact_id=FieldProperty(schema.ObjectId)
def index(self):
result = Message.index(self)
author = self.author()
result.update(
title_s='Comment on %s by %s' % (
self.artifact.shorthand_id(), author.display_name),
type_s=self.type_s)
return result
@property
def artifact(self):
return MyArtifact.query.get(_id=self.artifact_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):
return self.artifact.url() + '#comment-' + self._id
def shorthand_id(self):
return '%s-%s' % (self.artifact.shorthand_id, self._id)