Improve downloading series zip files
This commit is contained in:
+5
-10
@@ -15,6 +15,7 @@ from django.core.exceptions import (
|
||||
)
|
||||
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin
|
||||
from django.views.generic import View
|
||||
from django.views.generic.detail import DetailView
|
||||
from generic.mixins import SuperuserRequiredMixin
|
||||
|
||||
@@ -121,15 +122,13 @@ import logging
|
||||
from copy import deepcopy
|
||||
from django.forms.models import model_to_dict
|
||||
|
||||
from generic.views import ExamCloneMixin, ExamViews
|
||||
from generic.views import ExamCloneMixin, ExamViews, SeriesImagesZipViewBase
|
||||
from reversion.views import RevisionMixin
|
||||
import reversion
|
||||
|
||||
|
||||
from rest_framework import viewsets
|
||||
|
||||
from zipview.views import BaseZipView
|
||||
|
||||
import os
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -1074,13 +1073,9 @@ def series_order_upload_filename(request, pk):
|
||||
return redirect("atlas:series_detail", pk=pk)
|
||||
|
||||
|
||||
class SeriesImagesZipView(SuperuserRequiredMixin, BaseZipView):
|
||||
"""Download all images from an image series"""
|
||||
|
||||
def get_files(self):
|
||||
series = Series.objects.get(pk=self.kwargs["pk"])
|
||||
|
||||
return [i.image.file for i in series.get_images()]
|
||||
# Vendored in from BaseZipView
|
||||
class SeriesImagesZipView(SeriesImagesZipViewBase, SuperuserRequiredMixin):
|
||||
series_object = Series
|
||||
|
||||
|
||||
def create_series_findings(request):
|
||||
|
||||
@@ -51,6 +51,10 @@ from generic.filters import CidUserFilter, ExaminationFilter
|
||||
from generic.tables import CidUserExamTable, CidUserTable, ExaminationTable
|
||||
from generic.mixins import SuperuserRequiredMixin
|
||||
|
||||
import zipfile
|
||||
from django.core.files.base import ContentFile
|
||||
|
||||
|
||||
from .forms import (
|
||||
CidGroupExamForm,
|
||||
CidUserForm,
|
||||
@@ -3189,3 +3193,34 @@ class ExaminationDelete(RevisionMixin, SuperuserRequiredMixin, DeleteView):
|
||||
class ExaminationUpdate(UpdateView, SuperuserRequiredMixin):
|
||||
model = Examination
|
||||
form_class = ExaminationForm
|
||||
|
||||
|
||||
class SeriesImagesZipViewBase(SuperuserRequiredMixin, View):
|
||||
"""Download all images from an image series"""
|
||||
http_method_names = ['get']
|
||||
series_object = None
|
||||
|
||||
def get_files(self):
|
||||
series = self.series_object.objects.get(pk=self.kwargs["pk"])
|
||||
|
||||
return [i.image.file for i in series.get_images()]
|
||||
|
||||
def get_archive_name(self, request):
|
||||
#series = Series.objects.get(pk=self.kwargs["pk"])
|
||||
return f"Series {self.kwargs['pk']}.zip"
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
temp_file = ContentFile(b"", name=self.get_archive_name(request))
|
||||
with zipfile.ZipFile(temp_file, mode='w', compression=zipfile.ZIP_DEFLATED) as zip_file:
|
||||
files = self.get_files()
|
||||
for file_ in files:
|
||||
path = os.path.split(file_.name)[-1]
|
||||
zip_file.writestr(path, file_.read())
|
||||
|
||||
file_size = temp_file.tell()
|
||||
temp_file.seek(0)
|
||||
|
||||
response = HttpResponse(temp_file, content_type='application/zip')
|
||||
response['Content-Disposition'] = 'attachment; filename=%s' % self.get_archive_name(request)
|
||||
response['Content-Length'] = file_size
|
||||
return response
|
||||
+3
-8
@@ -75,7 +75,7 @@ from django.forms.models import model_to_dict
|
||||
from longs.forms import LongCreationDefaultForm
|
||||
from longs.models import LongCreationDefault
|
||||
|
||||
from generic.views import AuthorRequiredMixin, ExamCloneMixin, ExamCreateBase, ExamDeleteBase, ExamUpdateBase, ExamViews
|
||||
from generic.views import AuthorRequiredMixin, ExamCloneMixin, ExamCreateBase, ExamDeleteBase, ExamUpdateBase, ExamViews, SeriesImagesZipViewBase
|
||||
from reversion.views import RevisionMixin
|
||||
import reversion
|
||||
|
||||
@@ -1244,13 +1244,8 @@ def refresh_exam_question_json(request, pk):
|
||||
return redirect("longs:exam_overview", pk=pk)
|
||||
|
||||
|
||||
class SeriesImagesZipView(SuperuserRequiredMixin, BaseZipView):
|
||||
"""Download all images from an image series"""
|
||||
|
||||
def get_files(self):
|
||||
series = LongSeries.objects.get(pk=self.kwargs["pk"])
|
||||
|
||||
return [i.image.file for i in series.images.all()]
|
||||
class SeriesImagesZipView(SeriesImagesZipViewBase, SuperuserRequiredMixin):
|
||||
series_object = LongSeries
|
||||
|
||||
|
||||
def question_review(request, pk):
|
||||
|
||||
Reference in New Issue
Block a user