Add sponsors page; remove some unused files.
[edumed.git] / catalogue / management / commands / importlessons.py
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.
4 #
5 import os
6 import sys
7 import time
8 from optparse import make_option
9 from django.conf import settings
10 from django.core.management.base import BaseCommand
11 from django.core.management.color import color_style
12 from django.core.files import File
13
14 from catalogue.models import Lesson
15
16 #from search import Index
17
18
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     )
24     help = 'Imports lessons from the specified directories.'
25     args = 'directory [directory ...]'
26
27     def import_book(self, file_path, options):
28         verbose = options.get('verbose')
29         with open(file_path) as f:
30             lesson = Lesson.publish(f)
31
32     def handle(self, *directories, **options):
33         from django.db import transaction
34
35         self.style = color_style()
36         
37         verbose = options.get('verbose')
38
39         # Start transaction management.
40         transaction.commit_unless_managed()
41         transaction.enter_transaction_management()
42         transaction.managed(True)
43
44         files_imported = 0
45         files_skipped = 0
46
47         for dir_name in directories:
48             if not os.path.isdir(dir_name):
49                 print self.style.ERROR("%s: Not a directory. Skipping." % dir_name)
50             else:
51                 # files queue
52                 files = sorted(os.listdir(dir_name))
53                 postponed = {}
54                 while files:
55                     file_name = files.pop(0)
56                     file_path = os.path.join(dir_name, file_name)
57                     file_base, ext = os.path.splitext(file_path)
58
59                     # Skip files that are not XML files
60                     if not ext == '.xml':
61                         continue
62
63                     if verbose > 0:
64                         print "Parsing '%s'" % file_path
65                     else:
66                         sys.stdout.write('.')
67                         sys.stdout.flush()
68
69                     # Import book files
70                     self.import_book(file_path, options)
71                     files_imported += 1
72                     transaction.commit()
73
74         # Print results
75         print
76         print "Results: %d files imported, %d skipped, %d total." % (
77             files_imported, files_skipped, files_imported + files_skipped)
78         print
79
80         transaction.commit()
81         transaction.leave_transaction_management()