add command to import legacy lessons
authorJan Szejko <janek37@gmail.com>
Fri, 6 Oct 2017 15:23:16 +0000 (17:23 +0200)
committerJan Szejko <janek37@gmail.com>
Fri, 6 Oct 2017 15:23:16 +0000 (17:23 +0200)
apps/catalogue/management/commands/import_lessons.py [new file with mode: 0644]
apps/catalogue/models/book.py

diff --git a/apps/catalogue/management/commands/import_lessons.py b/apps/catalogue/management/commands/import_lessons.py
new file mode 100644 (file)
index 0000000..7ff494f
--- /dev/null
@@ -0,0 +1,68 @@
+# -*- coding: utf-8 -*-
+
+from optparse import make_option
+from lxml import etree
+import os
+
+from django.core.management.base import BaseCommand
+from django.core.management.color import color_style
+from django.db import transaction
+from librarian import ParseError, ValidationError
+
+from catalogue.models import Book
+
+
+class Command(BaseCommand):
+    option_list = BaseCommand.option_list + (
+        make_option('-q', '--quiet', action='store_false', dest='verbose', default=True, help='Less output'),
+    )
+    help = 'Imports XML files.'
+    args = 'directory'
+
+    def handle(self, directory, *args, **options):
+
+        self.style = color_style()
+
+        verbose = options.get('verbose')
+
+        # Start transaction management.
+        transaction.commit_unless_managed()
+        transaction.enter_transaction_management()
+        transaction.managed(True)
+
+        book_count = 0
+        commit_args = {
+            "author_name": 'Platforma',
+            "description": 'Automatycznie zaimportowane',
+            "publishable": True,
+        }
+        for xml_filename in os.listdir(directory):
+            if verbose:
+                print xml_filename
+            text = open(os.path.join(directory, xml_filename)).read().decode('utf-8')
+            try:
+                tree = etree.fromstring(text)
+                slug = xml_filename.split('.')[0]
+            except Exception as e:
+                print xml_filename, 'error: ', repr(e)
+            else:
+                title = tree.find('.//header').text
+                print book_count, slug, title
+                Book.create(
+                    text=text,
+                    creator=None,
+                    slug=slug,
+                    title=title,
+                    gallery=slug,
+                    commit_args=commit_args,
+                )
+                book_count += 1
+
+        # Print results
+        print
+        print "Results:"
+        print "Imported %d books" % book_count
+        print
+
+        transaction.commit()
+        transaction.leave_transaction_management()
index 7589587..5990428 100755 (executable)
@@ -92,10 +92,10 @@ class Book(models.Model):
 
     @classmethod
     @transaction.commit_on_success
-    def create(cls, creator, text, **kwargs):
+    def create(cls, creator, text, commit_args=None, **kwargs):
         b = cls.objects.create(**kwargs)
         b.chunk_set.all().update(creator=creator)
-        b[0].commit(text, author=creator)
+        b[0].commit(text, author=creator, **(commit_args or {}))
         return b
 
     def add(self, *args, **kwargs):