|
1 # -*- coding: UTF-8 -*- |
|
2 import json |
|
3 import logging |
|
4 import os |
|
5 import imghdr |
|
6 |
|
7 from django.conf import settings |
|
8 |
|
9 from iconolab.management.commands.importimages import BaseImportImagesCommand |
|
10 from iconolab.models import (Collection, ItemMetadata) |
|
11 |
|
12 if settings.IMPORT_LOGGER_NAME and settings.LOGGING['loggers'].get(settings.IMPORT_LOGGER_NAME, ''): |
|
13 logger = logging.getLogger(settings.IMPORT_LOGGER_NAME) |
|
14 else: |
|
15 logger = logging.getLogger(__name__) |
|
16 |
|
17 class Command(BaseImportImagesCommand): |
|
18 help = 'import images from a directory into the media folder and creates item and image objects' |
|
19 |
|
20 def add_arguments(self, parser): |
|
21 parser.add_argument('source_dir') |
|
22 parser.add_argument( |
|
23 '--collection-name', |
|
24 dest='collection_name', |
|
25 default=False, |
|
26 help='insert extracted data into the specified collection' |
|
27 |
|
28 ) |
|
29 parser.add_argument( |
|
30 '--no-jpg-conversion', |
|
31 dest='no-jpg-conversion', |
|
32 default=False, |
|
33 help='use this option if you only want the image copied and not converted' |
|
34 ) |
|
35 |
|
36 |
|
37 def handle(self, *args, **options): |
|
38 |
|
39 print('# Logging with logger '+logger.name) |
|
40 logger.debug('# Initializing command with args: %r', options) |
|
41 |
|
42 self.source_dir = options.get('source_dir') |
|
43 |
|
44 collection_name = options.get('collection_name') |
|
45 |
|
46 if collection_name: |
|
47 print('## Finding collection with id ' + |
|
48 collection_name) |
|
49 try: |
|
50 collection = Collection.objects.get( |
|
51 name=collection_name) |
|
52 except Collection.DoesNotExist: |
|
53 raise ValueError('!!! Collection with primary key ' + |
|
54 collection_name +' was not found, aborting !!!') |
|
55 else: |
|
56 raise ValueError( |
|
57 '!!! No collection fixture or collection id, aborting because we can\'t properly generate data. !!!') |
|
58 |
|
59 |
|
60 '''Listing image files in target directory''' |
|
61 |
|
62 print( |
|
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')) |
|
65 |
|
66 for dirname, dirs, files in os.walk(self.source_dir): |
|
67 for filename in files: |
|
68 filename_without_extension, extension = os.path.splitext(filename) |
|
69 if imghdr.what(os.path.join(dirname, filename)) is None: |
|
70 continue |
|
71 |
|
72 json_path = os.path.join(dirname, filename_without_extension + ".json") |
|
73 if not os.path.isfile(json_path): |
|
74 continue |
|
75 |
|
76 with open(json_path) as json_data: |
|
77 eso_data = json.load(json_data) |
|
78 eso_image = eso_data['image'] |
|
79 |
|
80 path_images = os.path.join(filename_without_extension, filename) |
|
81 image_list = [path_images] |
|
82 image_dir = filename_without_extension |
|
83 |
|
84 natural_key = ItemMetadata.get_natural_key(collection, eso_image['id']) |
|
85 |
|
86 if ItemMetadata.objects.filter( |
|
87 item__collection=collection, natural_key=natural_key).exists(): |
|
88 print('#### An item with ' + |
|
89 natural_key +' for natural key, already exists in database in the import collection') |
|
90 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( |
|
98 natural_key, collection, eso_data, image_list, options, self.source_dir) |
|
99 |
|
100 print('# All done!') |