dav_submission: added email notification, zip-download and permission

check.
This commit is contained in:
2019-11-06 13:25:06 +01:00
parent bcacb033df
commit 6f999e419d
6 changed files with 95 additions and 15 deletions

View File

@@ -6,15 +6,19 @@ import os
import pytz
import re
import urllib
import zipfile
from django.apps import apps
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.core.exceptions import PermissionDenied
from django.http import FileResponse, Http404
from django.urls import reverse_lazy
from django.utils import timezone
from django.utils.decorators import method_decorator
from django.utils.translation import ugettext as _
from django.views import generic
from .emails import NewSubmissionMail
from .forms import UploadForm
app_config = apps.get_containing_app_config(__package__)
@@ -71,9 +75,16 @@ class ListView(generic.ListView):
qs = [all_metadata[subdir] for subdir in subdirs]
return qs
@method_decorator(login_required)
def dispatch(self, request, *args, **kwargs):
if not request.user.has_perm('download_submissions'):
raise PermissionDenied()
return super(ListView, self).dispatch(request, *args, **kwargs)
class DownloadView(generic.DetailView):
def get(self, request, *args, **kwargs):
cached_zip_file_name = app_config.settings.cached_zip_file_name
base_path = app_config.settings.upload_path
pk = kwargs.get('pk')
@@ -82,11 +93,22 @@ class DownloadView(generic.DetailView):
else:
subdir = urllib.parse.unquote_plus(pk)
path = os.path.join(base_path, subdir)
submission_dir = os.path.join(base_path, subdir)
if not os.path.isdir(path):
if not os.path.isdir(submission_dir):
raise Http404()
cached_zip = os.path.join(submission_dir, cached_zip_file_name)
if not os.path.isfile(cached_zip):
with open(cached_zip, 'wb') as cache_f:
with zipfile.ZipFile(cache_f, 'w') as z:
for filename in os.listdir(submission_dir):
if filename == cached_zip_file_name:
continue
z.write(os.path.join(submission_dir, filename), os.path.join(subdir, filename))
zip_f = open(cached_zip, 'rb')
file_name = subdir
file_ext = '.zip'
mime_type = 'application/zip'
@@ -95,15 +117,18 @@ class DownloadView(generic.DetailView):
file_ext=file_ext,
)
raise Exception('Not Implemented yet')
file_obj = open(path, 'rb')
response = FileResponse(streaming_content=file_obj, content_type=mime_type)
response = FileResponse(streaming_content=zip_f, content_type=mime_type)
disposition_header = 'attachment; filename="{}"'.format(disposition_file_name)
response['Content-Disposition'] = disposition_header
return response
@method_decorator(login_required)
def dispatch(self, request, *args, **kwargs):
if not request.user.has_perm('download_submissions'):
raise PermissionDenied()
return super(DownloadView, self).dispatch(request, *args, **kwargs)
class UploadView(generic.edit.FormView):
initial = {
@@ -235,6 +260,8 @@ Beschreibung:
logger.error('dav_submission.views.UploadView.form_valid(): Catched Exception #3: %s', str(e))
return super(UploadView, self).form_valid(form)
mail = NewSubmissionMail(metadata_format_kwargs)
mail.send()
return super(UploadView, self).form_valid(form)
def post(self, request, *args, **kwargs):