Skip to content

Commit cb37125

Browse files
committed
added new button for download all
1 parent 5e7e01b commit cb37125

File tree

3 files changed

+77
-7
lines changed

3 files changed

+77
-7
lines changed

pyobs_archive/api/views.py

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,56 @@ def preview_view(request, frame_id):
296296
return HttpResponse(bio.getvalue(), content_type="image/png")
297297

298298

299-
@api_view(['POST'])
300299
@permission_classes([IsAuthenticated])
301300
def zip_view(request):
301+
if request.method == 'POST':
302+
return zip_view_post(request)
303+
elif request.method == 'GET':
304+
return zip_view_get(request)
305+
else:
306+
raise Http404
307+
308+
309+
def zip_view_post(request):
310+
# get frames
311+
frames = []
312+
for frame_id in request.POST.getlist('frame_ids[]'):
313+
# get frame
314+
frames.append(_frame(frame_id))
315+
316+
# download
317+
return _download_zip(request, frames)
318+
319+
320+
def zip_view_get(request):
321+
# get offset and limit
322+
try:
323+
offset = int(request.GET.get('offset', default=0))
324+
limit = int(request.GET.get('limit', default=1000))
325+
except ValueError:
326+
raise ParseError('Invalid values for offset/limit.')
327+
328+
# limit to 1000
329+
limit = max(0, min(limit, 1000))
330+
offset = max(0, offset)
331+
332+
# sort
333+
sort = request.GET.get('sort', default='DATE_OBS')
334+
order = request.GET.get('order', default='asc')
335+
sort_string = ('' if order == 'asc' else '-') + sort
336+
337+
# filter
338+
data = filter_frames(Frame.objects, request)
339+
340+
# and frames
341+
root = settings.ARCHIVE_ROOT
342+
frames = [(frame, os.path.join(root, frame.path, frame.basename + '.fits.fz')) for frame in data]
343+
344+
# download
345+
return _download_zip(request, frames)
346+
347+
348+
def _download_zip(request, frames):
302349
# get archive root
303350
root = settings.ARCHIVE_ROOT
304351

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

311358
# add files
312-
for frame_id in request.POST.getlist('frame_ids[]'):
313-
# get frame
314-
frame, filename = _frame(frame_id)
315-
359+
for frame, filename in frames:
316360
# add file to zip
317-
zip_file.write(filename, arcname=os.path.join(archive_name, os.path.basename(filename)))
361+
if os.path.exists(filename):
362+
zip_file.write(filename, arcname=os.path.join(archive_name, os.path.basename(filename)))
318363

319364
# create and return response
320365
response = StreamingHttpResponse(zip_file, content_type='application/zip')

pyobs_archive/frontend/static/js/app.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ $(function () {
5959
setRequestHeader(xhr);
6060
}
6161
},
62+
onLoadSuccess: function() {
63+
// update download search button
64+
let downloadSearchBtn = $('#downloadSearchBtn');
65+
let rows = this.totalRows;
66+
downloadSearchBtn.html('Download all (' + rows + ')');
67+
downloadSearchBtn.attr('href', 'frames/zip?q=a' + buildQueryParms());
68+
},
69+
onCheck: on_check,
70+
onUncheck: on_check,
71+
onCheckAll: on_check,
72+
onUncheckAll: on_check,
6273
totalField: 'count',
6374
dataField: 'results',
6475
pagination: true,
@@ -117,6 +128,14 @@ $(function () {
117128
}]
118129
});
119130

131+
function on_check() {
132+
133+
// update download button
134+
let downloadBtn = $('#downloadBtn');
135+
var rows = $('#table').bootstrapTable('getSelections').length;
136+
downloadBtn.html('Download selected (' + rows + ')');
137+
}
138+
120139
$('#daterange').daterangepicker({
121140
'locale': {
122141
'format': 'YYYY-MM-DD HH:mm'

pyobs_archive/frontend/templates/archive/index.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,13 @@
8484
<div class="btn-toolbar" role="group">
8585
<form id="zip-form">
8686
{% csrf_token %}
87-
<button type="button" id="downloadBtn" class="btn btn-primary">Download</button>
87+
<button type="button" id="downloadBtn" class="btn btn-success">Download selected</button>
88+
</form>
89+
</div>
90+
<div class="btn-toolbar ml-2" role="group">
91+
<form id="zip-form">
92+
{% csrf_token %}
93+
<a id="downloadSearchBtn" href="#" class="btn btn-primary" style="color: white">Download all</a>
8894
</form>
8995
</div>
9096
</div>

0 commit comments

Comments
 (0)