Commit 3b84b41f authored by Michal Čihař's avatar Michal Čihař

Add contributor stats

Issue #835
Signed-off-by: default avatarMichal Čihař <michal@cihar.com>
parent 71291d33
......@@ -13,6 +13,7 @@ Released on ? 2015.
* Added support for some GNU extensions to C printf format.
* Documentation improvements.
* Added support for generating translator credits.
* Added support for generating contributor stats.
weblate 2.4
-----------
......
......@@ -20,7 +20,7 @@
from weblate.trans.tests.test_views import ViewTestCase
from weblate.trans.views.reports import generate_credits
from weblate.trans.views.reports import generate_credits, generate_counts
from django.core.urlresolvers import reverse
from django.utils import timezone
from datetime import timedelta
......@@ -102,3 +102,59 @@ class ReportsTest(ViewTestCase):
'Weblate Test</a></li></ul></td>\n</tr>\n'
'</table>'
)
def test_counts_one(self):
self.add_change()
data = generate_counts(
self.subproject,
timezone.now() - timedelta(days=1),
timezone.now() + timedelta(days=1)
)
self.assertEqual(
data,
[{
'count': 1,
'name': 'Weblate Test',
'words': 2,
'email': u'noreply@weblate.org'
}]
)
def get_counts(self, style):
self.add_change()
return self.client.post(
reverse('counts', kwargs=self.kw_subproject),
{
'style': style,
'start_date': '2000-01-01',
'end_date': '2100-01-01'
},
)
def test_counts_view_json(self):
response = self.get_counts('json')
data = json.loads(response.content)
self.assertEqual(
data,
[{
'count': 1,
'email': 'noreply@weblate.org',
'name': 'Weblate Test',
'words': 2
}]
)
def test_counts_view_rst(self):
response = self.get_counts('rst')
self.assertContains(response, 'noreply@weblate.org')
def test_counts_view_html(self):
response = self.get_counts('html')
self.assertEqual(
response.content,
'<table>\n'
'<tr>\n<td>Weblate Test</td>\n'
'<td>noreply@weblate.org</td>\n'
'<td>2</td>\n<td>1</td>\n'
'\n</tr>\n</table>'
)
......@@ -121,3 +121,102 @@ def get_credits(request, project, subproject):
'\n'.join(result),
content_type='{0}; charset=utf-8'.format(mime),
)
def generate_counts(component, start_date, end_date):
"""Generates credits data for given component."""
result = {}
for translation in component.translation_set.all():
authors = Change.objects.content().filter(
translation=translation,
timestamp__range=(start_date, end_date),
).values_list(
'author__email', 'author__first_name', 'unit__num_words',
)
for email, name, words in authors:
if email not in result:
result[email] = {
'name': name,
'email': email,
'words': words,
'count': 1,
}
else:
result[email]['words'] += words
result[email]['count'] += 1
return result.values()
@login_required
@require_POST
def get_counts(request, project, subproject):
"""View for work counts"""
obj = get_subproject(request, project, subproject)
if not can_view_reports(request.user, obj.project):
raise PermissionDenied()
form = CreditsForm(request.POST)
if not form.is_valid():
return redirect(obj)
data = generate_counts(
obj,
form.cleaned_data['start_date'],
form.cleaned_data['end_date'],
)
if form.cleaned_data['style'] == 'json':
return HttpResponse(
json.dumps(data),
content_type='application/json'
)
if form.cleaned_data['style'] == 'html':
start = '<table>'
row_start = '<tr>'
cell_format = u'<td>{0}</td>\n'
row_end = '</tr>'
mime = 'text/html'
end = '</table>'
else:
heading = ' '.join(['=' * 25] * 4)
start = '{0}\n{1:25} {2:25} {3:25} {4:25}\n{0}'.format(
heading,
'Email',
'Name',
'Words',
'Count'
)
row_start = ''
cell_format = u'{0:25} '
row_end = ''
mime = 'text/plain'
end = heading
result = []
result.append(start)
for item in data:
result.append(row_start)
result.append(
u'{0}{1}{2}{3}'.format(
cell_format.format(item['name']),
cell_format.format(item['email']),
cell_format.format(item['words']),
cell_format.format(item['count']),
)
)
result.append(row_end)
result.append(end)
return HttpResponse(
'\n'.join(result),
content_type='{0}; charset=utf-8'.format(mime),
)
......@@ -190,6 +190,11 @@ urlpatterns = patterns(
'weblate.trans.views.reports.get_credits',
name='credits',
),
url(
r'^counts/' + SUBPROJECT + '$',
'weblate.trans.views.reports.get_counts',
name='counts',
),
url(
r'^new-lang/' + SUBPROJECT + '$',
'weblate.trans.views.basic.new_language',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment