Skip to content

Commit

Permalink
Add a simple "user stats" extension that uses the new infobox features.
Browse files Browse the repository at this point in the history
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
davidt committed Apr 29, 2016
1 parent fed333e commit 9279adf
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
Empty file.
72 changes: 72 additions & 0 deletions rb_user_stats/rb_user_stats/extension.py
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')
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>
28 changes: 28 additions & 0 deletions rb_user_stats/setup.py
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',
],
}
)

0 comments on commit 9279adf

Please sign in to comment.