1 # -*- coding: utf-8 -*-
3 from optparse import make_option
7 from django.core.management.base import BaseCommand
8 from django.core.management.color import color_style
9 from django.db import transaction
11 from catalogue.models import Book
14 class Command(BaseCommand):
15 option_list = BaseCommand.option_list + (
16 make_option('-q', '--quiet', action='store_false', dest='verbose', default=True, help='Less output'),
18 help = 'Imports XML files.'
21 def handle(self, directory, *args, **options):
23 self.style = color_style()
25 verbose = options.get('verbose')
27 # Start transaction management.
28 transaction.commit_unless_managed()
29 transaction.enter_transaction_management()
30 transaction.managed(True)
34 "author_name": 'Platforma',
35 "description": 'Automatycznie zaimportowane',
38 for xml_filename in os.listdir(directory):
41 text = open(os.path.join(directory, xml_filename)).read().decode('utf-8')
43 tree = etree.fromstring(text)
44 slug = xml_filename.split('.')[0]
45 except Exception as e:
46 print xml_filename, 'error: ', repr(e)
48 title = tree.find('.//header').text
49 print book_count, slug, title
56 commit_args=commit_args,
63 print "Imported %d books" % book_count
67 transaction.leave_transaction_management()