From: Radek Czajka Date: Fri, 4 Oct 2019 13:29:22 +0000 (+0200) Subject: Remove unnecessary storage class. X-Git-Url: https://git.mdrn.pl/redakcja.git/commitdiff_plain/f9c19bbcbc514aebcff44b37ca5b92b4a0e181cf?hp=39a72bbc1bbd88a9ca247e8d0bba7002a77eec04 Remove unnecessary storage class. --- diff --git a/src/catalogue/migrations/0001_initial.py b/src/catalogue/migrations/0001_initial.py index 8035a1ab..bed9eb1c 100644 --- a/src/catalogue/migrations/0001_initial.py +++ b/src/catalogue/migrations/0001_initial.py @@ -5,8 +5,8 @@ from django.db import models, migrations import datetime import django.db.models.deletion from django.conf import settings +import django.core.files.storage import dvcs.models -import dvcs.storage class Migration(migrations.Migration): @@ -88,7 +88,7 @@ class Migration(migrations.Migration): ('description', models.TextField(default=b'', verbose_name='description', blank=True)), ('created_at', models.DateTimeField(default=datetime.datetime.now, editable=False, db_index=True)), ('publishable', models.BooleanField(default=False, verbose_name='publishable')), - ('data', models.FileField(upload_to=dvcs.models.data_upload_to, storage=dvcs.storage.GzipFileSystemStorage(location=settings.CATALOGUE_REPO_PATH), verbose_name='data')), + ('data', models.FileField(upload_to=dvcs.models.data_upload_to, storage=django.core.files.storage.FileSystemStorage(location=settings.CATALOGUE_REPO_PATH), verbose_name='data')), ('author', models.ForeignKey(verbose_name='author', blank=True, to=settings.AUTH_USER_MODEL, null=True, on_delete=models.CASCADE)), ('merge_parent', models.ForeignKey(related_name='merge_children', default=None, blank=True, to='catalogue.ChunkChange', null=True, verbose_name='merge parent', on_delete=models.CASCADE)), ('parent', models.ForeignKey(related_name='children', default=None, blank=True, to='catalogue.ChunkChange', null=True, verbose_name='parent', on_delete=models.CASCADE)), @@ -162,7 +162,7 @@ class Migration(migrations.Migration): ('description', models.TextField(default=b'', verbose_name='description', blank=True)), ('created_at', models.DateTimeField(default=datetime.datetime.now, editable=False, db_index=True)), ('publishable', models.BooleanField(default=False, verbose_name='publishable')), - ('data', models.FileField(upload_to=dvcs.models.data_upload_to, storage=dvcs.storage.GzipFileSystemStorage(location=settings.CATALOGUE_IMAGE_REPO_PATH), verbose_name='data')), + ('data', models.FileField(upload_to=dvcs.models.data_upload_to, storage=django.core.files.storage.FileSystemStorage(location=settings.CATALOGUE_IMAGE_REPO_PATH), verbose_name='data')), ('author', models.ForeignKey(verbose_name='author', blank=True, to=settings.AUTH_USER_MODEL, null=True, on_delete=models.CASCADE)), ('merge_parent', models.ForeignKey(related_name='merge_children', default=None, blank=True, to='catalogue.ImageChange', null=True, verbose_name='merge parent', on_delete=models.CASCADE)), ('parent', models.ForeignKey(related_name='children', default=None, blank=True, to='catalogue.ImageChange', null=True, verbose_name='parent', on_delete=models.CASCADE)), diff --git a/src/dvcs/models.py b/src/dvcs/models.py index 6a047680..ed771c43 100644 --- a/src/dvcs/models.py +++ b/src/dvcs/models.py @@ -3,9 +3,11 @@ # from datetime import datetime import os.path +from zlib import compress, decompress from django.contrib.auth.models import User from django.core.files.base import ContentFile +from django.core.files.storage import FileSystemStorage from django.db import models, transaction from django.db.models.base import ModelBase from django.utils.text import format_lazy @@ -14,7 +16,6 @@ import merge3 from django.conf import settings from dvcs.signals import post_commit, post_publishable -from dvcs.storage import GzipFileSystemStorage class Tag(models.Model): @@ -119,11 +120,17 @@ class Change(models.Model): self.revision = tree_rev + 1 return super(Change, self).save(*args, **kwargs) + def save_text(self, text, **kwargs): + self.data.save( + '', + ContentFile(compress(text.encode('utf-8'))), + **kwargs + ) + def materialize(self): - f = self.data.storage.open(self.data.name) - text = f.read() - f.close() - return str(text, 'utf-8') + with self.data.open('rb') as f: + content = f.read() + return decompress(content).decode('utf-8') def merge_with(self, other, author=None, author_name=None, author_email=None, @@ -146,7 +153,7 @@ class Change(models.Model): author_name=author_name, author_email=author_email, description=description) - merge_node.data.save('', ContentFile(result)) + merge_node.save_text(result) return merge_node def revert(self, **kwargs): @@ -178,7 +185,7 @@ def create_tag_model(model): def create_change_model(model): name = model.__name__ + 'Change' - repo = GzipFileSystemStorage(location=model.REPO_PATH) + repo = FileSystemStorage(location=model.REPO_PATH) class Meta(Change.Meta): app_label = model._meta.app_label @@ -280,7 +287,7 @@ class Document(models.Model, metaclass=DocumentMeta): description=kwargs.get('description', ''), publishable=publishable, parent=parent) change.tags.set(tags) - change.data.save('', ContentFile(text.encode('utf-8'))) + change.save_text(text) change.save() if self.head: diff --git a/src/dvcs/storage.py b/src/dvcs/storage.py deleted file mode 100755 index 76051aba..00000000 --- a/src/dvcs/storage.py +++ /dev/null @@ -1,25 +0,0 @@ -# This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later. -# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. -# -from zlib import compress, decompress - -from django.core.files.base import ContentFile, File -from django.core.files.storage import FileSystemStorage -from django.utils.deconstruct import deconstructible -from django.utils.encoding import force_bytes - - -@deconstructible -class GzipFileSystemStorage(FileSystemStorage): - def _open(self, name, mode='rb'): - """TODO: This is good for reading; what about writing?""" - f = open(self.path(name), 'rb') - text = f.read() - f.close() - return ContentFile(decompress(text)) - - def _save(self, name, content): - data = force_bytes(content.read()) - content = ContentFile(compress(data)) - - return super(GzipFileSystemStorage, self)._save(name, content) diff --git a/src/dvcs/tests/migrations/0001_initial.py b/src/dvcs/tests/migrations/0001_initial.py index e6e2b513..18f44268 100644 --- a/src/dvcs/tests/migrations/0001_initial.py +++ b/src/dvcs/tests/migrations/0001_initial.py @@ -4,10 +4,10 @@ import datetime import os.path from django.conf import settings +import django.core.files.storage from django.db import migrations, models import django.db.models.deletion import dvcs.models -import dvcs.storage class Migration(migrations.Migration): @@ -39,7 +39,7 @@ class Migration(migrations.Migration): ('description', models.TextField(blank=True, default=b'', verbose_name='description')), ('created_at', models.DateTimeField(db_index=True, default=datetime.datetime.now, editable=False)), ('publishable', models.BooleanField(default=False, verbose_name='publishable')), - ('data', models.FileField(storage=dvcs.storage.GzipFileSystemStorage(location=os.path.join(settings.MEDIA_ROOT, 'dvcs')), upload_to=dvcs.models.data_upload_to, verbose_name='data')), + ('data', models.FileField(storage=django.core.files.storage.FileSystemStorage(location=os.path.join(settings.MEDIA_ROOT, 'dvcs')), upload_to=dvcs.models.data_upload_to, verbose_name='data')), ('author', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL, verbose_name='author')), ('merge_parent', models.ForeignKey(blank=True, default=None, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='merge_children', to='tests.ADocumentChange', verbose_name='merge parent')), ('parent', models.ForeignKey(blank=True, default=None, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='children', to='tests.ADocumentChange', verbose_name='parent')),