Skip to content

Commit

Permalink
Merge pull request #346 from RTIInternational/irr-download
Browse files Browse the repository at this point in the history
add IRR log download option -- MERGE AFTER PR #342
  • Loading branch information
AstridKery authored Jul 17, 2024
2 parents 45bd474 + cb3cfb1 commit ae860a4
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
31 changes: 31 additions & 0 deletions backend/django/core/templates/projects/detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,9 @@ <h5 class="panel-title">
{% endif %}
{% endif %}
{% endif %}
{% if project.percentage_irr > 0 %}
<button id="download-irr-log-btn" class="btn btn-primary" onclick="downloadIRRLog({{ project.pk }})">Download IRR Log</button>
{% endif %}
</div>
</div>
</div>
Expand Down Expand Up @@ -346,6 +349,32 @@ <h5 class="panel-title">
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
Expand Down Expand Up @@ -396,5 +425,7 @@ <h5 class="panel-title">
}
})
});


</script>
{% endblock %}
3 changes: 3 additions & 0 deletions backend/django/core/urls/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@
re_path(
r"^download_data/(?P<project_pk>\d+)/(?P<unverified>\d)/$", api.download_data
),
re_path(
r"^download_irr_log/(?P<project_pk>\d+)/$", api.download_irr_log
),
re_path(
r"^download_model/(?P<project_pk>\d+)/(?P<unverified>\d)/$", api.download_model
),
Expand Down
21 changes: 20 additions & 1 deletion backend/django/core/views/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,))
Expand Down

0 comments on commit ae860a4

Please sign in to comment.