From 9f7843e0f2b11e57ca4f5308ddc5a863f8de0908 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Sun, 29 Dec 2019 20:03:11 +0100 Subject: [PATCH] misc: add management command to import posts from old weblog --- chloro/phyll/management/__init__.py | 0 chloro/phyll/management/commands/__init__.py | 0 .../management/commands/load-old-posts.py | 82 +++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 chloro/phyll/management/__init__.py create mode 100644 chloro/phyll/management/commands/__init__.py create mode 100644 chloro/phyll/management/commands/load-old-posts.py diff --git a/chloro/phyll/management/__init__.py b/chloro/phyll/management/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/chloro/phyll/management/commands/__init__.py b/chloro/phyll/management/commands/__init__.py new file mode 100644 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 index 0000000..0e2795c --- /dev/null +++ b/chloro/phyll/management/commands/load-old-posts.py @@ -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 . + +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) -- 2.39.2