Remove use of storage wrappers

Use a dynamic subclass instead that always deconstructs to a fake
subclass with a __eq__ method that always returns True. This should
trick makemigrations into never creating a new migrations for
changes to the storage class or the arguments.

Signed-off-by: Roberto Rosario <Roberto.Rosario@mayan-edms.com>
This commit is contained in:
Roberto Rosario
2018-12-29 03:41:47 -04:00
parent 442bf5dc4b
commit 000fe87c37
11 changed files with 59 additions and 35 deletions

View File

@@ -72,6 +72,16 @@ class ErrorLogNamespace(object):
return ErrorLogEntry.objects.filter(namespace=self.name)
class FakeStorageSubclass(object):
"""
Placeholder class to allow serializing the real storage subclass to
support migrations.
"""
def __eq__(self, other):
return True
class MissingItem(object):
_registry = []

View File

@@ -1,10 +1,10 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.16 on 2018-12-28 04:22
# Generated by Django 1.11.16 on 2018-12-29 07:38
from __future__ import unicode_literals
from django.db import migrations, models
import mayan.apps.common.classes
import mayan.apps.common.models
import mayan.apps.common.storages
class Migration(migrations.Migration):
@@ -17,6 +17,6 @@ class Migration(migrations.Migration):
migrations.AlterField(
model_name='shareduploadedfile',
name='file',
field=models.FileField(storage=mayan.apps.common.storages.storage_sharedupload_wrapper, upload_to=mayan.apps.common.models.upload_to, verbose_name='File'),
field=models.FileField(storage=mayan.apps.common.classes.FakeStorageSubclass(), upload_to=mayan.apps.common.models.upload_to, verbose_name='File'),
),
]

View File

@@ -13,7 +13,7 @@ from django.utils.encoding import force_text, python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _
from .managers import ErrorLogEntryManager, UserLocaleProfileManager
from .storages import storage_sharedupload_wrapper
from .storages import storage_sharedupload
logger = logging.getLogger(__name__)
@@ -58,7 +58,7 @@ class SharedUploadedFile(models.Model):
that runs out of process.
"""
file = models.FileField(
storage=storage_sharedupload_wrapper, upload_to=upload_to,
storage=storage_sharedupload, upload_to=upload_to,
verbose_name=_('File')
)
filename = models.CharField(max_length=255, verbose_name=_('Filename'))

View File

@@ -1,13 +1,8 @@
from __future__ import unicode_literals
from django.utils.module_loading import import_string
from .settings import setting_shared_storage, setting_shared_storage_arguments
from .utils import get_storage_subclass
storage_sharedupload = import_string(
storage_sharedupload = get_storage_subclass(
dotted_path=setting_shared_storage.value
)(**setting_shared_storage_arguments.value)
def storage_sharedupload_wrapper():
return storage_sharedupload

View File

@@ -12,6 +12,7 @@ from django.urls.base import get_script_prefix
from django.utils.datastructures import MultiValueDict
from django.utils.http import urlencode as django_urlencode
from django.utils.http import urlquote as django_urlquote
from django.utils.module_loading import import_string
from django.utils.six.moves import reduce as reduce_function
from django.utils.six.moves import xmlrpc_client
@@ -100,6 +101,28 @@ def get_descriptor(file_input, read=True):
return file_input
def get_storage_subclass(dotted_path):
"""
Import a storage class and return a subclass that will always return eq
True to avoid creating a new migration when for runtime storage class
changes.
"""
imported_storage_class = import_string(dotted_path=dotted_path)
class StorageSubclass(imported_storage_class):
def __init__(self, *args, **kwargs):
return super(StorageSubclass, self).__init__(*args, **kwargs)
def __eq__(self, other):
return True
def deconstruct(self):
return ('mayan.apps.common.classes.FakeStorageSubclass', (), {})
return StorageSubclass
def TemporaryFile(*args, **kwargs):
kwargs.update({'dir': setting_temporary_directory.value})
return tempfile.TemporaryFile(*args, **kwargs)

View File

@@ -1,10 +1,10 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.16 on 2018-12-28 04:22
# Generated by Django 1.11.16 on 2018-12-29 07:37
from __future__ import unicode_literals
from django.db import migrations, models
import mayan.apps.common.classes
import mayan.apps.document_signatures.models
import mayan.apps.document_signatures.storages
class Migration(migrations.Migration):
@@ -17,6 +17,6 @@ class Migration(migrations.Migration):
migrations.AlterField(
model_name='detachedsignature',
name='signature_file',
field=models.FileField(blank=True, null=True, storage=mayan.apps.document_signatures.storages.storage_detachedsignature_wrapper, upload_to=mayan.apps.document_signatures.models.upload_to, verbose_name='Signature file'),
field=models.FileField(blank=True, null=True, storage=mayan.apps.common.classes.FakeStorageSubclass(), upload_to=mayan.apps.document_signatures.models.upload_to, verbose_name='Signature file'),
),
]

View File

@@ -15,7 +15,7 @@ from mayan.apps.django_gpg.models import Key
from mayan.apps.documents.models import DocumentVersion
from .managers import EmbeddedSignatureManager
from .storages import storage_detachedsignature_wrapper
from .storages import storage_detachedsignature
logger = logging.getLogger(__name__)
@@ -127,7 +127,7 @@ class EmbeddedSignature(SignatureBaseModel):
@python_2_unicode_compatible
class DetachedSignature(SignatureBaseModel):
signature_file = models.FileField(
blank=True, null=True, storage=storage_detachedsignature_wrapper,
blank=True, null=True, storage=storage_detachedsignature,
upload_to=upload_to, verbose_name=_('Signature file')
)
@@ -143,7 +143,7 @@ class DetachedSignature(SignatureBaseModel):
def delete(self, *args, **kwargs):
if self.signature_file.name:
self.signature_file.storage().delete(name=self.signature_file.name)
self.signature_file.storage.delete(name=self.signature_file.name)
super(DetachedSignature, self).delete(*args, **kwargs)
def save(self, *args, **kwargs):

View File

@@ -2,14 +2,12 @@ from __future__ import unicode_literals
from django.utils.module_loading import import_string
from mayan.apps.common.utils import get_storage_subclass
from .settings import (
setting_storage_backend, setting_storage_backend_arguments
)
storage_detachedsignature = import_string(
storage_detachedsignature = get_storage_subclass(
dotted_path=setting_storage_backend.value
)(**setting_storage_backend_arguments.value)
def storage_detachedsignature_wrapper():
return

View File

@@ -1,9 +1,9 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.16 on 2018-12-28 04:22
# Generated by Django 1.11.16 on 2018-12-29 07:45
from __future__ import unicode_literals
from django.db import migrations, models
import mayan.apps.documents.storages
import mayan.apps.common.classes
import mayan.apps.documents.utils
@@ -17,6 +17,6 @@ class Migration(migrations.Migration):
migrations.AlterField(
model_name='documentversion',
name='file',
field=models.FileField(storage=mayan.apps.documents.storages.storage_documentversion_wrapper, upload_to=mayan.apps.documents.utils.document_uuid_function, verbose_name='File'),
field=models.FileField(storage=mayan.apps.common.classes.FakeStorageSubclass(), upload_to=mayan.apps.documents.utils.document_uuid_function, verbose_name='File'),
),
]

View File

@@ -21,7 +21,7 @@ from ..literals import DOCUMENT_IMAGES_CACHE_NAME
from ..managers import DocumentVersionManager
from ..settings import setting_fix_orientation
from ..signals import post_document_created, post_version_upload
from ..storages import storage_documentversion_wrapper
from ..storages import storage_documentversion
from ..utils import document_hash_function, document_uuid_function
from .document_models import Document
@@ -67,7 +67,7 @@ class DocumentVersion(models.Model):
# File related fields
file = models.FileField(
storage=storage_documentversion_wrapper, upload_to=document_uuid_function,
storage=storage_documentversion, upload_to=document_uuid_function,
verbose_name=_('File')
)
mimetype = models.CharField(
@@ -137,7 +137,7 @@ class DocumentVersion(models.Model):
be in the document storage. This is a diagnostic flag to help users
detect if the storage has desynchronized (ie: Amazon's S3).
"""
return self.file.storage().exists(self.file.name)
return self.file.storage.exists(self.file.name)
def fix_orientation(self):
for page in self.pages.all():
@@ -329,7 +329,7 @@ class DocumentVersion(models.Model):
@property
def size(self):
if self.exists():
return self.file.storage().size(self.file.name)
return self.file.storage.size(self.file.name)
else:
return None

View File

@@ -2,20 +2,18 @@ from __future__ import unicode_literals
from django.utils.module_loading import import_string
from mayan.apps.common.utils import get_storage_subclass
from .settings import (
setting_documentimagecache_storage,
setting_documentimagecache_storage_arguments, setting_storage_backend,
setting_storage_backend_arguments
)
storage_documentversion = import_string(
storage_documentversion = get_storage_subclass(
dotted_path=setting_storage_backend.value
)(**setting_storage_backend_arguments.value)
storage_documentimagecache = import_string(
dotted_path=setting_documentimagecache_storage.value
)(**setting_documentimagecache_storage_arguments.value)
def storage_documentversion_wrapper():
return storage_documentversion