diff --git a/dav_submission/templates/dav_submission/list.html b/dav_submission/templates/dav_submission/list.html new file mode 100644 index 0000000..3307eb8 --- /dev/null +++ b/dav_submission/templates/dav_submission/list.html @@ -0,0 +1,67 @@ +{% extends 'dav_submission/base.html' %} +{% load i18n %} +{% load bootstrap3 %} + +{% block page-container %} +

Einreichungen

+
+ + + + + + + + + + + + + + {% for object in object_list %} + + + + + + {% endfor %} + +
{% trans 'Titel' %}{% trans 'Absender' %}{% trans 'Datum' %}
+ {% bootstrap_icon 'download-alt' %} +   + {{ object.title }} + + {{ object.name }} ({{ object.email_address }}) + + {{ object.timestamp|date:'l, d. F Y H:i:s e' }} +
+ +
+{% endblock page-container %} diff --git a/dav_submission/urls.py b/dav_submission/urls.py index 3107b34..c6ebfd9 100644 --- a/dav_submission/urls.py +++ b/dav_submission/urls.py @@ -4,5 +4,7 @@ from . import views urlpatterns = [ url(r'^$', views.UploadView.as_view(), name='root'), - url(r'danke', views.SuccessView.as_view(), name='success'), + url(r'^danke', views.SuccessView.as_view(), name='success'), + url(r'^download/(?P.+)', views.DownloadView.as_view(), name='download'), + url(r'^download', views.ListView.as_view(), name='list'), ] diff --git a/dav_submission/views.py b/dav_submission/views.py index 9cef100..64de78e 100644 --- a/dav_submission/views.py +++ b/dav_submission/views.py @@ -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',