Code cleanup
This commit is contained in:
@@ -1,6 +1,4 @@
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.conf import settings
|
||||
|
||||
from common.utils import validate_path
|
||||
from navigation.api import register_links, register_top_menu, \
|
||||
@@ -28,6 +26,7 @@ from documents.conf.settings import ZOOM_MIN_LEVEL
|
||||
from documents.conf.settings import CACHE_PATH
|
||||
from documents.widgets import document_thumbnail
|
||||
|
||||
|
||||
# Document page links expressions
|
||||
def is_first_page(context):
|
||||
return context['object'].page_number <= 1
|
||||
@@ -35,7 +34,7 @@ def is_first_page(context):
|
||||
|
||||
def is_last_page(context):
|
||||
return context['object'].page_number >= context['object'].document.documentpage_set.count()
|
||||
|
||||
|
||||
|
||||
def is_min_zoom(context):
|
||||
return context['zoom'] <= ZOOM_MIN_LEVEL
|
||||
@@ -129,7 +128,7 @@ register_links(['setup_document_type_metadata', 'document_type_filename_delete',
|
||||
register_links(['document_type_filename_create', 'document_type_filename_list', 'document_type_filename_edit', 'document_type_filename_delete'], [document_type_filename_create], menu_name='sidebar')
|
||||
register_links(['document_type_filename_edit', 'document_type_filename_delete'], [document_type_filename_return_to_document_type], menu_name='sidebar')
|
||||
|
||||
# Register document links
|
||||
# Register document links
|
||||
register_links(Document, [document_edit, document_print, document_delete, document_download, document_find_duplicates, document_clear_transformations, document_create_siblings])
|
||||
register_multi_item_links(['document_find_duplicates', 'folder_view', 'index_instance_list', 'document_type_document_list', 'search', 'results', 'document_group_view', 'document_list', 'document_list_recent'], [document_multiple_clear_transformations, document_multiple_delete])
|
||||
|
||||
@@ -185,8 +184,8 @@ register_model_list_columns(Document, [
|
||||
])
|
||||
|
||||
register_top_menu(
|
||||
'documents',
|
||||
link={'famfam': 'page', 'text': _(u'documents'), 'view': 'document_list_recent'},
|
||||
'documents',
|
||||
link={'famfam': 'page', 'text': _(u'documents'), 'view': 'document_list_recent'},
|
||||
children_path_regex=[
|
||||
r'^documents/[^t]', r'^metadata/[^s]', r'comments', r'tags/document', r'grouping/[^s]', r'history/list/for_object/documents'
|
||||
],
|
||||
|
||||
@@ -178,7 +178,7 @@ class DocumentForm(forms.ModelForm):
|
||||
if instance:
|
||||
if hasattr(instance, 'document_type'):
|
||||
document_type = instance.document_type
|
||||
|
||||
|
||||
if document_type:
|
||||
filenames_qs = document_type.documenttypefilename_set.filter(enabled=True)
|
||||
if filenames_qs.count() > 0:
|
||||
@@ -190,7 +190,7 @@ class DocumentForm(forms.ModelForm):
|
||||
new_filename = forms.CharField(
|
||||
label=_('New document filename'), required=False
|
||||
)
|
||||
|
||||
|
||||
|
||||
class DocumentForm_edit(DocumentForm):
|
||||
"""
|
||||
@@ -199,7 +199,7 @@ class DocumentForm_edit(DocumentForm):
|
||||
class Meta:
|
||||
model = Document
|
||||
exclude = ('file', 'document_type', 'tags')
|
||||
|
||||
|
||||
|
||||
class DocumentPropertiesForm(DetailForm):
|
||||
"""
|
||||
|
||||
@@ -32,5 +32,5 @@ class DocumentPageTransformationManager(models.Manager):
|
||||
)
|
||||
except Exception, e:
|
||||
warnings.append(e)
|
||||
|
||||
return transformations, warnings
|
||||
|
||||
return transformations, warnings
|
||||
|
||||
@@ -13,7 +13,7 @@ from taggit.managers import TaggableManager
|
||||
from dynamic_search.api import register
|
||||
from converter.api import get_page_count
|
||||
from converter.api import get_available_transformations_choices
|
||||
from converter.api import create_image_cache_filename, convert
|
||||
from converter.api import convert
|
||||
from converter.exceptions import UnknownFormat, UnkownConvertError
|
||||
|
||||
from documents.utils import get_document_mimetype
|
||||
@@ -21,7 +21,6 @@ from documents.conf.settings import CHECKSUM_FUNCTION
|
||||
from documents.conf.settings import UUID_FUNCTION
|
||||
from documents.conf.settings import STORAGE_BACKEND
|
||||
from documents.conf.settings import PREVIEW_SIZE
|
||||
from documents.conf.settings import THUMBNAIL_SIZE
|
||||
from documents.conf.settings import CACHE_PATH
|
||||
|
||||
from documents.managers import RecentDocumentManager, \
|
||||
@@ -30,8 +29,8 @@ from documents.utils import document_save_to_temp_dir
|
||||
from documents.literals import PICTURE_ERROR_SMALL, PICTURE_ERROR_MEDIUM, \
|
||||
PICTURE_UNKNOWN_SMALL, PICTURE_UNKNOWN_MEDIUM
|
||||
from converter.literals import DEFAULT_ZOOM_LEVEL, DEFAULT_ROTATION, \
|
||||
DEFAULT_FILE_FORMAT, DEFAULT_PAGE_NUMBER
|
||||
|
||||
DEFAULT_PAGE_NUMBER
|
||||
|
||||
# document image cache name hash function
|
||||
HASH_FUNCTION = lambda x: hashlib.sha256(x).hexdigest()
|
||||
|
||||
@@ -63,7 +62,7 @@ class DocumentType(models.Model):
|
||||
class Meta:
|
||||
verbose_name = _(u'document type')
|
||||
verbose_name_plural = _(u'documents types')
|
||||
ordering = ['name']
|
||||
ordering = ['name']
|
||||
|
||||
|
||||
class Document(models.Model):
|
||||
@@ -84,7 +83,7 @@ class Document(models.Model):
|
||||
description = models.TextField(blank=True, null=True, verbose_name=_(u'description'), db_index=True)
|
||||
|
||||
tags = TaggableManager()
|
||||
|
||||
|
||||
comments = generic.GenericRelation(
|
||||
Comment,
|
||||
content_type_field='content_type',
|
||||
@@ -209,7 +208,7 @@ class Document(models.Model):
|
||||
exists in storage
|
||||
"""
|
||||
return self.file.storage.exists(self.file.path)
|
||||
|
||||
|
||||
def apply_default_transformations(self, transformations):
|
||||
#Only apply default transformations on new documents
|
||||
if reduce(lambda x, y: x + y, [page.documentpagetransformation_set.count() for page in self.documentpage_set.all()]) == 0:
|
||||
@@ -223,7 +222,7 @@ class Document(models.Model):
|
||||
)
|
||||
|
||||
page_transformation.save()
|
||||
|
||||
|
||||
def get_image_cache_name(self, page):
|
||||
document_page = self.documentpage_set.get(page_number=page)
|
||||
transformations, warnings = document_page.get_transformation_list()
|
||||
@@ -234,16 +233,16 @@ class Document(models.Model):
|
||||
else:
|
||||
document_file = document_save_to_temp_dir(self, self.checksum)
|
||||
return convert(document_file, output_filepath=cache_file_path, page=page, transformations=transformations)
|
||||
|
||||
|
||||
def get_image(self, size=PREVIEW_SIZE, page=DEFAULT_PAGE_NUMBER, zoom=DEFAULT_ZOOM_LEVEL, rotation=DEFAULT_ROTATION):
|
||||
try:
|
||||
image_cache_name = self.get_image_cache_name(page=page)
|
||||
output_file = convert(image_cache_name, cleanup_files=False, size=size, zoom=zoom, rotation=rotation)
|
||||
except UnknownFormat:
|
||||
output_file = os.path.join(settings.MEDIA_ROOT, u'images', PICTURE_UNKNOWN_SMALL)
|
||||
except UnkownConvertError:
|
||||
except UnkownConvertError:
|
||||
output_file = os.path.join(settings.MEDIA_ROOT, u'images', PICTURE_ERROR_SMALL)
|
||||
except Exception, e:
|
||||
except:
|
||||
output_file = os.path.join(settings.MEDIA_ROOT, u'images', PICTURE_ERROR_SMALL)
|
||||
return output_file
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@ from django.template import Library, Node, Variable
|
||||
|
||||
from converter.api import get_document_dimensions
|
||||
|
||||
from documents.views import calculate_converter_arguments
|
||||
from documents.conf.settings import PRINT_SIZE
|
||||
|
||||
register = Library()
|
||||
|
||||
@@ -59,7 +59,7 @@ urlpatterns = patterns('documents.views',
|
||||
url(r'^type/(?P<document_type_id>\d+)/list/documents/$', 'document_type_document_list', (), 'document_type_document_list'),
|
||||
url(r'^type/(?P<document_type_id>\d+)/edit/$', 'document_type_edit', (), 'document_type_edit'),
|
||||
url(r'^type/(?P<document_type_id>\d+)/delete/$', 'document_type_delete', (), 'document_type_delete'),
|
||||
|
||||
|
||||
url(r'^type/(?P<document_type_id>\d+)/filename/list/$', 'document_type_filename_list', (), 'document_type_filename_list'),
|
||||
url(r'^type/filename/(?P<document_type_filename_id>\d+)/edit/$', 'document_type_filename_edit', (), 'document_type_filename_edit'),
|
||||
url(r'^type/filename/(?P<document_type_filename_id>\d+)/delete/$', 'document_type_filename_delete', (), 'document_type_filename_delete'),
|
||||
|
||||
@@ -46,8 +46,8 @@ def get_document_mimetype(document):
|
||||
library
|
||||
"""
|
||||
file_mimetype = u''
|
||||
file_mime_encoding = u''
|
||||
|
||||
file_mime_encoding = u''
|
||||
|
||||
if USE_PYTHON_MAGIC:
|
||||
if document.exists():
|
||||
try:
|
||||
@@ -62,5 +62,5 @@ def get_document_mimetype(document):
|
||||
source.close()
|
||||
else:
|
||||
file_mimetype, file_mime_encoding = mimetypes.guess_type(document.get_fullname())
|
||||
|
||||
|
||||
return file_mimetype, file_mime_encoding
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import os
|
||||
import urlparse
|
||||
import copy
|
||||
|
||||
@@ -10,7 +9,6 @@ from django.contrib import messages
|
||||
from django.views.generic.list_detail import object_list
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.views.generic.create_update import delete_object, update_object
|
||||
from django.conf import settings
|
||||
from django.utils.http import urlencode
|
||||
|
||||
import sendfile
|
||||
@@ -24,8 +22,6 @@ from converter.literals import DEFAULT_ZOOM_LEVEL, DEFAULT_ROTATION, \
|
||||
DEFAULT_FILE_FORMAT, DEFAULT_PAGE_NUMBER
|
||||
from filetransfers.api import serve_file
|
||||
from grouping.utils import get_document_group_subtemplate
|
||||
from metadata.api import save_metadata_list, \
|
||||
decode_metadata_from_url, metadata_repr_as_list
|
||||
from metadata.forms import MetadataFormSet, MetadataSelectionForm
|
||||
from navigation.utils import resolve_to_name
|
||||
from permissions.api import check_permissions
|
||||
@@ -33,7 +29,6 @@ from document_indexing.api import update_indexes, delete_indexes
|
||||
from history.api import create_history
|
||||
|
||||
from documents.conf.settings import PREVIEW_SIZE
|
||||
from documents.conf.settings import THUMBNAIL_SIZE
|
||||
from documents.conf.settings import STORAGE_BACKEND
|
||||
from documents.conf.settings import ZOOM_PERCENT_STEP
|
||||
from documents.conf.settings import ZOOM_MAX_LEVEL
|
||||
@@ -52,7 +47,7 @@ from documents.literals import HISTORY_DOCUMENT_CREATED, \
|
||||
HISTORY_DOCUMENT_EDITED, HISTORY_DOCUMENT_DELETED
|
||||
|
||||
from documents.forms import DocumentTypeSelectForm, \
|
||||
DocumentForm, DocumentForm_edit, DocumentPropertiesForm, \
|
||||
DocumentForm_edit, DocumentPropertiesForm, \
|
||||
DocumentPreviewForm, \
|
||||
DocumentPageForm, DocumentPageTransformationForm, \
|
||||
DocumentContentForm, DocumentPageForm_edit, \
|
||||
@@ -61,9 +56,7 @@ from documents.forms import DocumentTypeSelectForm, \
|
||||
from documents.wizards import DocumentCreateWizard
|
||||
from documents.models import Document, DocumentType, DocumentPage, \
|
||||
DocumentPageTransformation, RecentDocument, DocumentTypeFilename
|
||||
from documents.literals import PICTURE_ERROR_SMALL, PICTURE_ERROR_MEDIUM, \
|
||||
PICTURE_UNKNOWN_SMALL, PICTURE_UNKNOWN_MEDIUM
|
||||
|
||||
|
||||
# Document type permissions
|
||||
from documents.literals import PERMISSION_DOCUMENT_TYPE_EDIT, \
|
||||
PERMISSION_DOCUMENT_TYPE_DELETE, PERMISSION_DOCUMENT_TYPE_CREATE
|
||||
@@ -446,10 +439,10 @@ def _find_duplicate_list(request, source_document_list=Document.objects.all(), i
|
||||
'hide_links': True,
|
||||
'multi_select_as_buttons': True,
|
||||
}
|
||||
|
||||
|
||||
if extra_context:
|
||||
context.update(extra_context)
|
||||
|
||||
|
||||
return render_to_response('generic_list.html', context,
|
||||
context_instance=RequestContext(request))
|
||||
|
||||
@@ -548,7 +541,7 @@ def document_page_view(request, document_page_id):
|
||||
zoom_text = u'(%d%%)' % zoom
|
||||
else:
|
||||
zoom_text = u''
|
||||
|
||||
|
||||
if rotation != 0 and rotation != 360:
|
||||
rotation_text = u'(%d°)' % rotation
|
||||
else:
|
||||
@@ -779,10 +772,10 @@ def document_hard_copy(request, document_id):
|
||||
|
||||
RecentDocument.objects.add_document_for_user(request.user, document)
|
||||
|
||||
arguments, warnings = calculate_converter_arguments(document, size=PRINT_SIZE, file_format=DEFAULT_FILE_FORMAT)
|
||||
#arguments, warnings = calculate_converter_arguments(document, size=PRINT_SIZE, file_format=DEFAULT_FILE_FORMAT)
|
||||
|
||||
# Pre-generate
|
||||
convert_document(document, **arguments)
|
||||
#convert_document(document, **arguments)
|
||||
|
||||
# Extract dimension values ignoring any unit
|
||||
page_width = request.GET.get('page_width', dict(PAGE_SIZE_DIMENSIONS)[DEFAULT_PAPER_SIZE][0])
|
||||
@@ -828,9 +821,9 @@ def document_type_list(request):
|
||||
|
||||
def document_type_document_list(request, document_type_id):
|
||||
check_permissions(request.user, [PERMISSION_DOCUMENT_VIEW])
|
||||
|
||||
|
||||
document_type = get_object_or_404(DocumentType, pk=document_type_id)
|
||||
|
||||
|
||||
return document_list(
|
||||
request,
|
||||
object_list=Document.objects.filter(document_type=document_type),
|
||||
@@ -867,7 +860,7 @@ def document_type_edit(request, document_type_id):
|
||||
'object_name': _(u'document type'),
|
||||
'next': next
|
||||
},
|
||||
context_instance=RequestContext(request))
|
||||
context_instance=RequestContext(request))
|
||||
|
||||
|
||||
def document_type_delete(request, document_type_id):
|
||||
@@ -907,7 +900,7 @@ def document_type_delete(request, document_type_id):
|
||||
|
||||
def document_type_create(request):
|
||||
check_permissions(request.user, [PERMISSION_DOCUMENT_TYPE_CREATE])
|
||||
|
||||
|
||||
if request.method == 'POST':
|
||||
form = DocumentTypeForm(request.POST)
|
||||
if form.is_valid():
|
||||
@@ -948,7 +941,7 @@ def document_type_filename_list(request, document_type_id):
|
||||
}
|
||||
|
||||
return render_to_response('generic_list.html', context,
|
||||
context_instance=RequestContext(request))
|
||||
context_instance=RequestContext(request))
|
||||
|
||||
|
||||
def document_type_filename_edit(request, document_type_filename_id):
|
||||
@@ -982,7 +975,7 @@ def document_type_filename_edit(request, document_type_filename_id):
|
||||
'document_type': document_type_filename.document_type,
|
||||
},
|
||||
context_instance=RequestContext(request))
|
||||
|
||||
|
||||
|
||||
def document_type_filename_delete(request, document_type_filename_id):
|
||||
check_permissions(request.user, [PERMISSION_DOCUMENT_TYPE_EDIT])
|
||||
@@ -1017,14 +1010,14 @@ def document_type_filename_delete(request, document_type_filename_id):
|
||||
}
|
||||
|
||||
return render_to_response('generic_confirm.html', context,
|
||||
context_instance=RequestContext(request))
|
||||
context_instance=RequestContext(request))
|
||||
|
||||
|
||||
def document_type_filename_create(request, document_type_id):
|
||||
check_permissions(request.user, [PERMISSION_DOCUMENT_TYPE_EDIT])
|
||||
|
||||
|
||||
document_type = get_object_or_404(DocumentType, pk=document_type_id)
|
||||
|
||||
|
||||
if request.method == 'POST':
|
||||
form = DocumentTypeFilenameForm_create(request.POST)
|
||||
if form.is_valid():
|
||||
|
||||
Reference in New Issue
Block a user