3 from django.core.management.base import BaseCommand
4 from django.core.management.color import color_style
5 from optparse import make_option
7 from catalogue.models import Book
10 class Command(BaseCommand):
11 option_list = BaseCommand.option_list + (
12 make_option('--verbosity', action='store', dest='verbosity', default='1',
13 type='choice', choices=['0', '1', '2'],
14 help='Verbosity level; 0=minimal output, 1=normal output, 2=all output'),
16 help = 'Imports books from the specified directories.'
17 args = 'directory [directory ...]'
19 def handle(self, *directories, **options):
20 from django.db import transaction
22 self.style = color_style()
24 verbosity = int(options.get('verbosity', 1))
25 show_traceback = options.get('traceback', False)
27 # Start transaction management.
28 transaction.commit_unless_managed()
29 transaction.enter_transaction_management()
30 transaction.managed(True)
32 for dir_name in directories:
33 if not os.path.isdir(dir_name):
34 print self.style.ERROR("Skipping '%s': not a directory." % dir_name)
36 for file_name in os.listdir(dir_name):
37 file_path = os.path.join(dir_name, file_name)
38 if not os.path.splitext(file_name)[1] == '.xml':
39 print self.style.NOTICE("Skipping '%s': not an XML file." % file_path)
42 print "Parsing '%s'" % file_path
44 Book.from_xml_file(file_path)
47 transaction.leave_transaction_management()