Fix futher Django Warnings.

This commit is contained in:
Roberto Rosario
2016-01-22 04:52:47 -04:00
parent 20535529dc
commit 6b3fe7a876
12 changed files with 86 additions and 21 deletions

View File

@@ -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)

View File

@@ -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

View File

@@ -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
)

View File

@@ -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()

View File

@@ -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)

View File

@@ -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):
DocumentType = sender.get_model('DocumentType')
if not DocumentType.objects.count():
DocumentType.objects.create(label=_('Default'))

View File

@@ -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

View File

@@ -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(

View File

@@ -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

View File

@@ -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

View File

@@ -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()

View File

@@ -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: