src/iconolab_episteme/management/commands/importimages.py
changeset 3 16fb4f5efa69
parent 0 df27f9610c82
--- a/src/iconolab_episteme/management/commands/importimages.py	Thu Jun 28 15:15:39 2018 +0200
+++ b/src/iconolab_episteme/management/commands/importimages.py	Thu Jun 28 15:19:43 2018 +0200
@@ -27,13 +27,6 @@
     def add_arguments(self, parser):
         parser.add_argument('source_dir')
         parser.add_argument(
-            '--encoding',
-            dest='encoding',
-            default='utf-8',
-            help='JSON file encoding'
-
-        )
-        parser.add_argument(
             '--collection-id',
             dest='collection_id',
             default=False,
@@ -45,49 +38,32 @@
             default=False,
             help='use this option if you only want the image copied and not converted'
         )
-        parser.add_argument(
-            '--folders',
-            dest='import_folders',
-            default=False,
-            action='store_const',
-            const=True,
-            help='option to create folders'
-        )
-        # parser.add_argument(
-        #     '--folders-regexp',
-        #     dest='folders_regexp',
-        #     default=False,
-        #     help='regexp used to extract the folder name/number'
-        # )
-        # parser.add_argument(
-        #     '--folders-metadata',
-        #     dest='folders_metadata',
-        #     default='REF',
-        #     help='metadata from which to extract the folder name/number'
-        # )
 
     def handle(self, *args, **options):
 
+        #Set no image size limit to PIL to be able to process big images.
+        ImagePIL.MAX_IMAGE_PIXELS = None
+
         print('# Logging with logger '+logger.name)
         logger.debug('# Initializing command with args: %r', options)
       
         self.source_dir = options.get('source_dir')
 
-        if options.get('collection_id'):
-            print('## Finding collection with id ' + 
-                    options.get('collection_id'))
+        collection_id = options.get('collection_id')
+
+        if not collection_id:
+            raise CommandError("No collection id, aborting")
+
+        print('## Finding collection with id %s' % collection_id) 
+
+        try:
             try:
-                collection = Collection.objects.get(
-                    pk=options.get('collection_id'))
-            except Collection.DoesNotExist:
-                raise ValueError('!!! Collection with primary key ' +
-                                    options.get('collection_id')+' was not found, aborting !!!')
-        else:
-            raise ValueError(
-                '!!! No collection fixture or collection id, aborting because we can\'t properly generate data. !!!')
-
-
-        '''Listing image files in target directory'''
+                collection = Collection.objects.get(pk=int(collection_id))
+            except ValueError:
+                collection = Collection.objects.get(name=collection_id)
+        except Collection.DoesNotExist:
+            raise CommandError('!!! Collection with id ' + collection_id
+                                +' was not found, aborting !!!')
 
         print(
             '## Converting image and moving it to static dir, creating Image and Item objects')
@@ -95,14 +71,18 @@
 
         for dirname, dirs, files in os.walk(self.source_dir):
             for filename in files:
+                print("::Examining %s" % filename)
                 filename_without_extension, extension = os.path.splitext(filename)
                 if imghdr.what(os.path.join(dirname, filename)) is None:
+                    print("-> This is not an image: continue")
                     continue
 
                 json_path = os.path.join(dirname, filename_without_extension + ".json")
                 if not os.path.isfile(json_path):
+                    print("-> has not a matching json: continue")
                     continue
 
+                print("-> Processing %s" %json_path)
                 with open(json_path) as json_data:
                     eso_data = json.load(json_data)
                     eso_object = eso_data['object']
@@ -124,7 +104,11 @@
                         except FileExistsError:
                             print(image_dir, "directory already exists")
 
-                        self.create_item_and_metadata(
-                            natural_key, collection, eso_data, image_list, options, self.source_dir)
+                        try:
+                            self.create_item_and_metadata(
+                                natural_key, collection, eso_data, image_list, options, self.source_dir)
+                        except Exception as e:
+                            print("!!! Exception processing %s : %s" % (json_path, e))
+                            continue
 
         print('# All done!')
\ No newline at end of file