Add out of process bulk document upload
This commit is contained in:
0
apps/sources/management/__init__.py
Normal file
0
apps/sources/management/__init__.py
Normal file
0
apps/sources/management/commands/__init__.py
Normal file
0
apps/sources/management/commands/__init__.py
Normal file
48
apps/sources/management/commands/bulk_upload.py
Normal file
48
apps/sources/management/commands/bulk_upload.py
Normal file
@@ -0,0 +1,48 @@
|
||||
from __future__ import absolute_import
|
||||
|
||||
import os
|
||||
from optparse import make_option
|
||||
|
||||
from django.core.management.base import BaseCommand, CommandError, LabelCommand
|
||||
|
||||
from ...models import OutOfProcess
|
||||
from ...compressed_file import CompressedFile, NotACompressedFile
|
||||
|
||||
|
||||
class Command(LabelCommand):
|
||||
args = '<filename>'
|
||||
help = 'Upload documents from a compressed file in to the database.'
|
||||
option_list = LabelCommand.option_list + (
|
||||
make_option('--noinput', action='store_false', dest='interactive',
|
||||
default=True, help='Do not ask the user for confirmation before '
|
||||
'starting.'),
|
||||
#make_option('--metadata', action='store', dest='metadata',
|
||||
# help='A metadata dictionary to apply to the documents.'),
|
||||
)
|
||||
|
||||
def handle_label(self, label, **options):
|
||||
if not os.access(label, os.R_OK):
|
||||
raise CommandError("File '%s' is not readable." % label)
|
||||
|
||||
if _confirm(options['interactive']) == 'yes':
|
||||
print 'Beginning upload...'
|
||||
fd = open(label)
|
||||
source = OutOfProcess()
|
||||
try:
|
||||
result = source.upload_file(fd, filename=None, use_file_name=False, document_type=None, expand=True, metadata_dict_list=None, user=None, document=None, new_version_data=None, verbose=True)
|
||||
except NotACompressedFile:
|
||||
print '%s is not a compressed file.'
|
||||
else:
|
||||
print 'Finished.'
|
||||
|
||||
fd.close()
|
||||
else:
|
||||
print 'Cancelled.'
|
||||
|
||||
|
||||
def _confirm(interactive):
|
||||
if not interactive:
|
||||
return 'yes'
|
||||
return raw_input('You have requested to bulk upload a number of documents from a compressed file.\n'
|
||||
'Are you sure you want to do this?\n'
|
||||
'Type \'yes\' to continue, or any other value to cancel: ')
|
||||
@@ -55,13 +55,17 @@ class BaseModel(models.Model):
|
||||
def get_transformation_list(self):
|
||||
return SourceTransformation.transformations.get_for_object_as_list(self)
|
||||
|
||||
def upload_file(self, file_object, filename=None, use_file_name=False, document_type=None, expand=False, metadata_dict_list=None, user=None, document=None, new_version_data=None):
|
||||
def upload_file(self, file_object, filename=None, use_file_name=False, document_type=None, expand=False, metadata_dict_list=None, user=None, document=None, new_version_data=None, verbose=False):
|
||||
if expand:
|
||||
try:
|
||||
cf = CompressedFile(file_object)
|
||||
count = 1
|
||||
for fp in cf.children():
|
||||
if verbose:
|
||||
print 'Uploading file #%d: %s' % (count, fp)
|
||||
self.upload_single_file(fp, None, document_type, metadata_dict_list, user)
|
||||
fp.close()
|
||||
count += 1
|
||||
|
||||
except NotACompressedFile:
|
||||
self.upload_single_file(file_object, filename, document_type, metadata_dict_list, user)
|
||||
@@ -256,3 +260,23 @@ class SourceTransformation(models.Model):
|
||||
ordering = ('order',)
|
||||
verbose_name = _(u'document source transformation')
|
||||
verbose_name_plural = _(u'document source transformations')
|
||||
|
||||
|
||||
class OutOfProcess(BaseModel):
|
||||
#icon = models.CharField(blank=True, null=True, max_length=24, choices=SOURCE_ICON_CHOICES, verbose_name=_(u'icon'), help_text=_(u'An icon to visually distinguish this source.'))
|
||||
|
||||
#def save(self, *args, **kwargs):
|
||||
# if not self.icon:
|
||||
# self.icon = self.default_icon
|
||||
# super(BaseModel, self).save(*args, **kwargs)
|
||||
|
||||
is_interactive = False
|
||||
#source_type = SOURCE_CHOICE_WEB_FORM
|
||||
#default_icon = SOURCE_ICON_DISK
|
||||
|
||||
#uncompress = models.CharField(max_length=1, choices=SOURCE_INTERACTIVE_UNCOMPRESS_CHOICES, verbose_name=_(u'uncompress'), help_text=_(u'Whether to expand or not compressed archives.'))
|
||||
#Default path
|
||||
|
||||
class Meta(BaseModel.Meta):
|
||||
verbose_name = _(u'out of process')
|
||||
verbose_name_plural = _(u'out of process')
|
||||
|
||||
Reference in New Issue
Block a user