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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
|
# Copyright 2009-2011 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
__metaclass__ = type
__all__ = [
'CodeReviewCommentAddView',
'CodeReviewCommentContextMenu',
'CodeReviewCommentPrimaryContext',
'CodeReviewCommentSummary',
'CodeReviewCommentView',
'CodeReviewDisplayComment',
]
from lazr.delegates import delegates
from lazr.restful.interface import copy_field
from zope.app.form.browser import (
DropdownWidget,
TextAreaWidget,
)
from zope.interface import (
implements,
Interface,
)
from zope.schema import Text
from lp import _
from lp.app.browser.launchpadform import (
action,
custom_widget,
LaunchpadFormView,
)
from lp.code.interfaces.codereviewcomment import ICodeReviewComment
from lp.code.interfaces.codereviewvote import ICodeReviewVoteReference
from lp.services.comments.interfaces.conversation import IComment
from lp.services.config import config
from lp.services.librarian.interfaces import ILibraryFileAlias
from lp.services.propertycache import (
cachedproperty,
get_property_cache,
)
from lp.services.webapp import (
canonical_url,
ContextMenu,
LaunchpadView,
Link,
)
from lp.services.webapp.interfaces import IPrimaryContext
class ICodeReviewDisplayComment(IComment, ICodeReviewComment):
"""Marker interface for displaying code review comments."""
class CodeReviewDisplayComment:
"""A code review comment or activity or both.
The CodeReviewComment itself does not implement the IComment interface as
this is purely a display interface, and doesn't make sense to have display
only code in the model itself.
"""
implements(ICodeReviewDisplayComment)
delegates(ICodeReviewComment, 'comment')
def __init__(self, comment, from_superseded=False):
self.comment = comment
get_property_cache(self).has_body = bool(self.comment.message_body)
self.has_footer = self.comment.vote is not None
# The date attribute is used to sort the comments in the conversation.
self.date = self.comment.message.datecreated
self.from_superseded = from_superseded
@property
def extra_css_class(self):
if self.from_superseded:
return 'from-superseded'
else:
return ''
@cachedproperty
def comment_author(self):
"""The author of the comment."""
return self.comment.message.owner
@cachedproperty
def has_body(self):
"""Is there body text?"""
return bool(self.body_text)
@cachedproperty
def body_text(self):
"""Get the body text for the message."""
return self.comment.message_body
@cachedproperty
def comment_date(self):
"""The date of the comment."""
return self.comment.message.datecreated
@cachedproperty
def all_attachments(self):
return self.comment.getAttachments()
@cachedproperty
def display_attachments(self):
# Attachments to show.
return [DiffAttachment(alias) for alias in self.all_attachments[0]]
@cachedproperty
def other_attachments(self):
# Attachments to not show.
return self.all_attachments[1]
class CodeReviewCommentPrimaryContext:
"""The primary context is the comment is that of the source branch."""
implements(IPrimaryContext)
def __init__(self, comment):
self.context = IPrimaryContext(
comment.branch_merge_proposal).context
class CodeReviewCommentContextMenu(ContextMenu):
"""Context menu for branches."""
usedfor = ICodeReviewComment
links = ['reply']
def reply(self):
enabled = self.context.branch_merge_proposal.isMergable()
return Link('+reply', 'Reply', icon='add', enabled=enabled)
class DiffAttachment:
"""An attachment that we are going to display."""
implements(ILibraryFileAlias)
delegates(ILibraryFileAlias, 'alias')
def __init__(self, alias):
self.alias = alias
@cachedproperty
def text(self):
"""Read the text out of the librarin."""
self.alias.open()
try:
return self.alias.read(config.diff.max_read_size)
finally:
self.alias.close()
@cachedproperty
def diff_text(self):
"""Get the text and attempt to decode it."""
try:
diff = self.text.decode('utf-8')
except UnicodeDecodeError:
diff = self.text.decode('windows-1252', 'replace')
# Strip off the trailing carriage returns.
return diff.rstrip('\n')
class CodeReviewCommentView(LaunchpadView):
"""Standard view of a CodeReviewComment"""
page_title = "Code review comment"
@cachedproperty
def comment(self):
"""The decorated code review comment."""
return CodeReviewDisplayComment(self.context)
@property
def page_description(self):
return self.context.message_body
# Should the comment be shown in full?
full_comment = True
# Show comment expanders?
show_expanders = False
class CodeReviewCommentSummary(CodeReviewCommentView):
"""Summary view of a CodeReviewComment"""
# How many lines do we show in the main view?
SHORT_MESSAGE_LENGTH = 3
# Show comment expanders?
show_expanders = True
# Should the comment be shown in full?
@property
def full_comment(self):
"""Show the full comment if it is short."""
return not self.is_long_message
@cachedproperty
def _comment_lines(self):
return self.context.message.text_contents.splitlines()
@property
def is_long_message(self):
return len(self._comment_lines) > self.SHORT_MESSAGE_LENGTH
@property
def message_summary(self):
"""Return an elided message with the first X lines of the comment."""
short_message = (
'\n'.join(self._comment_lines[:self.SHORT_MESSAGE_LENGTH]))
short_message += "..."
return short_message
class IEditCodeReviewComment(Interface):
"""Interface for use as a schema for CodeReviewComment forms."""
vote = copy_field(ICodeReviewComment['vote'], required=False)
review_type = copy_field(
ICodeReviewVoteReference['review_type'],
description=u'Lowercase keywords describing the type of review you '
'are performing.')
comment = Text(title=_('Comment'), required=False)
class CodeReviewCommentAddView(LaunchpadFormView):
"""View for adding a CodeReviewComment."""
class MyDropWidget(DropdownWidget):
"Override the default no-value display name to -Select-."
_messageNoValue = 'Comment only'
schema = IEditCodeReviewComment
custom_widget('comment', TextAreaWidget, cssClass='codereviewcomment')
custom_widget('vote', MyDropWidget)
page_title = 'Reply to code review comment'
@property
def initial_values(self):
"""The initial values are used to populate the form fields.
In this case, the default value of the comment should be the
quoted comment being replied to.
"""
if self.is_reply:
comment = self.reply_to.as_quoted_email
else:
comment = ''
return {'comment': comment}
@property
def is_reply(self):
"""True if this comment is a reply to another comment, else False."""
return ICodeReviewComment.providedBy(self.context)
@property
def branch_merge_proposal(self):
"""The BranchMergeProposal being commented on."""
if self.is_reply:
return self.context.branch_merge_proposal
else:
return self.context
@cachedproperty
def reply_to(self):
"""The comment being replied to, or None."""
if self.is_reply:
return CodeReviewDisplayComment(self.context)
else:
return None
@action('Save Comment', name='add')
def add_action(self, action, data):
"""Create the comment..."""
vote = data.get('vote')
review_type = data.get('review_type')
self.branch_merge_proposal.createComment(
self.user, subject=None, content=data['comment'],
parent=self.reply_to, vote=vote, review_type=review_type)
@property
def next_url(self):
"""Always take the user back to the merge proposal itself."""
return canonical_url(self.branch_merge_proposal)
cancel_url = next_url
|