Parent: [a65737] (diff)

Child: [88aa0d] (diff)

Download this file

ticket.py    271 lines (221 with data), 8.7 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
from time import sleep
from datetime import datetime
from pylons import c
from pymongo.errors import OperationFailure
import pymongo
from ming import schema
from ming.orm.mapped_class import MappedClass
from ming.orm.property import FieldProperty, ForeignIdProperty, RelationProperty
from datetime import datetime
from pyforge.model import Artifact, VersionedArtifact, Snapshot, Message, project_orm_session, Project
from pyforge.model import File, User, Feed
from pyforge.lib import helpers as h
class Globals(MappedClass):
class __mongometa__:
name = 'globals'
session = project_orm_session
type_s = 'Globals'
_id = FieldProperty(schema.ObjectId)
app_config_id = ForeignIdProperty('AppConfig', if_missing=lambda:c.app.config._id)
last_ticket_num = FieldProperty(int)
status_names = FieldProperty(str)
custom_fields = FieldProperty([{str:None}])
class TicketHistory(Snapshot):
class __mongometa__:
name = 'ticket_history'
def original(self):
return Ticket.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 assigned_to(self):
if self.data.assigned_to_id is None: return None
return User.query.get(_id=self.data.assigned_to_id)
def index(self):
result = Snapshot.index(self)
result.update(
title_s='Version %d of %s' % (
self.version,self.original().summary),
type_s='Ticket Snapshot',
text=self.data.summary)
return result
class Bin(Artifact):
class __mongometa__:
name = 'bin'
type_s = 'Bin'
_id = FieldProperty(schema.ObjectId)
summary = FieldProperty(str)
terms = FieldProperty(str, if_missing='')
def url(self):
return self.app_config.url() + 'search?q=' + str(self.terms)
def shorthand_id(self):
return 'bin(' + str(self.summary) + ')'
def index(self):
result = Artifact.index(self)
result.update(
type_s=self.type_s,
summary_t=self.summary,
terms_s=self.terms)
return result
class Ticket(VersionedArtifact):
class __mongometa__:
name = 'ticket'
history_class = TicketHistory
type_s = 'Ticket'
_id = FieldProperty(schema.ObjectId)
created_date = FieldProperty(datetime, if_missing=datetime.utcnow)
parent_id = FieldProperty(schema.ObjectId, if_missing=None)
ticket_num = FieldProperty(int)
summary = FieldProperty(str)
description = FieldProperty(str, if_missing='')
reported_by_id = FieldProperty(schema.ObjectId, if_missing=lambda:c.user._id)
assigned_to_id = FieldProperty(schema.ObjectId, if_missing=None)
milestone = FieldProperty(str, if_missing='')
status = FieldProperty(str, if_missing='')
custom_fields = FieldProperty({str:None})
comments = RelationProperty('Comment')
def commit(self):
VersionedArtifact.commit(self)
if self.version > 1:
hist = TicketHistory.query.get(artifact_id=self._id, version=self.version-1)
old = hist.data
changes = []
fields = [
('Summary', old.summary, self.summary),
('Status', old.status, self.status) ]
for key in self.custom_fields:
fields.append((key, old.custom_fields.get(key, ''), self.custom_fields[key]))
for title, o, n in fields:
if o != n:
changes.append('%s updated: %r => %r' % (
title, o, n))
o = hist.assigned_to()
n = self.assigned_to()
if o != n:
changes.append('Owner updated: %r => %r' % (
o and o.username, n and n.username))
if old.description != self.description:
changes.append('Description updated:')
changes.append(h.diff_text(old.description, self.description))
description = '<br>'.join(changes)
else:
description = 'Ticket %s created: %s' % (self.ticket_num, self.summary)
Feed.post(self, description)
def url(self):
return self.app_config.url() + str(self.ticket_num) + '/'
def shorthand_id(self):
return '#' + str(self.ticket_num)
def index(self):
result = VersionedArtifact.index(self)
result.update(
title_s='Ticket %s' % self.ticket_num,
version_i=self.version,
type_s=self.type_s,
ticket_num_i=self.ticket_num,
summary_t=self.summary,
milestone_s=self.milestone,
status_s=self.status,
text=self.description)
for k,v in self.custom_fields.iteritems():
result[k + '_s'] = str(v)
return result
def reported_by(self):
return User.query.get(_id=self.reported_by_id) or User.anonymous
def assigned_to(self):
if self.assigned_to_id is None: return None
return User.query.get(_id=self.assigned_to_id)
def assigned_to_name(self):
who = self.assigned_to()
if who is None: return 'nobody'
return who.display_name
@property
def attachments(self):
return Attachment.by_metadata(ticket_id=self._id)
def root_comments(self):
if '_id' in self:
return Comment.query.find(dict(ticket_id=self._id, reply_to=None))
else:
return []
def all_comments(self):
if '_id' in self:
return Comment.query.find(dict(ticket_id=self._id))
else:
return []
def ordered_comments(self, limit=None):
if '_id' in self:
if limit:
q = Comment.query.find(dict(ticket_id=self._id),limit=limit)
else:
q = Comment.query.find(dict(ticket_id=self._id))
q = q.sort([('created_date', pymongo.DESCENDING)])
return q
else:
return []
def reply(self, text):
Feed.post(self, 'Comment: %s' % text)
c = Comment(ticket_id=self._id, text=text)
return c
class Comment(Message):
class __mongometa__:
name = 'ticket_comment'
type_s = 'Ticket Comment'
version = FieldProperty(0)
created_date = FieldProperty(datetime, if_missing=datetime.utcnow)
ticket_id = ForeignIdProperty(Ticket)
kind = FieldProperty(str, if_missing='comment')
reply_to_id = FieldProperty(schema.ObjectId, if_missing=None)
text = FieldProperty(str)
ticket = RelationProperty('Ticket')
def index(self):
result = Message.index(self)
author = self.author()
result.update(
title_s='Comment on %s by %s' % (
self.ticket.shorthand_id(),
author.display_name
),
type_s=self.type_s
)
return result
@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.ticket.url() + '#comment-' + str(self._id)
def shorthand_id(self):
return '%s-%s' % (self.ticket.shorthand_id, self._id)
def reply(self, text):
r = Message.reply(self)
r.text = text
Feed.post(self.ticket, 'Comment: %s', text)
return r
class Attachment(File):
class __mongometa__:
name = 'attachment.files'
indexes = [
'metadata.filename',
'metadata.ticket_id' ]
# Override the metadata schema here
metadata=FieldProperty(dict(
ticket_id=schema.ObjectId,
app_config_id=schema.ObjectId,
filename=str))
@property
def ticket(self):
return Ticket.query.get(_id=self.metadata.ticket_id)
def url(self):
return self.ticket.url() + 'attachment/' + self.filename
MappedClass.compile_all()