<s> from __future__ import unicode_literals <EOL> import logging <EOL> from django . contrib . auth . models import User <EOL> from django . db import OperationalError <EOL> from mayan . celery import app <EOL> from common . models import SharedUploadedFile <EOL> from . literals import ( <EOL> UPDATE_PAGE_COUNT_RETRY_DELAY , UPLOAD_NEW_VERSION_RETRY_DELAY , <EOL> NEW_DOCUMENT_RETRY_DELAY <EOL> ) <EOL> from . models import Document , DocumentPage , DocumentType , DocumentVersion <EOL> logger = logging . getLogger ( __name__ ) <EOL> @ app . task ( ignore_result = True ) <EOL> def task_check_delete_periods ( ) : <EOL> DocumentType . objects . check_delete_periods ( ) <EOL> @ app . task ( ignore_result = True ) <EOL> def task_check_trash_periods ( ) : <EOL> DocumentType . objects . check_trash_periods ( ) <EOL> @ app . task ( ignore_result = True ) <EOL> def task_clear_image_cache ( ) : <EOL> logger . info ( '<STR_LIT>' ) <EOL> Document . objects . invalidate_cache ( ) <EOL> logger . info ( '<STR_LIT>' ) <EOL> @ app . task ( ignore_result = True ) <EOL> def task_delete_stubs ( ) : <EOL> logger . info ( '<STR_LIT>' ) <EOL> Document . objects . delete_stubs ( ) <EOL> logger . info ( '<STR_LIT>' ) <EOL> @ app . task ( compression = '<STR_LIT>' ) <EOL> def task_get_document_page_image ( document_page_id , * args , ** kwargs ) : <EOL> document_page = DocumentPage . objects . get ( pk = document_page_id ) <EOL> return document_page . get_image ( * args , ** kwargs ) <EOL> @ app . task ( bind = True , default_retry_delay = UPDATE_PAGE_COUNT_RETRY_DELAY , ignore_result = True ) <EOL> def task_update_page_count ( self , version_id ) : <EOL> document_version = DocumentVersion . objects . get ( pk = version_id ) <EOL> try : <EOL> document_version . update_page_count ( ) <EOL> except OperationalError as exception : <EOL> logger . warning ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , document_version , <EOL> exception <EOL> ) <EOL> raise self . retry ( exc = exception ) <EOL> @ app . task ( bind = True , default_retry_delay = NEW_DOCUMENT_RETRY_DELAY , ignore_result = True ) <EOL> def task_upload_new_document ( self , document_type_id , shared_uploaded_file_id , description = None , label = None , language = None , user_id = None ) : <EOL> try : <EOL> document_type = DocumentType . objects . get ( pk = document_type_id ) <EOL> shared_file = SharedUploadedFile . objects . get ( <EOL> pk = shared_uploaded_file_id <EOL> ) <EOL> if user_id : <EOL> user = User . objects . get ( pk = user_id ) <EOL> else : <EOL> user = None <EOL> except OperationalError as exception : <EOL> logger . warning ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , exception <EOL> ) <EOL> raise self . retry ( exc = exception ) <EOL> try : <EOL> with shared_file . open as file_object : <EOL> document_version = document_type . new_document ( <EOL> self , file_object = file_object , label = label , <EOL> description = description , language = language , _user = user <EOL> ) <EOL> except OperationalError as exception : <EOL> logger . warning ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , exception <EOL> ) <EOL> raise self . retry ( exc = exception ) <EOL> try : <EOL> shared_file . delete ( ) <EOL> except OperationalError as exception : <EOL> logger . warning ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> document_version . document , exception <EOL> ) <EOL> @ app . task ( bind = True , default_retry_delay = UPLOAD_NEW_VERSION_RETRY_DELAY , ignore_result = True ) <EOL> def task_upload_new_version ( self , document_id , shared_uploaded_file_id , user_id , comment = None ) : <EOL> try : <EOL> document = Document . objects . get ( pk = document_id ) <EOL> shared_file = SharedUploadedFile . objects . get ( <EOL> pk = shared_uploaded_file_id <EOL> ) <EOL> if user_id : <EOL> user = User . objects . get ( pk = user_id ) <EOL> else : <EOL> user = None <EOL> except OperationalError as exception : <EOL> logger . warning ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , document , exception <EOL> ) <EOL> raise self . retry ( exc = exception ) <EOL> with shared_file . open ( ) as file_object : <EOL> document_version = DocumentVersion ( <EOL> document = document , comment = comment or '<STR_LIT>' , file = file_object <EOL> ) <EOL> try : <EOL> document_version . save ( _user = user ) <EOL> except Warning as warning : <EOL> logger . info ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , document , warning <EOL> ) <EOL> shared_file . delete ( ) <EOL> except OperationalError as exception : <EOL> logger . warning ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , document , exception <EOL> ) <EOL> raise self . retry ( exc = exception ) <EOL> except Exception as exception : <EOL> logger . error ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , document , exception <EOL> ) <EOL> try : <EOL> shared_file . delete ( ) <EOL> except OperationalError as exception : <EOL> logger . warning ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , shared_file , exception <EOL> ) <EOL> else : <EOL> try : <EOL> shared_file . delete ( ) <EOL> except OperationalError as exception : <EOL> logger . warning ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , shared_file , exception <EOL> ) </s>
