Merge remote-tracking branch 'origin/master' into development
This commit is contained in:
@@ -4,6 +4,7 @@ import logging
|
||||
|
||||
from django.http import HttpResponse
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.utils.encoding import force_text
|
||||
|
||||
from django_downloadview import DownloadMixin, VirtualFile
|
||||
from rest_framework import generics, status
|
||||
@@ -120,16 +121,18 @@ class APIDocumentDownloadView(DownloadMixin, generics.RetrieveAPIView):
|
||||
|
||||
|
||||
class APIDocumentListView(generics.ListCreateAPIView):
|
||||
"""
|
||||
Returns a list of all the documents.
|
||||
"""
|
||||
|
||||
filter_backends = (MayanObjectPermissionsFilter,)
|
||||
mayan_object_permissions = {'GET': (permission_document_view,)}
|
||||
mayan_view_permissions = {'POST': (permission_document_create,)}
|
||||
permission_classes = (MayanPermission,)
|
||||
queryset = Document.objects.all()
|
||||
|
||||
def get(self, *args, **kwargs):
|
||||
"""
|
||||
Returns a list of all the documents.
|
||||
"""
|
||||
return super(APIDocumentListView, self).get(*args, **kwargs)
|
||||
|
||||
def get_serializer_class(self):
|
||||
if self.request.method == 'GET':
|
||||
return DocumentSerializer
|
||||
@@ -142,8 +145,34 @@ class APIDocumentListView(generics.ListCreateAPIView):
|
||||
def post(self, *args, **kwargs):
|
||||
"""
|
||||
Create a new document.
|
||||
Endpoint returns a 202 status code to indicate that a document is not
|
||||
immediately created at request. From the request data, the creation of
|
||||
a document is instead queued as a background task. An ID that
|
||||
represents the eventual document is returned.
|
||||
---
|
||||
omit_serializer: false
|
||||
parameters:
|
||||
- name: description
|
||||
paramType: form
|
||||
type: file string
|
||||
- name: document_type
|
||||
paramType: form
|
||||
required: true
|
||||
type: file string
|
||||
- name: file
|
||||
paramType: form
|
||||
required: true
|
||||
type: file object
|
||||
- name: label
|
||||
paramType: form
|
||||
type: file string
|
||||
- name: language
|
||||
paramType: form
|
||||
type: file string
|
||||
responseMessages:
|
||||
- code: 202
|
||||
message: Accepted
|
||||
"""
|
||||
|
||||
return super(APIDocumentListView, self).post(*args, **kwargs)
|
||||
|
||||
|
||||
@@ -170,7 +199,7 @@ class APIDocumentVersionDownloadView(DownloadMixin, generics.RetrieveAPIView):
|
||||
|
||||
def get_file(self):
|
||||
instance = self.get_object()
|
||||
return VirtualFile(instance.file, name=unicode(instance))
|
||||
return VirtualFile(instance.file, name=force_text(instance))
|
||||
|
||||
def get_serializer_class(self):
|
||||
return None
|
||||
|
||||
@@ -70,6 +70,7 @@ from .permissions import (
|
||||
permission_document_trash, permission_document_version_revert,
|
||||
permission_document_view
|
||||
)
|
||||
from .queues import * # NOQA
|
||||
# Just import to initialize the search models
|
||||
from .search import document_search, document_page_search # NOQA
|
||||
from .statistics import (
|
||||
|
||||
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Binary file not shown.
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,23 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.10.7 on 2017-07-05 19:42
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('documents', '0037_auto_20161231_0617'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='documentversion',
|
||||
name='checksum',
|
||||
field=models.TextField(
|
||||
blank=True, db_index=True, editable=False, null=True,
|
||||
verbose_name='Checksum'
|
||||
),
|
||||
),
|
||||
]
|
||||
@@ -8,7 +8,7 @@ from django.conf import settings
|
||||
from django.core.files import File
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.db import models, transaction
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
from django.utils.encoding import force_text, python_2_unicode_compatible
|
||||
from django.utils.timezone import now
|
||||
from django.utils.translation import ugettext, ugettext_lazy as _
|
||||
|
||||
@@ -52,7 +52,7 @@ def HASH_FUNCTION(data):
|
||||
|
||||
|
||||
def UUID_FUNCTION(*args, **kwargs):
|
||||
return unicode(uuid.uuid4())
|
||||
return force_text(uuid.uuid4())
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
@@ -237,7 +237,11 @@ class Document(models.Model):
|
||||
Returns a boolean value that indicates if the document's
|
||||
latest version file exists in storage
|
||||
"""
|
||||
return self.latest_version.exists()
|
||||
latest_version = self.latest_version
|
||||
if latest_version:
|
||||
return latest_version.exists()
|
||||
else:
|
||||
return False
|
||||
|
||||
def invalidate_cache(self):
|
||||
for document_version in self.versions.all():
|
||||
@@ -379,7 +383,8 @@ class DocumentVersion(models.Model):
|
||||
blank=True, editable=False, max_length=64, null=True
|
||||
)
|
||||
checksum = models.TextField(
|
||||
blank=True, editable=False, null=True, verbose_name=_('Checksum')
|
||||
blank=True, db_index=True, editable=False, null=True,
|
||||
verbose_name=_('Checksum')
|
||||
)
|
||||
|
||||
def __str__(self):
|
||||
@@ -420,6 +425,7 @@ class DocumentVersion(models.Model):
|
||||
self.update_mimetype(save=False)
|
||||
self.save()
|
||||
self.update_page_count(save=False)
|
||||
self.fix_orientation()
|
||||
|
||||
logger.info(
|
||||
'New document version "%s" created for document: %s',
|
||||
@@ -428,7 +434,7 @@ class DocumentVersion(models.Model):
|
||||
|
||||
self.document.is_stub = False
|
||||
if not self.document.label:
|
||||
self.document.label = unicode(self.file)
|
||||
self.document.label = force_text(self.file)
|
||||
|
||||
self.document.save()
|
||||
except Exception as exception:
|
||||
@@ -466,6 +472,15 @@ class DocumentVersion(models.Model):
|
||||
"""
|
||||
return self.file.storage.exists(self.file.name)
|
||||
|
||||
def fix_orientation(self):
|
||||
for page in self.pages.all():
|
||||
degrees = page.detect_orientation()
|
||||
if degrees:
|
||||
Transformation.objects.add_for_model(
|
||||
obj=page, transformation=TransformationRotate,
|
||||
arguments='{{"degrees": {}}}'.format(360 - degrees)
|
||||
)
|
||||
|
||||
def get_intermidiate_file(self):
|
||||
cache_filename = self.cache_filename
|
||||
logger.debug('Intermidiate filename: %s', cache_filename)
|
||||
@@ -570,7 +585,7 @@ class DocumentVersion(models.Model):
|
||||
"""
|
||||
if self.exists():
|
||||
source = self.open()
|
||||
self.checksum = unicode(HASH_FUNCTION(source.read()))
|
||||
self.checksum = force_text(HASH_FUNCTION(source.read()))
|
||||
source.close()
|
||||
if save:
|
||||
self.save()
|
||||
@@ -668,7 +683,7 @@ class DocumentPage(models.Model):
|
||||
return _(
|
||||
'Page %(page_num)d out of %(total_pages)d of %(document)s'
|
||||
) % {
|
||||
'document': unicode(self.document),
|
||||
'document': force_text(self.document),
|
||||
'page_num': self.page_number,
|
||||
'total_pages': self.document_version.pages.count()
|
||||
}
|
||||
@@ -693,6 +708,16 @@ class DocumentPage(models.Model):
|
||||
def document(self):
|
||||
return self.document_version.document
|
||||
|
||||
def detect_orientation(self):
|
||||
with self.document_version.open() as file_object:
|
||||
converter = converter_class(
|
||||
file_object=file_object,
|
||||
mime_type=self.document_version.mimetype
|
||||
)
|
||||
return converter.detect_orientation(
|
||||
page_number=self.page_number
|
||||
)
|
||||
|
||||
def generate_image(self, *args, **kwargs):
|
||||
# Convert arguments into transformations
|
||||
transformations = kwargs.get('transformations', [])
|
||||
@@ -857,7 +882,7 @@ class RecentDocument(models.Model):
|
||||
objects = RecentDocumentManager()
|
||||
|
||||
def __str__(self):
|
||||
return unicode(self.document)
|
||||
return force_text(self.document)
|
||||
|
||||
def natural_key(self):
|
||||
return self.document.natural_key() + self.user.natural_key()
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
from __future__ import absolute_import, unicode_literals
|
||||
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from common.queues import queue_tools
|
||||
from task_manager.classes import CeleryQueue
|
||||
|
||||
queue_converter = CeleryQueue(
|
||||
name='converter', label=_('Converter'), transient=True
|
||||
)
|
||||
queue_documents_periodic = CeleryQueue(
|
||||
name='documents_periodic', label=_('Documents periodic'), transient=True
|
||||
)
|
||||
queue_uploads = CeleryQueue(
|
||||
name='uploads', label=_('Uploads')
|
||||
)
|
||||
|
||||
queue_documents_periodic.add_task_type(
|
||||
name='documents.tasks.task_check_delete_periods',
|
||||
label=_('Check document type delete periods')
|
||||
)
|
||||
queue_documents_periodic.add_task_type(
|
||||
name='documents.tasks.task_check_trash_periods',
|
||||
label=_('Check document type trash periods')
|
||||
)
|
||||
queue_documents_periodic.add_task_type(
|
||||
name='documents.tasks.task_delete_stubs',
|
||||
label=_('Delete document stubs')
|
||||
)
|
||||
|
||||
queue_tools.add_task_type(
|
||||
name='documents.tasks.task_clear_image_cache',
|
||||
label=_('Clear image cache')
|
||||
)
|
||||
|
||||
queue_converter.add_task_type(
|
||||
name='documents.tasks.task_generate_document_page_image',
|
||||
label=_('Generate document page image')
|
||||
)
|
||||
|
||||
queue_uploads.add_task_type(
|
||||
name='documents.tasks.task_update_page_count',
|
||||
label=_('Update document page count')
|
||||
)
|
||||
queue_uploads.add_task_type(
|
||||
name='documents.tasks.task_upload_new_version',
|
||||
label=_('Upload new document version')
|
||||
)
|
||||
@@ -20,6 +20,9 @@ document_search.add_model_field(
|
||||
)
|
||||
document_search.add_model_field(field='label', label=_('Label'))
|
||||
document_search.add_model_field(field='description', label=_('Description'))
|
||||
document_search.add_model_field(
|
||||
field='versions__checksum', label=_('Checksum')
|
||||
)
|
||||
|
||||
document_page_search = SearchModel(
|
||||
app_label='documents', model_name='DocumentPageResult',
|
||||
@@ -41,3 +44,6 @@ document_page_search.add_model_field(
|
||||
document_page_search.add_model_field(
|
||||
field='document_version__document__description', label=_('Description')
|
||||
)
|
||||
document_page_search.add_model_field(
|
||||
field='document_version__checksum', label=_('Checksum')
|
||||
)
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.utils.encoding import force_text
|
||||
|
||||
from rest_framework import serializers
|
||||
from rest_framework.reverse import reverse
|
||||
|
||||
@@ -281,7 +283,7 @@ class NewDocumentSerializer(serializers.ModelSerializer):
|
||||
description=self.validated_data.get('description', ''),
|
||||
document_type=self.validated_data['document_type'],
|
||||
label=self.validated_data.get(
|
||||
'label', unicode(self.validated_data['file'])
|
||||
'label', force_text(self.validated_data['file'])
|
||||
),
|
||||
language=self.validated_data.get(
|
||||
'language', setting_language.value
|
||||
|
||||
@@ -4,8 +4,7 @@ from __future__ import unicode_literals
|
||||
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.test import override_settings
|
||||
|
||||
from django_downloadview import assert_download_response
|
||||
from django.utils.encoding import force_text
|
||||
|
||||
from common.tests.test_views import GenericViewTestCase
|
||||
from converter.models import Transformation
|
||||
@@ -47,7 +46,7 @@ class GenericDocumentViewTestCase(GenericViewTestCase):
|
||||
|
||||
with open(TEST_SMALL_DOCUMENT_PATH) as file_object:
|
||||
self.document = self.document_type.new_document(
|
||||
file_object=file_object
|
||||
file_object=file_object, label=TEST_SMALL_DOCUMENT_FILENAME
|
||||
)
|
||||
|
||||
def tearDown(self):
|
||||
@@ -210,8 +209,8 @@ class DocumentsViewsTestCase(GenericDocumentViewTestCase):
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
with self.document.open() as file_object:
|
||||
assert_download_response(
|
||||
self, response, content=file_object.read(),
|
||||
self.assert_download_response(
|
||||
response, content=file_object.read(),
|
||||
basename=TEST_SMALL_DOCUMENT_FILENAME,
|
||||
mime_type=self.document.file_mimetype
|
||||
)
|
||||
@@ -240,8 +239,8 @@ class DocumentsViewsTestCase(GenericDocumentViewTestCase):
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
with self.document.open() as file_object:
|
||||
assert_download_response(
|
||||
self, response, content=file_object.read(),
|
||||
self.assert_download_response(
|
||||
response, content=file_object.read(),
|
||||
basename=TEST_SMALL_DOCUMENT_FILENAME,
|
||||
mime_type=self.document.file_mimetype
|
||||
)
|
||||
@@ -270,8 +269,8 @@ class DocumentsViewsTestCase(GenericDocumentViewTestCase):
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
with self.document.open() as file_object:
|
||||
assert_download_response(
|
||||
self, response, content=file_object.read(),
|
||||
self.assert_download_response(
|
||||
response, content=file_object.read(),
|
||||
basename='{} - {}'.format(
|
||||
TEST_SMALL_DOCUMENT_FILENAME,
|
||||
self.document.latest_version.timestamp
|
||||
@@ -453,7 +452,7 @@ class DocumentsViewsTestCase(GenericDocumentViewTestCase):
|
||||
)
|
||||
|
||||
self.assertContains(
|
||||
response, unicode(self.document.pages.first()), status_code=200
|
||||
response, force_text(self.document.pages.first()), status_code=200
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from ..widgets import DocumentThumbnailWidget
|
||||
|
||||
from .test_models import GenericDocumentTestCase
|
||||
|
||||
|
||||
class DocumentWidgetTestCase(GenericDocumentTestCase):
|
||||
def test_document_list_view_document_with_no_pages(self):
|
||||
document_thumbnail_widget = DocumentThumbnailWidget()
|
||||
self.document.pages.all().delete()
|
||||
result = document_thumbnail_widget.render(instance=self.document)
|
||||
|
||||
self.assertTrue(self.document.get_absolute_url() in result)
|
||||
@@ -7,6 +7,7 @@ from django.core.exceptions import PermissionDenied
|
||||
from django.core.urlresolvers import reverse, reverse_lazy
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.utils.encoding import force_text
|
||||
from django.utils.http import urlencode
|
||||
from django.utils.translation import ugettext_lazy as _, ungettext
|
||||
|
||||
@@ -454,7 +455,7 @@ class DocumentDownloadView(SingleObjectDownloadView):
|
||||
if isinstance(item, Document):
|
||||
return item.label
|
||||
else:
|
||||
return unicode(item)
|
||||
return force_text(item)
|
||||
|
||||
def get_document_queryset(self):
|
||||
id_list = self.request.GET.get(
|
||||
|
||||
@@ -2,6 +2,7 @@ from __future__ import unicode_literals
|
||||
|
||||
from django import forms
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.utils.encoding import force_text
|
||||
from django.utils.html import strip_tags
|
||||
from django.utils.http import urlencode
|
||||
from django.utils.safestring import mark_safe
|
||||
@@ -271,16 +272,34 @@ class DocumentThumbnailWidget(BaseDocumentThumbnailWidget):
|
||||
return {
|
||||
'pk': instance.pk,
|
||||
'version_pk': instance.latest_version.pk,
|
||||
'page_pk': instance.latest_version.pages.first().pk
|
||||
'page_pk': instance.pages.first().pk
|
||||
}
|
||||
|
||||
def get_click_view_url(self, instance):
|
||||
first_page = instance.pages.first()
|
||||
if first_page:
|
||||
return super(DocumentThumbnailWidget, self).get_click_view_url(
|
||||
instance=instance
|
||||
)
|
||||
else:
|
||||
return '#'
|
||||
|
||||
def get_preview_view_kwargs(self, instance):
|
||||
return {
|
||||
'pk': instance.pk,
|
||||
'version_pk': instance.latest_version.pk,
|
||||
'page_pk': instance.latest_version.pages.first().pk
|
||||
'page_pk': instance.pages.first().pk
|
||||
}
|
||||
|
||||
def get_preview_view_url(self, instance):
|
||||
first_page = instance.pages.first()
|
||||
if first_page:
|
||||
return super(DocumentThumbnailWidget, self).get_preview_view_url(
|
||||
instance=instance
|
||||
)
|
||||
else:
|
||||
return ''
|
||||
|
||||
def get_title(self, instance):
|
||||
return getattr(instance, 'label', None)
|
||||
|
||||
@@ -290,7 +309,7 @@ class DocumentThumbnailWidget(BaseDocumentThumbnailWidget):
|
||||
|
||||
class DocumentPageThumbnailWidget(BaseDocumentThumbnailWidget):
|
||||
def get_title(self, instance):
|
||||
return unicode(instance)
|
||||
return force_text(instance)
|
||||
|
||||
|
||||
class InteractiveDocumentPageWidget(BaseDocumentThumbnailWidget):
|
||||
|
||||
Reference in New Issue
Block a user