diff --git a/mayan/apps/common/classes.py b/mayan/apps/common/classes.py index e8c4b9c252..8aef2dbd7d 100644 --- a/mayan/apps/common/classes.py +++ b/mayan/apps/common/classes.py @@ -4,7 +4,6 @@ from django.core.exceptions import PermissionDenied from django.db import models from django.utils.translation import ugettext -from acls.models import AccessControlList from permissions import Permission @@ -126,6 +125,8 @@ class Filter(object): return unicode(self.label) def get_queryset(self, user): + from acls.models import AccessControlList + queryset = self.model.objects.all() for kwargs in self.filter_kwargs: queryset = queryset.filter(**kwargs) diff --git a/mayan/apps/common/widgets.py b/mayan/apps/common/widgets.py index 32033da86f..2dfd6551ee 100644 --- a/mayan/apps/common/widgets.py +++ b/mayan/apps/common/widgets.py @@ -3,7 +3,7 @@ from __future__ import unicode_literals import os from django import forms -from django.forms.util import flatatt +from django.forms.utils import flatatt from django.utils.encoding import force_unicode, force_text from django.utils.html import conditional_escape, format_html from django.utils.safestring import mark_safe diff --git a/mayan/apps/document_signatures/apps.py b/mayan/apps/document_signatures/apps.py index 69dc4a245b..f9e807da6d 100644 --- a/mayan/apps/document_signatures/apps.py +++ b/mayan/apps/document_signatures/apps.py @@ -1,14 +1,11 @@ from __future__ import unicode_literals -import io import logging from django.utils.translation import ugettext_lazy as _ from acls import ModelPermission from common import MayanAppConfig, menu_facet, menu_sidebar -from django_gpg.exceptions import GPGDecryptionError -from django_gpg.runtime import gpg from documents.models import Document, DocumentVersion from .hooks import document_pre_open_hook, document_version_post_save_hook @@ -34,8 +31,6 @@ class DocumentSignaturesApp(MayanAppConfig): def ready(self): super(DocumentSignaturesApp, self).ready() - DocumentVersionSignature = self.get_model('DocumentVersionSignature') - DocumentVersion.register_post_save_hook( 1, document_version_post_save_hook ) diff --git a/mayan/apps/document_signatures/hooks.py b/mayan/apps/document_signatures/hooks.py new file mode 100644 index 0000000000..4dcf94dcd7 --- /dev/null +++ b/mayan/apps/document_signatures/hooks.py @@ -0,0 +1,43 @@ +from __future__ import unicode_literals + +import io +import logging + +from django_gpg.exceptions import GPGDecryptionError +from django_gpg.runtime import gpg + +logger = logging.getLogger(__name__) + + +def document_pre_open_hook(descriptor, instance): + from .models import DocumentVersionSignature + + if DocumentVersionSignature.objects.has_embedded_signature(document_version=instance): + # If it has an embedded signature, decrypt + try: + result = gpg.decrypt_file(descriptor, close_descriptor=False) + # gpg return a string, turn it into a file like object + except GPGDecryptionError: + # At least return the original raw content + descriptor.seek(0) + return descriptor + else: + descriptor.close() + return io.BytesIO(result.data) + else: + return descriptor + + +def document_version_post_save_hook(instance): + logger.debug('instance: %s', instance) + from .models import DocumentVersionSignature + + try: + document_signature = DocumentVersionSignature.objects.get( + document_version=instance + ) + except DocumentVersionSignature.DoesNotExist: + document_signature = DocumentVersionSignature.objects.create( + document_version=instance + ) + document_signature.check_for_embedded_signature() diff --git a/mayan/apps/document_states/handlers.py b/mayan/apps/document_states/handlers.py index 291b9e03d3..35675647dc 100644 --- a/mayan/apps/document_states/handlers.py +++ b/mayan/apps/document_states/handlers.py @@ -1,8 +1,8 @@ from __future__ import unicode_literals -from .models import Workflow - def launch_workflow(sender, instance, created, **kwargs): + Workflow = sender.get_model('Workflow') + if created: Workflow.objects.launch_for(instance) diff --git a/mayan/apps/documents/handlers.py b/mayan/apps/documents/handlers.py index c188dc2288..393cc9ec78 100644 --- a/mayan/apps/documents/handlers.py +++ b/mayan/apps/documents/handlers.py @@ -2,9 +2,9 @@ from __future__ import unicode_literals from django.utils.translation import ugettext_lazy as _ -from .models import DocumentType - def create_default_document_type(sender, **kwargs): - if not DocumentType.objects.count(): + DocumentType = sender.get_model('DocumentType') + + if not DocumentType.objects.count(): DocumentType.objects.create(label=_('Default')) diff --git a/mayan/apps/documents/statistics.py b/mayan/apps/documents/statistics.py index 95937b9940..12d0a71f49 100644 --- a/mayan/apps/documents/statistics.py +++ b/mayan/apps/documents/statistics.py @@ -4,10 +4,10 @@ import datetime import qsstats -from .models import Document, DocumentPage, DocumentVersion - def new_documents_per_month(): + from .models import Document + qss = qsstats.QuerySetStats(Document.passthrough.all(), 'date_added') today = datetime.date.today() @@ -21,6 +21,8 @@ def new_documents_per_month(): def new_document_versions_per_month(): + from .models import DocumentVersion + qss = qsstats.QuerySetStats(DocumentVersion.objects.all(), 'document__date_added') today = datetime.date.today() @@ -34,6 +36,8 @@ def new_document_versions_per_month(): def new_document_pages_per_month(): + from .models import DocumentPage + qss = qsstats.QuerySetStats(DocumentPage.objects.all(), 'document_version__document__date_added') today = datetime.date.today() @@ -47,6 +51,8 @@ def new_document_pages_per_month(): def total_document_per_month(): + from .models import Document + qss = qsstats.QuerySetStats(Document.objects.all(), 'date_added') this_year = datetime.date.today().year @@ -72,6 +78,8 @@ def total_document_per_month(): def total_document_version_per_month(): + from .models import DocumentVersion + qss = qsstats.QuerySetStats(DocumentVersion.objects.all(), 'document__date_added') this_year = datetime.date.today().year @@ -97,6 +105,8 @@ def total_document_version_per_month(): def total_document_page_per_month(): + from .models import DocumentPage + qss = qsstats.QuerySetStats(DocumentPage.objects.all(), 'document_version__document__date_added') this_year = datetime.date.today().year diff --git a/mayan/apps/metadata/handlers.py b/mayan/apps/metadata/handlers.py index 9d559dbca2..dc27e86bf0 100644 --- a/mayan/apps/metadata/handlers.py +++ b/mayan/apps/metadata/handlers.py @@ -2,7 +2,6 @@ from __future__ import unicode_literals import logging -from .models import DocumentMetadata from .tasks import task_add_required_metadata_type, task_remove_metadata_type logger = logging.getLogger(__name__) @@ -37,6 +36,8 @@ def post_post_document_type_change_metadata(sender, instance, **kwargs): for metadata in instance.metadata.all(): metadata.delete(enforce_required=False) + DocumentMetadata = sender.get_model('DocumentMetadata') + # Add new document type metadata types to document for document_type_metadata_type in instance.document_type.metadata.filter(required=True): DocumentMetadata.objects.create( diff --git a/mayan/apps/ocr/apps.py b/mayan/apps/ocr/apps.py index 2b8afff5b9..bee3e8b293 100644 --- a/mayan/apps/ocr/apps.py +++ b/mayan/apps/ocr/apps.py @@ -33,16 +33,19 @@ from .permissions import permission_ocr_document, permission_ocr_content_view from .settings import ( setting_pdftotext_path, setting_tesseract_path ) -from .tasks import task_do_ocr logger = logging.getLogger(__name__) def document_ocr_submit(self): + from .tasks import task_do_ocr + task_do_ocr.apply_async(args=(self.latest_version.pk,)) def document_version_ocr_submit(self): + from .tasks import task_do_ocr + task_do_ocr.apply_async( kwargs={'document_version_pk': self.pk}, countdown=settings_db_sync_task_delay.value diff --git a/mayan/apps/ocr/handlers.py b/mayan/apps/ocr/handlers.py index 5d3f769eeb..959398e10b 100644 --- a/mayan/apps/ocr/handlers.py +++ b/mayan/apps/ocr/handlers.py @@ -4,7 +4,6 @@ import logging logger = logging.getLogger(__name__) -from .models import DocumentTypeSettings from .settings import setting_auto_ocr @@ -16,6 +15,8 @@ def post_version_upload_ocr(sender, instance, **kwargs): def initialize_new_ocr_settings(sender, instance, **kwargs): + DocumentTypeSettings = sender.get_model('DocumentTypeSettings') + if kwargs['created']: DocumentTypeSettings.objects.create( document_type=instance, auto_ocr=setting_auto_ocr.value diff --git a/mayan/apps/sources/handlers.py b/mayan/apps/sources/handlers.py index f3c4c163a6..0af79e626b 100644 --- a/mayan/apps/sources/handlers.py +++ b/mayan/apps/sources/handlers.py @@ -5,10 +5,11 @@ from django.utils.translation import ugettext_lazy as _ from converter.models import Transformation from .literals import SOURCE_UNCOMPRESS_CHOICE_ASK -from .models import POP3Email, IMAPEmail, WatchFolderSource, WebFormSource def create_default_document_source(sender, **kwargs): + WebFormSource = sender.get_model('WebFormSource') + if not WebFormSource.objects.count(): WebFormSource.objects.create( label=_('Default'), uncompress=SOURCE_UNCOMPRESS_CHOICE_ASK @@ -16,6 +17,8 @@ def create_default_document_source(sender, **kwargs): def copy_transformations_to_version(sender, **kwargs): + Transformation = sender.get_model('Transformation') + instance = kwargs['instance'] # TODO: Fix this, source should be previous version @@ -25,7 +28,11 @@ def copy_transformations_to_version(sender, **kwargs): ) -def initialize_periodic_tasks(**kwargs): +def initialize_periodic_tasks(sender, **kwargs): + POP3Email = sender.get_model('POP3Email') + IMAPEmail = sender.get_model('IMAPEmail') + WatchFolderSource = sender.get_model('WatchFolderSource') + for source in POP3Email.objects.filter(enabled=True): source.save() diff --git a/mayan/apps/statistics/classes.py b/mayan/apps/statistics/classes.py index 333b288d48..7943e0f6c0 100644 --- a/mayan/apps/statistics/classes.py +++ b/mayan/apps/statistics/classes.py @@ -7,8 +7,6 @@ from djcelery.models import PeriodicTask from mayan.celery import app -from .models import StatisticResult - class StatisticNamespace(object): _registry = {} @@ -45,6 +43,8 @@ class Statistic(object): @staticmethod def purge_schedules(): + from .models import StatisticResult + queryset = PeriodicTask.objects.filter(name__startswith='statistics.').exclude(name__in=Statistic.get_task_names()) for periodic_task in queryset: @@ -112,12 +112,16 @@ class Statistic(object): return 'statistics.task_execute_statistic_{}'.format(self.slug) def store_results(self, results): + from .models import StatisticResult + StatisticResult.objects.filter(slug=self.slug).delete() statistic_result, created = StatisticResult.objects.get_or_create(slug=self.slug) statistic_result.store_data(data=results) def get_results(self): + from .models import StatisticResult + try: return StatisticResult.objects.get(slug=self.slug).get_data() except StatisticResult.DoesNotExist: