-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a simple "user stats" extension that uses the new infobox features.
This change adds a new extension that populates the extra section of the user infobox with counts of that user's incoming and outgoing review requests. Testing done: Installed and enabled the extension. Hovered over a user and saw counts of their incoming and outgoing review requests. Reviewed at https://reviews.reviewboard.org/r/8080/
- Loading branch information
Showing
4 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
from __future__ import unicode_literals | ||
|
||
import time | ||
|
||
from django.utils import six | ||
from reviewboard.extensions.base import Extension | ||
from reviewboard.extensions.hooks import UserInfoboxHook | ||
from reviewboard.reviews.models import ReviewRequest | ||
|
||
|
||
class UserStatsInfoboxHook(UserInfoboxHook): | ||
"""Add user statistics to the infobox.""" | ||
|
||
def get_etag_data(self, user, request, local_site): | ||
"""Return data to include in the ETag calculation. | ||
When this extension is enabled, we don't want the browser to cache | ||
anything, because we don't save any work by doing so. Just return a | ||
cache-busting timestamp. | ||
Args: | ||
user (django.contrib.auth.models.User): | ||
The user whose infobox is being shown. | ||
request (django.http.HttpRequest): | ||
The request object. | ||
local_site (reviewboard.site.models.LocalSite): | ||
The current local site, if any. | ||
Returns: | ||
six.text_type: | ||
Data to include in the ETag. | ||
""" | ||
return six.text_type(time.time()) | ||
|
||
def get_extra_context(self, user, request, local_site): | ||
"""Return context to include when rendering the template. | ||
Args: | ||
user (django.contrib.auth.models.User): | ||
The user whose infobox is being shown. | ||
request (django.http.HttpRequest): | ||
The request object. | ||
local_site (reviewboard.site.models.LocalSite): | ||
The current local site, if any. | ||
Returns: | ||
dict: | ||
Additional data to include when rendering the template. | ||
""" | ||
return { | ||
'incoming': ReviewRequest.objects.to_user( | ||
user, user=request.user, local_site=local_site).count(), | ||
'outgoing': ReviewRequest.objects.from_user( | ||
user, user=request.user, local_site=local_site).count(), | ||
} | ||
|
||
|
||
class RBUserStats(Extension): | ||
"""The user statistics extension.""" | ||
|
||
metadata = { | ||
'Name': 'rb-user-stats', | ||
'Summary': 'Show statistics for Review Board users in the infobox', | ||
} | ||
|
||
def initialize(self): | ||
"""Initialize the extension.""" | ||
UserStatsInfoboxHook(self, 'rb-user-stats-infobox.html') |
2 changes: 2 additions & 0 deletions
2
rb_user_stats/rb_user_stats/templates/rb-user-stats-infobox.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<p>Outgoing Review Requests: {{outgoing}}</p> | ||
<p>Incoming Review Requests: {{incoming}}</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from __future__ import unicode_literals | ||
|
||
from reviewboard.extensions.packaging import setup | ||
|
||
|
||
PACKAGE = 'rb-user-stats' | ||
VERSION = '0.1' | ||
|
||
setup( | ||
name=PACKAGE, | ||
version=VERSION, | ||
description='Show statistics for Review Board users in the infobox', | ||
url='https://www.reviewboard.org/', | ||
author='Beanbag, Inc.', | ||
author_email='[email protected]', | ||
maintainer='Beanbag, Inc.', | ||
maintainer_email='[email protected]', | ||
packages=['rb_user_stats'], | ||
entry_points={ | ||
'reviewboard.extensions': | ||
'%s = rb_user_stats.extension:RBUserStats' % PACKAGE, | ||
}, | ||
package_data={ | ||
b'rb_user_stats': [ | ||
'templates/rb_user_stats/*.html', | ||
], | ||
} | ||
) |