From a93a88f134885d92eceb3a6238b65f9ef7cef086 Mon Sep 17 00:00:00 2001 From: kthoden Date: Thu, 27 Feb 2020 17:04:41 +0100 Subject: [PATCH] Import directory is variable now --- .../management/commands/publicationimport.py | 51 ++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/eoapublications/management/commands/publicationimport.py b/eoapublications/management/commands/publicationimport.py index f33bb93..b173d8c 100644 --- a/eoapublications/management/commands/publicationimport.py +++ b/eoapublications/management/commands/publicationimport.py @@ -16,6 +16,47 @@ class Command(BaseCommand): help = "Import a publication into the Django database." + def add_arguments(self, parser): + parser.add_argument( + 'inputDir', + default='import', + nargs='?', + metavar='INPUT_DIR', + help='import publication from INPUT_DIR. default: %(default)s' + ) + + def input_dir_sanitycheck(self, inputDir): + from os.path import exists, join + for f in \ + [ inputDir + , join( inputDir, "publication.cfg") + , join( inputDir, "Django.xml") + , join( inputDir, "Cover.jpg") + , join( inputDir, "images") + , join( inputDir, "images/embedded") \ + ] \ + : + if not exists( f ): + raise( FileNotFoundError(f) ) + + def dest_dir_sanitycheck(self, destDir): + from os.path import exists, join + if( exists( destDir ) ): + raise( FileExistsError( destDir ) ) + + def dbg_print( self, x, level=1, **options): + verbosity = options['verbosity'] if ('verbosity' in options.keys()) else 0 + if level <= verbosity: + print( x ) + + def dest_media_dir( self, inputDir ): + from os.path import join + config = configparser.ConfigParser(); config.read( join(inputDir, 'publication.cfg') ) + technical = config['Technical'] + mediaDestRel = join( technical['Serie'].lower() , technical['Number'] ) + return join( settings.MEDIA_ROOT, mediaDestRel ) + + def resize_image(self, source_image, dest_image, max_size, dimension): """Resize an image, preserve ratio. @@ -59,6 +100,7 @@ def process_as_string(self,xmlElement): return strResult # def process_as_string ends here + def process_index_entries(self, xmlIndex, xmlElement, xmlChapter, Newelement, Newpublication, index_type): """Construct the popups for index entries next to paragraphs.""" @@ -223,9 +265,16 @@ def progress(self, count, total, status=''): def handle(self, **options): """The main bit.""" + from os import getcwd, mkdir, makedirs + from os.path import exists, join + + inputDir = join( getcwd(), options['inputDir'] ) + self.input_dir_sanitycheck( inputDir ) + + strDir = inputDir # Get current directory, and append /import - strDir = os.getcwd() + '/import' + # strDir = os.getcwd() + '/import' # Read .cfg-File cfgPublication = configparser.ConfigParser() cfgPublication.read(strDir + '/publication.cfg')