dav_submission: added first stuff for downloading submissions.
This commit is contained in:
67
dav_submission/templates/dav_submission/list.html
Normal file
67
dav_submission/templates/dav_submission/list.html
Normal file
@@ -0,0 +1,67 @@
|
||||
{% extends 'dav_submission/base.html' %}
|
||||
{% load i18n %}
|
||||
{% load bootstrap3 %}
|
||||
|
||||
{% block page-container %}
|
||||
<h3 class="top-most">Einreichungen</h3>
|
||||
<div>
|
||||
<table id="objects_table" class="table table-striped">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>{% trans 'Titel' %}</th>
|
||||
<th>{% trans 'Absender' %}</th>
|
||||
<th>{% trans 'Datum' %}</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<th><input type="text" placeholder="{% trans 'Filter' %}" /></th>
|
||||
<th><input type="text" placeholder="{% trans 'Filter' %}" /></th>
|
||||
<th><input type="text" placeholder="{% trans 'Filter' %}" /></th>
|
||||
</tr>
|
||||
<tbody>
|
||||
{% for object in object_list %}
|
||||
<tr>
|
||||
<td>
|
||||
<a href="{% url 'dav_submission:download' object.pk %}"
|
||||
class="btn btn-xs btn-primary"
|
||||
title="Download">{% bootstrap_icon 'download-alt' %}</a>
|
||||
|
||||
{{ object.title }}
|
||||
</td>
|
||||
<td>
|
||||
{{ object.name }} (<a href="mailto:{{ object.email_address }}">{{ object.email_address }}</a>)
|
||||
</td>
|
||||
<td data-order="{{ object.timestamp|date:'U' }}">
|
||||
{{ object.timestamp|date:'l, d. F Y H:i:s e' }}
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
<script type="text/javascript">
|
||||
$(document).ready( function () {
|
||||
var table = $("#objects_table").DataTable( {
|
||||
orderCellsTop: true,
|
||||
order: [[2, "asc"]],
|
||||
paging: false,
|
||||
language: {
|
||||
search: "{% trans 'Filter' %}:",
|
||||
info: "{% trans 'Zeige _TOTAL_ Einträge' %}",
|
||||
infoFiltered: "{% trans 'aus insgesamt _MAX_ Einträgen' %}",
|
||||
infoEmpty: "{% trans 'Zeige 0 Einträge' %}",
|
||||
infoPostFix: ".",
|
||||
emptyTable: "{% trans 'Keine Daten vorhanden.' %}",
|
||||
zeroRecords: "{% trans 'Keine passenden Einträge.' %}",
|
||||
|
||||
}
|
||||
} );
|
||||
$("#objects_table thead input").on( "keyup change", function() {
|
||||
table
|
||||
.column( $(this).parent().index() )
|
||||
.search( this.value )
|
||||
.draw();
|
||||
} );
|
||||
$("#objects_table_filter").hide();
|
||||
} );
|
||||
</script>
|
||||
</div>
|
||||
{% endblock page-container %}
|
||||
@@ -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<pk>.+)', views.DownloadView.as_view(), name='download'),
|
||||
url(r'^download', views.ListView.as_view(), name='list'),
|
||||
]
|
||||
|
||||
@@ -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