Convert sources app to use organizations.
This commit is contained in:
@@ -11,6 +11,7 @@ import poplib
|
||||
|
||||
import yaml
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.core.files import File
|
||||
from django.core.files.base import ContentFile
|
||||
@@ -18,7 +19,7 @@ from django.db import models, transaction
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from model_utils.managers import InheritanceManager
|
||||
from model_utils.managers import InheritanceManager, InheritanceQuerySet
|
||||
|
||||
from common.compressed_files import CompressedFile, NotACompressedFile
|
||||
from converter.literals import DIMENSION_SEPARATOR
|
||||
@@ -28,7 +29,9 @@ from documents.models import Document, DocumentType
|
||||
from documents.settings import setting_language
|
||||
from metadata.api import save_metadata_list, set_bulk_metadata
|
||||
from metadata.models import MetadataType
|
||||
from tags.models import Tag
|
||||
from organizations.models import Organization
|
||||
from organizations.managers import CurrentOrganizationManager
|
||||
from organizations.shortcuts import get_current_organization
|
||||
|
||||
from .classes import Attachment, SourceUploadedFile, StagingFile
|
||||
from .literals import (
|
||||
@@ -43,12 +46,24 @@ from .literals import (
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class OrganizationSourceManager(InheritanceManager, CurrentOrganizationManager):
|
||||
def get_queryset(self):
|
||||
return InheritanceQuerySet(self.model).filter(
|
||||
**{self._get_field_name() + '__id': settings.ORGANIZATION_ID}
|
||||
)
|
||||
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Source(models.Model):
|
||||
organization = models.ForeignKey(
|
||||
Organization, default=get_current_organization
|
||||
)
|
||||
label = models.CharField(max_length=64, verbose_name=_('Label'))
|
||||
enabled = models.BooleanField(default=True, verbose_name=_('Enabled'))
|
||||
|
||||
objects = InheritanceManager()
|
||||
on_organization = OrganizationSourceManager()
|
||||
|
||||
@classmethod
|
||||
def class_fullname(cls):
|
||||
@@ -63,7 +78,7 @@ class Source(models.Model):
|
||||
def upload_document(self, file_object, document_type, description=None, label=None, language=None, metadata_dict_list=None, metadata_dictionary=None, tag_ids=None, user=None):
|
||||
try:
|
||||
with transaction.atomic():
|
||||
document = Document.objects.create(
|
||||
document = Document.on_organization.create(
|
||||
description=description or '', document_type=document_type,
|
||||
label=label or file_object.name,
|
||||
language=language or setting_language.value
|
||||
@@ -93,7 +108,7 @@ class Source(models.Model):
|
||||
)
|
||||
|
||||
if tag_ids:
|
||||
for tag in Tag.objects.filter(pk__in=tag_ids):
|
||||
for tag in Tag.on_organization.filter(pk__in=tag_ids):
|
||||
tag.documents.add(document)
|
||||
|
||||
except Exception as exception:
|
||||
@@ -147,6 +162,7 @@ class Source(models.Model):
|
||||
|
||||
class InteractiveSource(Source):
|
||||
objects = InheritanceManager()
|
||||
on_organization = OrganizationSourceManager()
|
||||
|
||||
class Meta:
|
||||
verbose_name = _('Interactive source')
|
||||
@@ -183,6 +199,9 @@ class StagingFolderSource(InteractiveSource):
|
||||
verbose_name=_('Delete after upload')
|
||||
)
|
||||
|
||||
objects = InheritanceManager()
|
||||
on_organization = OrganizationSourceManager()
|
||||
|
||||
def get_preview_size(self):
|
||||
dimensions = []
|
||||
dimensions.append(unicode(self.preview_width))
|
||||
@@ -248,6 +267,12 @@ class WebFormSource(InteractiveSource):
|
||||
def get_upload_file_object(self, form_data):
|
||||
return SourceUploadedFile(source=self, file=form_data['file'])
|
||||
|
||||
# Custom model managers are not inherited is base class is not abstract.
|
||||
# This will change in Django 1.10
|
||||
# https://docs.djangoproject.com/en/1.8/topics/db/managers/#custom-managers-and-model-inheritance
|
||||
objects = InheritanceManager()
|
||||
on_organization = OrganizationSourceManager()
|
||||
|
||||
class Meta:
|
||||
verbose_name = _('Web form')
|
||||
verbose_name_plural = _('Web forms')
|
||||
@@ -256,6 +281,11 @@ class WebFormSource(InteractiveSource):
|
||||
class OutOfProcessSource(Source):
|
||||
is_interactive = False
|
||||
|
||||
# Custom model managers are not inherited is base class is not abstract.
|
||||
# This will change in Django 1.10
|
||||
# https://docs.djangoproject.com/en/1.8/topics/db/managers/#custom-managers-and-model-inheritance
|
||||
on_organization = OrganizationSourceManager()
|
||||
|
||||
class Meta:
|
||||
verbose_name = _('Out of process')
|
||||
verbose_name_plural = _('Out of process')
|
||||
@@ -280,6 +310,12 @@ class IntervalBaseModel(OutOfProcessSource):
|
||||
max_length=1, verbose_name=_('Uncompress')
|
||||
)
|
||||
|
||||
# Custom model managers are not inherited is base class is not abstract.
|
||||
# This will change in Django 1.10
|
||||
# https://docs.djangoproject.com/en/1.8/topics/db/managers/#custom-managers-and-model-inheritance
|
||||
objects = InheritanceManager()
|
||||
on_organization = OrganizationSourceManager()
|
||||
|
||||
def _get_periodic_task_name(self, pk=None):
|
||||
return 'check_interval_source-%i' % (pk or self.pk)
|
||||
|
||||
@@ -368,6 +404,12 @@ class EmailBaseModel(IntervalBaseModel):
|
||||
), verbose_name=_('Store email body')
|
||||
)
|
||||
|
||||
# Custom model managers are not inherited is base class is not abstract.
|
||||
# This will change in Django 1.10
|
||||
# https://docs.djangoproject.com/en/1.8/topics/db/managers/#custom-managers-and-model-inheritance
|
||||
objects = InheritanceManager()
|
||||
on_organization = OrganizationSourceManager()
|
||||
|
||||
def clean(self):
|
||||
if self.subject_metadata_type:
|
||||
if self.subject_metadata_type.pk not in self.document_type.metadata.values_list('metadata_type', flat=True):
|
||||
@@ -488,6 +530,12 @@ class POP3Email(EmailBaseModel):
|
||||
default=DEFAULT_POP3_TIMEOUT, verbose_name=_('Timeout')
|
||||
)
|
||||
|
||||
# Custom model managers are not inherited is base class is not abstract.
|
||||
# This will change in Django 1.10
|
||||
# https://docs.djangoproject.com/en/1.8/topics/db/managers/#custom-managers-and-model-inheritance
|
||||
objects = InheritanceManager()
|
||||
on_organization = OrganizationSourceManager()
|
||||
|
||||
def check_source(self):
|
||||
logger.debug('Starting POP3 email fetch')
|
||||
logger.debug('host: %s', self.host)
|
||||
@@ -535,6 +583,12 @@ class IMAPEmail(EmailBaseModel):
|
||||
max_length=64, verbose_name=_('Mailbox')
|
||||
)
|
||||
|
||||
# Custom model managers are not inherited is base class is not abstract.
|
||||
# This will change in Django 1.10
|
||||
# https://docs.djangoproject.com/en/1.8/topics/db/managers/#custom-managers-and-model-inheritance
|
||||
objects = InheritanceManager()
|
||||
on_organization = OrganizationSourceManager()
|
||||
|
||||
# http://www.doughellmann.com/PyMOTW/imaplib/
|
||||
def check_source(self):
|
||||
logger.debug('Starting IMAP email fetch')
|
||||
@@ -579,6 +633,12 @@ class WatchFolderSource(IntervalBaseModel):
|
||||
verbose_name=_('Folder path')
|
||||
)
|
||||
|
||||
# Custom model managers are not inherited is base class is not abstract.
|
||||
# This will change in Django 1.10
|
||||
# https://docs.djangoproject.com/en/1.8/topics/db/managers/#custom-managers-and-model-inheritance
|
||||
objects = InheritanceManager()
|
||||
on_organization = OrganizationSourceManager()
|
||||
|
||||
def check_source(self):
|
||||
# Force self.folder_path to unicode to avoid os.listdir returning
|
||||
# str for non-latin filenames, gh-issue #163
|
||||
|
||||
Reference in New Issue
Block a user