Merge branch 'smart_staging' into master_merge_test
Conflicts: apps/common/__init__.py requirements/development.txt requirements/production.txt
This commit is contained in:
@@ -1,26 +1,39 @@
|
||||
import os
|
||||
import tempfile
|
||||
import hashlib
|
||||
|
||||
from django.db import models
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.contrib.auth.models import User
|
||||
from django.contrib.contenttypes import generic
|
||||
from django.contrib.comments.models import Comment
|
||||
from django.conf import settings
|
||||
|
||||
from taggit.managers import TaggableManager
|
||||
from dynamic_search.api import register
|
||||
from converter.api import get_page_count
|
||||
from converter import TRANFORMATION_CHOICES
|
||||
from converter.api import get_available_transformations_choices
|
||||
from converter.api import create_image_cache_filename, convert
|
||||
from converter.exceptions import UnknownFormat, UnkownConvertError
|
||||
|
||||
from documents.utils import get_document_mimetype
|
||||
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 AVAILABLE_TRANSFORMATIONS
|
||||
from documents.conf.settings import DEFAULT_TRANSFORMATIONS
|
||||
from documents.managers import RecentDocumentManager
|
||||
from documents.conf.settings import PREVIEW_SIZE
|
||||
from documents.conf.settings import THUMBNAIL_SIZE
|
||||
from documents.conf.settings import CACHE_PATH
|
||||
|
||||
available_transformations = ([(name, data['label']) for name, data in AVAILABLE_TRANSFORMATIONS.items()])
|
||||
from documents.managers import RecentDocumentManager, \
|
||||
DocumentPageTransformationManager
|
||||
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
|
||||
|
||||
# document image cache name hash function
|
||||
HASH_FUNCTION = lambda x: hashlib.sha256(x).hexdigest()
|
||||
|
||||
|
||||
def get_filename_from_uuid(instance, filename):
|
||||
@@ -92,7 +105,7 @@ class Document(models.Model):
|
||||
mimetype, page count and transformation when originally created
|
||||
"""
|
||||
new_document = not self.pk
|
||||
|
||||
transformations = kwargs.pop('transformations', None)
|
||||
super(Document, self).save(*args, **kwargs)
|
||||
|
||||
if new_document:
|
||||
@@ -101,7 +114,8 @@ class Document(models.Model):
|
||||
self.update_mimetype(save=False)
|
||||
self.save()
|
||||
self.update_page_count(save=False)
|
||||
self.apply_default_transformations()
|
||||
if transformations:
|
||||
self.apply_default_transformations(transformations)
|
||||
|
||||
@models.permalink
|
||||
def get_absolute_url(self):
|
||||
@@ -195,21 +209,43 @@ class Document(models.Model):
|
||||
exists in storage
|
||||
"""
|
||||
return self.file.storage.exists(self.file.path)
|
||||
|
||||
def apply_default_transformations(self):
|
||||
|
||||
def apply_default_transformations(self, transformations):
|
||||
#Only apply default transformations on new documents
|
||||
if DEFAULT_TRANSFORMATIONS and reduce(lambda x, y: x + y, [page.documentpagetransformation_set.count() for page in self.documentpage_set.all()]) == 0:
|
||||
for transformation in DEFAULT_TRANSFORMATIONS:
|
||||
if 'name' in transformation:
|
||||
for document_page in self.documentpage_set.all():
|
||||
page_transformation = DocumentPageTransformation(
|
||||
document_page=document_page,
|
||||
order=0,
|
||||
transformation=transformation['name'])
|
||||
if 'arguments' in transformation:
|
||||
page_transformation.arguments = transformation['arguments']
|
||||
if reduce(lambda x, y: x + y, [page.documentpagetransformation_set.count() for page in self.documentpage_set.all()]) == 0:
|
||||
for transformation in transformations:
|
||||
for document_page in self.documentpage_set.all():
|
||||
page_transformation = DocumentPageTransformation(
|
||||
document_page=document_page,
|
||||
order=0,
|
||||
transformation=transformation.get('transformation'),
|
||||
arguments=transformation.get('arguments')
|
||||
)
|
||||
|
||||
page_transformation.save()
|
||||
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()
|
||||
hash_value = HASH_FUNCTION(u''.join([self.checksum, unicode(page), unicode(transformations)]))
|
||||
cache_file_path = os.path.join(CACHE_PATH, hash_value)
|
||||
if os.path.exists(cache_file_path):
|
||||
return cache_file_path
|
||||
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:
|
||||
output_file = os.path.join(settings.MEDIA_ROOT, u'images', PICTURE_ERROR_SMALL)
|
||||
except Exception, e:
|
||||
output_file = os.path.join(settings.MEDIA_ROOT, u'images', PICTURE_ERROR_SMALL)
|
||||
return output_file
|
||||
|
||||
|
||||
class DocumentTypeFilename(models.Model):
|
||||
@@ -251,26 +287,13 @@ class DocumentPage(models.Model):
|
||||
verbose_name = _(u'document page')
|
||||
verbose_name_plural = _(u'document pages')
|
||||
|
||||
def get_transformation_list(self):
|
||||
return DocumentPageTransformation.objects.get_for_document_page_as_list(self)
|
||||
|
||||
@models.permalink
|
||||
def get_absolute_url(self):
|
||||
return ('document_page_view', [self.pk])
|
||||
|
||||
def get_transformation_string(self):
|
||||
transformation_list = []
|
||||
warnings = []
|
||||
for page_transformation in self.documentpagetransformation_set.all():
|
||||
try:
|
||||
if page_transformation.transformation in TRANFORMATION_CHOICES:
|
||||
transformation_list.append(
|
||||
TRANFORMATION_CHOICES[page_transformation.transformation] % eval(
|
||||
page_transformation.arguments
|
||||
)
|
||||
)
|
||||
except Exception, e:
|
||||
warnings.append(e)
|
||||
|
||||
return u' '.join(transformation_list), warnings
|
||||
|
||||
|
||||
class DocumentPageTransformation(models.Model):
|
||||
"""
|
||||
@@ -279,9 +302,11 @@ class DocumentPageTransformation(models.Model):
|
||||
"""
|
||||
document_page = models.ForeignKey(DocumentPage, verbose_name=_(u'document page'))
|
||||
order = models.PositiveIntegerField(default=0, blank=True, null=True, verbose_name=_(u'order'), db_index=True)
|
||||
transformation = models.CharField(choices=available_transformations, max_length=128, verbose_name=_(u'transformation'))
|
||||
transformation = models.CharField(choices=get_available_transformations_choices(), max_length=128, verbose_name=_(u'transformation'))
|
||||
arguments = models.TextField(blank=True, null=True, verbose_name=_(u'arguments'), help_text=_(u'Use dictionaries to indentify arguments, example: {\'degrees\':90}'))
|
||||
|
||||
objects = DocumentPageTransformationManager()
|
||||
|
||||
def __unicode__(self):
|
||||
return u'"%s" for %s' % (self.get_transformation_display(), unicode(self.document_page))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user