diff --git a/backend/django/core/templates/projects/detail.html b/backend/django/core/templates/projects/detail.html index fa134b86..39179cd0 100644 --- a/backend/django/core/templates/projects/detail.html +++ b/backend/django/core/templates/projects/detail.html @@ -285,6 +285,9 @@
{% endif %} {% endif %} {% endif %} + {% if project.percentage_irr > 0 %} + + {% endif %} @@ -346,6 +349,32 @@
xhttp.send(); } +/* + * When the download IRR log button is pressed, download the IRR log as a csv file + */ +function downloadIRRLog(projectId) { + var url = `/api/download_irr_log/${projectId}/`; + var xhttp = new XMLHttpRequest(); + xhttp.onreadystatechange = function() { + if (xhttp.readyState === 4 && xhttp.status === 200) { + var blob = new Blob([xhttp.response], {type: 'text/csv'}); + var downloadUrl = window.URL.createObjectURL(blob); + var a = document.createElement('a'); + a.href = downloadUrl; + a.download = 'irr_log_' + projectId + '.csv'; + document.body.appendChild(a); + a.click(); + a.remove(); + window.URL.revokeObjectURL(downloadUrl); + } else if (xhttp.readyState === 4 && xhttp.status !== 200) { + console.error('Error downloading the file:', xhttp.statusText); + } + }; + xhttp.open('GET', url, true); + xhttp.responseType = 'blob'; // Set the response type to blob for binary data + xhttp.send(); +} + /* * When the ingest datatable button is pressed, SMART pulls the entire @@ -396,5 +425,7 @@
} }) }); + + {% endblock %} diff --git a/backend/django/core/urls/api.py b/backend/django/core/urls/api.py index 0d45336c..54cd1697 100644 --- a/backend/django/core/urls/api.py +++ b/backend/django/core/urls/api.py @@ -101,6 +101,9 @@ re_path( r"^download_data/(?P\d+)/(?P\d)/$", api.download_data ), + re_path( + r"^download_irr_log/(?P\d+)/$", api.download_irr_log + ), re_path( r"^download_model/(?P\d+)/(?P\d)/$", api.download_model ), diff --git a/backend/django/core/views/api.py b/backend/django/core/views/api.py index 7faec5da..885b9e45 100644 --- a/backend/django/core/views/api.py +++ b/backend/django/core/views/api.py @@ -10,7 +10,7 @@ from rest_framework.decorators import api_view, permission_classes from rest_framework.response import Response -from core.models import Project +from core.models import Project, IRRLog from core.permissions import IsAdminOrCreator from core.templatetags import project_extras from core.utils.util import get_labeled_data @@ -135,6 +135,25 @@ def download_model(request, project_pk, unverified): return response +@api_view(["GET"]) +@permission_classes((IsAdminOrCreator,)) +def download_irr_log(request, project_pk): + response = HttpResponse( + content_type='text/csv', + headers={'Content-Disposition': f'attachment; filename="irr_log_{project_pk}.csv"'}, + ) + + writer = csv.writer(response) + writer.writerow(['text', 'label', 'username', 'timestamp']) + + logs = IRRLog.objects.filter(data__project_id=project_pk).select_related('data', 'profile', 'label') + + for log in logs: + label_name = log.label.name if log.label else '' + writer.writerow([log.data.text, label_name, log.profile.user, log.timestamp ]) + + return response + @api_view(["POST"]) @permission_classes((IsAdminOrCreator,))