Skip to content

Commit 2a4ae27

Browse files
Add project views metrics to sidebar
1 parent 2eea13e commit 2a4ae27

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

physionet-django/project/templates/project/published_project.html

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,23 @@ <h5 class="card-header">Discovery</h5>
242242
</div>
243243
</div>
244244

245+
<div class="card my-4">
246+
<h5 class="card-header">Metrics</h5>
247+
<div class="card-body text-center">
248+
<div class="d-flex justify-content-around mb-2">
249+
<div>
250+
<h3 class="mb-0">{{ project_views_count }}</h3>
251+
<small class="text-muted">Current Version</small>
252+
</div>
253+
<div>
254+
<h3 class="mb-0">{{ all_versions_views_count }}</h3>
255+
<small class="text-muted">All Versions</small>
256+
</div>
257+
</div>
258+
<small class="text-muted mb-0"><em>Count of Registered Users</em></small>
259+
</div>
260+
</div>
261+
245262
<div class="card my-4">
246263
<h5 class="card-header">Corresponding Author</h5>
247264
<div class="card-body">

physionet-django/project/test_views.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,3 +1705,32 @@ def test_bibtex_in_citation_text_all(self):
17051705

17061706
self.assertIn('BibTeX', citations)
17071707
self.assertIn('@article{', citations['BibTeX'])
1708+
1709+
1710+
class TestFileViewsMetric(TestMixin):
1711+
"""Test the file views metric on published project pages."""
1712+
1713+
def test_project_views_count_displayed(self):
1714+
"""Project views count is displayed on the published project page."""
1715+
project = PublishedProject.objects.get(title='Demo ECG Signal Toolbox')
1716+
response = self.client.get(reverse('published_project',
1717+
args=(project.slug, project.version)))
1718+
self.assertEqual(response.status_code, 200)
1719+
self.assertContains(response, 'Current Version')
1720+
self.assertContains(response, 'All Versions')
1721+
self.assertEqual(response.context['project_views_count'], 0)
1722+
self.assertEqual(response.context['all_versions_views_count'], 0)
1723+
1724+
def test_project_views_increments_on_authenticated_view(self):
1725+
"""Project views count increments when authenticated user views files."""
1726+
project = PublishedProject.objects.get(title='Demo ECG Signal Toolbox')
1727+
1728+
self.client.login(username='rgmark@mit.edu', password='Tester11!')
1729+
# First visit creates the AccessLog
1730+
self.client.get(reverse('published_project',
1731+
args=(project.slug, project.version)))
1732+
# Second visit sees the incremented count
1733+
response = self.client.get(reverse('published_project',
1734+
args=(project.slug, project.version)))
1735+
self.assertEqual(response.context['project_views_count'], 1)
1736+
self.assertEqual(response.context['all_versions_views_count'], 1)

physionet-django/project/views.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2013,6 +2013,18 @@ def published_project(request, project_slug, version, subdir=''):
20132013
except AWS.DoesNotExist:
20142014
user_in_access_point_policy = False
20152015

2016+
content_type = ContentType.objects.get_for_model(project)
2017+
project_views_count = AccessLog.objects.filter(
2018+
object_id=project.id,
2019+
content_type=content_type
2020+
).values('user').distinct().count()
2021+
2022+
all_version_ids = PublishedProject.objects.filter(slug=project_slug).values_list('id', flat=True)
2023+
all_versions_views_count = AccessLog.objects.filter(
2024+
object_id__in=all_version_ids,
2025+
content_type=content_type
2026+
).values('user').distinct().count()
2027+
20162028
context = {
20172029
'project': project,
20182030
'authors': authors,
@@ -2046,6 +2058,8 @@ def published_project(request, project_slug, version, subdir=''):
20462058
'show_platform_wide_citation': show_platform_wide_citation,
20472059
'main_platform_citation': main_platform_citation,
20482060
'user_country_blocked': user_country_blocked,
2061+
'project_views_count': project_views_count,
2062+
'all_versions_views_count': all_versions_views_count,
20492063
}
20502064
# The file and directory contents
20512065
if can_view_files:

0 commit comments

Comments
 (0)