Skip to content

Commit 100339b

Browse files
Add File Views metric to project sidebar
1 parent 2eea13e commit 100339b

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,14 @@ <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+
<h2 class="mb-0">{{ file_views_count }}</h2>
249+
<small class="text-muted">File Views</small>
250+
</div>
251+
</div>
252+
245253
<div class="card my-4">
246254
<h5 class="card-header">Corresponding Author</h5>
247255
<div class="card-body">

physionet-django/project/test_views.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,3 +1705,29 @@ 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_file_views_count_displayed(self):
1714+
"""File 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, 'File Views')
1720+
self.assertEqual(response.context['file_views_count'], 0)
1721+
1722+
def test_file_views_increments_on_authenticated_view(self):
1723+
"""File views count increments when authenticated user views files."""
1724+
project = PublishedProject.objects.get(title='Demo ECG Signal Toolbox')
1725+
1726+
self.client.login(username='rgmark@mit.edu', password='Tester11!')
1727+
# First visit creates the AccessLog
1728+
self.client.get(reverse('published_project',
1729+
args=(project.slug, project.version)))
1730+
# Second visit sees the incremented count
1731+
response = self.client.get(reverse('published_project',
1732+
args=(project.slug, project.version)))
1733+
self.assertEqual(response.context['file_views_count'], 1)

physionet-django/project/views.py

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

2016+
# Count unique users who have viewed this project's files
2017+
file_views_count = AccessLog.objects.filter(
2018+
object_id=project.id,
2019+
content_type=ContentType.objects.get_for_model(project)
2020+
).values('user').distinct().count()
2021+
20162022
context = {
20172023
'project': project,
20182024
'authors': authors,
@@ -2046,6 +2052,7 @@ def published_project(request, project_slug, version, subdir=''):
20462052
'show_platform_wide_citation': show_platform_wide_citation,
20472053
'main_platform_citation': main_platform_citation,
20482054
'user_country_blocked': user_country_blocked,
2055+
'file_views_count': file_views_count,
20492056
}
20502057
# The file and directory contents
20512058
if can_view_files:

0 commit comments

Comments
 (0)