[Patchew-devel] [PATCH 3/5] models: confine blob handling to migrations

Paolo Bonzini posted 5 patches 2 years, 8 months ago
[Patchew-devel] [PATCH 3/5] models: confine blob handling to migrations
Posted by Paolo Bonzini 2 years, 8 months ago
Blobs are not used anymore by api.models except for migrating historical
databases.  Move the api.blobs code into the api.migrations package.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 api/blobs.py                             | 30 ------------------------
 api/migrations/0030_deblob_properties.py |  4 ++--
 api/migrations/0062_deblob_messages.py   |  6 ++---
 api/migrations/__init__.py               | 24 +++++++++++++++----
 api/models.py                            | 16 +++----------
 5 files changed, 28 insertions(+), 52 deletions(-)
 delete mode 100644 api/blobs.py

diff --git a/api/blobs.py b/api/blobs.py
deleted file mode 100644
index 502076e..0000000
--- a/api/blobs.py
+++ /dev/null
@@ -1,30 +0,0 @@
-#!/usr/bin/env python3
-#
-# Copyright 2016, 2018 Red Hat, Inc.
-#
-# Authors:
-#     Fam Zheng <famz@redhat.com>
-#     Paolo Bonzini <pbonzini@redhat.com>
-#
-# This work is licensed under the MIT License.  Please see the LICENSE file or
-# http://opensource.org/licenses/MIT.
-
-
-import os
-import uuid
-
-from django.conf import settings
-import lzma
-
-
-def load_blob(name):
-    fn = os.path.join(settings.DATA_DIR, "blob", name + ".xz")
-    return lzma.open(fn, "r").read().decode("utf-8")
-
-
-def delete_blob(name):
-    fn = os.path.join(settings.DATA_DIR, "blob", name + ".xz")
-    try:
-        os.remove(fn)
-    except FileNotFoundError:
-        pass
diff --git a/api/migrations/0030_deblob_properties.py b/api/migrations/0030_deblob_properties.py
index 5fca390..826bf68 100644
--- a/api/migrations/0030_deblob_properties.py
+++ b/api/migrations/0030_deblob_properties.py
@@ -3,7 +3,7 @@ from __future__ import unicode_literals
 
 from django.db import migrations
 
-from api import blobs
+from . import load_blob
 
 
 def deblob_properties(apps, schema_editor):
@@ -12,7 +12,7 @@ def deblob_properties(apps, schema_editor):
         for obj in objects:
             obj.blob = False
             if obj.value is not None:
-                obj.value = blobs.load_blob(obj.value)
+                obj.value = load_blob(obj.value)
             obj.save()
 
     # We can't import the models directly as they may be a newer
diff --git a/api/migrations/0062_deblob_messages.py b/api/migrations/0062_deblob_messages.py
index 22ee12f..47894fa 100644
--- a/api/migrations/0062_deblob_messages.py
+++ b/api/migrations/0062_deblob_messages.py
@@ -3,7 +3,7 @@ from __future__ import unicode_literals
 
 from django.db import migrations, transaction
 
-from api import blobs
+from . import load_blob, delete_blob
 
 def deblob_messages(apps, schema_editor):
     Project = apps.get_model("api", "Project")
@@ -19,10 +19,10 @@ def deblob_messages(apps, schema_editor):
                 q = Message.objects.filter(project=p, date__lte=first_date, mbox_bytes=None).order_by("-date")[:1000]
                 for msg in q:
                     try:
-                        mbox_decoded = blobs.load_blob(msg.message_id)
+                        mbox_decoded = load_blob(msg.message_id)
                         msg.mbox_bytes = mbox_decoded.encode("utf-8")
                         msg.save()
-                        blobs.delete_blob(msg.message_id)
+                        delete_blob(msg.message_id)
                         done += 1
                     except Exception as e:
                         print(msg, type(e))
diff --git a/api/migrations/__init__.py b/api/migrations/__init__.py
index 02f4c63..35b5483 100644
--- a/api/migrations/__init__.py
+++ b/api/migrations/__init__.py
@@ -1,22 +1,38 @@
 #!/usr/bin/env python3
 #
-# Copyright 2018 Red Hat, Inc.
+# Copyright 2016, 2018 Red Hat, Inc.
 #
 # Authors:
+#     Fam Zheng <famz@redhat.com>
 #     Paolo Bonzini <pbonzini@redhat.com>
 #
 # This work is licensed under the MIT License.  Please see the LICENSE file or
 # http://opensource.org/licenses/MIT.
 
+from django.conf import settings
 from django.db import migrations
 
 import json
-from api import blobs
+import lzma
+import os
+import uuid
 
 
+def load_blob(name):
+    fn = os.path.join(settings.DATA_DIR, "blob", name + ".xz")
+    return lzma.open(fn, "r").read().decode("utf-8")
+
+
+def delete_blob(name):
+    fn = os.path.join(settings.DATA_DIR, "blob", name + ".xz")
+    try:
+        os.remove(fn)
+    except FileNotFoundError:
+        pass
+
 def load_blob_json_safe(name):
     try:
-        return json.loads(blobs.load_blob(name))
+        return json.loads(load_blob(name))
     except Exception as e:
         return "Failed to load blob %s: %s" % (name, e)
 
@@ -44,7 +60,7 @@ def get_property(model, name, **kwargs):
 def delete_property_blob(model, name, **kwargs):
     mp = get_property_raw(model, name, **kwargs)
     if hasattr(mp, "blob") and mp.blob:
-        blobs.delete_blob(mp.value)
+        delete_blob(mp.value)
 
 
 def set_property(model, name, value, **kwargs):
diff --git a/api/models.py b/api/models.py
index ce38913..7bf8021 100644
--- a/api/models.py
+++ b/api/models.py
@@ -22,7 +22,6 @@ import lzma
 from mbox import MboxMessage, decode_payload
 from patchew.tags import lines_iter
 from event import emit_event, declare_event
-from .blobs import delete_blob, load_blob
 import mod
 
 
@@ -493,7 +492,7 @@ class MessageManager(models.Manager):
         msg.is_patch = m.is_patch()
         msg.patch_num = m.get_num()[0]
         msg.project = project
-        msg.save_mbox(mbox)
+        msg.mbox_bytes = mbox.encode("utf-8")
         msg.save()
         emit_event("MessageAdded", message=msg)
         self.update_series(msg)
@@ -533,7 +532,7 @@ class MessageManager(models.Manager):
             msg.project = p
             if self.filter(message_id=msgid, project__name=p.name).first():
                 raise self.DuplicateMessageError(msgid)
-            msg.save_mbox(mbox)
+            msg.mbox_bytes = mbox.encode("utf-8")
             msg.save()
             emit_event("MessageAdded", message=msg)
             self.update_series(msg)
@@ -654,12 +653,6 @@ class Message(models.Model):
     maintainers = jsonfield.JSONField(blank=True, default=[])
     properties = jsonfield.JSONField(default={})
 
-    def save_mbox(self, mbox):
-        mbox_bytes = mbox.encode("utf-8")
-        if self.mbox_bytes is None:
-            delete_blob(self.message_id)
-        self.mbox_bytes = mbox_bytes
-
     def get_mbox_obj(self):
         if not hasattr(self, "_mbox_obj"):
             self._mbox_obj = MboxMessage(self.mbox)
@@ -667,10 +660,7 @@ class Message(models.Model):
 
     def get_mbox(self):
         if not hasattr(self, "_mbox_decoded"):
-            if self.mbox_bytes:
-                self._mbox_decoded = str(self.mbox_bytes, "utf-8")
-            else:
-                self._mbox_decoded = load_blob(self.message_id)
+            self._mbox_decoded = str(self.mbox_bytes, "utf-8")
         return self._mbox_decoded
 
     mbox = property(get_mbox)
-- 
2.35.1


_______________________________________________
Patchew-devel mailing list
Patchew-devel@redhat.com
https://listman.redhat.com/mailman/listinfo/patchew-devel