Add support for preserving the file extenstion when downloading a document
version. GitLab issue #415. Signed-off-by: Roberto Rosario <roberto.rosario.gonzalez@gmail.com>
This commit is contained in:
@@ -25,6 +25,8 @@
|
|||||||
- Convert document version view to item list view.
|
- Convert document version view to item list view.
|
||||||
- Add support for browsing individual document versions.
|
- Add support for browsing individual document versions.
|
||||||
- Add support for dropdown menus to the item list view template.
|
- Add support for dropdown menus to the item list view template.
|
||||||
|
- Add support for preserving the file extenstion when downloading a document
|
||||||
|
version. GitLab #415.
|
||||||
|
|
||||||
2.6.4 (2017-07-26)
|
2.6.4 (2017-07-26)
|
||||||
==================
|
==================
|
||||||
|
|||||||
@@ -233,6 +233,17 @@ class DocumentDownloadForm(forms.Form):
|
|||||||
self.fields['compressed'].widget.attrs.update({'disabled': True})
|
self.fields['compressed'].widget.attrs.update({'disabled': True})
|
||||||
|
|
||||||
|
|
||||||
|
class DocumentVersionDownloadForm(DocumentDownloadForm):
|
||||||
|
preserve_extension = forms.BooleanField(
|
||||||
|
label=_('Preserve extension'), required=False,
|
||||||
|
help_text=_(
|
||||||
|
'Takes the file extension and moves it to the end of the '
|
||||||
|
'filename allowing operating systems that rely on file '
|
||||||
|
'extensions to open the downloaded document version correctly.'
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class DocumentPrintForm(forms.Form):
|
class DocumentPrintForm(forms.Form):
|
||||||
page_group = forms.ChoiceField(
|
page_group = forms.ChoiceField(
|
||||||
choices=PAGE_RANGE_CHOICES, initial=PAGE_RANGE_ALL,
|
choices=PAGE_RANGE_CHOICES, initial=PAGE_RANGE_ALL,
|
||||||
|
|||||||
@@ -396,9 +396,7 @@ class DocumentVersion(models.Model):
|
|||||||
verbose_name_plural = _('Document version')
|
verbose_name_plural = _('Document version')
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return Template(
|
return self.get_rendered_string()
|
||||||
'{{ instance.document }} - {{ instance.timestamp }}'
|
|
||||||
).render(context=Context({'instance': self}))
|
|
||||||
|
|
||||||
def delete(self, *args, **kwargs):
|
def delete(self, *args, **kwargs):
|
||||||
for page in self.pages.all():
|
for page in self.pages.all():
|
||||||
@@ -411,6 +409,16 @@ class DocumentVersion(models.Model):
|
|||||||
def get_absolute_url(self):
|
def get_absolute_url(self):
|
||||||
return reverse('documents:document_version_view', args=(self.pk,))
|
return reverse('documents:document_version_view', args=(self.pk,))
|
||||||
|
|
||||||
|
def get_rendered_string(self):
|
||||||
|
return Template(
|
||||||
|
'{{ instance.document }} - {{ instance.timestamp }}'
|
||||||
|
).render(context=Context({'instance': self}))
|
||||||
|
|
||||||
|
def get_rendered_timestamp(self):
|
||||||
|
return Template('{{ instance.timestamp }}').render(
|
||||||
|
context=Context({'instance': self})
|
||||||
|
)
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
"""
|
"""
|
||||||
Overloaded save method that updates the document version's checksum,
|
Overloaded save method that updates the document version's checksum,
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
from __future__ import absolute_import, unicode_literals
|
from __future__ import absolute_import, unicode_literals
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
import os
|
||||||
|
|
||||||
from django.contrib import messages
|
from django.contrib import messages
|
||||||
from django.shortcuts import get_object_or_404
|
from django.shortcuts import get_object_or_404
|
||||||
|
from django.utils.encoding import force_text
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
|
||||||
from acls.models import AccessControlList
|
from acls.models import AccessControlList
|
||||||
@@ -12,7 +14,7 @@ from common.generics import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
from ..events import event_document_view
|
from ..events import event_document_view
|
||||||
from ..forms import DocumentVersionPreviewForm
|
from ..forms import DocumentVersionDownloadForm, DocumentVersionPreviewForm
|
||||||
from ..models import Document, DocumentVersion
|
from ..models import Document, DocumentVersion
|
||||||
from ..permissions import (
|
from ..permissions import (
|
||||||
permission_document_download, permission_document_version_revert,
|
permission_document_download, permission_document_version_revert,
|
||||||
@@ -81,10 +83,23 @@ class DocumentVersionRevertView(ConfirmView):
|
|||||||
|
|
||||||
|
|
||||||
class DocumentVersionDownloadFormView(DocumentDownloadFormView):
|
class DocumentVersionDownloadFormView(DocumentDownloadFormView):
|
||||||
|
form_class = DocumentVersionDownloadForm
|
||||||
model = DocumentVersion
|
model = DocumentVersion
|
||||||
multiple_download_view = None
|
multiple_download_view = None
|
||||||
|
querystring_form_fields = (
|
||||||
|
'compressed', 'zip_filename', 'preserve_extension'
|
||||||
|
)
|
||||||
single_download_view = 'documents:document_version_download'
|
single_download_view = 'documents:document_version_download'
|
||||||
|
|
||||||
|
def get_extra_context(self):
|
||||||
|
result = super(
|
||||||
|
DocumentVersionDownloadFormView, self
|
||||||
|
).get_extra_context()
|
||||||
|
|
||||||
|
result['title'] = _('Download document version')
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
def get_document_queryset(self):
|
def get_document_queryset(self):
|
||||||
id_list = self.request.GET.get(
|
id_list = self.request.GET.get(
|
||||||
'id_list', self.request.POST.get('id_list', '')
|
'id_list', self.request.POST.get('id_list', '')
|
||||||
@@ -102,6 +117,25 @@ class DocumentVersionDownloadView(DocumentDownloadView):
|
|||||||
model = DocumentVersion
|
model = DocumentVersion
|
||||||
object_permission = permission_document_download
|
object_permission = permission_document_download
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_item_file(item):
|
||||||
|
return item.file
|
||||||
|
|
||||||
|
def get_item_label(self, item):
|
||||||
|
preserve_extension = self.request.GET.get(
|
||||||
|
'preserve_extension', self.request.POST.get(
|
||||||
|
'preserve_extension', False
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
if preserve_extension:
|
||||||
|
filename, extension = os.path.splitext(item.document.label)
|
||||||
|
return '{} ({}){}'.format(
|
||||||
|
filename, item.get_rendered_timestamp(), extension
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
return force_text(item)
|
||||||
|
|
||||||
|
|
||||||
class DocumentVersionView(SingleObjectDetailView):
|
class DocumentVersionView(SingleObjectDetailView):
|
||||||
form_class = DocumentVersionPreviewForm
|
form_class = DocumentVersionPreviewForm
|
||||||
|
|||||||
@@ -402,6 +402,7 @@ class DocumentDownloadFormView(FormView):
|
|||||||
form_class = DocumentDownloadForm
|
form_class = DocumentDownloadForm
|
||||||
model = Document
|
model = Document
|
||||||
multiple_download_view = 'documents:document_multiple_download'
|
multiple_download_view = 'documents:document_multiple_download'
|
||||||
|
querystring_form_fields = ('compressed', 'zip_filename')
|
||||||
single_download_view = 'documents:document_download'
|
single_download_view = 'documents:document_download'
|
||||||
|
|
||||||
def get_document_queryset(self):
|
def get_document_queryset(self):
|
||||||
@@ -447,16 +448,23 @@ class DocumentDownloadFormView(FormView):
|
|||||||
return kwargs
|
return kwargs
|
||||||
|
|
||||||
def form_valid(self, form):
|
def form_valid(self, form):
|
||||||
querystring = urlencode(
|
querystring_dictionary = {}
|
||||||
|
|
||||||
|
for field in self.querystring_form_fields:
|
||||||
|
data = form.cleaned_data[field]
|
||||||
|
if data:
|
||||||
|
querystring_dictionary[field] = data
|
||||||
|
|
||||||
|
querystring_dictionary.update(
|
||||||
{
|
{
|
||||||
'compressed': form.cleaned_data['compressed'],
|
|
||||||
'zip_filename': form.cleaned_data['zip_filename'],
|
|
||||||
'id_list': ','.join(
|
'id_list': ','.join(
|
||||||
map(str, self.queryset.values_list('pk', flat=True))
|
map(str, self.queryset.values_list('pk', flat=True))
|
||||||
)
|
)
|
||||||
}, doseq=True
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
querystring = urlencode(querystring_dictionary, doseq=True)
|
||||||
|
|
||||||
if self.queryset.count() > 1:
|
if self.queryset.count() > 1:
|
||||||
url = reverse(self.multiple_download_view)
|
url = reverse(self.multiple_download_view)
|
||||||
else:
|
else:
|
||||||
@@ -497,17 +505,7 @@ class DocumentDownloadView(SingleObjectDownloadView):
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_item_file(item):
|
def get_item_file(item):
|
||||||
if isinstance(item, Document):
|
return item.open()
|
||||||
return item.open()
|
|
||||||
else:
|
|
||||||
return item.file
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def get_item_label(item):
|
|
||||||
if isinstance(item, Document):
|
|
||||||
return item.label
|
|
||||||
else:
|
|
||||||
return force_text(item)
|
|
||||||
|
|
||||||
def get_document_queryset(self):
|
def get_document_queryset(self):
|
||||||
id_list = self.request.GET.get(
|
id_list = self.request.GET.get(
|
||||||
@@ -532,10 +530,9 @@ class DocumentDownloadView(SingleObjectDownloadView):
|
|||||||
if self.request.GET.get('compressed') == 'True' or queryset.count() > 1:
|
if self.request.GET.get('compressed') == 'True' or queryset.count() > 1:
|
||||||
compressed_file = CompressedFile()
|
compressed_file = CompressedFile()
|
||||||
for item in queryset:
|
for item in queryset:
|
||||||
descriptor = item.open()
|
descriptor = DocumentDownloadView.get_item_file(item=item)
|
||||||
compressed_file.add_file(
|
compressed_file.add_file(
|
||||||
descriptor,
|
descriptor, arcname=self.get_item_label(item=item)
|
||||||
arcname=DocumentDownloadView.get_item_label(item=item)
|
|
||||||
)
|
)
|
||||||
descriptor.close()
|
descriptor.close()
|
||||||
DocumentDownloadView.commit_event(
|
DocumentDownloadView.commit_event(
|
||||||
@@ -559,11 +556,12 @@ class DocumentDownloadView(SingleObjectDownloadView):
|
|||||||
|
|
||||||
return DocumentDownloadView.VirtualFile(
|
return DocumentDownloadView.VirtualFile(
|
||||||
DocumentDownloadView.get_item_file(item=item),
|
DocumentDownloadView.get_item_file(item=item),
|
||||||
name=DocumentDownloadView.get_item_label(
|
name=self.get_item_label(item=item)
|
||||||
item=item
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def get_item_label(self, item):
|
||||||
|
return item.label
|
||||||
|
|
||||||
|
|
||||||
class DocumentUpdatePageCountView(MultipleObjectConfirmActionView):
|
class DocumentUpdatePageCountView(MultipleObjectConfirmActionView):
|
||||||
model = Document
|
model = Document
|
||||||
|
|||||||
Reference in New Issue
Block a user