1 # -*- coding: UTF-8 -*- |
|
2 import json |
|
3 import logging |
|
4 import os |
|
5 import pprint |
|
6 import re |
|
7 import shutil |
|
8 import imghdr |
|
9 |
|
10 from django.conf import settings |
|
11 from django.core.management.base import BaseCommand, CommandError |
|
12 from PIL import Image as ImagePIL |
|
13 from sorl.thumbnail import get_thumbnail |
|
14 |
|
15 from iconolab.management.commands.importimages import BaseImportImagesCommand |
|
16 from iconolab.models import (Collection, Folder, Image, ImageStats, Item, |
|
17 ItemMetadata, MetaCategory) |
|
18 |
|
19 if settings.IMPORT_LOGGER_NAME and settings.LOGGING['loggers'].get(settings.IMPORT_LOGGER_NAME, ''): |
|
20 logger = logging.getLogger(settings.IMPORT_LOGGER_NAME) |
|
21 else: |
|
22 logger = logging.getLogger(__name__) |
|
23 |
|
24 class Command(BaseImportImagesCommand): |
|
25 help = 'import images from a directory into the media folder and creates item and image objects' |
|
26 |
|
27 def add_arguments(self, parser): |
|
28 parser.add_argument('source_dir') |
|
29 parser.add_argument( |
|
30 '--collection-id', |
|
31 dest='collection_id', |
|
32 default=False, |
|
33 help='insert extracted data into the specified collection instead of trying to load a collection fixture', |
|
34 ) |
|
35 parser.add_argument( |
|
36 '--no-jpg-conversion', |
|
37 dest='no-jpg-conversion', |
|
38 default=False, |
|
39 help='use this option if you only want the image copied and not converted' |
|
40 ) |
|
41 |
|
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 |
|
46 |
|
47 print('# Logging with logger '+logger.name) |
|
48 logger.debug('# Initializing command with args: %r', options) |
|
49 |
|
50 self.source_dir = options.get('source_dir') |
|
51 |
|
52 collection_id = options.get('collection_id') |
|
53 |
|
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: |
|
60 try: |
|
61 collection = Collection.objects.get(pk=int(collection_id)) |
|
62 except ValueError: |
|
63 collection = Collection.objects.get(name=collection_id) |
|
64 except Collection.DoesNotExist: |
|
65 raise CommandError('!!! Collection with id ' + collection_id |
|
66 +' was not found, aborting !!!') |
|
67 |
|
68 print( |
|
69 '## Converting image and moving it to static dir, creating Image and Item objects') |
|
70 print('### Images will be stored in ' + os.path.join(settings.MEDIA_ROOT,'uploads')) |
|
71 |
|
72 for dirname, dirs, files in os.walk(self.source_dir): |
|
73 for filename in files: |
|
74 print("::Examining %s" % filename) |
|
75 filename_without_extension, extension = os.path.splitext(filename) |
|
76 if imghdr.what(os.path.join(dirname, filename)) is None: |
|
77 print("-> This is not an image: continue") |
|
78 continue |
|
79 |
|
80 json_path = os.path.join(dirname, filename_without_extension + ".json") |
|
81 if not os.path.isfile(json_path): |
|
82 print("-> has not a matching json: continue") |
|
83 continue |
|
84 |
|
85 print("-> Processing %s" %json_path) |
|
86 with open(json_path) as json_data: |
|
87 eso_data = json.load(json_data) |
|
88 eso_object = eso_data['object'] |
|
89 eso_image = eso_data['image'] |
|
90 |
|
91 path_images = os.path.join(filename_without_extension, filename) |
|
92 image_list = [path_images] |
|
93 image_dir = filename_without_extension |
|
94 |
|
95 natural_key = ItemMetadata.get_natural_key(collection, eso_image['id']) |
|
96 |
|
97 if ItemMetadata.objects.filter(item__collection=collection, natural_key=natural_key).exists(): |
|
98 print('#### An item with ' + |
|
99 natural_key +' for natural key, already exists in database in the import collection') |
|
100 else: |
|
101 try: |
|
102 os.mkdir(os.path.join(settings.MEDIA_ROOT,'uploads', image_dir)) |
|
103 print(image_dir, "directory created") |
|
104 except FileExistsError: |
|
105 print(image_dir, "directory already exists") |
|
106 |
|
107 try: |
|
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 |
|
113 |
|
114 print('# All done!') |
|