Changed a way we access fields in TaggableModelForm to be compatibile with django...
[wolnelektury.git] / catalogue / management / commands / importbooks.py
1 from django.core.management.base import BaseCommand, CommandError
2 from django.core.management.color import color_style
3 from optparse import make_option
4 import sys
5 import os
6
7 from catalogue.lib.dcparser import parse
8 from catalogue.lib.slughifi import slughifi
9 from catalogue.models import Book, Tag
10
11
12 class Command(BaseCommand):
13     option_list = BaseCommand.option_list + (
14         make_option('--verbosity', action='store', dest='verbosity', default='1',
15             type='choice', choices=['0', '1', '2'],
16             help='Verbosity level; 0=minimal output, 1=normal output, 2=all output'),
17     )
18     help = 'Imports books from the specified directories.'
19     args = 'directory [directory ...]'
20
21     def handle(self, *directories, **options):
22         from django.db import transaction
23
24         self.style = color_style()
25
26         verbosity = int(options.get('verbosity', 1))
27         show_traceback = options.get('traceback', False)
28
29         # Start transaction management.
30         transaction.commit_unless_managed()
31         transaction.enter_transaction_management()
32         transaction.managed(True)
33
34         for dir_name in directories:
35             if not os.path.isdir(dir_name):
36                 print self.style.ERROR("Skipping '%s': not a directory." % dir_name)
37             else:
38                 for file_name in os.listdir(dir_name):
39                     file_path = os.path.join(dir_name, file_name)
40                     if not os.path.splitext(file_name)[1] == '.xml':
41                         print self.style.NOTICE("Skipping '%s': not an XML file." % file_path)
42                         continue
43                     if verbosity > 1:
44                         print "Parsing '%s'" % file_path
45                         
46                     book_info = parse(file_path)
47                     book = Book(title=book_info.title, slug=slughifi(book_info.title))
48                     book.save()
49                     
50                     book_tags = []
51                     for category in ('kind', 'genre', 'author', 'epoch'):    
52                         tag_name = getattr(book_info, category)
53                         tag_sort_key = tag_name
54                         if category == 'author':
55                             tag_sort_key = tag_name.last_name
56                             tag_name = ' '.join(tag_name.first_names) + ' ' + tag_name.last_name
57                         tag, created = Tag.objects.get_or_create(name=tag_name,
58                             slug=slughifi(tag_name), sort_key=slughifi(tag_sort_key), category=category)
59                         tag.save()
60                         book_tags.append(tag)
61                     book.tags = book_tags
62         
63         
64         transaction.commit()
65         transaction.leave_transaction_management()
66