Skip to content

Commit

Permalink
add collection export feature
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-kaimika committed Oct 8, 2024
1 parent 85215b7 commit e8f78d1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
12 changes: 12 additions & 0 deletions templates/components/filter-form.html
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,18 @@
<input type="submit" class="button small primary expanded" id="add_to_collection" value="Add to Collection">
</div>
</form>
<form id="export_collection_form" data-abide method="post" action="/api/export_roi_list">
{% csrf_token %}
<label>Export Collection:</label>
<div>
<select name="collection" style="width:100%">
{% for collection in collections %}
<option value="{{ collection.name }}">{{ collection.name }}</option>
{% endfor %}
</select>
<input type="submit" class="button small primary expanded" id="export_collection" value="Export Collection">
</div>
</form>
{% else %}
To annotate images, please <a href="{% url 'manage:login' %}">login</a>.
{% endif %}
Expand Down
1 change: 1 addition & 0 deletions web/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
path('api/roi_annotations', views.roi_annotations, name='annotations'),
path('api/create_label', views.create_label, name='create_label'),
path('api/roi_list', views.roi_list, name='roi_list'),
path('api/export_roi_list', views.export_roi_list, name='export_roi_list'),
path('api/create_or_verify_annotations', views.create_or_verify_annotations, name='create_or_verify_annotations'),
path('api/move_or_copy_to_collection', views.move_or_copy_to_collection, name='move_or_copy_to_collection'),
path('api/get_labels', views.get_labels, name='get_labels'),
Expand Down
35 changes: 34 additions & 1 deletion web/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import csv
import os
from pprint import pprint
import json

from django.contrib.auth.models import User
from django.http import JsonResponse, HttpResponseBadRequest, HttpResponse
from django.http import JsonResponse, HttpResponseBadRequest, HttpResponse, HttpResponseForbidden
from django.shortcuts import render, get_object_or_404
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_POST
Expand Down Expand Up @@ -35,6 +38,36 @@ def index(request):
'ROI_ID_DESC': ['-roi_id'],
}

def export_roi_list(request):
if not request.user.is_authenticated:
return HttpResponseForbidden()

collection = request.POST.get('collection')
if not collection:
return HttpResponseBadRequest()

rois = ROI.objects \
.filter(collections__name=collection) \
.order_by("roi_id") \
.values_list('collections__name', 'winning_annotation__label__name', 'path')

response = HttpResponse(
content_type="text/csv",
headers={"Content-Disposition": f'attachment; filename="{collection}-rois.csv"'},
)

writer = csv.writer(response)
writer.writerow(["Collection", "Label", "Filename", "Path"])
for roi in rois:
writer.writerow([
roi[0],
roi[1],
os.path.basename(roi[2]),
os.path.dirname(roi[2]),
])

return response

@require_POST
def roi_list(request):
requested_label = request.POST.get('label')
Expand Down

0 comments on commit e8f78d1

Please sign in to comment.