/
grids.py
646 lines (514 loc) · 22.7 KB
/
grids.py
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
import pytz
from django.contrib.auth.models import User
from django.http import Http404
from django.utils.translation import gettext_lazy as _
from djblets.datagrid.grids import (
Column,
DateTimeColumn,
DataGrid as DjbletsDataGrid,
AlphanumericDataGrid as DjbletsAlphanumericDataGrid)
from djblets.util.templatetags.djblets_utils import ageid
from reviewboard.accounts.models import ReviewRequestVisit
from reviewboard.datagrids.columns import (BugsColumn,
DateTimeSinceColumn,
DiffSizeColumn,
DiffUpdatedColumn,
DiffUpdatedSinceColumn,
FullNameColumn,
GroupMemberCountColumn,
GroupsColumn,
MyCommentsColumn,
NewUpdatesColumn,
PendingCountColumn,
PeopleColumn,
RepositoryColumn,
ReviewCountColumn,
ReviewGroupStarColumn,
ReviewRequestCheckboxColumn,
ReviewRequestIDColumn,
ReviewRequestStarColumn,
ReviewSummaryColumn,
ShipItColumn,
SummaryColumn,
ToMeColumn,
UsernameColumn)
from reviewboard.datagrids.sidebar import Sidebar, DataGridSidebarMixin
from reviewboard.datagrids.builtin_items import (IncomingSection,
OutgoingSection,
OverviewSection,
UserGroupsItem,
UserProfileItem)
from reviewboard.reviews.models import Group, ReviewRequest, Review
from reviewboard.site.urlresolvers import local_site_reverse
class ShowClosedReviewRequestsMixin(object):
"""A mixin for showing or hiding closed review requests."""
def load_extra_state(self, profile, allow_hide_closed=True):
"""Load extra state for the datagrid."""
if profile:
self.show_closed = profile.show_closed
try:
self.show_closed = (
int(self.request.GET.get('show-closed',
self.show_closed)) != 0)
except ValueError:
# do nothing
pass
if allow_hide_closed and not self.show_closed:
self.queryset = self.queryset.filter(**{
self.status_query_field: 'P',
})
self.queryset = self.queryset.filter(**{
self.site_query_field: self.local_site,
})
if profile and self.show_closed != profile.show_closed:
profile.show_closed = self.show_closed
return ['show_closed']
return []
class DataGridJSMixin(object):
"""Mixin that provides enhanced JavaScript support for datagrids.
This contains additional information on the JavaScript views/models
to load for the page, allowing for enhanced functionality in datagrids.
"""
#: A list of extra CSS static bundles to load on the page.
css_bundle_names = []
#: A list of extra JavaScript static bundles to load on the page.
js_bundle_names = []
#: The JavaScript Model to use for the page state.
js_model_class = 'RB.DatagridPage'
#: The JavaScript View to use for the page rendering.
js_view_class = 'RB.DatagridPageView'
#: Whether or not to periodically reload the contents of the datagrid.
periodic_reload = False
#: Extra data to pass to the JavaScript Model.
extra_js_model_data = None
def __init__(self, *args, **kwargs):
"""Initialize the mixin.
This will pull out the Local Site, which is common to all datagrids,
and store it for later use and for JavaScript attribute population.
Args:
*args (tuple):
Positional arguments passed to the datagrid.
**kwargs (dict):
Keyword arguments passed to the datagrid.
"""
self.local_site = kwargs.pop('local_site', None)
super(DataGridJSMixin, self).__init__(*args, **kwargs)
def get_js_model_attrs(self):
"""Return attributes for the JavaScript model.
These will be passed to the model specified in
:py:attr:`js_model_class` during construction.
Subclasses can override this to provide additional data.
Returns:
dict:
Attributes to provide to the JavaScript model.
"""
attrs = {}
if self.extra_js_model_data:
attrs['data'] = self.extra_js_model_data
if self.local_site:
attrs['localSiteName'] = self.local_site.name
return attrs
def get_js_model_options(self):
"""Return options for the JavaScript model.
These will be passed to the model specified in
:py:attr:`js_model_class` during construction.
Subclasses can override this to provide additional data.
Returns:
dict:
Options to provide to the JavaScript model.
"""
return {}
def get_js_view_options(self):
"""Return options for the JavaScript view.
These will be passed to the view specified in
:py:attr:`js_view_class` during construction.
Subclasses can override this to provide additional data.
Returns:
dict:
Options to provide to the JavaScript view.
"""
return {
'periodicReload': self.periodic_reload,
}
class DataGrid(DataGridJSMixin, DjbletsDataGrid):
"""Base class for a datagrid in Review Board.
This contains additional information on JavaScript views/models
to load for the page.
"""
class AlphanumericDataGrid(DataGridJSMixin, DjbletsAlphanumericDataGrid):
"""Base class for an alphanumeric datagrid in Review Board.
This contains additional information on JavaScript views/models
to load for the page.
"""
class ReviewRequestDataGrid(ShowClosedReviewRequestsMixin, DataGrid):
"""A datagrid that displays a list of review requests.
This datagrid accepts the show_closed parameter in the URL, allowing
submitted review requests to be filtered out or displayed.
"""
new_updates = NewUpdatesColumn()
my_comments = MyCommentsColumn()
star = ReviewRequestStarColumn()
ship_it = ShipItColumn()
summary = SummaryColumn()
submitter = UsernameColumn(label=_('Owner'),
user_relation=['submitter'])
branch = Column(
label=_('Branch'),
db_field='branch',
shrink=True,
sortable=True,
link=False)
bugs_closed = BugsColumn()
repository = RepositoryColumn()
time_added = DateTimeColumn(
label=_('Posted'),
detailed_label=_('Posted Time'),
format='F jS, Y, P',
shrink=True,
css_class=lambda r: ageid(r.time_added))
last_updated = DateTimeColumn(
label=_('Last Updated'),
format='F jS, Y, P',
shrink=True,
db_field='last_updated',
field_name='last_updated',
css_class=lambda r: ageid(r.last_updated))
diff_updated = DiffUpdatedColumn(
format='F jS, Y, P',
shrink=True,
css_class=lambda r: ageid(r.diffset_history.last_diff_updated))
time_added_since = DateTimeSinceColumn(
label=_('Posted'),
detailed_label=_('Posted Time (Relative)'),
field_name='time_added', shrink=True,
css_class=lambda r: ageid(r.time_added))
last_updated_since = DateTimeSinceColumn(
label=_('Last Updated'),
detailed_label=_('Last Updated (Relative)'), shrink=True,
db_field='last_updated',
field_name='last_updated',
css_class=lambda r: ageid(r.last_updated))
diff_updated_since = DiffUpdatedSinceColumn(
detailed_label=_('Diff Updated (Relative)'),
shrink=True,
css_class=lambda r: ageid(r.diffset_history.last_diff_updated))
diff_size = DiffSizeColumn()
review_count = ReviewCountColumn()
target_groups = GroupsColumn()
target_people = PeopleColumn()
to_me = ToMeColumn()
review_id = ReviewRequestIDColumn()
status_query_field = 'status'
site_query_field = 'local_site'
def __init__(self, *args, **kwargs):
"""Initialize the datagrid."""
super(ReviewRequestDataGrid, self).__init__(*args, **kwargs)
self.listview_template = 'datagrids/review_request_listview.html'
self.profile_sort_field = 'sort_review_request_columns'
self.profile_columns_field = 'review_request_columns'
self.show_closed = True
self.submitter_url_name = 'user'
self.default_sort = ['-last_updated']
self.default_columns = [
'star', 'summary', 'submitter', 'time_added', 'last_updated_since'
]
# Add local timezone info to the columns.
user = self.request.user
if user.is_authenticated:
profile = user.get_profile()
self.timezone = pytz.timezone(profile.timezone)
self.time_added.timezone = self.timezone
self.last_updated.timezone = self.timezone
self.diff_updated.timezone = self.timezone
def load_extra_state(self, profile, allow_hide_closed=True):
"""Load extra state for the datagrid."""
return super(ReviewRequestDataGrid, self).load_extra_state(
profile, allow_hide_closed)
def post_process_queryset(self, queryset):
"""Add additional data to the queryset."""
q = queryset.with_counts(self.request.user)
return super(ReviewRequestDataGrid, self).post_process_queryset(q)
def link_to_object(self, state, obj, value):
"""Return a link to the given object."""
if value and isinstance(value, User):
return local_site_reverse('user', request=self.request,
args=[value])
return obj.get_absolute_url()
class ReviewDataGrid(ShowClosedReviewRequestsMixin, DataGrid):
"""A datagrid that displays a list of reviews.
This datagrid accepts the show_closed parameter in the URL, allowing
submitted review requests to be filtered out or displayed.
"""
timestamp = DateTimeColumn(
label=_('Date Reviewed'),
format='F jS, Y',
shrink=True)
submitter = UsernameColumn(label=_('Owner'),
user_relation=['review_request', 'submitter'])
review_summary = ReviewSummaryColumn()
status_query_field = 'review_request__status'
site_query_field = 'review_request__local_site'
def __init__(self, *args, **kwargs):
"""Initialize the datagrid."""
super(ReviewDataGrid, self).__init__(*args, **kwargs)
self.listview_template = 'datagrids/review_request_listview.html'
self.show_closed = True
self.default_sort = ['-timestamp']
self.default_columns = [
'submitter', 'review_summary', 'timestamp',
]
# Add local timezone info to the columns.
user = self.request.user
if user.is_authenticated:
profile = user.get_profile()
self.timezone = pytz.timezone(profile.timezone)
self.timestamp.timezone = self.timezone
class DashboardDataGrid(DataGridSidebarMixin, ReviewRequestDataGrid):
"""Displays the dashboard.
The dashboard is the main place where users see what review requests
are out there that may need their attention.
"""
new_updates = NewUpdatesColumn()
my_comments = MyCommentsColumn()
selected = ReviewRequestCheckboxColumn()
sidebar = Sidebar(
[
OverviewSection,
OutgoingSection,
IncomingSection,
],
default_view_id='overview')
js_model_class = 'RB.Dashboard'
js_view_class = 'RB.DashboardView'
periodic_reload = True
def __init__(self, *args, **kwargs):
"""Initialize the datagrid."""
super(DashboardDataGrid, self).__init__(*args, **kwargs)
self.listview_template = 'datagrids/hideable_listview.html'
self.profile_sort_field = 'sort_dashboard_columns'
self.profile_columns_field = 'dashboard_columns'
self.default_view = 'overview'
self.show_closed = False
self.show_archived = False
self.default_sort = ['-last_updated']
self.default_columns = [
'selected', 'new_updates', 'ship_it', 'my_comments', 'summary',
'submitter', 'last_updated_since'
]
self.extra_js_model_data = {
'show_archived': self.show_archived,
}
self.user = self.request.user
self.profile = self.user.get_profile()
self.site_profile = self.user.get_site_profile(self.local_site)
def load_extra_state(self, profile):
"""Load extra state for the datagrid."""
group_name = self.request.GET.get('group', '')
view = self.request.GET.get('view', self.default_view)
user = self.request.user
changed_fields = []
if view == 'outgoing':
self.queryset = ReviewRequest.objects.from_user(
user, user, local_site=self.local_site)
self.title = _('All Outgoing Review Requests')
elif view == 'overview':
self.queryset = ReviewRequest.objects.to_or_from_user(
user, user, local_site=self.local_site)
self.title = _('Open Incoming and Outgoing Review Requests')
elif view == 'mine':
self.queryset = ReviewRequest.objects.from_user(
user, user, None, local_site=self.local_site)
self.title = _('All My Review Requests')
elif view == 'to-me':
self.queryset = ReviewRequest.objects.to_user_directly(
user, user, local_site=self.local_site)
self.title = _('Incoming Review Requests to Me')
elif view in ('to-group', 'to-watched-group'):
if group_name:
# to-group is special because we want to make sure that the
# group exists and show a 404 if it doesn't. Otherwise, we'll
# show an empty datagrid with the name.
try:
group = Group.objects.get(name=group_name,
local_site=self.local_site)
if not group.is_accessible_by(user):
raise Http404
except Group.DoesNotExist:
raise Http404
self.queryset = ReviewRequest.objects.to_group(
group_name, self.local_site, user)
self.title = _('Incoming Review Requests to %s') % group_name
else:
self.queryset = ReviewRequest.objects.to_user_groups(
user, user, local_site=self.local_site)
self.title = _('All Incoming Review Requests to My Groups')
elif view == 'starred':
self.queryset = self.profile.starred_review_requests.public(
user=user, local_site=self.local_site, status=None)
self.title = _('Starred Review Requests')
elif view == 'incoming':
self.queryset = ReviewRequest.objects.to_user(
user, user, local_site=self.local_site)
self.title = _('All Incoming Review Requests')
else:
raise Http404
if profile and 'show_archived' in profile.extra_data:
self.show_archived = profile.extra_data['show_archived']
try:
show = self.request.GET.get('show-archived', self.show_archived)
self.show_archived = int(show) != 0
except ValueError:
pass
if not self.show_archived:
hidden_q = ReviewRequestVisit.objects.filter(
user=user).exclude(visibility=ReviewRequestVisit.VISIBLE)
hidden_q = hidden_q.values_list('review_request_id', flat=True)
self.queryset = self.queryset.exclude(pk__in=hidden_q)
if (profile and
self.show_archived != profile.extra_data.get('show_archived')):
profile.extra_data['show_archived'] = self.show_archived
changed_fields.append('extra_data')
self.extra_js_model_data['show_archived'] = self.show_archived
changed_fields += (
super(DashboardDataGrid, self)
.load_extra_state(profile, allow_hide_closed=False)
)
return changed_fields
class UsersDataGrid(AlphanumericDataGrid):
"""A datagrid showing a list of users registered on Review Board."""
username = UsernameColumn(label=_('Username'))
fullname = FullNameColumn(label=_('Full Name'), link=True, expand=True)
pending_count = PendingCountColumn(_('Open Review Requests'),
field_name='directed_review_requests',
shrink=True)
def __init__(self, request,
queryset=User.objects.all(),
title=_('All users'),
local_site=None):
"""Initialize the datagrid."""
if local_site:
qs = queryset.filter(local_site=local_site)
else:
qs = queryset
super(UsersDataGrid, self).__init__(request, qs, title=title,
sortable_column='username',
extra_regex=r'^[0-9_\-\.].*')
self.listview_template = 'datagrids/user_listview.html'
self.default_sort = ['username']
self.profile_sort_field = 'sort_submitter_columns'
self.profile_columns_field = 'submitter_columns'
self.default_columns = [
'username', 'fullname', 'pending_count'
]
self.show_inactive = False
def link_to_object(self, state, obj, value):
"""Return a link to the given object."""
return local_site_reverse('user', request=self.request,
args=[obj.username])
def load_extra_state(self, profile):
"""Load extra state for the datagrid.
This handles hiding or showing inactive users.
Args:
profile (reviewboard.accounts.models.Profile):
The user profile which contains some basic
configurable settings.
Returns:
bool:
Always returns False.
"""
show_inactive = self.request.GET.get('show-inactive', 0)
try:
self.show_inactive = int(show_inactive)
except ValueError:
pass
if not self.show_inactive:
self.queryset = self.queryset.filter(is_active=True)
return []
class GroupDataGrid(DataGrid):
"""A datagrid showing a list of review groups accessible by the user."""
star = ReviewGroupStarColumn()
name = Column(_('Group ID'), link=True, sortable=True)
displayname = Column(_('Group Name'), field_name='display_name',
link=True, expand=True)
pending_count = PendingCountColumn(_('Open Review Requests'),
field_name='review_requests',
link=True,
shrink=True)
member_count = GroupMemberCountColumn(_('Members'),
field_name='members',
shrink=True)
def __init__(self, request, title=_('All groups'), *args, **kwargs):
"""Initialize the datagrid."""
local_site = kwargs.get('local_site')
queryset = Group.objects.accessible(request.user,
local_site=local_site)
super(GroupDataGrid, self).__init__(request, queryset=queryset,
title=title, *args, **kwargs)
self.profile_sort_field = 'sort_group_columns'
self.profile_columns_field = 'group_columns'
self.default_sort = ['name']
self.default_columns = [
'star', 'name', 'displayname', 'pending_count'
]
@staticmethod
def link_to_object(state, obj, value):
"""Return a link to the given object."""
return obj.get_absolute_url()
class UserPageDataGridMixin(DataGridSidebarMixin):
"""An abstract class for data grids on the user page.
This will display information about the user on the side.
"""
sidebar = Sidebar([
UserProfileItem,
UserGroupsItem,
])
class UserPageReviewRequestDataGrid(UserPageDataGridMixin,
ReviewRequestDataGrid):
"""A data grid for the user page.
This will show the review requests the user has out for review.
"""
tab_title = _('Review Requests')
def __init__(self, request, user, *args, **kwargs):
"""Initialize the datagrid."""
queryset = ReviewRequest.objects.from_user(
user.username,
user=request.user,
status=None,
with_counts=True,
local_site=kwargs.get('local_site'),
filter_private=True,
show_inactive=True)
super(UserPageReviewRequestDataGrid, self).__init__(
request,
queryset=queryset,
title=_("%s's Review Requests") % user.username,
extra_context={
'pii_safe_title': _("User's Review Requests"),
},
*args, **kwargs)
self.groups = user.review_groups.accessible(request.user)
self.user = user
class UserPageReviewsDataGrid(UserPageDataGridMixin, ReviewDataGrid):
"""A data grid for the user page.
This will show reviews the user has made on other review requests.
"""
tab_title = _('Reviews')
def __init__(self, request, user, *args, **kwargs):
"""Initialize the datagrid."""
queryset = Review.objects.from_user(
user.username,
user=request.user,
public=True,
filter_private=True,
status=None,
local_site=kwargs.get('local_site'))
super(UserPageReviewsDataGrid, self).__init__(
request,
queryset=queryset,
title=_("%s's Reviews") % user.username,
extra_context={
'pii_safe_title': _("User's Reviews"),
},
*args, **kwargs)
self.groups = user.review_groups.accessible(request.user)
self.user = user