Use the new post version upload signal to ensure OCR starts only when all of a new

document version pages have been created
This commit is contained in:
Roberto Rosario
2014-10-27 05:18:25 -04:00
parent 9e85da2f2b
commit 546a593e26
2 changed files with 11 additions and 9 deletions

View File

@@ -37,6 +37,7 @@ from .managers import (DocumentManager, DocumentPageTransformationManager,
from .runtime import storage_backend
from .settings import (CACHE_PATH, CHECKSUM_FUNCTION, DISPLAY_SIZE, LANGUAGE,
UUID_FUNCTION, ZOOM_MAX_LEVEL, ZOOM_MIN_LEVEL)
from .signals import post_version_upload
HASH_FUNCTION = lambda x: hashlib.sha256(x).hexdigest() # document image cache name hash function
logger = logging.getLogger(__name__)
@@ -365,7 +366,7 @@ class DocumentVersion(models.Model):
Overloaded save method that updates the document version's checksum,
mimetype, page count and transformation when created
"""
new_document = not self.pk
new_document_version = not self.pk
# Only do this for new documents
transformations = kwargs.pop('transformations', None)
@@ -374,7 +375,7 @@ class DocumentVersion(models.Model):
for key in sorted(DocumentVersion._post_save_hooks):
DocumentVersion._post_save_hooks[key](self)
if new_document:
if new_document_version:
# Only do this for new documents
self.update_checksum(save=False)
self.update_mimetype(save=False)
@@ -384,6 +385,8 @@ class DocumentVersion(models.Model):
if transformations:
self.apply_default_transformations(transformations)
post_version_upload.send(sender=self.__class__, instance=self)
def update_checksum(self, save=True):
"""
Open a document version's file and update the checksum field using the

View File

@@ -2,7 +2,6 @@ from __future__ import absolute_import
import logging
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.utils.translation import ugettext_lazy as _
@@ -10,6 +9,7 @@ from south.signals import post_migrate
from acls.api import class_permissions
from documents.models import Document, DocumentVersion
from documents.signals import post_version_upload
from main.api import register_maintenance_links
from navigation.api import register_links, register_multi_item_links
from project_tools.api import register_tool
@@ -36,13 +36,12 @@ def document_ocr_submit(self):
task_do_ocr.apply_async(args=[self.pk], queue='ocr')
@receiver(post_save, dispatch_uid='document_post_save', sender=DocumentVersion)
def document_post_save(sender, instance, **kwargs):
logger.debug('received post save signal')
@receiver(post_version_upload, dispatch_uid='post_version_upload_ocr', sender=DocumentVersion)
def post_version_upload_ocr(sender, instance, **kwargs):
logger.debug('received post_version_upload')
logger.debug('instance: %s' % instance)
if kwargs.get('created', False):
if instance.document.document_type.ocr:
instance.document.submit_for_ocr()
if instance.document.document_type.ocr:
instance.document.submit_for_ocr()
@receiver(post_migrate, dispatch_uid='create_default_queue')