ignore *.css.map
[wolnelektury.git] / src / catalogue / management / commands / importbooks.py
index 338fea6..5280778 100644 (file)
@@ -4,15 +4,14 @@
 #
 import os
 import sys
-import time
 from optparse import make_option
 from django.conf import settings
 from django.core.management.base import BaseCommand
 from django.core.management.color import color_style
 from django.core.files import File
-from catalogue.utils import trim_query_log
+from django.db import transaction
 from librarian.picture import ImageStore
-from wolnelektury.management.profile import profile
+from wolnelektury.management.profile import profile
 
 from catalogue.models import Book
 from picture.models import Picture
@@ -23,18 +22,16 @@ from search.index import Index
 class Command(BaseCommand):
     option_list = BaseCommand.option_list + (
         make_option('-q', '--quiet', action='store_false', dest='verbose', default=True,
-            help='Verbosity level; 0=minimal output, 1=normal output, 2=all output'),
+                    help='Verbosity level; 0=minimal output, 1=normal output, 2=all output'),
         make_option('-f', '--force', action='store_true', dest='force', default=False,
-            help='Overwrite works already in the catalogue'),
+                    help='Overwrite works already in the catalogue'),
         make_option('-D', '--dont-build', dest='dont_build',
-            metavar="FORMAT,...",
-            help="Skip building specified formats"),
+                    metavar="FORMAT,...",
+                    help="Skip building specified formats"),
         make_option('-S', '--no-search-index', action='store_false', dest='search_index', default=True,
-            help='Skip indexing imported works for search'),
-        make_option('-w', '--wait-until', dest='wait_until', metavar='TIME',
-            help='Wait until specified time (Y-M-D h:m:s)'),
+                    help='Skip indexing imported works for search'),
         make_option('-p', '--picture', action='store_true', dest='import_picture', default=False,
-            help='Import pictures'),
+                    help='Import pictures'),
     )
     help = 'Imports books from the specified directories.'
     args = 'directory [directory ...]'
@@ -72,24 +69,14 @@ class Command(BaseCommand):
                 raise ex
         return picture
 
-    #    @profile
+    # @profile
+    @transaction.atomic
     def handle(self, *directories, **options):
-        from django.db import transaction
-
         self.style = color_style()
 
         verbose = options.get('verbose')
         import_picture = options.get('import_picture')
 
-        wait_until = None
-        if options.get('wait_until'):
-            wait_until = time.mktime(time.strptime(options.get('wait_until'), '%Y-%m-%d %H:%M:%S'))
-            if verbose > 0:
-                print "Will wait until %s; it's %f seconds from now" % (
-                    time.strftime('%Y-%m-%d %H:%M:%S',
-                    time.localtime(wait_until)), wait_until - time.time())
-
-        index = None
         if options.get('search_index') and not settings.NO_SEARCH_INDEX:
             index = Index()
             try:
@@ -99,11 +86,6 @@ class Command(BaseCommand):
                 index.index.rollback()
                 raise e
 
-        # Start transaction management.
-        transaction.commit_unless_managed()
-        transaction.enter_transaction_management()
-        transaction.managed(True)
-
         files_imported = 0
         files_skipped = 0
 
@@ -115,7 +97,6 @@ class Command(BaseCommand):
                 files = sorted(os.listdir(dir_name))
                 postponed = {}
                 while files:
-                    trim_query_log(0)
                     file_name = files.pop(0)
                     file_path = os.path.join(dir_name, file_name)
                     file_base, ext = os.path.splitext(file_path)
@@ -138,10 +119,10 @@ class Command(BaseCommand):
                             self.import_book(file_path, options)
 
                         files_imported += 1
-                        transaction.commit()
 
                     except (Book.AlreadyExists, Picture.AlreadyExists):
-                        print self.style.ERROR('%s: Book or Picture already imported. Skipping. To overwrite use --force.' %
+                        print self.style.ERROR(
+                            '%s: Book or Picture already imported. Skipping. To overwrite use --force.' %
                             file_path)
                         files_skipped += 1
 
@@ -161,13 +142,3 @@ class Command(BaseCommand):
         print "Results: %d files imported, %d skipped, %d total." % (
             files_imported, files_skipped, files_imported + files_skipped)
         print
-
-        if wait_until:
-            print 'Waiting...'
-            try:
-                time.sleep(wait_until - time.time())
-            except IOError:
-                print "it's already too late"
-
-        transaction.commit()
-        transaction.leave_transaction_management()