Added fields for MP3 and Ogg Vorbis files to Book model.
[wolnelektury.git] / apps / catalogue / management / commands / importbooks.py
1 import os
2 import sys
3 from optparse import make_option
4
5 from django.core.management.base import BaseCommand
6 from django.core.management.color import color_style
7 from django.core.files import File
8
9 from catalogue.models import Book
10
11
12 class Command(BaseCommand):
13     option_list = BaseCommand.option_list + (
14         make_option('-q', '--quiet', action='store_false', dest='verbose', default=True,
15             help='Verbosity level; 0=minimal output, 1=normal output, 2=all output'),
16         make_option('-f', '--force', action='store_true', dest='force', default=False,
17             help='Print status messages to stdout')
18     )
19     help = 'Imports books from the specified directories.'
20     args = 'directory [directory ...]'
21
22     def handle(self, *directories, **options):
23         from django.db import transaction
24
25         self.style = color_style()
26
27         verbose = options.get('verbose')
28         force = options.get('force')
29         show_traceback = options.get('traceback', False)
30
31         # Start transaction management.
32         transaction.commit_unless_managed()
33         transaction.enter_transaction_management()
34         transaction.managed(True)
35
36         files_imported = 0
37         files_skipped = 0
38         
39         for dir_name in directories:
40             if not os.path.isdir(dir_name):
41                 print self.style.ERROR("%s: Not a directory. Skipping." % dir_name)
42             else:
43                 for file_name in os.listdir(dir_name):
44                     file_path = os.path.join(dir_name, file_name)
45                     file_base, ext = os.path.splitext(file_path)
46                     
47                     # Skip files that are not XML files
48                     if not ext == '.xml':
49                         print self.style.NOTICE("%s: Not an XML file. Skipping." % file_path)
50                         continue
51                     
52                     if verbose > 0:
53                         print "Parsing '%s'" % file_path
54                     else:
55                         sys.stdout.write('.')
56                         sys.stdout.flush()
57                     
58                     # Import book files
59                     try:
60                         book = Book.from_xml_file(file_path, overwrite=force)
61                         files_imported += 1
62                         
63                         if os.path.isfile(file_base + '.pdf'):
64                             book.pdf_file.save('%s.pdf' % book.slug, File(file(file_base + '.pdf')))
65                             if verbose:
66                                 print "Importing %s.pdf" % file_base 
67                         if os.path.isfile(file_base + '.odt'):
68                             book.odt_file.save('%s.odt' % book.slug, File(file(file_base + '.odt')))
69                             if verbose:
70                                 print "Importing %s.odt" % file_base
71                         if os.path.isfile(file_base + '.txt'):
72                             book.txt_file.save('%s.txt' % book.slug, File(file(file_base + '.txt')))
73                             if verbose:
74                                 print "Importing %s.txt" % file_base
75                     
76                         book.save()
77                     
78                     except Book.AlreadyExists, msg:
79                         print self.style.ERROR('%s: Book already imported. Skipping. To overwrite use --force.' %
80                             file_path)
81                         files_skipped += 1
82                         
83         # Print results
84         print
85         print "Results: %d files imported, %d skipped, %d total." % (
86             files_imported, files_skipped, files_imported + files_skipped)
87         print
88                         
89         transaction.commit()
90         transaction.leave_transaction_management()
91