--- /dev/null
+# 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)