def download_file(request, file_id, item_id, slug): dataset = DataSet.objects.get(pk=item_id) user = request.user if dataset.is_active: try: downloading_file = DataSetFile.objects.get(pk=file_id) dataset_download = DataSetDownload(parent_dataset=dataset, downloader=user, file_name=downloading_file.get_short_name(), is_readme=downloading_file.is_readme, is_download_all=False) dataset_download.save() response = HttpResponse(downloading_file.file_contents.read(), content_type='application/octet-stream') response['Content-Disposition'] = 'attachment; filename=%s' % downloading_file.get_short_name() response['Content-Length'] = downloading_file.file_contents.size return response except ObjectDoesNotExist: # i sjhould add an error message when the file is not found or for whatever reaAson the serving does not happen return HttpResponseRedirect(reverse('epic.datasets.views.view_dataset', kwargs={'item_id': dataset.id, 'slug': dataset.slug,})) else: return HttpResponseRedirect(reverse('epic.datasets.views.view_dataset', kwargs={'item_id': dataset.id, 'slug': dataset.slug,}))
def download_all(request, item_id, slug): project = get_object_or_404(Project, pk=item_id) user = request.user if project.is_active: datasets = project.datasets.all() temp = tempfile.TemporaryFile() archive = zipfile.ZipFile(temp, 'w') project_download = ProjectDownload(parent_project=project, downloader=user) project_download.save() for dataset in datasets: dataset_zip_file_name = dataset.slug + "-all.zip" dataset_download = DataSetDownload(parent_dataset=dataset, downloader=user, file_name=dataset_zip_file_name, is_readme=True, is_download_all=True, parent_project=project_download) dataset_download.save() for file in dataset.files.all(): file.file_contents.open('r') archive.writestr( (dataset.name + '/' + file.get_short_name()), file.file_contents.read()) file.file_contents.close() archive.close() wrapper = FileWrapper(temp) response = HttpResponse(wrapper, content_type='application/zip') response['Content-Disposition'] = 'attachment; filename=%s.zip' % project.name response['Content-Length'] = temp.tell() temp.seek(0) return response else: view_project_url = get_item_url(project, 'epic.projects.views.view_project') return HttpResponseRedirect(view_project_url)
def download_all_files(request, item_id, slug): dataset = DataSet.objects.get(pk=item_id) user = request.user if dataset.is_active: temp = tempfile.TemporaryFile() archive = zipfile.ZipFile(temp, 'w') dataset_zip_file_name = dataset.slug + "-all.zip" dataset_download = DataSetDownload(parent_dataset=dataset, downloader=user, file_name=dataset_zip_file_name, is_readme=True, is_download_all=True) dataset_download.save() for file in dataset.files.all(): file.file_contents.open('r') archive.writestr(_safe(file.get_short_name()), file.file_contents.read()) file.file_contents.close() archive.close() wrapper = FileWrapper(temp) response = HttpResponse(wrapper, content_type='application/zip') response['Content-Disposition'] = 'attachment; filename=%s.zip' % dataset.name response['Content-Length'] = temp.tell() temp.seek(0) return response else: return HttpResponseRedirect(reverse('epic.datasets.views.view_dataset', kwargs={'item_id': dataset.id, 'slug': dataset.slug,}))