misc: add management command to import posts from old weblog
authorFrédéric Péters <fpeters@0d.be>
Sun, 29 Dec 2019 19:03:11 +0000 (20:03 +0100)
committerFrédéric Péters <fpeters@0d.be>
Mon, 30 Dec 2019 08:35:50 +0000 (09:35 +0100)
chloro/phyll/management/__init__.py [new file with mode: 0644]
chloro/phyll/management/commands/__init__.py [new file with mode: 0644]
chloro/phyll/management/commands/load-old-posts.py [new file with mode: 0644]

diff --git a/chloro/phyll/management/__init__.py b/chloro/phyll/management/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/chloro/phyll/management/commands/__init__.py b/chloro/phyll/management/commands/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/chloro/phyll/management/commands/load-old-posts.py b/chloro/phyll/management/commands/load-old-posts.py
new file mode 100644 (file)
index 0000000..0e2795c
--- /dev/null
@@ -0,0 +1,82 @@
+# chloro - personal space
+# Copyright (C) 2019  Frederic Peters
+#
+# This program is free software: you can redistribute it and/or modify it
+# under the terms of the GNU Affero General Public License as published
+# by the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+import datetime
+import os
+import pickle
+import sys
+
+from django.core.management.base import BaseCommand
+from django.utils.timezone import make_aware
+
+from chloro.phyll.models import Note
+
+
+class PickledPost:
+    pass
+
+
+class PickledComment:
+    pass
+
+
+class UnpicklerClass(pickle.Unpickler):
+    def find_class(self, module, name):
+        if module == 'copy_reg':
+            module = 'copyreg'
+        elif module == '__builtin__':
+            module = 'builtins'
+        elif (module, name) in (('chlorophyll.posts', 'Post'), ('posts', 'Post')):
+            return PickledPost
+        elif (module, name) in (('chlorophyll.posts', 'Comment'), ('posts', 'Comment')):
+            return PickledComment
+        __import__(module)
+        mod = sys.modules[module]
+        klass = getattr(mod, name)
+        return klass
+
+
+class Command(BaseCommand):
+    def handle(self, verbosity, **options):
+        categories = {
+            1: ('code',),
+            2: ('divers',),
+            3: ('debian', 'code'),
+            4: ('tricoteuse', 'divers'),
+            5: ('bouquins', 'divers'),
+            6: ('antipub', 'divers'),
+            7: ('musique', 'divers'),
+            8: ('gnome', 'code'),
+            9: ('radio',),
+            10: ('ecriture', 'divers'),
+        }
+        slugs = {}
+        for filename in sorted(os.listdir('posts'), key=lambda x: int(x)):
+            old_post = UnpicklerClass(open(os.path.join('posts', filename), 'rb'), encoding='utf-8').load()
+            if old_post.url_name in slugs:
+                old_post.url_name = '%s-2' % old_post.url_name
+            slugs[old_post.url_name] = True
+            note, created = Note.objects.get_or_create(slug=old_post.url_name)
+            note.title = old_post.title
+            note.text = old_post.content_html
+            note.text = note.text.replace('src="/', 'src="/media/')
+            note.creation_timestamp = make_aware(datetime.datetime(*old_post.issued[:6]))
+            note.published = bool(note.creation_timestamp.date() >= datetime.date(2014, 1, 1))
+            note.save()
+            for tag in categories.get(old_post.category_id):
+                note.tags.add(tag)
+            if getattr(old_post, 'language', None):
+                note.tags.add('lang-%s' % old_post.language)