#
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
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 ...]'
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:
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
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)
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
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()