Remove unnecessary storage class.
authorRadek Czajka <rczajka@rczajka.pl>
Fri, 4 Oct 2019 13:29:22 +0000 (15:29 +0200)
committerRadek Czajka <rczajka@rczajka.pl>
Fri, 4 Oct 2019 13:29:22 +0000 (15:29 +0200)
src/catalogue/migrations/0001_initial.py
src/dvcs/models.py
src/dvcs/storage.py [deleted file]
src/dvcs/tests/migrations/0001_initial.py

index 8035a1a..bed9eb1 100644 (file)
@@ -5,8 +5,8 @@ from django.db import models, migrations
 import datetime
 import django.db.models.deletion
 from django.conf import settings
 import datetime
 import django.db.models.deletion
 from django.conf import settings
+import django.core.files.storage
 import dvcs.models
 import dvcs.models
-import dvcs.storage
 
 
 class Migration(migrations.Migration):
 
 
 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')),
                 ('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)),
                 ('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')),
                 ('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)),
                 ('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)),
index 6a04768..ed771c4 100644 (file)
@@ -3,9 +3,11 @@
 #
 from datetime import datetime
 import os.path
 #
 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.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
 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 django.conf import settings
 from dvcs.signals import post_commit, post_publishable
-from dvcs.storage import GzipFileSystemStorage
 
 
 class Tag(models.Model):
 
 
 class Tag(models.Model):
@@ -119,11 +120,17 @@ class Change(models.Model):
                 self.revision = tree_rev + 1
         return super(Change, self).save(*args, **kwargs)
 
                 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):
     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, 
 
     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)
                     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):
         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'
 
 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
 
     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)
             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:
         change.save()
 
         if self.head:
diff --git a/src/dvcs/storage.py b/src/dvcs/storage.py
deleted file mode 100755 (executable)
index 76051ab..0000000
+++ /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)
index e6e2b51..18f4426 100644 (file)
@@ -4,10 +4,10 @@
 import datetime
 import os.path
 from django.conf import settings
 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
 from django.db import migrations, models
 import django.db.models.deletion
 import dvcs.models
-import dvcs.storage
 
 
 class Migration(migrations.Migration):
 
 
 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')),
                 ('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')),
                 ('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')),