dav_submission: added first stuff for downloading submissions.
This commit is contained in:
@@ -3,9 +3,13 @@ import codecs
|
||||
import datetime
|
||||
import logging
|
||||
import os
|
||||
import pytz
|
||||
import re
|
||||
import urllib
|
||||
from django.apps import apps
|
||||
from django.contrib import messages
|
||||
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.translation import ugettext as _
|
||||
@@ -17,6 +21,90 @@ app_config = apps.get_containing_app_config(__package__)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class ListView(generic.ListView):
|
||||
template_name = 'dav_submission/list.html'
|
||||
|
||||
def get_queryset(self):
|
||||
base_path = app_config.settings.upload_path
|
||||
metadata_file_name = app_config.settings.metadata_file_name
|
||||
|
||||
subdirs = os.listdir(base_path)
|
||||
all_metadata = {}
|
||||
for subdir in subdirs:
|
||||
metadata_file_path = os.path.join(base_path, subdir, metadata_file_name)
|
||||
if hasattr(urllib, 'quote_plus'):
|
||||
pk = urllib.quote_plus(subdir)
|
||||
else:
|
||||
pk = urllib.parse.quote_plus(subdir)
|
||||
metadata = {
|
||||
'pk': pk,
|
||||
'name': None,
|
||||
'email_address': None,
|
||||
'title': None,
|
||||
'timestamp': None,
|
||||
}
|
||||
with open(metadata_file_path) as f:
|
||||
for line in f:
|
||||
mo = re.match(r'^Absender: (.*) <(.*)>$', line)
|
||||
if mo is not None:
|
||||
metadata['name'] = mo.group(1)
|
||||
metadata['email_address'] = mo.group(2)
|
||||
continue
|
||||
mo = re.match(r'^Titel: (.*)$', line)
|
||||
if mo is not None:
|
||||
metadata['title'] = mo.group(1)
|
||||
continue
|
||||
mo = re.match(r'^Datum: ([0-9]{2}.[0-9]{2}.[0-9]{4}) ([0-9]{2}:[0-9]{2}:[0-9]{2}) (.*)$', line)
|
||||
if mo is not None:
|
||||
date_str = mo.group(1)
|
||||
time_str = mo.group(2)
|
||||
zone_str = mo.group(3)
|
||||
datetime_str = '{} {}'.format(date_str, time_str)
|
||||
timestamp = datetime.datetime.strptime(datetime_str, '%d.%m.%Y %H:%M:%S')
|
||||
tz = pytz.timezone(zone_str)
|
||||
metadata['timestamp'] = tz.localize(timestamp)
|
||||
continue
|
||||
|
||||
all_metadata[subdir] = metadata
|
||||
|
||||
sorted(subdirs)
|
||||
qs = [all_metadata[subdir] for subdir in subdirs]
|
||||
return qs
|
||||
|
||||
|
||||
class DownloadView(generic.DetailView):
|
||||
def get(self, request, *args, **kwargs):
|
||||
base_path = app_config.settings.upload_path
|
||||
|
||||
pk = kwargs.get('pk')
|
||||
if hasattr(urllib, 'unquote_plus'):
|
||||
subdir = urllib.unquote_plus(pk)
|
||||
else:
|
||||
subdir = urllib.parse.unquote_plus(pk)
|
||||
|
||||
path = os.path.join(base_path, subdir)
|
||||
|
||||
if not os.path.isdir(path):
|
||||
raise Http404()
|
||||
|
||||
file_name = subdir
|
||||
file_ext = '.zip'
|
||||
mime_type = 'application/zip'
|
||||
disposition_file_name = '{file_name}{file_ext}'.format(
|
||||
file_name=file_name,
|
||||
file_ext=file_ext,
|
||||
)
|
||||
|
||||
raise Exception('Not Implemented yet')
|
||||
file_obj = open(path, 'rb')
|
||||
|
||||
response = FileResponse(streaming_content=file_obj, content_type=mime_type)
|
||||
disposition_header = 'attachment; filename="{}"'.format(disposition_file_name)
|
||||
response['Content-Disposition'] = disposition_header
|
||||
|
||||
return response
|
||||
|
||||
|
||||
class UploadView(generic.edit.FormView):
|
||||
initial = {
|
||||
# 'name': u'heinzel',
|
||||
|
||||
Reference in New Issue
Block a user