src/iconolab_episteme/management/commands/importimages.py
changeset 3 16fb4f5efa69
parent 0 df27f9610c82
equal deleted inserted replaced
2:d3fe1866eb5b 3:16fb4f5efa69
    25     help = 'import images from a directory into the media folder and creates item and image objects'
    25     help = 'import images from a directory into the media folder and creates item and image objects'
    26 
    26 
    27     def add_arguments(self, parser):
    27     def add_arguments(self, parser):
    28         parser.add_argument('source_dir')
    28         parser.add_argument('source_dir')
    29         parser.add_argument(
    29         parser.add_argument(
    30             '--encoding',
       
    31             dest='encoding',
       
    32             default='utf-8',
       
    33             help='JSON file encoding'
       
    34 
       
    35         )
       
    36         parser.add_argument(
       
    37             '--collection-id',
    30             '--collection-id',
    38             dest='collection_id',
    31             dest='collection_id',
    39             default=False,
    32             default=False,
    40             help='insert extracted data into the specified collection instead of trying to load a collection fixture',
    33             help='insert extracted data into the specified collection instead of trying to load a collection fixture',
    41         )
    34         )
    43             '--no-jpg-conversion',
    36             '--no-jpg-conversion',
    44             dest='no-jpg-conversion',
    37             dest='no-jpg-conversion',
    45             default=False,
    38             default=False,
    46             help='use this option if you only want the image copied and not converted'
    39             help='use this option if you only want the image copied and not converted'
    47         )
    40         )
    48         parser.add_argument(
       
    49             '--folders',
       
    50             dest='import_folders',
       
    51             default=False,
       
    52             action='store_const',
       
    53             const=True,
       
    54             help='option to create folders'
       
    55         )
       
    56         # parser.add_argument(
       
    57         #     '--folders-regexp',
       
    58         #     dest='folders_regexp',
       
    59         #     default=False,
       
    60         #     help='regexp used to extract the folder name/number'
       
    61         # )
       
    62         # parser.add_argument(
       
    63         #     '--folders-metadata',
       
    64         #     dest='folders_metadata',
       
    65         #     default='REF',
       
    66         #     help='metadata from which to extract the folder name/number'
       
    67         # )
       
    68 
    41 
    69     def handle(self, *args, **options):
    42     def handle(self, *args, **options):
       
    43 
       
    44         #Set no image size limit to PIL to be able to process big images.
       
    45         ImagePIL.MAX_IMAGE_PIXELS = None
    70 
    46 
    71         print('# Logging with logger '+logger.name)
    47         print('# Logging with logger '+logger.name)
    72         logger.debug('# Initializing command with args: %r', options)
    48         logger.debug('# Initializing command with args: %r', options)
    73       
    49       
    74         self.source_dir = options.get('source_dir')
    50         self.source_dir = options.get('source_dir')
    75 
    51 
    76         if options.get('collection_id'):
    52         collection_id = options.get('collection_id')
    77             print('## Finding collection with id ' + 
    53 
    78                     options.get('collection_id'))
    54         if not collection_id:
       
    55             raise CommandError("No collection id, aborting")
       
    56 
       
    57         print('## Finding collection with id %s' % collection_id) 
       
    58 
       
    59         try:
    79             try:
    60             try:
    80                 collection = Collection.objects.get(
    61                 collection = Collection.objects.get(pk=int(collection_id))
    81                     pk=options.get('collection_id'))
    62             except ValueError:
    82             except Collection.DoesNotExist:
    63                 collection = Collection.objects.get(name=collection_id)
    83                 raise ValueError('!!! Collection with primary key ' +
    64         except Collection.DoesNotExist:
    84                                     options.get('collection_id')+' was not found, aborting !!!')
    65             raise CommandError('!!! Collection with id ' + collection_id
    85         else:
    66                                 +' was not found, aborting !!!')
    86             raise ValueError(
       
    87                 '!!! No collection fixture or collection id, aborting because we can\'t properly generate data. !!!')
       
    88 
       
    89 
       
    90         '''Listing image files in target directory'''
       
    91 
    67 
    92         print(
    68         print(
    93             '## Converting image and moving it to static dir, creating Image and Item objects')
    69             '## Converting image and moving it to static dir, creating Image and Item objects')
    94         print('### Images will be stored in ' + os.path.join(settings.MEDIA_ROOT,'uploads'))
    70         print('### Images will be stored in ' + os.path.join(settings.MEDIA_ROOT,'uploads'))
    95 
    71 
    96         for dirname, dirs, files in os.walk(self.source_dir):
    72         for dirname, dirs, files in os.walk(self.source_dir):
    97             for filename in files:
    73             for filename in files:
       
    74                 print("::Examining %s" % filename)
    98                 filename_without_extension, extension = os.path.splitext(filename)
    75                 filename_without_extension, extension = os.path.splitext(filename)
    99                 if imghdr.what(os.path.join(dirname, filename)) is None:
    76                 if imghdr.what(os.path.join(dirname, filename)) is None:
       
    77                     print("-> This is not an image: continue")
   100                     continue
    78                     continue
   101 
    79 
   102                 json_path = os.path.join(dirname, filename_without_extension + ".json")
    80                 json_path = os.path.join(dirname, filename_without_extension + ".json")
   103                 if not os.path.isfile(json_path):
    81                 if not os.path.isfile(json_path):
       
    82                     print("-> has not a matching json: continue")
   104                     continue
    83                     continue
   105 
    84 
       
    85                 print("-> Processing %s" %json_path)
   106                 with open(json_path) as json_data:
    86                 with open(json_path) as json_data:
   107                     eso_data = json.load(json_data)
    87                     eso_data = json.load(json_data)
   108                     eso_object = eso_data['object']
    88                     eso_object = eso_data['object']
   109                     eso_image = eso_data['image']
    89                     eso_image = eso_data['image']
   110                 
    90                 
   122                             os.mkdir(os.path.join(settings.MEDIA_ROOT,'uploads', image_dir))
   102                             os.mkdir(os.path.join(settings.MEDIA_ROOT,'uploads', image_dir))
   123                             print(image_dir, "directory created")
   103                             print(image_dir, "directory created")
   124                         except FileExistsError:
   104                         except FileExistsError:
   125                             print(image_dir, "directory already exists")
   105                             print(image_dir, "directory already exists")
   126 
   106 
   127                         self.create_item_and_metadata(
   107                         try:
   128                             natural_key, collection, eso_data, image_list, options, self.source_dir)
   108                             self.create_item_and_metadata(
       
   109                                 natural_key, collection, eso_data, image_list, options, self.source_dir)
       
   110                         except Exception as e:
       
   111                             print("!!! Exception processing %s : %s" % (json_path, e))
       
   112                             continue
   129 
   113 
   130         print('# All done!')
   114         print('# All done!')