src/iconolab_episteme/management/commands/importimages.py
changeset 30 7b479c7b6861
parent 28 15f63c5dfe3f
equal deleted inserted replaced
29:9de311703ab9 30:7b479c7b6861
    61 
    61 
    62         print(
    62         print(
    63             '## Converting image and moving it to static dir, creating Image and Item objects')
    63             '## Converting image and moving it to static dir, creating Image and Item objects')
    64         print('### Images will be stored in ' + os.path.join(settings.MEDIA_ROOT, 'uploads'))
    64         print('### Images will be stored in ' + os.path.join(settings.MEDIA_ROOT, 'uploads'))
    65 
    65 
    66         for dirname, dirs, files in os.walk(self.source_dir):
    66         for dirname, _, files in os.walk(self.source_dir):
    67             for filename in files:
    67             for filename in files:
    68                 filename_without_extension, extension = os.path.splitext(filename)
    68                 filename_without_extension, _ = os.path.splitext(filename)
    69                 if imghdr.what(os.path.join(dirname, filename)) is None:
    69                 if imghdr.what(os.path.join(dirname, filename)) is None:
    70                     continue
    70                     continue
    71 
    71 
    72                 json_path = os.path.join(dirname, filename_without_extension + ".json")
    72                 json_path = os.path.join(dirname, filename_without_extension + ".json")
    73                 if not os.path.isfile(json_path):
    73                 if not os.path.isfile(json_path):
    74                     continue
    74                     continue
    75 
    75 
    76                 with open(json_path) as json_data:
    76                 with open(json_path) as json_data:
    77                     eso_data = json.load(json_data)
    77                     item_data = json.load(json_data)
    78                     eso_image = eso_data['image']
    78                     item_image = item_data['image']
    79                 
    79                 
    80                     path_images = os.path.join(dirname, filename)
    80                     path_images = os.path.join(dirname, filename)
    81                     image_list = [path_images]
    81                     image_list = [path_images]
    82                     image_dir = filename_without_extension
       
    83 
    82 
    84                     natural_key = ItemMetadata.get_natural_key(collection, eso_image['id'])
    83                     natural_key = ItemMetadata.get_natural_key(collection, item_image['id'])
    85 
    84 
    86                     if ItemMetadata.objects.filter(
    85                     if ItemMetadata.objects.filter(
    87                             item__collection=collection, natural_key=natural_key).exists():
    86                             item__collection=collection, natural_key=natural_key).exists():
    88                         print('#### An item with ' +
    87                         print('#### An item with ' +
    89                               natural_key +' for natural key, already exists in database in the import collection')
    88                               natural_key +' for natural key, already exists in database in the import collection')
    90                     else:
    89                     else:
    91                         try:
       
    92                             os.mkdir(os.path.join(settings.MEDIA_ROOT, 'uploads', image_dir))
       
    93                             print(image_dir, "directory created")
       
    94                         except FileExistsError:
       
    95                             print(image_dir, "directory already exists")
       
    96 
       
    97                         self.create_item_and_metadata(
    90                         self.create_item_and_metadata(
    98                             natural_key, collection, eso_data, image_list, options, self.source_dir)
    91                             natural_key, collection, item_data, image_list, options)
    99 
    92 
   100         print('# All done!')
    93         print('# All done!')