From 546a593e26ec1be34bdba92ac9250c168177bd8b Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Mon, 27 Oct 2014 05:18:25 -0400 Subject: [PATCH] Use the new post version upload signal to ensure OCR starts only when all of a new document version pages have been created --- mayan/apps/documents/models.py | 7 +++++-- mayan/apps/ocr/__init__.py | 13 ++++++------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/mayan/apps/documents/models.py b/mayan/apps/documents/models.py index 722aa7ae98..e08c91a4e8 100644 --- a/mayan/apps/documents/models.py +++ b/mayan/apps/documents/models.py @@ -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 diff --git a/mayan/apps/ocr/__init__.py b/mayan/apps/ocr/__init__.py index 5cbb099ad6..81f41af517 100644 --- a/mayan/apps/ocr/__init__.py +++ b/mayan/apps/ocr/__init__.py @@ -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')