# HG changeset patch # User ymh # Date 1533139763 -7200 # Node ID 68ea0b0633088e5d013a049ac060742362be78af # Parent 03b9debd09c50b7764d6615af913c25464ffac64 Change the way images are handled and stored in importimages. Images are now renamed. The expected list of image must be a list of readable paths. Increment the version nb diff -r 03b9debd09c5 -r 68ea0b063308 src/iconolab/__init__.py --- a/src/iconolab/__init__.py Wed Aug 01 13:45:33 2018 +0200 +++ b/src/iconolab/__init__.py Wed Aug 01 18:09:23 2018 +0200 @@ -1,4 +1,4 @@ -VERSION = (0, 1, 7, "final", 0) +VERSION = (0, 1, 8, "final", 0) VERSION_STR = ".".join(map(lambda i: "%02d" % (i,), VERSION[:2])) diff -r 03b9debd09c5 -r 68ea0b063308 src/iconolab/management/commands/importimages.py --- a/src/iconolab/management/commands/importimages.py Wed Aug 01 13:45:33 2018 +0200 +++ b/src/iconolab/management/commands/importimages.py Wed Aug 01 18:09:23 2018 +0200 @@ -1,4 +1,5 @@ import json +import math import os import shutil @@ -16,21 +17,25 @@ def handle(self, *args, **options): raise NotImplementedError + def get_image_dir(self, natural_key, collection): + return os.path.join(collection.name, natural_key) + + ## def create_item_and_metadata( self, natural_key, collection, metadata_dict, image_files, - options, - source_dir, + options ): target_dir = os.path.join(settings.MEDIA_ROOT, Image.media.field.upload_to) if not os.path.isdir(target_dir): raise CommandError("Image target dir does not exists : %s" % target_dir) - # TODO: Make it fully transactional. if there is a problem on the image creation, some data files can parsists. + # TODO: Make it fully transactional. if there is a problem on the image creation, some data + # files can persists. with transaction.atomic(): print('#### Creating item '+natural_key+' (natural key) in database') item_object = Item.objects.create( @@ -43,31 +48,35 @@ natural_key=natural_key ) + print('#### preparing image folder for item ' + natural_key) + item_image_dir = self.get_image_dir(natural_key, collection) + target_item_image_dir = os.path.join(target_dir, item_image_dir) + os.mkdirs(target_item_image_dir, exist_ok=True) + print('-->' + target_item_image_dir, "directory created") + print('#### Computing item image(s)') - for image in image_files: - (image_name, ext) = os.path.splitext(image) + image_file_name_fill = math.log10(len(image_files))+1 + for i, image in enumerate(image_files): + (_, ext) = os.path.splitext(image) + dest_image_name = "{0}_{1:0{2}d}.jpg".format(natural_key, i+1, image_file_name_fill) + image_path = os.path.join(target_item_image_dir, dest_image_name) if options.get('no-jpg-conversion') or ext in settings.NO_IMG_CONVERSION_EXTS: - print('##### Copying file '+str(image)+' without converting') - image_path = os.path.join(target_dir, image) - new_image_name = image - shutil.copy(os.path.join(source_dir, image), image_path) + print('##### Copying file %s to %s without converting'%(str(image), image_path)) + shutil.copy(image, image_path) else: - image_path = os.path.join(target_dir, image_name) + '.jpg' - new_image_name = image_name+'.jpg' if os.path.isfile(image_path): - print('##### A jpeg file already exists in target dir for ' + image) + print('##### A jpeg file already exists in target dir for %s' % image) else: - jpeg_img_path = image_path - im = ImagePIL.open(os.path.join(source_dir, image)) - print('##### Generating or copying jpeg for '+image) + im = ImagePIL.open(image) + print('##### Generating or copying jpeg for %s to %s' % (image, image_path)) im.thumbnail(im.size) - im.save(jpeg_img_path, 'JPEG', quality=options.get( + im.save(image_path, 'JPEG', quality=options.get( 'jpeg_quality', settings.IMG_JPG_DEFAULT_QUALITY)) new_image = Image.objects.create( item=item_object, - media='uploads/'+new_image_name, - name=new_image_name + media=os.path.join(Image.media.field.upload_to, item_image_dir, dest_image_name), + name=os.path.join(item_image_dir, dest_image_name) ) ImageStats.objects.create( image=new_image