lesson activity icons
[edumed.git] / catalogue / management / commands / importlessons.py
index 6efd246..1eefc46 100755 (executable)
@@ -11,7 +11,8 @@ from django.core.management.base import BaseCommand
 from django.core.management.color import color_style
 from django.core.files import File
 
-from catalogue.models import Lesson
+from librarian import IOFile
+from catalogue.models import Lesson, Section
 
 #from search import Index
 
@@ -26,8 +27,13 @@ class Command(BaseCommand):
 
     def import_book(self, file_path, options):
         verbose = options.get('verbose')
-        with open(file_path) as f:
-            lesson = Lesson.publish(f)
+        iofile = IOFile.from_filename(file_path)
+        basename, ext = file_path.rsplit('.', 1)
+        if os.path.isdir(basename):
+            for att_name in os.listdir(basename):
+                iofile.attachments[att_name] = IOFile.from_filename(
+                    os.path.join(basename, att_name))
+        lesson = Lesson.publish(iofile)
 
     def handle(self, *directories, **options):
         from django.db import transaction
@@ -67,9 +73,24 @@ class Command(BaseCommand):
                         sys.stdout.flush()
 
                     # Import book files
-                    self.import_book(file_path, options)
-                    files_imported += 1
-                    transaction.commit()
+                    try:
+                        self.import_book(file_path, options)
+                        files_imported += 1
+                        transaction.commit()
+                    except Section.IncompleteError, e:
+                        if file_name not in postponed or postponed[file_name] < files_imported:
+                            # Push it back into the queue, maybe the missing lessons will show up.
+                            if verbose > 0:
+                                print self.style.NOTICE('Waiting for missing lessons.')
+                            files.append(file_name)
+                            postponed[file_name] = files_imported
+                        else:
+                            # We're in a loop, nothing's being imported - some lesson is really missing.
+                            raise e
+                    except BaseException, e:
+                        import traceback
+                        traceback.print_exc()
+                        files_skipped += 1
 
         # Print results
         print