Skip to content

Commit

Permalink
added new button for download all
Browse files Browse the repository at this point in the history
  • Loading branch information
thusser committed Feb 15, 2023
1 parent 5e7e01b commit cb37125
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 7 deletions.
57 changes: 51 additions & 6 deletions pyobs_archive/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,56 @@ def preview_view(request, frame_id):
return HttpResponse(bio.getvalue(), content_type="image/png")


@api_view(['POST'])
@permission_classes([IsAuthenticated])
def zip_view(request):
if request.method == 'POST':
return zip_view_post(request)
elif request.method == 'GET':
return zip_view_get(request)
else:
raise Http404


def zip_view_post(request):
# get frames
frames = []
for frame_id in request.POST.getlist('frame_ids[]'):
# get frame
frames.append(_frame(frame_id))

# download
return _download_zip(request, frames)


def zip_view_get(request):
# get offset and limit
try:
offset = int(request.GET.get('offset', default=0))
limit = int(request.GET.get('limit', default=1000))
except ValueError:
raise ParseError('Invalid values for offset/limit.')

# limit to 1000
limit = max(0, min(limit, 1000))
offset = max(0, offset)

# sort
sort = request.GET.get('sort', default='DATE_OBS')
order = request.GET.get('order', default='asc')
sort_string = ('' if order == 'asc' else '-') + sort

# filter
data = filter_frames(Frame.objects, request)

# and frames
root = settings.ARCHIVE_ROOT
frames = [(frame, os.path.join(root, frame.path, frame.basename + '.fits.fz')) for frame in data]

# download
return _download_zip(request, frames)


def _download_zip(request, frames):
# get archive root
root = settings.ARCHIVE_ROOT

Expand All @@ -309,12 +356,10 @@ def zip_view(request):
zip_file = zipstream.ZipFile()

# add files
for frame_id in request.POST.getlist('frame_ids[]'):
# get frame
frame, filename = _frame(frame_id)

for frame, filename in frames:
# add file to zip
zip_file.write(filename, arcname=os.path.join(archive_name, os.path.basename(filename)))
if os.path.exists(filename):
zip_file.write(filename, arcname=os.path.join(archive_name, os.path.basename(filename)))

# create and return response
response = StreamingHttpResponse(zip_file, content_type='application/zip')
Expand Down
19 changes: 19 additions & 0 deletions pyobs_archive/frontend/static/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ $(function () {
setRequestHeader(xhr);
}
},
onLoadSuccess: function() {
// update download search button
let downloadSearchBtn = $('#downloadSearchBtn');
let rows = this.totalRows;
downloadSearchBtn.html('Download all (' + rows + ')');
downloadSearchBtn.attr('href', 'frames/zip?q=a' + buildQueryParms());
},
onCheck: on_check,
onUncheck: on_check,
onCheckAll: on_check,
onUncheckAll: on_check,
totalField: 'count',
dataField: 'results',
pagination: true,
Expand Down Expand Up @@ -117,6 +128,14 @@ $(function () {
}]
});

function on_check() {

// update download button
let downloadBtn = $('#downloadBtn');
var rows = $('#table').bootstrapTable('getSelections').length;
downloadBtn.html('Download selected (' + rows + ')');
}

$('#daterange').daterangepicker({
'locale': {
'format': 'YYYY-MM-DD HH:mm'
Expand Down
8 changes: 7 additions & 1 deletion pyobs_archive/frontend/templates/archive/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,13 @@
<div class="btn-toolbar" role="group">
<form id="zip-form">
{% csrf_token %}
<button type="button" id="downloadBtn" class="btn btn-primary">Download</button>
<button type="button" id="downloadBtn" class="btn btn-success">Download selected</button>
</form>
</div>
<div class="btn-toolbar ml-2" role="group">
<form id="zip-form">
{% csrf_token %}
<a id="downloadSearchBtn" href="#" class="btn btn-primary" style="color: white">Download all</a>
</form>
</div>
</div>
Expand Down

0 comments on commit cb37125

Please sign in to comment.