PEP8 cleanups.
This commit is contained in:
@@ -162,9 +162,9 @@ class APIDocumentVersionCreateView(generics.CreateAPIView):
|
||||
)
|
||||
|
||||
if serializer.is_valid():
|
||||
# Nested resource we take the document pk from the URL and insert it
|
||||
# so that it needs not to be specified by the user, we mark it as
|
||||
# a read only field in the serializer
|
||||
# Nested resource we take the document pk from the URL and insert
|
||||
# it so that it needs not to be specified by the user, we mark
|
||||
# it as a read only field in the serializer
|
||||
document = get_object_or_404(Document, pk=kwargs['pk'])
|
||||
|
||||
document.new_version(
|
||||
@@ -361,7 +361,8 @@ class APIDocumentTypeDocumentListView(generics.ListAPIView):
|
||||
)
|
||||
except PermissionDenied:
|
||||
AccessControlList.objects.check_access(
|
||||
permission_document_type_view, self.request.user, document_type
|
||||
permission_document_type_view, self.request.user,
|
||||
document_type
|
||||
)
|
||||
|
||||
return document_type.documents.all()
|
||||
|
||||
@@ -84,8 +84,10 @@ class DocumentsApp(MayanAppConfig):
|
||||
|
||||
MissingItem(
|
||||
label=_('Create a document type'),
|
||||
description=_('Every uploaded document must be assigned a document type, it is the basic way Mayan EDMS categorizes documents.'),
|
||||
condition=lambda: not DocumentType.objects.exists(),
|
||||
description=_(
|
||||
'Every uploaded document must be assigned a document type, '
|
||||
'it is the basic way Mayan EDMS categorizes documents.'
|
||||
), condition=lambda: not DocumentType.objects.exists(),
|
||||
view='documents:document_type_list'
|
||||
)
|
||||
|
||||
|
||||
@@ -42,7 +42,9 @@ class DocumentPreviewForm(forms.Form):
|
||||
super(DocumentPreviewForm, self).__init__(*args, **kwargs)
|
||||
self.fields['preview'].initial = document
|
||||
try:
|
||||
self.fields['preview'].label = _('Document pages (%d)') % document.page_count
|
||||
self.fields['preview'].label = _(
|
||||
'Document pages (%d)'
|
||||
) % document.page_count
|
||||
except AttributeError:
|
||||
self.fields['preview'].label = _('Document pages (%d)') % 0
|
||||
|
||||
@@ -62,13 +64,16 @@ class DocumentForm(forms.ModelForm):
|
||||
|
||||
super(DocumentForm, self).__init__(*args, **kwargs)
|
||||
|
||||
# Is a document (documents app edit) and has been saved (sources app upload)?
|
||||
# Is a document (documents app edit) and has been saved (sources
|
||||
# app upload)?
|
||||
if self.instance and self.instance.pk:
|
||||
document_type = self.instance.document_type
|
||||
|
||||
filenames_qs = document_type.filenames.filter(enabled=True)
|
||||
if filenames_qs.count():
|
||||
self.fields['document_type_available_filenames'] = forms.ModelChoiceField(
|
||||
self.fields[
|
||||
'document_type_available_filenames'
|
||||
] = forms.ModelChoiceField(
|
||||
queryset=filenames_qs,
|
||||
required=False,
|
||||
label=_('Quick document rename')
|
||||
@@ -119,12 +124,20 @@ class DocumentTypeFilenameForm_create(forms.ModelForm):
|
||||
class DocumentDownloadForm(forms.Form):
|
||||
compressed = forms.BooleanField(
|
||||
label=_('Compress'), required=False,
|
||||
help_text=_('Download the document in the original format or in a compressed manner. This option is selectable only when downloading one document, for multiple documents, the bundle will always be downloads as a compressed file.')
|
||||
help_text=_(
|
||||
'Download the document in the original format or in a compressed '
|
||||
'manner. This option is selectable only when downloading one '
|
||||
'document, for multiple documents, the bundle will always be '
|
||||
'downloads as a compressed file.'
|
||||
)
|
||||
)
|
||||
zip_filename = forms.CharField(
|
||||
initial=DEFAULT_ZIP_FILENAME, label=_('Compressed filename'),
|
||||
required=False,
|
||||
help_text=_('The filename of the compressed file that will contain the documents to be downloaded, if the previous option is selected.')
|
||||
help_text=_(
|
||||
'The filename of the compressed file that will contain the '
|
||||
'documents to be downloaded, if the previous option is selected.'
|
||||
)
|
||||
)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
|
||||
+209
-47
@@ -38,67 +38,229 @@ def is_min_zoom(context):
|
||||
|
||||
|
||||
# Facet
|
||||
link_document_preview = Link(permissions=[permission_document_view], text=_('Preview'), view='documents:document_preview', args='object.id')
|
||||
link_document_properties = Link(permissions=[permission_document_view], text=_('Properties'), view='documents:document_properties', args='object.id')
|
||||
link_document_version_list = Link(permissions=[permission_document_view], text=_('Versions'), view='documents:document_version_list', args='object.pk')
|
||||
link_document_pages = Link(permissions=[permission_document_view], text=_('Pages'), view='documents:document_pages', args='resolved_object.pk')
|
||||
link_document_preview = Link(
|
||||
permissions=[permission_document_view], text=_('Preview'),
|
||||
view='documents:document_preview', args='object.id'
|
||||
)
|
||||
link_document_properties = Link(
|
||||
permissions=[permission_document_view], text=_('Properties'),
|
||||
view='documents:document_properties', args='object.id'
|
||||
)
|
||||
link_document_version_list = Link(
|
||||
permissions=[permission_document_view], text=_('Versions'),
|
||||
view='documents:document_version_list', args='object.pk'
|
||||
)
|
||||
link_document_pages = Link(
|
||||
permissions=[permission_document_view], text=_('Pages'),
|
||||
view='documents:document_pages', args='resolved_object.pk'
|
||||
)
|
||||
|
||||
# Actions
|
||||
link_document_clear_transformations = Link(permissions=[permission_transformation_delete], text=_('Clear transformations'), view='documents:document_clear_transformations', args='object.id')
|
||||
link_document_delete = Link(permissions=[permission_document_delete], tags='dangerous', text=_('Delete'), view='documents:document_delete', args='object.id')
|
||||
link_document_trash = Link(permissions=[permission_document_trash], tags='dangerous', text=_('Move to trash'), view='documents:document_trash', args='object.id')
|
||||
link_document_edit = Link(permissions=[permission_document_properties_edit], text=_('Edit properties'), view='documents:document_edit', args='object.id')
|
||||
link_document_document_type_edit = Link(permissions=[permission_document_properties_edit], text=_('Change type'), view='documents:document_document_type_edit', args='object.id')
|
||||
link_document_download = Link(permissions=[permission_document_download], text=_('Download'), view='documents:document_download', args='object.id')
|
||||
link_document_print = Link(permissions=[permission_document_print], text=_('Print'), view='documents:document_print', args='object.id')
|
||||
link_document_update_page_count = Link(permissions=[permission_document_tools], text=_('Reset page count'), view='documents:document_update_page_count', args='object.pk')
|
||||
link_document_restore = Link(permissions=[permission_document_restore], text=_('Restore'), view='documents:document_restore', args='object.pk')
|
||||
link_document_multiple_clear_transformations = Link(permissions=[permission_transformation_delete], text=_('Clear transformations'), view='documents:document_multiple_clear_transformations')
|
||||
link_document_multiple_trash = Link(permissions=[permission_document_trash], tags='dangerous', text=_('Move to trash'), view='documents:document_multiple_trash')
|
||||
link_document_multiple_delete = Link(permissions=[permission_document_delete], tags='dangerous', text=_('Delete'), view='documents:document_multiple_delete')
|
||||
link_document_multiple_document_type_edit = Link(permissions=[permission_document_properties_edit], text=_('Change type'), view='documents:document_multiple_document_type_edit')
|
||||
link_document_multiple_download = Link(permissions=[permission_document_download], text=_('Download'), view='documents:document_multiple_download')
|
||||
link_document_multiple_update_page_count = Link(permissions=[permission_document_tools], text=_('Reset page count'), view='documents:document_multiple_update_page_count')
|
||||
link_document_multiple_restore = Link(permissions=[permission_document_restore], text=_('Restore'), view='documents:document_multiple_restore')
|
||||
link_document_version_download = Link(args='object.pk', permissions=[permission_document_download], text=_('Download'), view='documents:document_version_download')
|
||||
link_document_clear_transformations = Link(
|
||||
permissions=[permission_transformation_delete],
|
||||
text=_('Clear transformations'),
|
||||
view='documents:document_clear_transformations', args='object.id'
|
||||
)
|
||||
link_document_delete = Link(
|
||||
permissions=[permission_document_delete], tags='dangerous',
|
||||
text=_('Delete'), view='documents:document_delete', args='object.id'
|
||||
)
|
||||
link_document_trash = Link(
|
||||
permissions=[permission_document_trash], tags='dangerous',
|
||||
text=_('Move to trash'), view='documents:document_trash', args='object.id'
|
||||
)
|
||||
link_document_edit = Link(
|
||||
permissions=[permission_document_properties_edit],
|
||||
text=_('Edit properties'), view='documents:document_edit',
|
||||
args='object.id'
|
||||
)
|
||||
link_document_document_type_edit = Link(
|
||||
permissions=[permission_document_properties_edit], text=_('Change type'),
|
||||
view='documents:document_document_type_edit', args='object.id'
|
||||
)
|
||||
link_document_download = Link(
|
||||
permissions=[permission_document_download], text=_('Download'),
|
||||
view='documents:document_download', args='object.id'
|
||||
)
|
||||
link_document_print = Link(
|
||||
permissions=[permission_document_print], text=_('Print'),
|
||||
view='documents:document_print', args='object.id'
|
||||
)
|
||||
link_document_update_page_count = Link(
|
||||
permissions=[permission_document_tools], text=_('Reset page count'),
|
||||
view='documents:document_update_page_count', args='object.pk'
|
||||
)
|
||||
link_document_restore = Link(
|
||||
permissions=[permission_document_restore], text=_('Restore'),
|
||||
view='documents:document_restore', args='object.pk'
|
||||
)
|
||||
link_document_multiple_clear_transformations = Link(
|
||||
permissions=[permission_transformation_delete],
|
||||
text=_('Clear transformations'),
|
||||
view='documents:document_multiple_clear_transformations'
|
||||
)
|
||||
link_document_multiple_trash = Link(
|
||||
permissions=[permission_document_trash], tags='dangerous',
|
||||
text=_('Move to trash'), view='documents:document_multiple_trash'
|
||||
)
|
||||
link_document_multiple_delete = Link(
|
||||
permissions=[permission_document_delete], tags='dangerous',
|
||||
text=_('Delete'), view='documents:document_multiple_delete'
|
||||
)
|
||||
link_document_multiple_document_type_edit = Link(
|
||||
permissions=[permission_document_properties_edit], text=_('Change type'),
|
||||
view='documents:document_multiple_document_type_edit'
|
||||
)
|
||||
link_document_multiple_download = Link(
|
||||
permissions=[permission_document_download], text=_('Download'),
|
||||
view='documents:document_multiple_download'
|
||||
)
|
||||
link_document_multiple_update_page_count = Link(
|
||||
permissions=[permission_document_tools], text=_('Reset page count'),
|
||||
view='documents:document_multiple_update_page_count'
|
||||
)
|
||||
link_document_multiple_restore = Link(
|
||||
permissions=[permission_document_restore], text=_('Restore'),
|
||||
view='documents:document_multiple_restore'
|
||||
)
|
||||
link_document_version_download = Link(
|
||||
args='object.pk', permissions=[permission_document_download],
|
||||
text=_('Download'), view='documents:document_version_download'
|
||||
)
|
||||
|
||||
# Views
|
||||
link_document_list = Link(icon='fa fa-file', text=_('All documents'), view='documents:document_list')
|
||||
link_document_list_recent = Link(icon='fa fa-clock-o', text=_('Recent documents'), view='documents:document_list_recent')
|
||||
link_document_list_deleted = Link(icon='fa fa-trash', text=_('Trash'), view='documents:document_list_deleted')
|
||||
link_document_list = Link(
|
||||
icon='fa fa-file', text=_('All documents'), view='documents:document_list'
|
||||
)
|
||||
link_document_list_recent = Link(
|
||||
icon='fa fa-clock-o', text=_('Recent documents'),
|
||||
view='documents:document_list_recent'
|
||||
)
|
||||
link_document_list_deleted = Link(
|
||||
icon='fa fa-trash', text=_('Trash'),
|
||||
view='documents:document_list_deleted'
|
||||
)
|
||||
|
||||
# Tools
|
||||
link_clear_image_cache = Link(
|
||||
icon='fa fa-file-image-o',
|
||||
description=_('Clear the graphics representations used to speed up the documents\' display and interactive transformations results.'),
|
||||
description=_(
|
||||
'Clear the graphics representations used to speed up the documents\' '
|
||||
'display and interactive transformations results.'
|
||||
),
|
||||
permissions=[permission_document_tools], text=_('Clear document cache'),
|
||||
view='documents:document_clear_image_cache'
|
||||
)
|
||||
link_trash_can_empty = Link(permissions=[permission_empty_trash], text=_('Empty trash'), view='documents:trash_can_empty')
|
||||
link_trash_can_empty = Link(
|
||||
permissions=[permission_empty_trash], text=_('Empty trash'),
|
||||
view='documents:trash_can_empty'
|
||||
)
|
||||
|
||||
# Document pages
|
||||
link_document_page_navigation_first = Link(conditional_disable=is_first_page, icon='fa fa-step-backward', keep_query=True, permissions=[permission_document_view], text=_('First page'), view='documents:document_page_navigation_first', args='resolved_object.pk')
|
||||
link_document_page_navigation_last = Link(conditional_disable=is_last_page, icon='fa fa-step-forward', keep_query=True, text=_('Last page'), permissions=[permission_document_view], view='documents:document_page_navigation_last', args='resolved_object.pk')
|
||||
link_document_page_navigation_previous = Link(conditional_disable=is_first_page, icon='fa fa-arrow-left', keep_query=True, permissions=[permission_document_view], text=_('Previous page'), view='documents:document_page_navigation_previous', args='resolved_object.pk')
|
||||
link_document_page_navigation_next = Link(conditional_disable=is_last_page, icon='fa fa-arrow-right', keep_query=True, text=_('Next page'), permissions=[permission_document_view], view='documents:document_page_navigation_next', args='resolved_object.pk')
|
||||
link_document_page_return = Link(icon='fa fa-file', permissions=[permission_document_view], text=_('Document'), view='documents:document_preview', args='resolved_object.document.pk')
|
||||
link_document_page_rotate_left = Link(icon='fa fa-rotate-left', permissions=[permission_document_view], text=_('Rotate left'), view='documents:document_page_rotate_left', args='resolved_object.pk')
|
||||
link_document_page_rotate_right = Link(icon='fa fa-rotate-right', permissions=[permission_document_view], text=_('Rotate right'), view='documents:document_page_rotate_right', args='resolved_object.pk')
|
||||
link_document_page_view = Link(permissions=[permission_document_view], text=_('Page image'), view='documents:document_page_view', args='resolved_object.pk')
|
||||
link_document_page_view_reset = Link(permissions=[permission_document_view], text=_('Reset view'), view='documents:document_page_view_reset', args='resolved_object.pk')
|
||||
link_document_page_zoom_in = Link(conditional_disable=is_max_zoom, icon='fa fa-search-plus', permissions=[permission_document_view], text=_('Zoom in'), view='documents:document_page_zoom_in', args='resolved_object.pk')
|
||||
link_document_page_zoom_out = Link(conditional_disable=is_min_zoom, icon='fa fa-search-minus', permissions=[permission_document_view], text=_('Zoom out'), view='documents:document_page_zoom_out', args='resolved_object.pk')
|
||||
link_document_page_navigation_first = Link(
|
||||
conditional_disable=is_first_page, icon='fa fa-step-backward',
|
||||
keep_query=True, permissions=[permission_document_view],
|
||||
text=_('First page'), view='documents:document_page_navigation_first',
|
||||
args='resolved_object.pk'
|
||||
)
|
||||
link_document_page_navigation_last = Link(
|
||||
conditional_disable=is_last_page, icon='fa fa-step-forward',
|
||||
keep_query=True, text=_('Last page'),
|
||||
permissions=[permission_document_view],
|
||||
view='documents:document_page_navigation_last', args='resolved_object.pk'
|
||||
)
|
||||
link_document_page_navigation_previous = Link(
|
||||
conditional_disable=is_first_page, icon='fa fa-arrow-left',
|
||||
keep_query=True, permissions=[permission_document_view],
|
||||
text=_('Previous page'),
|
||||
view='documents:document_page_navigation_previous',
|
||||
args='resolved_object.pk'
|
||||
)
|
||||
link_document_page_navigation_next = Link(
|
||||
conditional_disable=is_last_page, icon='fa fa-arrow-right',
|
||||
keep_query=True, text=_('Next page'),
|
||||
permissions=[permission_document_view],
|
||||
view='documents:document_page_navigation_next', args='resolved_object.pk'
|
||||
)
|
||||
link_document_page_return = Link(
|
||||
icon='fa fa-file', permissions=[permission_document_view],
|
||||
text=_('Document'), view='documents:document_preview',
|
||||
args='resolved_object.document.pk'
|
||||
)
|
||||
link_document_page_rotate_left = Link(
|
||||
icon='fa fa-rotate-left', permissions=[permission_document_view],
|
||||
text=_('Rotate left'), view='documents:document_page_rotate_left',
|
||||
args='resolved_object.pk'
|
||||
)
|
||||
link_document_page_rotate_right = Link(
|
||||
icon='fa fa-rotate-right', permissions=[permission_document_view],
|
||||
text=_('Rotate right'), view='documents:document_page_rotate_right',
|
||||
args='resolved_object.pk'
|
||||
)
|
||||
link_document_page_view = Link(
|
||||
permissions=[permission_document_view], text=_('Page image'),
|
||||
view='documents:document_page_view', args='resolved_object.pk'
|
||||
)
|
||||
link_document_page_view_reset = Link(
|
||||
permissions=[permission_document_view], text=_('Reset view'),
|
||||
view='documents:document_page_view_reset', args='resolved_object.pk'
|
||||
)
|
||||
link_document_page_zoom_in = Link(
|
||||
conditional_disable=is_max_zoom, icon='fa fa-search-plus',
|
||||
permissions=[permission_document_view], text=_('Zoom in'),
|
||||
view='documents:document_page_zoom_in', args='resolved_object.pk'
|
||||
)
|
||||
link_document_page_zoom_out = Link(
|
||||
conditional_disable=is_min_zoom, icon='fa fa-search-minus',
|
||||
permissions=[permission_document_view], text=_('Zoom out'),
|
||||
view='documents:document_page_zoom_out', args='resolved_object.pk'
|
||||
)
|
||||
|
||||
# Document versions
|
||||
link_document_version_revert = Link(condition=is_not_current_version, permissions=[permission_document_version_revert], tags='dangerous', text=_('Revert'), view='documents:document_version_revert', args='object.pk')
|
||||
link_document_version_revert = Link(
|
||||
condition=is_not_current_version,
|
||||
permissions=[permission_document_version_revert], tags='dangerous',
|
||||
text=_('Revert'), view='documents:document_version_revert',
|
||||
args='object.pk'
|
||||
)
|
||||
|
||||
# Document type related links
|
||||
link_document_type_create = Link(permissions=[permission_document_type_create], text=_('Create document type'), view='documents:document_type_create')
|
||||
link_document_type_delete = Link(permissions=[permission_document_type_delete], tags='dangerous', text=_('Delete'), view='documents:document_type_delete', args='resolved_object.id')
|
||||
link_document_type_edit = Link(permissions=[permission_document_type_edit], text=_('Edit'), view='documents:document_type_edit', args='resolved_object.id')
|
||||
link_document_type_filename_create = Link(permissions=[permission_document_type_edit], text=_('Add filename to document type'), view='documents:document_type_filename_create', args='document_type.id')
|
||||
link_document_type_filename_delete = Link(permissions=[permission_document_type_edit], tags='dangerous', text=_('Delete'), view='documents:document_type_filename_delete', args='resolved_object.id')
|
||||
link_document_type_filename_edit = Link(permissions=[permission_document_type_edit], text=_('Edit'), view='documents:document_type_filename_edit', args='resolved_object.id')
|
||||
link_document_type_filename_list = Link(permissions=[permission_document_type_view], text=_('Filenames'), view='documents:document_type_filename_list', args='resolved_object.id')
|
||||
link_document_type_list = Link(permissions=[permission_document_type_view], text=_('Document types'), view='documents:document_type_list')
|
||||
link_document_type_setup = Link(icon='fa fa-file', permissions=[permission_document_type_view], text=_('Document types'), view='documents:document_type_list')
|
||||
link_document_type_create = Link(
|
||||
permissions=[permission_document_type_create],
|
||||
text=_('Create document type'), view='documents:document_type_create'
|
||||
)
|
||||
link_document_type_delete = Link(
|
||||
permissions=[permission_document_type_delete], tags='dangerous',
|
||||
text=_('Delete'), view='documents:document_type_delete',
|
||||
args='resolved_object.id'
|
||||
)
|
||||
link_document_type_edit = Link(
|
||||
permissions=[permission_document_type_edit], text=_('Edit'),
|
||||
view='documents:document_type_edit', args='resolved_object.id'
|
||||
)
|
||||
link_document_type_filename_create = Link(
|
||||
permissions=[permission_document_type_edit],
|
||||
text=_('Add filename to document type'),
|
||||
view='documents:document_type_filename_create', args='document_type.id'
|
||||
)
|
||||
link_document_type_filename_delete = Link(
|
||||
permissions=[permission_document_type_edit], tags='dangerous',
|
||||
text=_('Delete'), view='documents:document_type_filename_delete',
|
||||
args='resolved_object.id'
|
||||
)
|
||||
link_document_type_filename_edit = Link(
|
||||
permissions=[permission_document_type_edit], text=_('Edit'),
|
||||
view='documents:document_type_filename_edit', args='resolved_object.id'
|
||||
)
|
||||
link_document_type_filename_list = Link(
|
||||
permissions=[permission_document_type_view], text=_('Filenames'),
|
||||
view='documents:document_type_filename_list', args='resolved_object.id'
|
||||
)
|
||||
link_document_type_list = Link(
|
||||
permissions=[permission_document_type_view], text=_('Document types'),
|
||||
view='documents:document_type_list'
|
||||
)
|
||||
link_document_type_setup = Link(
|
||||
icon='fa fa-file', permissions=[permission_document_type_view],
|
||||
text=_('Document types'), view='documents:document_type_list'
|
||||
)
|
||||
|
||||
@@ -13,7 +13,9 @@ logger = logging.getLogger(__name__)
|
||||
class RecentDocumentManager(models.Manager):
|
||||
def add_document_for_user(self, user, document):
|
||||
if user.is_authenticated():
|
||||
new_recent, created = self.model.objects.get_or_create(user=user, document=document)
|
||||
new_recent, created = self.model.objects.get_or_create(
|
||||
user=user, document=document
|
||||
)
|
||||
if not created:
|
||||
# document already in the recent list, just save to force
|
||||
# accessed date and time update
|
||||
@@ -25,7 +27,9 @@ class RecentDocumentManager(models.Manager):
|
||||
document_model = apps.get_model('documents', 'document')
|
||||
|
||||
if user.is_authenticated():
|
||||
return document_model.objects.filter(recentdocument__user=user).order_by('-recentdocument__datetime_accessed')
|
||||
return document_model.objects.filter(
|
||||
recentdocument__user=user
|
||||
).order_by('-recentdocument__datetime_accessed')
|
||||
else:
|
||||
return document_model.objects.none()
|
||||
|
||||
@@ -37,7 +41,9 @@ class DocumentTypeManager(models.Manager):
|
||||
|
||||
class DocumentManager(models.Manager):
|
||||
def get_queryset(self):
|
||||
return TrashCanQuerySet(self.model, using=self._db).filter(in_trash=False)
|
||||
return TrashCanQuerySet(
|
||||
self.model, using=self._db
|
||||
).filter(in_trash=False)
|
||||
|
||||
def invalidate_cache(self):
|
||||
for document in self.model.objects.all():
|
||||
@@ -50,7 +56,9 @@ class PassthroughManager(models.Manager):
|
||||
|
||||
class TrashCanManager(models.Manager):
|
||||
def get_queryset(self):
|
||||
return super(TrashCanManager, self).get_queryset().filter(in_trash=True)
|
||||
return super(
|
||||
TrashCanManager, self
|
||||
).get_queryset().filter(in_trash=True)
|
||||
|
||||
|
||||
class TrashCanQuerySet(models.QuerySet):
|
||||
|
||||
+174
-53
@@ -43,7 +43,8 @@ from .signals import (
|
||||
post_document_created, post_document_type_change, post_version_upload
|
||||
)
|
||||
|
||||
HASH_FUNCTION = lambda x: hashlib.sha256(x).hexdigest() # document image cache name hash function
|
||||
# document image cache name hash function
|
||||
HASH_FUNCTION = lambda x: hashlib.sha256(x).hexdigest()
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@@ -57,11 +58,29 @@ class DocumentType(models.Model):
|
||||
Define document types or classes to which a specific set of
|
||||
properties can be attached
|
||||
"""
|
||||
label = models.CharField(max_length=32, unique=True, verbose_name=_('Label'))
|
||||
trash_time_period = models.PositiveIntegerField(blank=True, help_text=_('Amount of time after which documents of this type will be moved to the trash.'), null=True, verbose_name=_('Trash time period'))
|
||||
trash_time_unit = models.CharField(blank=True, choices=TIME_DELTA_UNIT_CHOICES, null=True, max_length=8, verbose_name=_('Trash time unit'))
|
||||
delete_time_period = models.PositiveIntegerField(default=DEFAULT_DELETE_PERIOD, help_text=_('Amount of time after which documents of this type in the trash will be deleted.'), verbose_name=_('Delete time period'))
|
||||
delete_time_unit = models.CharField(choices=TIME_DELTA_UNIT_CHOICES, default=DEFAULT_DELETE_TIME_UNIT, max_length=8, verbose_name=_('Delete time unit'))
|
||||
label = models.CharField(
|
||||
max_length=32, unique=True, verbose_name=_('Label')
|
||||
)
|
||||
trash_time_period = models.PositiveIntegerField(
|
||||
blank=True, help_text=_(
|
||||
'Amount of time after which documents of this type will be '
|
||||
'moved to the trash.'
|
||||
), null=True, verbose_name=_('Trash time period')
|
||||
)
|
||||
trash_time_unit = models.CharField(
|
||||
blank=True, choices=TIME_DELTA_UNIT_CHOICES, null=True, max_length=8,
|
||||
verbose_name=_('Trash time unit')
|
||||
)
|
||||
delete_time_period = models.PositiveIntegerField(
|
||||
default=DEFAULT_DELETE_PERIOD, help_text=_(
|
||||
'Amount of time after which documents of this type in the trash '
|
||||
'will be deleted.'
|
||||
), verbose_name=_('Delete time period')
|
||||
)
|
||||
delete_time_unit = models.CharField(
|
||||
choices=TIME_DELTA_UNIT_CHOICES, default=DEFAULT_DELETE_TIME_UNIT,
|
||||
max_length=8, verbose_name=_('Delete time unit')
|
||||
)
|
||||
|
||||
objects = DocumentTypeManager()
|
||||
|
||||
@@ -77,13 +96,21 @@ class DocumentType(models.Model):
|
||||
def new_document(self, file_object, label=None, description=None, language=None, _user=None):
|
||||
try:
|
||||
with transaction.atomic():
|
||||
document = self.documents.create(description=description or '', label=label or unicode(file_object), language=language or setting_language.value)
|
||||
document = self.documents.create(
|
||||
description=description or '',
|
||||
label=label or unicode(file_object),
|
||||
language=language or setting_language.value
|
||||
)
|
||||
document.save(_user=_user)
|
||||
|
||||
document.new_version(file_object=file_object, _user=_user)
|
||||
return document
|
||||
except Exception as exception:
|
||||
logger.critical('Unexpected exception while trying to create new document "%s" from document type "%s"; %s', label or unicode(file_object), self, exception)
|
||||
logger.critical(
|
||||
'Unexpected exception while trying to create new document '
|
||||
'"%s" from document type "%s"; %s',
|
||||
label or unicode(file_object), self, exception
|
||||
)
|
||||
raise
|
||||
|
||||
class Meta:
|
||||
@@ -98,15 +125,38 @@ class Document(models.Model):
|
||||
Defines a single document with it's fields and properties
|
||||
"""
|
||||
|
||||
uuid = models.CharField(default=UUID_FUNCTION, editable=False, max_length=48)
|
||||
document_type = models.ForeignKey(DocumentType, related_name='documents', verbose_name=_('Document type'))
|
||||
label = models.CharField(blank=True, db_index=True, default='', max_length=255, help_text=_('The name of the document'), verbose_name=_('Label'))
|
||||
description = models.TextField(blank=True, default='', verbose_name=_('Description'))
|
||||
date_added = models.DateTimeField(auto_now_add=True, db_index=True, verbose_name=_('Added'))
|
||||
language = models.CharField(blank=True, choices=setting_language_choices.value, default=setting_language.value, max_length=8, verbose_name=_('Language'))
|
||||
in_trash = models.BooleanField(default=False, editable=False, verbose_name=_('In trash?'))
|
||||
deleted_date_time = models.DateTimeField(blank=True, editable=True, null=True, verbose_name=_('Date and time trashed'))
|
||||
is_stub = models.BooleanField(default=True, editable=False, verbose_name=_('Is stub?'))
|
||||
uuid = models.CharField(
|
||||
default=UUID_FUNCTION, editable=False, max_length=48
|
||||
)
|
||||
document_type = models.ForeignKey(
|
||||
DocumentType, related_name='documents',
|
||||
verbose_name=_('Document type')
|
||||
)
|
||||
label = models.CharField(
|
||||
blank=True, db_index=True, default='', max_length=255,
|
||||
help_text=_('The name of the document'), verbose_name=_('Label')
|
||||
)
|
||||
description = models.TextField(
|
||||
blank=True, default='', verbose_name=_('Description')
|
||||
)
|
||||
date_added = models.DateTimeField(
|
||||
auto_now_add=True, db_index=True, verbose_name=_('Added')
|
||||
)
|
||||
language = models.CharField(
|
||||
blank=True, choices=setting_language_choices.value,
|
||||
default=setting_language.value, max_length=8,
|
||||
verbose_name=_('Language')
|
||||
)
|
||||
in_trash = models.BooleanField(
|
||||
default=False, editable=False, verbose_name=_('In trash?')
|
||||
)
|
||||
deleted_date_time = models.DateTimeField(
|
||||
blank=True, editable=True, null=True,
|
||||
verbose_name=_('Date and time trashed')
|
||||
)
|
||||
is_stub = models.BooleanField(
|
||||
default=True, editable=False, verbose_name=_('Is stub?')
|
||||
)
|
||||
|
||||
objects = DocumentManager()
|
||||
passthrough = PassthroughManager()
|
||||
@@ -123,7 +173,9 @@ class Document(models.Model):
|
||||
self.document_type = document_type
|
||||
self.save()
|
||||
if has_changed or force:
|
||||
post_document_type_change.send(sender=self.__class__, instance=self)
|
||||
post_document_type_change.send(
|
||||
sender=self.__class__, instance=self
|
||||
)
|
||||
|
||||
def invalidate_cache(self):
|
||||
for document_version in self.versions.all():
|
||||
@@ -174,7 +226,9 @@ class Document(models.Model):
|
||||
def new_version(self, file_object, comment=None, _user=None):
|
||||
logger.info('Creating new document version for document: %s', self)
|
||||
|
||||
document_version = DocumentVersion(document=self, comment=comment or '', file=File(file_object))
|
||||
document_version = DocumentVersion(
|
||||
document=self, comment=comment or '', file=File(file_object)
|
||||
)
|
||||
document_version.save(_user=_user)
|
||||
|
||||
logger.info('New document version queued for document: %s', self)
|
||||
@@ -238,7 +292,9 @@ class Document(models.Model):
|
||||
|
||||
# TODO: look to remove, only used by the OCR parser
|
||||
def document_save_to_temp_dir(self, filename, buffer_size=1024 * 1024):
|
||||
temporary_path = os.path.join(setting_temporary_directory.value, filename)
|
||||
temporary_path = os.path.join(
|
||||
setting_temporary_directory.value, filename
|
||||
)
|
||||
return self.save_to_file(temporary_path, buffer_size)
|
||||
|
||||
|
||||
@@ -265,15 +321,30 @@ class DocumentVersion(models.Model):
|
||||
def register_post_save_hook(cls, order, func):
|
||||
cls._post_save_hooks[order] = func
|
||||
|
||||
document = models.ForeignKey(Document, related_name='versions', verbose_name=_('Document'))
|
||||
timestamp = models.DateTimeField(auto_now_add=True, db_index=True, verbose_name=_('Timestamp'))
|
||||
comment = models.TextField(blank=True, default='', verbose_name=_('Comment'))
|
||||
document = models.ForeignKey(
|
||||
Document, related_name='versions', verbose_name=_('Document')
|
||||
)
|
||||
timestamp = models.DateTimeField(
|
||||
auto_now_add=True, db_index=True, verbose_name=_('Timestamp')
|
||||
)
|
||||
comment = models.TextField(
|
||||
blank=True, default='', verbose_name=_('Comment')
|
||||
)
|
||||
|
||||
# File related fields
|
||||
file = models.FileField(storage=storage_backend, upload_to=UUID_FUNCTION, verbose_name=_('File'))
|
||||
mimetype = models.CharField(blank=True, editable=False, max_length=255, null=True)
|
||||
encoding = models.CharField(blank=True, editable=False, max_length=64, null=True)
|
||||
checksum = models.TextField(blank=True, editable=False, null=True, verbose_name=_('Checksum'))
|
||||
file = models.FileField(
|
||||
storage=storage_backend, upload_to=UUID_FUNCTION,
|
||||
verbose_name=_('File')
|
||||
)
|
||||
mimetype = models.CharField(
|
||||
blank=True, editable=False, max_length=255, null=True
|
||||
)
|
||||
encoding = models.CharField(
|
||||
blank=True, editable=False, max_length=64, null=True
|
||||
)
|
||||
checksum = models.TextField(
|
||||
blank=True, editable=False, null=True, verbose_name=_('Checksum')
|
||||
)
|
||||
|
||||
class Meta:
|
||||
verbose_name = _('Document version')
|
||||
@@ -308,7 +379,10 @@ class DocumentVersion(models.Model):
|
||||
self.save()
|
||||
self.update_page_count(save=False)
|
||||
|
||||
logger.info('New document version "%s" created for document: %s', self, self.document)
|
||||
logger.info(
|
||||
'New document version "%s" created for document: %s',
|
||||
self, self.document
|
||||
)
|
||||
|
||||
self.document.is_stub = False
|
||||
if not self.document.label:
|
||||
@@ -316,15 +390,22 @@ class DocumentVersion(models.Model):
|
||||
|
||||
self.document.save()
|
||||
except Exception as exception:
|
||||
logger.error('Error creating new document version for document "%s"; %s', self.document, exception)
|
||||
logger.error(
|
||||
'Error creating new document version for document "%s"; %s',
|
||||
self.document, exception
|
||||
)
|
||||
raise
|
||||
else:
|
||||
if new_document_version:
|
||||
event_document_new_version.commit(actor=user, target=self.document)
|
||||
event_document_new_version.commit(
|
||||
actor=user, target=self.document
|
||||
)
|
||||
post_version_upload.send(sender=self.__class__, instance=self)
|
||||
|
||||
if tuple(self.document.versions.all()) == (self,):
|
||||
post_document_created.send(sender=self.document.__class__, instance=self.document)
|
||||
post_document_created.send(
|
||||
sender=self.document.__class__, instance=self.document
|
||||
)
|
||||
|
||||
def invalidate_cache(self):
|
||||
cache_storage_backend.delete(self.cache_filename)
|
||||
@@ -333,8 +414,8 @@ class DocumentVersion(models.Model):
|
||||
|
||||
def update_checksum(self, save=True):
|
||||
"""
|
||||
Open a document version's file and update the checksum field using the
|
||||
user provided checksum function
|
||||
Open a document version's file and update the checksum field using
|
||||
the user provided checksum function
|
||||
"""
|
||||
if self.exists():
|
||||
source = self.open()
|
||||
@@ -346,7 +427,9 @@ class DocumentVersion(models.Model):
|
||||
def update_page_count(self, save=True):
|
||||
try:
|
||||
with self.open() as file_object:
|
||||
converter = converter_class(file_object=file_object, mime_type=self.mimetype)
|
||||
converter = converter_class(
|
||||
file_object=file_object, mime_type=self.mimetype
|
||||
)
|
||||
detected_pages = converter.get_page_count()
|
||||
except UnknownFileFormat:
|
||||
# If converter backend doesn't understand the format,
|
||||
@@ -372,7 +455,10 @@ class DocumentVersion(models.Model):
|
||||
"""
|
||||
Delete the subsequent versions after this one
|
||||
"""
|
||||
logger.info('Reverting to document document: %s to version: %s', self.document, self)
|
||||
logger.info(
|
||||
'Reverting to document document: %s to version: %s',
|
||||
self.document, self
|
||||
)
|
||||
|
||||
event_document_version_revert.commit(actor=user, target=self.document)
|
||||
|
||||
@@ -381,13 +467,15 @@ class DocumentVersion(models.Model):
|
||||
|
||||
def update_mimetype(self, save=True):
|
||||
"""
|
||||
Read a document verions's file and determine the mimetype by calling the
|
||||
get_mimetype wrapper
|
||||
Read a document verions's file and determine the mimetype by calling
|
||||
the get_mimetype wrapper
|
||||
"""
|
||||
if self.exists():
|
||||
try:
|
||||
with self.open() as file_object:
|
||||
self.mimetype, self.encoding = get_mimetype(file_object=file_object)
|
||||
self.mimetype, self.encoding = get_mimetype(
|
||||
file_object=file_object
|
||||
)
|
||||
except:
|
||||
self.mimetype = ''
|
||||
self.encoding = ''
|
||||
@@ -486,7 +574,10 @@ class DocumentVersion(models.Model):
|
||||
return self.open()
|
||||
except Exception as exception:
|
||||
# Cleanup in case of error
|
||||
logger.error('Error creating intermediate file "%s"; %s.', cache_filename, exception)
|
||||
logger.error(
|
||||
'Error creating intermediate file "%s"; %s.',
|
||||
cache_filename, exception
|
||||
)
|
||||
cache_storage_backend.delete(cache_filename)
|
||||
raise
|
||||
|
||||
@@ -497,8 +588,12 @@ class DocumentTypeFilename(models.Model):
|
||||
List of filenames available to a specific document type for the
|
||||
quick rename functionality
|
||||
"""
|
||||
document_type = models.ForeignKey(DocumentType, related_name='filenames', verbose_name=_('Document type'))
|
||||
filename = models.CharField(db_index=True, max_length=128, verbose_name=_('Filename'))
|
||||
document_type = models.ForeignKey(
|
||||
DocumentType, related_name='filenames', verbose_name=_('Document type')
|
||||
)
|
||||
filename = models.CharField(
|
||||
db_index=True, max_length=128, verbose_name=_('Filename')
|
||||
)
|
||||
enabled = models.BooleanField(default=True, verbose_name=_('Enabled'))
|
||||
|
||||
def __str__(self):
|
||||
@@ -516,11 +611,18 @@ class DocumentPage(models.Model):
|
||||
"""
|
||||
Model that describes a document version page
|
||||
"""
|
||||
document_version = models.ForeignKey(DocumentVersion, related_name='pages', verbose_name=_('Document version'))
|
||||
page_number = models.PositiveIntegerField(db_index=True, default=1, editable=False, verbose_name=_('Page number'))
|
||||
document_version = models.ForeignKey(
|
||||
DocumentVersion, related_name='pages',
|
||||
verbose_name=_('Document version')
|
||||
)
|
||||
page_number = models.PositiveIntegerField(
|
||||
db_index=True, default=1, editable=False, verbose_name=_('Page number')
|
||||
)
|
||||
|
||||
def __str__(self):
|
||||
return _('Page %(page_num)d out of %(total_pages)d of %(document)s') % {
|
||||
return _(
|
||||
'Page %(page_num)d out of %(total_pages)d of %(document)s'
|
||||
) % {
|
||||
'document': unicode(self.document),
|
||||
'page_num': self.page_number,
|
||||
'total_pages': self.document_version.pages.count()
|
||||
@@ -540,7 +642,9 @@ class DocumentPage(models.Model):
|
||||
|
||||
@property
|
||||
def siblings(self):
|
||||
return DocumentPage.objects.filter(document_version=self.document_version)
|
||||
return DocumentPage.objects.filter(
|
||||
document_version=self.document_version
|
||||
)
|
||||
|
||||
# Compatibility methods
|
||||
@property
|
||||
@@ -582,14 +686,18 @@ class DocumentPage(models.Model):
|
||||
|
||||
if cache_storage_backend.exists(cache_filename):
|
||||
logger.debug('Page cache file "%s" found', cache_filename)
|
||||
converter = converter_class(file_object=cache_storage_backend.open(cache_filename))
|
||||
converter = converter_class(
|
||||
file_object=cache_storage_backend.open(cache_filename)
|
||||
)
|
||||
|
||||
converter.seek(0)
|
||||
else:
|
||||
logger.debug('Page cache file "%s" not found', cache_filename)
|
||||
|
||||
try:
|
||||
converter = converter_class(file_object=self.document_version.get_intermidiate_file())
|
||||
converter = converter_class(
|
||||
file_object=self.document_version.get_intermidiate_file()
|
||||
)
|
||||
converter.seek(page_number=self.page_number - 1)
|
||||
|
||||
page_image = converter.get_page()
|
||||
@@ -598,7 +706,10 @@ class DocumentPage(models.Model):
|
||||
file_object.write(page_image.getvalue())
|
||||
except Exception as exception:
|
||||
# Cleanup in case of error
|
||||
logger.error('Error creating page cache file "%s"; %s', cache_filename, exception)
|
||||
logger.error(
|
||||
'Error creating page cache file "%s"; %s',
|
||||
cache_filename, exception
|
||||
)
|
||||
cache_storage_backend.delete(cache_filename)
|
||||
raise
|
||||
|
||||
@@ -611,13 +722,19 @@ class DocumentPage(models.Model):
|
||||
converter.transform(transformation=transformation)
|
||||
|
||||
if rotation:
|
||||
converter.transform(transformation=TransformationRotate(degrees=rotation))
|
||||
converter.transform(transformation=TransformationRotate(
|
||||
degrees=rotation)
|
||||
)
|
||||
|
||||
if size:
|
||||
converter.transform(transformation=TransformationResize(**dict(zip(('width', 'height'), (size.split('x'))))))
|
||||
converter.transform(transformation=TransformationResize(
|
||||
**dict(zip(('width', 'height'), (size.split('x')))))
|
||||
)
|
||||
|
||||
if zoom_level:
|
||||
converter.transform(transformation=TransformationZoom(percent=zoom_level))
|
||||
converter.transform(
|
||||
transformation=TransformationZoom(percent=zoom_level)
|
||||
)
|
||||
|
||||
page_image = converter.get_page()
|
||||
|
||||
@@ -635,8 +752,12 @@ class RecentDocument(models.Model):
|
||||
a given user
|
||||
"""
|
||||
user = models.ForeignKey(User, editable=False, verbose_name=_('User'))
|
||||
document = models.ForeignKey(Document, editable=False, verbose_name=_('Document'))
|
||||
datetime_accessed = models.DateTimeField(auto_now=True, db_index=True, verbose_name=_('Accessed'))
|
||||
document = models.ForeignKey(
|
||||
Document, editable=False, verbose_name=_('Document')
|
||||
)
|
||||
datetime_accessed = models.DateTimeField(
|
||||
auto_now=True, db_index=True, verbose_name=_('Accessed')
|
||||
)
|
||||
|
||||
objects = RecentDocumentManager()
|
||||
|
||||
|
||||
@@ -6,24 +6,61 @@ from permissions import PermissionNamespace
|
||||
|
||||
namespace = PermissionNamespace('documents', _('Documents'))
|
||||
|
||||
permission_document_create = namespace.add_permission(name='document_create', label=_('Create documents'))
|
||||
permission_document_delete = namespace.add_permission(name='document_delete', label=_('Delete documents'))
|
||||
permission_document_trash = namespace.add_permission(name='document_trash', label=_('Trash documents'))
|
||||
permission_document_download = namespace.add_permission(name='document_download', label=_('Download documents'))
|
||||
permission_document_edit = namespace.add_permission(name='document_edit', label=_('Edit documents'))
|
||||
permission_document_new_version = namespace.add_permission(name='document_new_version', label=_('Create new document versions'))
|
||||
permission_document_properties_edit = namespace.add_permission(name='document_properties_edit', label=_('Edit document properties'))
|
||||
permission_document_print = namespace.add_permission(name='document_print', label=_('Can print documents'))
|
||||
permission_document_restore = namespace.add_permission(name='document_restore', label=_('Restore deleted document'))
|
||||
permission_document_tools = namespace.add_permission(name='document_tools', label=_('Execute document modifying tools'))
|
||||
permission_document_version_revert = namespace.add_permission(name='document_version_revert', label=_('Revert documents to a previous version'))
|
||||
permission_document_view = namespace.add_permission(name='document_view', label=_('View documents'))
|
||||
permission_document_create = namespace.add_permission(
|
||||
name='document_create', label=_('Create documents')
|
||||
)
|
||||
permission_document_delete = namespace.add_permission(
|
||||
name='document_delete', label=_('Delete documents')
|
||||
)
|
||||
permission_document_trash = namespace.add_permission(
|
||||
name='document_trash', label=_('Trash documents')
|
||||
)
|
||||
permission_document_download = namespace.add_permission(
|
||||
name='document_download', label=_('Download documents')
|
||||
)
|
||||
permission_document_edit = namespace.add_permission(
|
||||
name='document_edit', label=_('Edit documents')
|
||||
)
|
||||
permission_document_new_version = namespace.add_permission(
|
||||
name='document_new_version', label=_('Create new document versions')
|
||||
)
|
||||
permission_document_properties_edit = namespace.add_permission(
|
||||
name='document_properties_edit', label=_('Edit document properties')
|
||||
)
|
||||
permission_document_print = namespace.add_permission(
|
||||
name='document_print', label=_('Can print documents')
|
||||
)
|
||||
permission_document_restore = namespace.add_permission(
|
||||
name='document_restore', label=_('Restore deleted document')
|
||||
)
|
||||
permission_document_tools = namespace.add_permission(
|
||||
name='document_tools', label=_('Execute document modifying tools')
|
||||
)
|
||||
permission_document_version_revert = namespace.add_permission(
|
||||
name='document_version_revert',
|
||||
label=_('Revert documents to a previous version')
|
||||
)
|
||||
permission_document_view = namespace.add_permission(
|
||||
name='document_view', label=_('View documents')
|
||||
)
|
||||
|
||||
permission_empty_trash = namespace.add_permission(name='document_empty_trash', label=_('Empty trash'))
|
||||
permission_empty_trash = namespace.add_permission(
|
||||
name='document_empty_trash', label=_('Empty trash')
|
||||
)
|
||||
|
||||
setup_namespace = PermissionNamespace('documents_setup', label=_('Documents setup'))
|
||||
setup_namespace = PermissionNamespace(
|
||||
'documents_setup', label=_('Documents setup')
|
||||
)
|
||||
|
||||
permission_document_type_create = setup_namespace.add_permission(name='document_type_create', label=_('Create document types'))
|
||||
permission_document_type_delete = setup_namespace.add_permission(name='document_type_delete', label=_('Delete document types'))
|
||||
permission_document_type_edit = setup_namespace.add_permission(name='document_type_edit', label=_('Edit document types'))
|
||||
permission_document_type_view = setup_namespace.add_permission(name='document_type_view', label=_('View document types'))
|
||||
permission_document_type_create = setup_namespace.add_permission(
|
||||
name='document_type_create', label=_('Create document types')
|
||||
)
|
||||
permission_document_type_delete = setup_namespace.add_permission(
|
||||
name='document_type_delete', label=_('Delete document types')
|
||||
)
|
||||
permission_document_type_edit = setup_namespace.add_permission(
|
||||
name='document_type_edit', label=_('Edit document types')
|
||||
)
|
||||
permission_document_type_view = setup_namespace.add_permission(
|
||||
name='document_type_view', label=_('View document types')
|
||||
)
|
||||
|
||||
@@ -6,9 +6,16 @@ from dynamic_search.classes import SearchModel
|
||||
|
||||
from .permissions import permission_document_view
|
||||
|
||||
document_search = SearchModel('documents', 'Document', permission=permission_document_view, serializer_string='documents.serializers.DocumentSerializer')
|
||||
document_search = SearchModel(
|
||||
'documents', 'Document', permission=permission_document_view,
|
||||
serializer_string='documents.serializers.DocumentSerializer'
|
||||
)
|
||||
|
||||
document_search.add_model_field(field='document_type__label', label=_('Document type'))
|
||||
document_search.add_model_field(field='versions__mimetype', label=_('MIME type'))
|
||||
document_search.add_model_field(
|
||||
field='document_type__label', label=_('Document type')
|
||||
)
|
||||
document_search.add_model_field(
|
||||
field='versions__mimetype', label=_('MIME type')
|
||||
)
|
||||
document_search.add_model_field(field='label', label=_('Label'))
|
||||
document_search.add_model_field(field='description', label=_('Description'))
|
||||
|
||||
@@ -40,11 +40,16 @@ class DocumentSerializer(serializers.ModelSerializer):
|
||||
versions = DocumentVersionSerializer(many=True, read_only=True)
|
||||
# TODO: Deprecate, move this as an entry point of DocumentVersion's pages
|
||||
image = serializers.HyperlinkedIdentityField(view_name='document-image')
|
||||
new_version = serializers.HyperlinkedIdentityField(view_name='document-new-version')
|
||||
new_version = serializers.HyperlinkedIdentityField(
|
||||
view_name='document-new-version'
|
||||
)
|
||||
document_type = DocumentTypeSerializer()
|
||||
|
||||
class Meta:
|
||||
fields = ('id', 'label', 'image', 'new_version', 'uuid', 'document_type', 'description', 'date_added', 'versions')
|
||||
fields = (
|
||||
'id', 'label', 'image', 'new_version', 'uuid', 'document_type',
|
||||
'description', 'date_added', 'versions'
|
||||
)
|
||||
model = Document
|
||||
|
||||
|
||||
@@ -53,7 +58,10 @@ class NewDocumentSerializer(serializers.Serializer):
|
||||
document_type = serializers.IntegerField()
|
||||
file = serializers.FileField()
|
||||
label = serializers.CharField(required=False)
|
||||
language = serializers.ChoiceField(blank_display_value=None, choices=setting_language_choices.value, default=setting_language.value, required=False)
|
||||
language = serializers.ChoiceField(
|
||||
blank_display_value=None, choices=setting_language_choices.value,
|
||||
default=setting_language.value, required=False
|
||||
)
|
||||
|
||||
|
||||
class RecentDocumentSerializer(serializers.ModelSerializer):
|
||||
|
||||
@@ -7,21 +7,75 @@ from django.utils.translation import ugettext_lazy as _
|
||||
from smart_settings import Namespace
|
||||
|
||||
# TODO: Findout method to make languages names' translatable.
|
||||
# YAML fails to serialize ugettext_lazy and ugettext is not allowed at this level
|
||||
LANGUAGE_CHOICES = [(i.terminology, i.name) for i in list(pycountry.languages)]
|
||||
# YAML fails to serialize ugettext_lazy and ugettext is not allowed at this
|
||||
# level
|
||||
LANGUAGE_CHOICES = [
|
||||
(i.terminology, i.name) for i in list(pycountry.languages)
|
||||
]
|
||||
|
||||
namespace = Namespace(name='documents', label=_('Documents'))
|
||||
setting_storage_backend = namespace.add_setting(global_name='DOCUMENTS_STORAGE_BACKEND', default='storage.backends.filebasedstorage.FileBasedStorage')
|
||||
setting_preview_size = namespace.add_setting(global_name='DOCUMENTS_PREVIEW_SIZE', default='640x480')
|
||||
setting_print_size = namespace.add_setting(global_name='DOCUMENTS_PRINT_SIZE', default='3600')
|
||||
setting_multipage_preview_size = namespace.add_setting(global_name='DOCUMENTS_MULTIPAGE_PREVIEW_SIZE', default='160x120')
|
||||
setting_thumbnail_size = namespace.add_setting(global_name='DOCUMENTS_THUMBNAIL_SIZE', default='50x50')
|
||||
setting_display_size = namespace.add_setting(global_name='DOCUMENTS_DISPLAY_SIZE', default='3600')
|
||||
setting_recent_count = namespace.add_setting(global_name='DOCUMENTS_RECENT_COUNT', default=40, help_text=_('Maximum number of recent (created, edited, viewed) documents to remember per user.'))
|
||||
setting_zoom_percent_step = namespace.add_setting(global_name='DOCUMENTS_ZOOM_PERCENT_STEP', default=25, help_text=_('Amount in percent zoom in or out a document page per user interaction.'))
|
||||
setting_zoom_max_level = namespace.add_setting(global_name='DOCUMENTS_ZOOM_MAX_LEVEL', default=300, help_text=_('Maximum amount in percent (%) to allow user to zoom in a document page interactively.'))
|
||||
setting_zoom_min_level = namespace.add_setting(global_name='DOCUMENTS_ZOOM_MIN_LEVEL', default=25, help_text=_('Minimum amount in percent (%) to allow user to zoom out a document page interactively.'))
|
||||
setting_rotation_step = namespace.add_setting(global_name='DOCUMENTS_ROTATION_STEP', default=90, help_text=_('Amount in degrees to rotate a document page per user interaction.'))
|
||||
setting_cache_storage_backend = namespace.add_setting(global_name='DOCUMENTS_CACHE_STORAGE_BACKEND', default='documents.storage.LocalCacheFileStorage')
|
||||
setting_language = namespace.add_setting(global_name='DOCUMENTS_LANGUAGE', default='eng', help_text=_('Default documents language (in ISO639-2 format).'))
|
||||
setting_language_choices = namespace.add_setting(global_name='DOCUMENTS_LANGUAGE_CHOICES', default=LANGUAGE_CHOICES, help_text=_('List of supported document languages.'))
|
||||
setting_storage_backend = namespace.add_setting(
|
||||
global_name='DOCUMENTS_STORAGE_BACKEND',
|
||||
default='storage.backends.filebasedstorage.FileBasedStorage'
|
||||
)
|
||||
setting_preview_size = namespace.add_setting(
|
||||
global_name='DOCUMENTS_PREVIEW_SIZE', default='640x480'
|
||||
)
|
||||
setting_print_size = namespace.add_setting(
|
||||
global_name='DOCUMENTS_PRINT_SIZE', default='3600'
|
||||
)
|
||||
setting_multipage_preview_size = namespace.add_setting(
|
||||
global_name='DOCUMENTS_MULTIPAGE_PREVIEW_SIZE', default='160x120'
|
||||
)
|
||||
setting_thumbnail_size = namespace.add_setting(
|
||||
global_name='DOCUMENTS_THUMBNAIL_SIZE', default='50x50'
|
||||
)
|
||||
setting_display_size = namespace.add_setting(
|
||||
global_name='DOCUMENTS_DISPLAY_SIZE', default='3600'
|
||||
)
|
||||
setting_recent_count = namespace.add_setting(
|
||||
global_name='DOCUMENTS_RECENT_COUNT', default=40,
|
||||
help_text=_(
|
||||
'Maximum number of recent (created, edited, viewed) documents to '
|
||||
'remember per user.'
|
||||
)
|
||||
)
|
||||
setting_zoom_percent_step = namespace.add_setting(
|
||||
global_name='DOCUMENTS_ZOOM_PERCENT_STEP', default=25,
|
||||
help_text=_(
|
||||
'Amount in percent zoom in or out a document page per user '
|
||||
'interaction.'
|
||||
)
|
||||
)
|
||||
setting_zoom_max_level = namespace.add_setting(
|
||||
global_name='DOCUMENTS_ZOOM_MAX_LEVEL', default=300,
|
||||
help_text=_(
|
||||
'Maximum amount in percent (%) to allow user to zoom in a document '
|
||||
'page interactively.'
|
||||
)
|
||||
)
|
||||
setting_zoom_min_level = namespace.add_setting(
|
||||
global_name='DOCUMENTS_ZOOM_MIN_LEVEL', default=25,
|
||||
help_text=_(
|
||||
'Minimum amount in percent (%) to allow user to zoom out a document '
|
||||
'page interactively.'
|
||||
)
|
||||
)
|
||||
setting_rotation_step = namespace.add_setting(
|
||||
global_name='DOCUMENTS_ROTATION_STEP', default=90,
|
||||
help_text=_(
|
||||
'Amount in degrees to rotate a document page per user interaction.'
|
||||
)
|
||||
)
|
||||
setting_cache_storage_backend = namespace.add_setting(
|
||||
global_name='DOCUMENTS_CACHE_STORAGE_BACKEND',
|
||||
default='documents.storage.LocalCacheFileStorage'
|
||||
)
|
||||
setting_language = namespace.add_setting(
|
||||
global_name='DOCUMENTS_LANGUAGE', default='eng',
|
||||
help_text=_('Default documents language (in ISO639-2 format).')
|
||||
)
|
||||
setting_language_choices = namespace.add_setting(
|
||||
global_name='DOCUMENTS_LANGUAGE_CHOICES', default=LANGUAGE_CHOICES,
|
||||
help_text=_('List of supported document languages.')
|
||||
)
|
||||
|
||||
@@ -3,5 +3,7 @@ from __future__ import unicode_literals
|
||||
from django.dispatch import Signal
|
||||
|
||||
post_version_upload = Signal(providing_args=['instance'], use_caching=True)
|
||||
post_document_type_change = Signal(providing_args=['instance'], use_caching=True)
|
||||
post_document_type_change = Signal(
|
||||
providing_args=['instance'], use_caching=True
|
||||
)
|
||||
post_document_created = Signal(providing_args=['instance'], use_caching=True)
|
||||
|
||||
@@ -14,7 +14,9 @@ def get_used_size(path, file_list):
|
||||
total_size = 0
|
||||
for filename in file_list:
|
||||
try:
|
||||
total_size += storage_backend.size(storage_backend.separator.join([path, filename]))
|
||||
total_size += storage_backend.size(
|
||||
storage_backend.separator.join([path, filename])
|
||||
)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
@@ -45,11 +47,19 @@ class DocumentStatistics(Statistic):
|
||||
results.extend([
|
||||
_('Document types: %d') % DocumentType.objects.count(),
|
||||
])
|
||||
document_stats = DocumentVersion.objects.annotate(page_count=Count('pages')).aggregate(Min('page_count'), Max('page_count'), Avg('page_count'))
|
||||
document_stats = DocumentVersion.objects.annotate(
|
||||
page_count=Count('pages')
|
||||
).aggregate(Min('page_count'), Max('page_count'), Avg('page_count'))
|
||||
results.extend([
|
||||
_('Minimum amount of pages per document: %d') % (document_stats['page_count__min'] or 0),
|
||||
_('Maximum amount of pages per document: %d') % (document_stats['page_count__max'] or 0),
|
||||
_('Average amount of pages per document: %f') % (document_stats['page_count__avg'] or 0),
|
||||
_(
|
||||
'Minimum amount of pages per document: %d'
|
||||
) % (document_stats['page_count__min'] or 0),
|
||||
_(
|
||||
'Maximum amount of pages per document: %d'
|
||||
) % (document_stats['page_count__max'] or 0),
|
||||
_(
|
||||
'Average amount of pages per document: %f'
|
||||
) % (document_stats['page_count__avg'] or 0),
|
||||
])
|
||||
|
||||
return results
|
||||
@@ -69,16 +79,25 @@ class DocumentUsageStatistics(Statistic):
|
||||
total_storage_documents, storage_used_space = storage_count()
|
||||
results.append(_('Documents in storage: %d') %
|
||||
total_storage_documents)
|
||||
results.append(_('Space used in storage: %(base_2)s (base 2), %(base_10)s (base 10), %(bytes)d bytes') % {
|
||||
'base_2': pretty_size(storage_used_space),
|
||||
'base_10': pretty_size_10(storage_used_space),
|
||||
'bytes': storage_used_space
|
||||
})
|
||||
results.append(
|
||||
_(
|
||||
'Space used in storage: %(base_2)s (base 2), %(base_10)s '
|
||||
'(base 10), %(bytes)d bytes'
|
||||
) % {
|
||||
'base_2': pretty_size(storage_used_space),
|
||||
'base_10': pretty_size_10(storage_used_space),
|
||||
'bytes': storage_used_space
|
||||
}
|
||||
)
|
||||
except NotImplementedError:
|
||||
pass
|
||||
|
||||
results.extend([
|
||||
_('Document pages in database: %d') % DocumentPage.objects.only('pk',).count(),
|
||||
])
|
||||
results.extend(
|
||||
[
|
||||
_(
|
||||
'Document pages in database: %d'
|
||||
) % DocumentPage.objects.only('pk',).count(),
|
||||
]
|
||||
)
|
||||
|
||||
return results
|
||||
|
||||
@@ -27,16 +27,31 @@ def task_check_delete_periods():
|
||||
logger.info('Executing')
|
||||
|
||||
for document_type in DocumentType.objects.all():
|
||||
logger.info('Checking deletion period of document type: %s', document_type)
|
||||
logger.info(
|
||||
'Checking deletion period of document type: %s', document_type
|
||||
)
|
||||
if document_type.delete_time_period and document_type.delete_time_unit:
|
||||
delta = timedelta(**{document_type.delete_time_unit: document_type.delete_time_period})
|
||||
logger.info('Document type: %s, has a deletion period delta of: %s', document_type, delta)
|
||||
delta = timedelta(
|
||||
**{
|
||||
document_type.delete_time_unit: document_type.delete_time_period
|
||||
}
|
||||
)
|
||||
logger.info(
|
||||
'Document type: %s, has a deletion period delta of: %s',
|
||||
document_type, delta
|
||||
)
|
||||
for document in DeletedDocument.objects.filter(document_type=document_type):
|
||||
if now() > document.deleted_date_time + delta:
|
||||
logger.info('Document "%s" with id: %d, trashed on: %s, exceded delete period', document, document.pk, document.deleted_date_time)
|
||||
logger.info(
|
||||
'Document "%s" with id: %d, trashed on: %s, exceded '
|
||||
'delete period', document, document.pk,
|
||||
document.deleted_date_time
|
||||
)
|
||||
document.delete()
|
||||
else:
|
||||
logger.info('Document type: %s, has a no retention delta', document_type)
|
||||
logger.info(
|
||||
'Document type: %s, has a no retention delta', document_type
|
||||
)
|
||||
|
||||
logger.info('Finshed')
|
||||
|
||||
@@ -46,16 +61,31 @@ def task_check_trash_periods():
|
||||
logger.info('Executing')
|
||||
|
||||
for document_type in DocumentType.objects.all():
|
||||
logger.info('Checking trash period of document type: %s', document_type)
|
||||
logger.info(
|
||||
'Checking trash period of document type: %s', document_type
|
||||
)
|
||||
if document_type.trash_time_period and document_type.trash_time_unit:
|
||||
delta = timedelta(**{document_type.trash_time_unit: document_type.trash_time_period})
|
||||
logger.info('Document type: %s, has a trash period delta of: %s', document_type, delta)
|
||||
delta = timedelta(
|
||||
**{
|
||||
document_type.trash_time_unit: document_type.trash_time_period
|
||||
}
|
||||
)
|
||||
logger.info(
|
||||
'Document type: %s, has a trash period delta of: %s',
|
||||
document_type, delta
|
||||
)
|
||||
for document in Document.objects.filter(document_type=document_type):
|
||||
if now() > document.date_added + delta:
|
||||
logger.info('Document "%s" with id: %d, added on: %s, exceded trash period', document, document.pk, document.date_added)
|
||||
logger.info(
|
||||
'Document "%s" with id: %d, added on: %s, exceded '
|
||||
'trash period', document, document.pk,
|
||||
document.date_added
|
||||
)
|
||||
document.delete()
|
||||
else:
|
||||
logger.info('Document type: %s, has a no retention delta', document_type)
|
||||
logger.info(
|
||||
'Document type: %s, has a no retention delta', document_type
|
||||
)
|
||||
|
||||
logger.info('Finshed')
|
||||
|
||||
@@ -80,7 +110,11 @@ def task_update_page_count(self, version_id):
|
||||
try:
|
||||
document_version.update_page_count()
|
||||
except OperationalError as exception:
|
||||
logger.warning('Operational error during attempt to update page count for document version: %s; %s. Retrying.', document_version, exception)
|
||||
logger.warning(
|
||||
'Operational error during attempt to update page count for '
|
||||
'document version: %s; %s. Retrying.', document_version,
|
||||
exception
|
||||
)
|
||||
raise self.retry(exc=exception)
|
||||
|
||||
|
||||
@@ -88,63 +122,100 @@ def task_update_page_count(self, version_id):
|
||||
def task_upload_new_document(self, document_type_id, shared_uploaded_file_id, description=None, label=None, language=None, user_id=None):
|
||||
try:
|
||||
document_type = DocumentType.objects.get(pk=document_type_id)
|
||||
shared_file = SharedUploadedFile.objects.get(pk=shared_uploaded_file_id)
|
||||
shared_file = SharedUploadedFile.objects.get(
|
||||
pk=shared_uploaded_file_id
|
||||
)
|
||||
if user_id:
|
||||
user = User.objects.get(pk=user_id)
|
||||
else:
|
||||
user = None
|
||||
|
||||
except OperationalError as exception:
|
||||
logger.warning('Operational error during attempt to gather data for new document: %s; Retrying.', exception)
|
||||
logger.warning(
|
||||
'Operational error during attempt to gather data for new '
|
||||
'document: %s; Retrying.', exception
|
||||
)
|
||||
raise self.retry(exc=exception)
|
||||
|
||||
try:
|
||||
with shared_file.open as file_object:
|
||||
document_version = document_type.new_document(self, file_object=file_object, label=label, description=description, language=language, _user=user)
|
||||
document_version = document_type.new_document(
|
||||
self, file_object=file_object, label=label,
|
||||
description=description, language=language, _user=user
|
||||
)
|
||||
except OperationalError as exception:
|
||||
logger.warning('Operational error during attempt to gather data for new document: %s; Retrying.', exception)
|
||||
logger.warning(
|
||||
'Operational error during attempt to gather data for new '
|
||||
'document: %s; Retrying.', exception
|
||||
)
|
||||
raise self.retry(exc=exception)
|
||||
|
||||
try:
|
||||
shared_file.delete()
|
||||
except OperationalError as exception:
|
||||
logger.warning('Operational error while trying to delete shared file used to upload new document: %s; %s. Retrying.', document_version.document, exception)
|
||||
logger.warning(
|
||||
'Operational error while trying to delete shared file used to '
|
||||
'upload new document: %s; %s. Retrying.',
|
||||
document_version.document, exception
|
||||
)
|
||||
|
||||
|
||||
@app.task(bind=True, default_retry_delay=UPLOAD_NEW_VERSION_RETRY_DELAY, ignore_result=True)
|
||||
def task_upload_new_version(self, document_id, shared_uploaded_file_id, user_id, comment=None):
|
||||
try:
|
||||
document = Document.objects.get(pk=document_id)
|
||||
shared_file = SharedUploadedFile.objects.get(pk=shared_uploaded_file_id)
|
||||
shared_file = SharedUploadedFile.objects.get(
|
||||
pk=shared_uploaded_file_id
|
||||
)
|
||||
if user_id:
|
||||
user = User.objects.get(pk=user_id)
|
||||
else:
|
||||
user = None
|
||||
|
||||
except OperationalError as exception:
|
||||
logger.warning('Operational error during attempt to retrieve shared data for new document version for:%s; %s. Retrying.', document, exception)
|
||||
logger.warning(
|
||||
'Operational error during attempt to retrieve shared data for '
|
||||
'new document version for:%s; %s. Retrying.', document, exception
|
||||
)
|
||||
raise self.retry(exc=exception)
|
||||
|
||||
with shared_file.open() as file_object:
|
||||
document_version = DocumentVersion(document=document, comment=comment, file=file_object)
|
||||
document_version = DocumentVersion(
|
||||
document=document, comment=comment, file=file_object
|
||||
)
|
||||
try:
|
||||
document_version.save(_user=user)
|
||||
except Warning as warning:
|
||||
# New document version are blocked
|
||||
logger.info('Warning during attempt to create new document version for document: %s; %s', document, warning)
|
||||
logger.info(
|
||||
'Warning during attempt to create new document version for '
|
||||
'document: %s; %s', document, warning
|
||||
)
|
||||
shared_file.delete()
|
||||
except OperationalError as exception:
|
||||
logger.warning('Operational error during attempt to create new document version for document: %s; %s. Retrying.', document, exception)
|
||||
logger.warning(
|
||||
'Operational error during attempt to create new document '
|
||||
'version for document: %s; %s. Retrying.', document, exception
|
||||
)
|
||||
raise self.retry(exc=exception)
|
||||
except Exception as exception:
|
||||
# This except and else block emulate a finally:
|
||||
logger.error('Unexpected error during attempt to create new document version for document: %s; %s', document, exception)
|
||||
logger.error(
|
||||
'Unexpected error during attempt to create new document '
|
||||
'version for document: %s; %s', document, exception
|
||||
)
|
||||
try:
|
||||
shared_file.delete()
|
||||
except OperationalError as exception:
|
||||
logger.warning('Operational error during attempt to delete shared file: %s; %s.', shared_file, exception)
|
||||
logger.warning(
|
||||
'Operational error during attempt to delete shared '
|
||||
'file: %s; %s.', shared_file, exception
|
||||
)
|
||||
else:
|
||||
try:
|
||||
shared_file.delete()
|
||||
except OperationalError as exception:
|
||||
logger.warning('Operational error during attempt to delete shared file: %s; %s.', shared_file, exception)
|
||||
logger.warning(
|
||||
'Operational error during attempt to delete shared '
|
||||
'file: %s; %s.', shared_file, exception
|
||||
)
|
||||
|
||||
@@ -22,13 +22,18 @@ from .test_models import (
|
||||
|
||||
class DocumentAPICreateDocumentTestCase(TestCase):
|
||||
"""
|
||||
Functional test to make sure all the moving parts to create a document from
|
||||
the API are working correctly
|
||||
Functional test to make sure all the moving parts to create a document
|
||||
from the API are working correctly
|
||||
"""
|
||||
|
||||
def setUp(self):
|
||||
self.admin_user = User.objects.create_superuser(username=TEST_ADMIN_USERNAME, email=TEST_ADMIN_EMAIL, password=TEST_ADMIN_PASSWORD)
|
||||
self.document_type = DocumentType.objects.create(label=TEST_DOCUMENT_TYPE)
|
||||
self.admin_user = User.objects.create_superuser(
|
||||
username=TEST_ADMIN_USERNAME, email=TEST_ADMIN_EMAIL,
|
||||
password=TEST_ADMIN_PASSWORD
|
||||
)
|
||||
self.document_type = DocumentType.objects.create(
|
||||
label=TEST_DOCUMENT_TYPE
|
||||
)
|
||||
|
||||
ocr_settings = self.document_type.ocr_settings
|
||||
ocr_settings.auto_ocr = False
|
||||
@@ -41,7 +46,12 @@ class DocumentAPICreateDocumentTestCase(TestCase):
|
||||
def test_uploading_a_document_using_token_auth(self):
|
||||
# Get the an user token
|
||||
token_client = APIClient()
|
||||
response = token_client.post(reverse('auth_token_obtain'), {'username': TEST_ADMIN_USERNAME, 'password': TEST_ADMIN_PASSWORD})
|
||||
response = token_client.post(
|
||||
reverse('auth_token_obtain'), {
|
||||
'username': TEST_ADMIN_USERNAME,
|
||||
'password': TEST_ADMIN_PASSWORD
|
||||
}
|
||||
)
|
||||
|
||||
# Be able to get authentication token
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
@@ -66,17 +76,26 @@ class DocumentAPICreateDocumentTestCase(TestCase):
|
||||
|
||||
# Create a blank document
|
||||
with open(TEST_SMALL_DOCUMENT_PATH) as file_descriptor:
|
||||
document_response = document_client.post(reverse('document-list'), {'document_type': self.document_type.pk, 'file': file_descriptor})
|
||||
document_response = document_client.post(
|
||||
reverse('document-list'), {
|
||||
'document_type': self.document_type.pk,
|
||||
'file': file_descriptor
|
||||
}
|
||||
)
|
||||
|
||||
self.assertEqual(document_response.status_code, status.HTTP_201_CREATED)
|
||||
|
||||
# The document was created in the DB?
|
||||
self.assertEqual(Document.objects.count(), 1)
|
||||
|
||||
new_version_url = reverse('document-new-version', args=[Document.objects.first().pk])
|
||||
new_version_url = reverse(
|
||||
'document-new-version', args=[Document.objects.first().pk]
|
||||
)
|
||||
|
||||
with open(TEST_DOCUMENT_PATH) as file_descriptor:
|
||||
response = document_client.post(new_version_url, {'file': file_descriptor})
|
||||
response = document_client.post(
|
||||
new_version_url, {'file': file_descriptor}
|
||||
)
|
||||
|
||||
# Make sure the document uploaded correctly
|
||||
document = Document.objects.first()
|
||||
@@ -86,13 +105,20 @@ class DocumentAPICreateDocumentTestCase(TestCase):
|
||||
self.assertEqual(document.file_mimetype, 'application/pdf')
|
||||
self.assertEqual(document.file_mime_encoding, 'binary')
|
||||
self.assertEqual(document.label, TEST_SMALL_DOCUMENT_FILENAME)
|
||||
self.assertEqual(document.checksum, 'c637ffab6b8bb026ed3784afdb07663fddc60099853fae2be93890852a69ecf3')
|
||||
self.assertEqual(
|
||||
document.checksum,
|
||||
'c637ffab6b8bb026ed3784afdb07663fddc60099853fae2be93890852a69ecf3'
|
||||
)
|
||||
self.assertEqual(document.page_count, 47)
|
||||
|
||||
# Make sure we can edit the document via the API
|
||||
document_url = reverse('document-detail', args=[Document.objects.first().pk])
|
||||
document_url = reverse(
|
||||
'document-detail', args=[Document.objects.first().pk]
|
||||
)
|
||||
|
||||
response = document_client.post(document_url, {'description': 'edited test document'})
|
||||
response = document_client.post(
|
||||
document_url, {'description': 'edited test document'}
|
||||
)
|
||||
|
||||
# self.assertTrue(document.description, 'edited test document')
|
||||
|
||||
|
||||
@@ -17,26 +17,47 @@ TEST_COMPRESSED_DOCUMENTS_FILENAME = 'compressed_documents.zip'
|
||||
TEST_SMALL_DOCUMENT_FILENAME = 'title_page.png'
|
||||
TEST_NON_ASCII_DOCUMENT_FILENAME = 'I18N_title_áéíóúüñÑ.png'
|
||||
TEST_NON_ASCII_COMPRESSED_DOCUMENT_FILENAME = 'I18N_title_áéíóúüñÑ.png.zip'
|
||||
TEST_DOCUMENT_PATH = os.path.join(settings.BASE_DIR, 'contrib', 'sample_documents', 'mayan_11_1.pdf')
|
||||
TEST_SMALL_DOCUMENT_PATH = os.path.join(settings.BASE_DIR, 'contrib', 'sample_documents', TEST_SMALL_DOCUMENT_FILENAME)
|
||||
TEST_NON_ASCII_DOCUMENT_PATH = os.path.join(settings.BASE_DIR, 'contrib', 'sample_documents', TEST_NON_ASCII_DOCUMENT_FILENAME)
|
||||
TEST_NON_ASCII_COMPRESSED_DOCUMENT_PATH = os.path.join(settings.BASE_DIR, 'contrib', 'sample_documents', TEST_NON_ASCII_COMPRESSED_DOCUMENT_FILENAME)
|
||||
TEST_DEU_DOCUMENT_PATH = os.path.join(settings.BASE_DIR, 'contrib', 'sample_documents', TEST_DEU_DOCUMENT_FILENAME)
|
||||
TEST_COMPRESSED_DOCUMENT_PATH = os.path.join(settings.BASE_DIR, 'contrib', 'sample_documents', TEST_COMPRESSED_DOCUMENTS_FILENAME)
|
||||
TEST_DOCUMENT_PATH = os.path.join(
|
||||
settings.BASE_DIR, 'contrib', 'sample_documents', 'mayan_11_1.pdf'
|
||||
)
|
||||
TEST_SMALL_DOCUMENT_PATH = os.path.join(
|
||||
settings.BASE_DIR, 'contrib', 'sample_documents',
|
||||
TEST_SMALL_DOCUMENT_FILENAME
|
||||
)
|
||||
TEST_NON_ASCII_DOCUMENT_PATH = os.path.join(
|
||||
settings.BASE_DIR, 'contrib', 'sample_documents',
|
||||
TEST_NON_ASCII_DOCUMENT_FILENAME
|
||||
)
|
||||
TEST_NON_ASCII_COMPRESSED_DOCUMENT_PATH = os.path.join(
|
||||
settings.BASE_DIR, 'contrib', 'sample_documents',
|
||||
TEST_NON_ASCII_COMPRESSED_DOCUMENT_FILENAME
|
||||
)
|
||||
TEST_DEU_DOCUMENT_PATH = os.path.join(
|
||||
settings.BASE_DIR, 'contrib', 'sample_documents',
|
||||
TEST_DEU_DOCUMENT_FILENAME
|
||||
)
|
||||
TEST_COMPRESSED_DOCUMENT_PATH = os.path.join(
|
||||
settings.BASE_DIR, 'contrib', 'sample_documents',
|
||||
TEST_COMPRESSED_DOCUMENTS_FILENAME
|
||||
)
|
||||
TEST_DOCUMENT_DESCRIPTION = 'test description'
|
||||
TEST_DOCUMENT_TYPE = 'test_document_type'
|
||||
|
||||
|
||||
class DocumentTestCase(TestCase):
|
||||
def setUp(self):
|
||||
self.document_type = DocumentType.objects.create(label=TEST_DOCUMENT_TYPE)
|
||||
self.document_type = DocumentType.objects.create(
|
||||
label=TEST_DOCUMENT_TYPE
|
||||
)
|
||||
|
||||
ocr_settings = self.document_type.ocr_settings
|
||||
ocr_settings.auto_ocr = False
|
||||
ocr_settings.save()
|
||||
|
||||
with open(TEST_DOCUMENT_PATH) as file_object:
|
||||
self.document = self.document_type.new_document(file_object=File(file_object), label='mayan_11_1.pdf')
|
||||
self.document = self.document_type.new_document(
|
||||
file_object=File(file_object), label='mayan_11_1.pdf'
|
||||
)
|
||||
|
||||
def tearDown(self):
|
||||
self.document_type.delete()
|
||||
@@ -50,7 +71,10 @@ class DocumentTestCase(TestCase):
|
||||
self.assertEqual(self.document.file_mimetype, 'application/pdf')
|
||||
self.assertEqual(self.document.file_mime_encoding, 'binary')
|
||||
self.assertEqual(self.document.label, 'mayan_11_1.pdf')
|
||||
self.assertEqual(self.document.checksum, 'c637ffab6b8bb026ed3784afdb07663fddc60099853fae2be93890852a69ecf3')
|
||||
self.assertEqual(
|
||||
self.document.checksum,
|
||||
'c637ffab6b8bb026ed3784afdb07663fddc60099853fae2be93890852a69ecf3'
|
||||
)
|
||||
self.assertEqual(self.document.page_count, 47)
|
||||
|
||||
def test_version_creation(self):
|
||||
@@ -58,7 +82,9 @@ class DocumentTestCase(TestCase):
|
||||
self.document.new_version(file_object=File(file_object))
|
||||
|
||||
with open(TEST_SMALL_DOCUMENT_PATH) as file_object:
|
||||
self.document.new_version(file_object=File(file_object), comment='test comment 1')
|
||||
self.document.new_version(
|
||||
file_object=File(file_object), comment='test comment 1'
|
||||
)
|
||||
|
||||
self.assertEqual(self.document.versions.count(), 3)
|
||||
|
||||
|
||||
@@ -22,20 +22,29 @@ class DocumentsViewsFunctionalTestCase(TestCase):
|
||||
"""
|
||||
|
||||
def setUp(self):
|
||||
self.document_type = DocumentType.objects.create(label=TEST_DOCUMENT_TYPE)
|
||||
self.document_type = DocumentType.objects.create(
|
||||
label=TEST_DOCUMENT_TYPE
|
||||
)
|
||||
ocr_settings = self.document_type.ocr_settings
|
||||
ocr_settings.auto_ocr = False
|
||||
ocr_settings.save()
|
||||
|
||||
self.admin_user = User.objects.create_superuser(username=TEST_ADMIN_USERNAME, email=TEST_ADMIN_EMAIL, password=TEST_ADMIN_PASSWORD)
|
||||
self.admin_user = User.objects.create_superuser(
|
||||
username=TEST_ADMIN_USERNAME, email=TEST_ADMIN_EMAIL,
|
||||
password=TEST_ADMIN_PASSWORD
|
||||
)
|
||||
self.client = Client()
|
||||
# Login the admin user
|
||||
logged_in = self.client.login(username=TEST_ADMIN_USERNAME, password=TEST_ADMIN_PASSWORD)
|
||||
logged_in = self.client.login(
|
||||
username=TEST_ADMIN_USERNAME, password=TEST_ADMIN_PASSWORD
|
||||
)
|
||||
self.assertTrue(logged_in)
|
||||
self.assertTrue(self.admin_user.is_authenticated())
|
||||
|
||||
with open(TEST_SMALL_DOCUMENT_PATH) as file_object:
|
||||
self.document = self.document_type.new_document(file_object=File(file_object), label='mayan_11_1.pdf')
|
||||
self.document = self.document_type.new_document(
|
||||
file_object=File(file_object), label='mayan_11_1.pdf'
|
||||
)
|
||||
|
||||
def tearDown(self):
|
||||
self.document_type.delete()
|
||||
@@ -45,12 +54,16 @@ class DocumentsViewsFunctionalTestCase(TestCase):
|
||||
self.assertEqual(Document.objects.count(), 1)
|
||||
|
||||
# Trash the document
|
||||
self.client.post(reverse('documents:document_trash', args=[self.document.pk]))
|
||||
self.client.post(
|
||||
reverse('documents:document_trash', args=[self.document.pk])
|
||||
)
|
||||
self.assertEqual(DeletedDocument.objects.count(), 1)
|
||||
self.assertEqual(Document.objects.count(), 0)
|
||||
|
||||
# Restore the document
|
||||
self.client.post(reverse('documents:document_restore', args=[self.document.pk]))
|
||||
self.client.post(
|
||||
reverse('documents:document_restore', args=[self.document.pk])
|
||||
)
|
||||
self.assertEqual(DeletedDocument.objects.count(), 0)
|
||||
self.assertEqual(Document.objects.count(), 1)
|
||||
|
||||
@@ -58,12 +71,16 @@ class DocumentsViewsFunctionalTestCase(TestCase):
|
||||
self.assertEqual(Document.objects.count(), 1)
|
||||
|
||||
# Trash the document
|
||||
self.client.post(reverse('documents:document_trash', args=[self.document.pk]))
|
||||
self.client.post(
|
||||
reverse('documents:document_trash', args=[self.document.pk])
|
||||
)
|
||||
self.assertEqual(DeletedDocument.objects.count(), 1)
|
||||
self.assertEqual(Document.objects.count(), 0)
|
||||
|
||||
# Delete the document
|
||||
self.client.post(reverse('documents:document_delete', args=[self.document.pk]))
|
||||
self.client.post(
|
||||
reverse('documents:document_delete', args=[self.document.pk])
|
||||
)
|
||||
self.assertEqual(DeletedDocument.objects.count(), 0)
|
||||
self.assertEqual(Document.objects.count(), 0)
|
||||
|
||||
@@ -72,8 +89,12 @@ class DocumentsViewsFunctionalTestCase(TestCase):
|
||||
self.assertContains(response, 'Total: 1', status_code=200)
|
||||
|
||||
# test document simple view
|
||||
response = self.client.get(reverse('documents:document_properties', args=[self.document.pk]))
|
||||
self.assertContains(response, 'roperties for document', status_code=200)
|
||||
response = self.client.get(
|
||||
reverse('documents:document_properties', args=[self.document.pk])
|
||||
)
|
||||
self.assertContains(
|
||||
response, 'roperties for document', status_code=200
|
||||
)
|
||||
|
||||
def test_document_type_views(self):
|
||||
# Check that there are no document types
|
||||
@@ -81,8 +102,12 @@ class DocumentsViewsFunctionalTestCase(TestCase):
|
||||
self.assertContains(response, 'Total: 1', status_code=200)
|
||||
|
||||
# Create a document type
|
||||
response = self.client.post(reverse('documents:document_type_create'), {'name': 'test document type 2'}, follow=True)
|
||||
#TODO: FIX self.assertContains(response, 'successfully', status_code=200)
|
||||
response = self.client.post(
|
||||
reverse('documents:document_type_create'),
|
||||
{'name': 'test document type 2'}, follow=True
|
||||
)
|
||||
#TODO: FIX
|
||||
# self.assertContains(response, 'successfully', status_code=200)
|
||||
|
||||
# Check that there are two document types
|
||||
response = self.client.get(reverse('documents:document_type_list'))
|
||||
@@ -91,18 +116,42 @@ class DocumentsViewsFunctionalTestCase(TestCase):
|
||||
self.assertEqual(self.document_type.label, TEST_DOCUMENT_TYPE)
|
||||
|
||||
# Edit the document type
|
||||
response = self.client.post(reverse('documents:document_type_edit', args=[self.document_type.pk]), data={'name': TEST_DOCUMENT_TYPE + 'partial'}, follow=True)
|
||||
#TODO: FIX self.assertContains(response, 'Document type edited successfully', status_code=200)
|
||||
response = self.client.post(
|
||||
reverse(
|
||||
'documents:document_type_edit', args=[self.document_type.pk]
|
||||
), data={'name': TEST_DOCUMENT_TYPE + 'partial'}, follow=True
|
||||
)
|
||||
#TODO: FIX
|
||||
# self.assertContains(
|
||||
# response, 'Document type edited successfully', status_code=200
|
||||
#)
|
||||
|
||||
# Reload document type model data
|
||||
#self.document_type = DocumentType.objects.get(pk=self.document_type.pk)
|
||||
#TODO: FIX self.assertEqual(self.document_type.name, TEST_DOCUMENT_TYPE + 'partial')
|
||||
#self.document_type = DocumentType.objects.get(
|
||||
# pk=self.document_type.pk
|
||||
#)
|
||||
#TODO: FIX#
|
||||
#self.assertEqual(
|
||||
# self.document_type.name, TEST_DOCUMENT_TYPE + 'partial'
|
||||
#)
|
||||
|
||||
# Delete the document type
|
||||
response = self.client.post(reverse('documents:document_type_delete', args=[self.document_type.pk]), follow=True)
|
||||
#TODO: FIX self.assertContains(response, 'Document type: {0} deleted successfully'.format(self.document_type.name), status_code=200)
|
||||
#response = self.client.post(
|
||||
# reverse(
|
||||
# 'documents:document_type_delete', args=[self.document_type.pk]
|
||||
# ), follow=True
|
||||
#)
|
||||
#TODO: FIX#
|
||||
# self.assertContains(
|
||||
# response, 'Document type: {0} deleted successfully'.format(
|
||||
# self.document_type.name
|
||||
# ), status_code=200
|
||||
#)
|
||||
|
||||
# Check that there are no document types
|
||||
response = self.client.get(reverse('documents:document_type_list'))
|
||||
#response = self.client.get(reverse('documents:document_type_list'))
|
||||
#TODO: FIX self.assertEqual(response.status_code, 200)
|
||||
#TODO: FIX self.assertContains(response, 'ocument types (0)', status_code=200)
|
||||
#TODO: FIX
|
||||
#self.assertContains(
|
||||
# response, 'ocument types (0)', status_code=200
|
||||
#)
|
||||
|
||||
+232
-56
@@ -21,74 +21,250 @@ from .views import (
|
||||
urlpatterns = patterns(
|
||||
'documents.views',
|
||||
url(r'^list/$', DocumentListView.as_view(), name='document_list'),
|
||||
url(r'^list/recent/$', RecentDocumentListView.as_view(), name='document_list_recent'),
|
||||
url(r'^list/deleted/$', DeletedDocumentListView.as_view(), name='document_list_deleted'),
|
||||
url(
|
||||
r'^list/recent/$', RecentDocumentListView.as_view(),
|
||||
name='document_list_recent'
|
||||
),
|
||||
url(
|
||||
r'^list/deleted/$', DeletedDocumentListView.as_view(),
|
||||
name='document_list_deleted'
|
||||
),
|
||||
|
||||
url(r'^(?P<document_id>\d+)/preview/$', 'document_preview', name='document_preview'),
|
||||
url(r'^(?P<document_id>\d+)/properties/$', 'document_properties', name='document_properties'),
|
||||
url(r'^(?P<pk>\d+)/restore/$', DocumentRestoreView.as_view(), name='document_restore'),
|
||||
url(r'^multiple/restore/$', DocumentManyRestoreView.as_view(), name='document_multiple_restore'),
|
||||
url(r'^(?P<pk>\d+)/delete/$', DeletedDocumentDeleteView.as_view(), name='document_delete'),
|
||||
url(r'^multiple/delete/$', DocumentManyDeleteView.as_view(), name='document_multiple_delete'),
|
||||
url(r'^(?P<document_id>\d+)/type/$', 'document_document_type_edit', name='document_document_type_edit'),
|
||||
url(r'^multiple/type/$', 'document_multiple_document_type_edit', name='document_multiple_document_type_edit'),
|
||||
url(r'^(?P<document_id>\d+)/trash/$', 'document_trash', name='document_trash'),
|
||||
url(r'^multiple/trash/$', 'document_multiple_trash', name='document_multiple_trash'),
|
||||
url(r'^(?P<document_id>\d+)/edit/$', 'document_edit', name='document_edit'),
|
||||
url(r'^(?P<document_id>\d+)/print/$', 'document_print', name='document_print'),
|
||||
url(r'^(?P<document_id>\d+)/reset_page_count/$', 'document_update_page_count', name='document_update_page_count'),
|
||||
url(r'^multiple/reset_page_count/$', 'document_multiple_update_page_count', name='document_multiple_update_page_count'),
|
||||
url(
|
||||
r'^(?P<document_id>\d+)/preview/$', 'document_preview',
|
||||
name='document_preview'
|
||||
),
|
||||
url(
|
||||
r'^(?P<document_id>\d+)/properties/$', 'document_properties',
|
||||
name='document_properties'
|
||||
),
|
||||
url(
|
||||
r'^(?P<pk>\d+)/restore/$', DocumentRestoreView.as_view(),
|
||||
name='document_restore'
|
||||
),
|
||||
url(
|
||||
r'^multiple/restore/$', DocumentManyRestoreView.as_view(),
|
||||
name='document_multiple_restore'
|
||||
),
|
||||
url(
|
||||
r'^(?P<pk>\d+)/delete/$', DeletedDocumentDeleteView.as_view(),
|
||||
name='document_delete'
|
||||
),
|
||||
url(
|
||||
r'^multiple/delete/$', DocumentManyDeleteView.as_view(),
|
||||
name='document_multiple_delete'
|
||||
),
|
||||
url(
|
||||
r'^(?P<document_id>\d+)/type/$', 'document_document_type_edit',
|
||||
name='document_document_type_edit'
|
||||
),
|
||||
url(
|
||||
r'^multiple/type/$', 'document_multiple_document_type_edit',
|
||||
name='document_multiple_document_type_edit'
|
||||
),
|
||||
url(
|
||||
r'^(?P<document_id>\d+)/trash/$', 'document_trash',
|
||||
name='document_trash'
|
||||
),
|
||||
url(
|
||||
r'^multiple/trash/$', 'document_multiple_trash',
|
||||
name='document_multiple_trash'
|
||||
),
|
||||
url(
|
||||
r'^(?P<document_id>\d+)/edit/$', 'document_edit',
|
||||
name='document_edit'
|
||||
),
|
||||
url(
|
||||
r'^(?P<document_id>\d+)/print/$', 'document_print',
|
||||
name='document_print'
|
||||
),
|
||||
url(
|
||||
r'^(?P<document_id>\d+)/reset_page_count/$',
|
||||
'document_update_page_count', name='document_update_page_count'
|
||||
),
|
||||
url(
|
||||
r'^multiple/reset_page_count/$',
|
||||
'document_multiple_update_page_count',
|
||||
name='document_multiple_update_page_count'
|
||||
),
|
||||
|
||||
url(r'^(?P<document_id>\d+)/display/$', 'get_document_image', {'size': setting_display_size.value}, 'document_display'),
|
||||
url(r'^(?P<document_id>\d+)/display/print/$', 'get_document_image', {'size': setting_print_size.value}, 'document_display_print'),
|
||||
url(
|
||||
r'^(?P<document_id>\d+)/display/$', 'get_document_image', {
|
||||
'size': setting_display_size.value
|
||||
}, 'document_display'
|
||||
),
|
||||
url(
|
||||
r'^(?P<document_id>\d+)/display/print/$', 'get_document_image', {
|
||||
'size': setting_print_size.value
|
||||
}, 'document_display_print'
|
||||
),
|
||||
|
||||
url(r'^(?P<document_id>\d+)/download/$', 'document_download', name='document_download'),
|
||||
url(r'^multiple/download/$', 'document_multiple_download', name='document_multiple_download'),
|
||||
url(r'^(?P<document_id>\d+)/clear_transformations/$', 'document_clear_transformations', name='document_clear_transformations'),
|
||||
url(
|
||||
r'^(?P<document_id>\d+)/download/$', 'document_download',
|
||||
name='document_download'
|
||||
),
|
||||
url(
|
||||
r'^multiple/download/$', 'document_multiple_download',
|
||||
name='document_multiple_download'
|
||||
),
|
||||
url(
|
||||
r'^(?P<document_id>\d+)/clear_transformations/$',
|
||||
'document_clear_transformations',
|
||||
name='document_clear_transformations'
|
||||
),
|
||||
|
||||
url(r'^(?P<document_pk>\d+)/version/all/$', 'document_version_list', name='document_version_list'),
|
||||
url(r'^document/version/(?P<document_version_pk>\d+)/download/$', 'document_download', name='document_version_download'),
|
||||
url(r'^document/version/(?P<document_version_pk>\d+)/revert/$', 'document_version_revert', name='document_version_revert'),
|
||||
url(
|
||||
r'^(?P<document_pk>\d+)/version/all/$', 'document_version_list',
|
||||
name='document_version_list'
|
||||
),
|
||||
url(
|
||||
r'^document/version/(?P<document_version_pk>\d+)/download/$',
|
||||
'document_download', name='document_version_download'
|
||||
),
|
||||
url(
|
||||
r'^document/version/(?P<document_version_pk>\d+)/revert/$',
|
||||
'document_version_revert', name='document_version_revert'
|
||||
),
|
||||
|
||||
url(r'^(?P<pk>\d+)/pages/all/$', DocumentPageListView.as_view(), name='document_pages'),
|
||||
url(
|
||||
r'^(?P<pk>\d+)/pages/all/$', DocumentPageListView.as_view(),
|
||||
name='document_pages'
|
||||
),
|
||||
|
||||
url(r'^multiple/clear_transformations/$', 'document_multiple_clear_transformations', name='document_multiple_clear_transformations'),
|
||||
url(r'^cache/clear/$', 'document_clear_image_cache', name='document_clear_image_cache'),
|
||||
url(r'^trash_can/empty/$', EmptyTrashCanView.as_view(), name='trash_can_empty'),
|
||||
url(
|
||||
r'^multiple/clear_transformations/$',
|
||||
'document_multiple_clear_transformations',
|
||||
name='document_multiple_clear_transformations'
|
||||
),
|
||||
url(
|
||||
r'^cache/clear/$', 'document_clear_image_cache',
|
||||
name='document_clear_image_cache'
|
||||
),
|
||||
url(
|
||||
r'^trash_can/empty/$', EmptyTrashCanView.as_view(),
|
||||
name='trash_can_empty'
|
||||
),
|
||||
|
||||
url(r'^page/(?P<document_page_id>\d+)/$', 'document_page_view', name='document_page_view'),
|
||||
url(r'^page/(?P<document_page_id>\d+)/navigation/next/$', 'document_page_navigation_next', name='document_page_navigation_next'),
|
||||
url(r'^page/(?P<document_page_id>\d+)/navigation/previous/$', 'document_page_navigation_previous', name='document_page_navigation_previous'),
|
||||
url(r'^page/(?P<document_page_id>\d+)/navigation/first/$', 'document_page_navigation_first', name='document_page_navigation_first'),
|
||||
url(r'^page/(?P<document_page_id>\d+)/navigation/last/$', 'document_page_navigation_last', name='document_page_navigation_last'),
|
||||
url(r'^page/(?P<document_page_id>\d+)/zoom/in/$', 'document_page_zoom_in', name='document_page_zoom_in'),
|
||||
url(r'^page/(?P<document_page_id>\d+)/zoom/out/$', 'document_page_zoom_out', name='document_page_zoom_out'),
|
||||
url(r'^page/(?P<document_page_id>\d+)/rotate/right/$', 'document_page_rotate_right', name='document_page_rotate_right'),
|
||||
url(r'^page/(?P<document_page_id>\d+)/rotate/left/$', 'document_page_rotate_left', name='document_page_rotate_left'),
|
||||
url(r'^page/(?P<document_page_id>\d+)/reset/$', 'document_page_view_reset', name='document_page_view_reset'),
|
||||
url(
|
||||
r'^page/(?P<document_page_id>\d+)/$', 'document_page_view',
|
||||
name='document_page_view'
|
||||
),
|
||||
url(
|
||||
r'^page/(?P<document_page_id>\d+)/navigation/next/$',
|
||||
'document_page_navigation_next', name='document_page_navigation_next'
|
||||
),
|
||||
url(
|
||||
r'^page/(?P<document_page_id>\d+)/navigation/previous/$',
|
||||
'document_page_navigation_previous',
|
||||
name='document_page_navigation_previous'
|
||||
),
|
||||
url(
|
||||
r'^page/(?P<document_page_id>\d+)/navigation/first/$',
|
||||
'document_page_navigation_first', name='document_page_navigation_first'
|
||||
),
|
||||
url(
|
||||
r'^page/(?P<document_page_id>\d+)/navigation/last/$',
|
||||
'document_page_navigation_last', name='document_page_navigation_last'
|
||||
),
|
||||
url(
|
||||
r'^page/(?P<document_page_id>\d+)/zoom/in/$',
|
||||
'document_page_zoom_in', name='document_page_zoom_in'
|
||||
),
|
||||
url(
|
||||
r'^page/(?P<document_page_id>\d+)/zoom/out/$',
|
||||
'document_page_zoom_out', name='document_page_zoom_out'
|
||||
),
|
||||
url(
|
||||
r'^page/(?P<document_page_id>\d+)/rotate/right/$',
|
||||
'document_page_rotate_right', name='document_page_rotate_right'
|
||||
),
|
||||
url(
|
||||
r'^page/(?P<document_page_id>\d+)/rotate/left/$',
|
||||
'document_page_rotate_left', name='document_page_rotate_left'
|
||||
),
|
||||
url(
|
||||
r'^page/(?P<document_page_id>\d+)/reset/$',
|
||||
'document_page_view_reset', name='document_page_view_reset'
|
||||
),
|
||||
|
||||
# Admin views
|
||||
url(r'^type/list/$', DocumentTypeListView.as_view(), name='document_type_list'),
|
||||
url(r'^type/create/$', DocumentTypeCreateView.as_view(), name='document_type_create'),
|
||||
url(r'^type/(?P<pk>\d+)/edit/$', DocumentTypeEditView.as_view(), name='document_type_edit'),
|
||||
url(r'^type/(?P<pk>\d+)/delete/$', DocumentTypeDeleteView.as_view(), name='document_type_delete'),
|
||||
url(r'^type/(?P<pk>\d+)/documents/$', DocumentTypeDocumentListView.as_view(), name='document_type_document_list'),
|
||||
url(r'^type/(?P<document_type_id>\d+)/filename/list/$', 'document_type_filename_list', name='document_type_filename_list'),
|
||||
url(r'^type/filename/(?P<document_type_filename_id>\d+)/edit/$', 'document_type_filename_edit', name='document_type_filename_edit'),
|
||||
url(r'^type/filename/(?P<document_type_filename_id>\d+)/delete/$', 'document_type_filename_delete', name='document_type_filename_delete'),
|
||||
url(r'^type/(?P<document_type_id>\d+)/filename/create/$', 'document_type_filename_create', name='document_type_filename_create'),
|
||||
url(
|
||||
r'^type/list/$', DocumentTypeListView.as_view(),
|
||||
name='document_type_list'
|
||||
),
|
||||
url(
|
||||
r'^type/create/$', DocumentTypeCreateView.as_view(),
|
||||
name='document_type_create'
|
||||
),
|
||||
url(
|
||||
r'^type/(?P<pk>\d+)/edit/$', DocumentTypeEditView.as_view(),
|
||||
name='document_type_edit'
|
||||
),
|
||||
url(
|
||||
r'^type/(?P<pk>\d+)/delete/$', DocumentTypeDeleteView.as_view(),
|
||||
name='document_type_delete'
|
||||
),
|
||||
url(
|
||||
r'^type/(?P<pk>\d+)/documents/$',
|
||||
DocumentTypeDocumentListView.as_view(),
|
||||
name='document_type_document_list'
|
||||
),
|
||||
url(
|
||||
r'^type/(?P<document_type_id>\d+)/filename/list/$',
|
||||
'document_type_filename_list', name='document_type_filename_list'
|
||||
),
|
||||
url(
|
||||
r'^type/filename/(?P<document_type_filename_id>\d+)/edit/$',
|
||||
'document_type_filename_edit', name='document_type_filename_edit'
|
||||
),
|
||||
url(
|
||||
r'^type/filename/(?P<document_type_filename_id>\d+)/delete/$',
|
||||
'document_type_filename_delete', name='document_type_filename_delete'
|
||||
),
|
||||
url(
|
||||
r'^type/(?P<document_type_id>\d+)/filename/create/$',
|
||||
'document_type_filename_create', name='document_type_filename_create'
|
||||
),
|
||||
)
|
||||
|
||||
api_urls = patterns(
|
||||
'',
|
||||
url(r'^documents/$', APIDocumentListView.as_view(), name='document-list'),
|
||||
url(r'^documents/recent/$', APIRecentDocumentListView.as_view(), name='document-recent-list'),
|
||||
url(r'^documents/(?P<pk>[0-9]+)/$', APIDocumentView.as_view(), name='document-detail'),
|
||||
url(r'^document_version/(?P<pk>[0-9]+)/$', APIDocumentVersionView.as_view(), name='documentversion-detail'),
|
||||
url(r'^document_page/(?P<pk>[0-9]+)/$', APIDocumentPageView.as_view(), name='documentpage-detail'),
|
||||
url(r'^documents/(?P<pk>[0-9]+)/image/$', APIDocumentImageView.as_view(), name='document-image'),
|
||||
url(r'^documents/(?P<pk>[0-9]+)/new_version/$', APIDocumentVersionCreateView.as_view(), name='document-new-version'),
|
||||
url(r'^documenttypes/(?P<pk>[0-9]+)/documents/$', APIDocumentTypeDocumentListView.as_view(), name='documenttype-document-list'),
|
||||
url(r'^documenttypes/(?P<pk>[0-9]+)/$', APIDocumentTypeView.as_view(), name='documenttype-detail'),
|
||||
url(r'^documenttypes/$', APIDocumentTypeListView.as_view(), name='documenttype-list'),
|
||||
url(
|
||||
r'^documents/recent/$', APIRecentDocumentListView.as_view(),
|
||||
name='document-recent-list'
|
||||
),
|
||||
url(
|
||||
r'^documents/(?P<pk>[0-9]+)/$', APIDocumentView.as_view(),
|
||||
name='document-detail'
|
||||
),
|
||||
url(
|
||||
r'^document_version/(?P<pk>[0-9]+)/$',
|
||||
APIDocumentVersionView.as_view(), name='documentversion-detail'
|
||||
),
|
||||
url(
|
||||
r'^document_page/(?P<pk>[0-9]+)/$', APIDocumentPageView.as_view(),
|
||||
name='documentpage-detail'
|
||||
),
|
||||
url(
|
||||
r'^documents/(?P<pk>[0-9]+)/image/$', APIDocumentImageView.as_view(),
|
||||
name='document-image'
|
||||
),
|
||||
url(
|
||||
r'^documents/(?P<pk>[0-9]+)/new_version/$',
|
||||
APIDocumentVersionCreateView.as_view(), name='document-new-version'
|
||||
),
|
||||
url(
|
||||
r'^documenttypes/(?P<pk>[0-9]+)/documents/$',
|
||||
APIDocumentTypeDocumentListView.as_view(),
|
||||
name='documenttype-document-list'
|
||||
),
|
||||
url(
|
||||
r'^documenttypes/(?P<pk>[0-9]+)/$', APIDocumentTypeView.as_view(),
|
||||
name='documenttype-detail'
|
||||
),
|
||||
url(
|
||||
r'^documenttypes/$', APIDocumentTypeListView.as_view(),
|
||||
name='documenttype-list'
|
||||
),
|
||||
)
|
||||
|
||||
@@ -2,7 +2,8 @@ from __future__ import unicode_literals
|
||||
|
||||
|
||||
def parse_range(astr):
|
||||
# http://stackoverflow.com/questions/4248399/page-range-for-printing-algorithm
|
||||
# http://stackoverflow.com/questions/4248399/
|
||||
# page-range-for-printing-algorithm
|
||||
result = set()
|
||||
for part in astr.split(','):
|
||||
x = part.split('-')
|
||||
|
||||
@@ -41,8 +41,8 @@ from .forms import (
|
||||
)
|
||||
from .literals import DOCUMENT_IMAGE_TASK_TIMEOUT
|
||||
from .models import (
|
||||
DeletedDocument, Document, DocumentType, DocumentPage, DocumentTypeFilename,
|
||||
DocumentVersion, RecentDocument
|
||||
DeletedDocument, Document, DocumentType, DocumentPage,
|
||||
DocumentTypeFilename, DocumentVersion, RecentDocument
|
||||
)
|
||||
from .permissions import (
|
||||
permission_document_delete, permission_document_download,
|
||||
@@ -94,11 +94,17 @@ class DeletedDocumentListView(DocumentListView):
|
||||
queryset = Document.trash.all()
|
||||
|
||||
try:
|
||||
Permission.check_permissions(self.request.user, [permission_document_view])
|
||||
Permission.check_permissions(
|
||||
self.request.user, [permission_document_view]
|
||||
)
|
||||
except PermissionDenied:
|
||||
AccessControlList.objects.check_access(permission_document_view, self.request.user, queryset)
|
||||
AccessControlList.objects.check_access(
|
||||
permission_document_view, self.request.user, queryset
|
||||
)
|
||||
|
||||
return DeletedDocument.objects.filter(pk__in=queryset.values_list('pk', flat=True))
|
||||
return DeletedDocument.objects.filter(
|
||||
pk__in=queryset.values_list('pk', flat=True)
|
||||
)
|
||||
|
||||
|
||||
class DeletedDocumentDeleteView(ConfirmView):
|
||||
@@ -107,12 +113,18 @@ class DeletedDocumentDeleteView(ConfirmView):
|
||||
}
|
||||
|
||||
def object_action(self, request, instance):
|
||||
source_document = get_object_or_404(Document.passthrough, pk=instance.pk)
|
||||
source_document = get_object_or_404(
|
||||
Document.passthrough, pk=instance.pk
|
||||
)
|
||||
|
||||
try:
|
||||
Permission.check_permissions(request.user, [permission_document_delete])
|
||||
Permission.check_permissions(
|
||||
request.user, [permission_document_delete]
|
||||
)
|
||||
except PermissionDenied:
|
||||
AccessControlList.objects.check_access(permission_document_delete, request.user, source_document)
|
||||
AccessControlList.objects.check_access(
|
||||
permission_document_delete, request.user, source_document
|
||||
)
|
||||
|
||||
instance.delete()
|
||||
messages.success(request, _('Document: %(document)s deleted.') % {
|
||||
@@ -132,16 +144,24 @@ class DocumentRestoreView(ConfirmView):
|
||||
}
|
||||
|
||||
def object_action(self, request, instance):
|
||||
source_document = get_object_or_404(Document.passthrough, pk=instance.pk)
|
||||
source_document = get_object_or_404(
|
||||
Document.passthrough, pk=instance.pk
|
||||
)
|
||||
|
||||
try:
|
||||
Permission.check_permissions(request.user, [permission_document_restore])
|
||||
Permission.check_permissions(
|
||||
request.user, [permission_document_restore]
|
||||
)
|
||||
except PermissionDenied:
|
||||
AccessControlList.objects.check_access(permission_document_restore, request.user, source_document)
|
||||
AccessControlList.objects.check_access(
|
||||
permission_document_restore, request.user, source_document
|
||||
)
|
||||
|
||||
instance.restore()
|
||||
messages.success(request, _('Document: %(document)s restored.') % {
|
||||
'document': instance}
|
||||
messages.success(
|
||||
request, _('Document: %(document)s restored.') % {
|
||||
'document': instance
|
||||
}
|
||||
)
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
@@ -204,7 +224,9 @@ class EmptyTrashCanView(ConfirmView):
|
||||
'title': _('Empty trash?')
|
||||
}
|
||||
view_permission = permission_empty_trash
|
||||
action_cancel_redirect = post_action_redirect = reverse_lazy('documents:document_list_deleted')
|
||||
action_cancel_redirect = post_action_redirect = reverse_lazy(
|
||||
'documents:document_list_deleted'
|
||||
)
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
for deleted_document in DeletedDocument.objects.all():
|
||||
@@ -231,27 +253,43 @@ def document_properties(request, document_id):
|
||||
try:
|
||||
Permission.check_permissions(request.user, [permission_document_view])
|
||||
except PermissionDenied:
|
||||
AccessControlList.objects.check_access(permission_document_view, request.user, document)
|
||||
AccessControlList.objects.check_access(
|
||||
permission_document_view, request.user, document
|
||||
)
|
||||
|
||||
document.add_as_recent_document_for_user(request.user)
|
||||
|
||||
document_fields = [
|
||||
{'label': _('Date added'), 'field': lambda x: x.date_added.date()},
|
||||
{'label': _('Time added'), 'field': lambda x: unicode(x.date_added.time()).split('.')[0]},
|
||||
{
|
||||
'label': _('Time added'),
|
||||
'field': lambda x: unicode(x.date_added.time()).split('.')[0]
|
||||
},
|
||||
{'label': _('UUID'), 'field': 'uuid'},
|
||||
]
|
||||
if document.latest_version:
|
||||
document_fields.extend([
|
||||
{'label': _('File mimetype'), 'field': lambda x: x.file_mimetype or _('None')},
|
||||
{'label': _('File encoding'), 'field': lambda x: x.file_mime_encoding or _('None')},
|
||||
{'label': _('File size'), 'field': lambda x: pretty_size(x.size) if x.size else '-'},
|
||||
{
|
||||
'label': _('File mimetype'),
|
||||
'field': lambda x: x.file_mimetype or _('None')
|
||||
},
|
||||
{
|
||||
'label': _('File encoding'),
|
||||
'field': lambda x: x.file_mime_encoding or _('None')
|
||||
},
|
||||
{
|
||||
'label': _('File size'),
|
||||
'field': lambda x: pretty_size(x.size) if x.size else '-'
|
||||
},
|
||||
{'label': _('Exists in storage'), 'field': 'exists'},
|
||||
{'label': _('File path in storage'), 'field': 'file'},
|
||||
{'label': _('Checksum'), 'field': 'checksum'},
|
||||
{'label': _('Pages'), 'field': 'page_count'},
|
||||
])
|
||||
|
||||
document_properties_form = DocumentPropertiesForm(instance=document, extra_fields=document_fields)
|
||||
document_properties_form = DocumentPropertiesForm(
|
||||
instance=document, extra_fields=document_fields
|
||||
)
|
||||
|
||||
return render_to_response('appearance/generic_form.html', {
|
||||
'form': document_properties_form,
|
||||
@@ -268,7 +306,9 @@ def document_preview(request, document_id):
|
||||
try:
|
||||
Permission.check_permissions(request.user, [permission_document_view])
|
||||
except PermissionDenied:
|
||||
AccessControlList.objects.check_access(permission_document_view, request.user, document)
|
||||
AccessControlList.objects.check_access(
|
||||
permission_document_view, request.user, document
|
||||
)
|
||||
|
||||
document.add_as_recent_document_for_user(request.user)
|
||||
|
||||
@@ -291,15 +331,23 @@ def document_trash(request, document_id=None, document_id_list=None):
|
||||
documents = [get_object_or_404(Document, pk=document_id)]
|
||||
post_action_redirect = reverse('documents:document_list_recent')
|
||||
elif document_id_list:
|
||||
documents = [get_object_or_404(Document, pk=document_id) for document_id in document_id_list.split(',')]
|
||||
documents = [
|
||||
get_object_or_404(Document, pk=document_id) for document_id in document_id_list.split(',')
|
||||
]
|
||||
else:
|
||||
messages.error(request, _('Must provide at least one document.'))
|
||||
return HttpResponseRedirect(request.META.get('HTTP_REFERER', reverse(settings.LOGIN_REDIRECT_URL)))
|
||||
return HttpResponseRedirect(
|
||||
request.META.get(
|
||||
'HTTP_REFERER', reverse(settings.LOGIN_REDIRECT_URL)
|
||||
)
|
||||
)
|
||||
|
||||
try:
|
||||
Permission.check_permissions(request.user, [permission_document_trash])
|
||||
except PermissionDenied:
|
||||
documents = AccessControlList.objects.filter_by_access(permission_document_trash, request.user, documents)
|
||||
documents = AccessControlList.objects.filter_by_access(
|
||||
permission_document_trash, request.user, documents
|
||||
)
|
||||
|
||||
previous = request.POST.get('previous', request.GET.get('previous', request.META.get('HTTP_REFERER', reverse(settings.LOGIN_REDIRECT_URL))))
|
||||
next = request.POST.get('next', request.GET.get('next', post_action_redirect if post_action_redirect else request.META.get('HTTP_REFERER', reverse(settings.LOGIN_REDIRECT_URL))))
|
||||
|
||||
@@ -21,8 +21,14 @@ class DocumentPageImageWidget(forms.widgets.Widget):
|
||||
rotation = final_attrs.get('rotation', 0)
|
||||
if value:
|
||||
output = []
|
||||
output.append('<div class="full-height scrollable mayan-page-wrapper-interactive" data-height-difference=230>')
|
||||
output.append(document_html_widget(value, zoom=zoom, rotation=rotation, image_class='lazy-load', nolazyload=False, size=setting_display_size.value))
|
||||
output.append(
|
||||
'<div class="full-height scrollable '
|
||||
'mayan-page-wrapper-interactive" data-height-difference=230>'
|
||||
)
|
||||
output.append(document_html_widget(
|
||||
value, zoom=zoom, rotation=rotation, image_class='lazy-load',
|
||||
nolazyload=False, size=setting_display_size.value)
|
||||
)
|
||||
output.append('</div>')
|
||||
return mark_safe(''.join(output))
|
||||
else:
|
||||
@@ -35,7 +41,10 @@ class DocumentPagesCarouselWidget(forms.widgets.Widget):
|
||||
"""
|
||||
def render(self, name, value, attrs=None):
|
||||
output = []
|
||||
output.append('<div id="carousel-container" class="full-height scrollable" data-height-difference=360>')
|
||||
output.append(
|
||||
'<div id="carousel-container" class="full-height scrollable" '
|
||||
'data-height-difference=360>'
|
||||
)
|
||||
|
||||
try:
|
||||
document_pages = value.pages.all()
|
||||
@@ -57,7 +66,14 @@ class DocumentPagesCarouselWidget(forms.widgets.Widget):
|
||||
post_load_class='lazy-load-carousel-loaded',
|
||||
)
|
||||
)
|
||||
output.append('<div class="carousel-item-page-number">%s</div>' % ugettext('Page %(page_number)d of %(total_pages)d') % {'page_number': page.page_number, 'total_pages': total_pages})
|
||||
output.append(
|
||||
'<div class="carousel-item-page-number">%s</div>' % ugettext(
|
||||
'Page %(page_number)d of %(total_pages)d'
|
||||
) % {
|
||||
'page_number': page.page_number,
|
||||
'total_pages': total_pages
|
||||
}
|
||||
)
|
||||
output.append('</div>')
|
||||
|
||||
output.append('</div>')
|
||||
@@ -66,11 +82,16 @@ class DocumentPagesCarouselWidget(forms.widgets.Widget):
|
||||
|
||||
|
||||
def document_thumbnail(document, **kwargs):
|
||||
return document_html_widget(document.latest_version.pages.first(), click_view='documents:document_display', **kwargs)
|
||||
return document_html_widget(
|
||||
document.latest_version.pages.first(),
|
||||
click_view='documents:document_display', **kwargs
|
||||
)
|
||||
|
||||
|
||||
def document_link(document):
|
||||
return mark_safe('<a href="%s">%s</a>' % (document.get_absolute_url(), document))
|
||||
return mark_safe('<a href="%s">%s</a>' % (
|
||||
document.get_absolute_url(), document)
|
||||
)
|
||||
|
||||
|
||||
def document_html_widget(document_page, click_view=None, click_view_arguments=None, zoom=DEFAULT_ZOOM_LEVEL, rotation=DEFAULT_ROTATION, gallery_name=None, fancybox_class='fancybox', image_class='lazy-load', title=None, size=setting_thumbnail_size.value, nolazyload=False, post_load_class=None):
|
||||
@@ -95,9 +116,15 @@ def document_html_widget(document_page, click_view=None, click_view_arguments=No
|
||||
|
||||
query_string = urlencode(query_dict)
|
||||
|
||||
preview_view = '%s?%s' % (reverse('document-image', args=[document.pk]), query_string)
|
||||
preview_view = '%s?%s' % (
|
||||
reverse('document-image', args=[document.pk]), query_string
|
||||
)
|
||||
|
||||
result.append('<div class="tc" id="document-%d-%d">' % (document.pk, page if page else 1))
|
||||
result.append(
|
||||
'<div class="tc" id="document-%d-%d">' % (
|
||||
document.pk, page if page else 1
|
||||
)
|
||||
)
|
||||
|
||||
if title:
|
||||
title_template = 'title="%s"' % strip_tags(title)
|
||||
@@ -105,17 +132,34 @@ def document_html_widget(document_page, click_view=None, click_view_arguments=No
|
||||
title_template = ''
|
||||
|
||||
if click_view:
|
||||
result.append('<a {gallery_template} class="{fancybox_class}" href="{image_data}" {title_template}>'.format(
|
||||
gallery_template=gallery_template,
|
||||
fancybox_class=fancybox_class,
|
||||
image_data='%s?%s' % (reverse(click_view, args=click_view_arguments or [document.pk]), query_string),
|
||||
title_template=title_template
|
||||
))
|
||||
result.append(
|
||||
'<a {gallery_template} class="{fancybox_class}" '
|
||||
'href="{image_data}" {title_template}>'.format(
|
||||
gallery_template=gallery_template,
|
||||
fancybox_class=fancybox_class,
|
||||
image_data='%s?%s' % (
|
||||
reverse(
|
||||
click_view, args=click_view_arguments or [document.pk]
|
||||
), query_string
|
||||
),
|
||||
title_template=title_template
|
||||
)
|
||||
)
|
||||
|
||||
if nolazyload:
|
||||
result.append('<img class="img-nolazyload" src="%s" alt="%s" />' % (preview_view, alt_text))
|
||||
result.append(
|
||||
'<img class="img-nolazyload" src="%s" alt="%s" />' % (
|
||||
preview_view, alt_text
|
||||
)
|
||||
)
|
||||
else:
|
||||
result.append('<img class="thin_border %s" data-src="%s" data-post-load-class="%s" src="%s" alt="%s" />' % (image_class, preview_view, post_load_class, static('appearance/images/loading.png'), alt_text))
|
||||
result.append(
|
||||
'<img class="thin_border %s" data-src="%s" '
|
||||
'data-post-load-class="%s" src="%s" alt="%s" />' % (
|
||||
image_class, preview_view, post_load_class,
|
||||
static('appearance/images/loading.png'), alt_text
|
||||
)
|
||||
)
|
||||
|
||||
if click_view:
|
||||
result.append('</a>')
|
||||
|
||||
Reference in New Issue
Block a user