1 # -*- coding: utf-8 -*-
 
   2 # This file is part of EduMed, licensed under GNU Affero GPLv3 or later.
 
   3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 
   7 from optparse import make_option
 
   9 from django.core.management.base import BaseCommand
 
  10 from django.core.management.color import color_style
 
  11 from django.db import transaction
 
  13 from catalogue.models import Lesson, Section
 
  14 from librarian import IOFile
 
  16 # from search import Index
 
  19 class Command(BaseCommand):
 
  20     option_list = BaseCommand.option_list + (
 
  21         make_option('-q', '--quiet', action='store_false', dest='verbose', default=True,
 
  22                     help='Verbosity level; 0=minimal output, 1=normal output, 2=all output'),
 
  23         make_option('-a', '--attachments', dest='attachments', metavar="PATH", default='materialy',
 
  24                     help='Attachments dir path.'),
 
  25         make_option('--ignore-incomplete', action='store_true', dest='ignore_incomplete', default=False,
 
  26                     help='Attachments dir path.'),
 
  28     help = 'Imports lessons from the specified directories.'
 
  29     args = 'directory [directory ...]'
 
  31     def import_book(self, file_path, options, attachments, ignore_incomplete=False):
 
  32         verbose = options.get('verbose')
 
  33         iofile = IOFile.from_filename(os.path.join(self.curdir, file_path))
 
  34         iofile.attachments = attachments
 
  35         return Lesson.publish(iofile, ignore_incomplete)
 
  38     def all_attachments(path):
 
  42             for name in os.listdir(path):
 
  43                 fullname = os.path.join(path, name)
 
  44                 if os.path.isdir(fullname):
 
  47                     f = IOFile.from_filename(fullname)
 
  48                     files[name.decode('utf-8')] = f
 
  49                     files.setdefault(name.replace(" ", "").decode('utf-8'), f)
 
  55     def handle(self, *directories, **options):
 
  58         self.style = color_style()
 
  60         verbose = options.get('verbose')
 
  61         self.curdir = os.path.abspath(os.curdir)
 
  66         for dir_name in directories:
 
  67             abs_dir = os.path.join(self.curdir, dir_name)
 
  68             if not os.path.isdir(abs_dir):
 
  69                 print self.style.ERROR("%s: Not a directory. Skipping." % abs_dir)
 
  71                 att_dir = os.path.join(abs_dir, options['attachments'])
 
  72                 attachments = self.all_attachments(att_dir)
 
  75                 files = sorted(os.listdir(abs_dir))
 
  77                 ignore_incomplete = set()
 
  79                     file_name = files.pop(0)
 
  80                     file_path = os.path.join(abs_dir, file_name)
 
  81                     file_base, ext = os.path.splitext(file_path)
 
  83                     # Skip files that are not XML files
 
  88                         print "Parsing '%s'" % file_path
 
  95                         lesson = self.import_book(
 
  96                             file_path, options, attachments,
 
  97                             ignore_incomplete=file_name in ignore_incomplete)
 
  98                     except Section.IncompleteError, e:
 
  99                         if file_name not in postponed or postponed[file_name] < files_imported:
 
 100                             # Push it back into the queue, maybe the missing lessons will show up.
 
 102                                 print self.style.NOTICE('Waiting for missing lessons.')
 
 103                             files.append(file_name)
 
 104                             postponed[file_name] = files_imported
 
 105                         elif options['ignore_incomplete'] and file_name not in ignore_incomplete:
 
 106                             files.append(file_name)
 
 107                             ignore_incomplete.add(file_name)
 
 108                             postponed[file_name] = files_imported
 
 110                             # We're in a loop, nothing's being imported - some lesson is really missing.
 
 112                     except BaseException:
 
 114                         traceback.print_exc()
 
 118                         if hasattr(lesson, 'level'):
 
 119                             levels.add(lesson.level)
 
 125             print "Rebuilding level packages:"
 
 128                 level.build_packages()
 
 132         print "Results: %d files imported, %d skipped, %d total." % (
 
 133             files_imported, files_skipped, files_imported + files_skipped)