Documents: Remove old image caching model

With the creation of the new general use file Cache system
the old DocumentPageCachedImage model and manager are no longer
needed. This commit removed the model and the manager, and add
a migration to remove any data in the model before removing it
from the database.

Signed-off-by: Roberto Rosario <roberto.rosario.gonzalez@gmail.com>
This commit is contained in:
Roberto Rosario
2018-12-04 15:21:57 -04:00
parent 66b04296f5
commit f4e0e06c66
3 changed files with 34 additions and 51 deletions

View File

@@ -34,21 +34,6 @@ class DocumentManager(models.Manager):
document.invalidate_cache()
class DocumentPageCachedImage(models.Manager):
def get_by_natural_key(self, filename, document_page_natural_key):
DocumentPage = apps.get_model(
app_label='documents', model_name='DocumentPage'
)
try:
document_page = DocumentPage.objects.get_by_natural_key(
*document_page_natural_key
)
except DocumentPage.DoesNotExist:
raise self.model.DoesNotExist
return self.get(document_page__pk=document_page.pk, filename=filename)
class DocumentPageManager(models.Manager):
def get_by_natural_key(self, page_number, document_version_natural_key):
DocumentVersion = apps.get_model(

View File

@@ -0,0 +1,31 @@
from __future__ import unicode_literals
from django.db import migrations
def operation_clear_old_cache(apps, schema_editor):
DocumentPageCachedImage = apps.get_model(
'documents', 'DocumentPageCachedImage'
)
for cached_image in DocumentPageCachedImage.objects.using(schema_editor.connection.alias).all():
# Delete each cached image directly to trigger the physical deletion
# of the stored file
cached_image.delete()
class Migration(migrations.Migration):
dependencies = [
('documents', '0047_auto_20180917_0737'),
]
operations = [
migrations.RunPython(code=operation_clear_old_cache),
migrations.RemoveField(
model_name='documentpagecachedimage',
name='document_page',
),
migrations.DeleteModel(
name='DocumentPageCachedImage',
),
]

View File

@@ -39,10 +39,9 @@ from .literals import (
DEFAULT_DELETE_PERIOD, DEFAULT_DELETE_TIME_UNIT, DOCUMENT_IMAGES_CACHE_NAME
)
from .managers import (
DocumentManager, DocumentPageCachedImage, DocumentPageManager,
DocumentVersionManager, DocumentTypeManager, DuplicatedDocumentManager,
FavoriteDocumentManager, PassthroughManager, RecentDocumentManager,
TrashCanManager
DocumentManager, DocumentPageManager, DocumentVersionManager,
DocumentTypeManager, DuplicatedDocumentManager, FavoriteDocumentManager,
PassthroughManager, RecentDocumentManager, TrashCanManager
)
from .permissions import permission_document_view
from .settings import (
@@ -972,38 +971,6 @@ class DocumentPage(models.Model):
return '{}-{}'.format(self.document_version.uuid, self.pk)
class DocumentPageCachedImage(models.Model):
document_page = models.ForeignKey(
on_delete=models.CASCADE, related_name='cached_images',
to=DocumentPage, verbose_name=_('Document page')
)
datetime = models.DateTimeField(
auto_now_add=True, db_index=True, verbose_name=_('Date time')
)
filename = models.CharField(max_length=128, verbose_name=_('Filename'))
file_size = models.PositiveIntegerField(
db_index=True, default=0, verbose_name=_('File size')
)
objects = DocumentPageCachedImage()
class Meta:
verbose_name = _('Document page cached image')
verbose_name_plural = _('Document page cached images')
#def delete(self, *args, **kwargs):
# storage_documentimagecache.delete(self.filename)
# return super(DocumentPageCachedImage, self).delete(*args, **kwargs)
def natural_key(self):
return (self.filename, self.document_page.natural_key())
natural_key.dependencies = ['documents.DocumentPage']
#def save(self, *args, **kwargs):
# self.file_size = storage_documentimagecache.size(self.filename)
# return super(DocumentPageCachedImage, self).save(*args, **kwargs)
class DocumentPageResult(DocumentPage):
class Meta:
ordering = ('document_version__document', 'page_number')