0
|
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 |
'--encoding', |
|
31 |
dest='encoding', |
|
32 |
default='utf-8', |
|
33 |
help='JSON file encoding' |
|
34 |
|
|
35 |
) |
|
36 |
parser.add_argument( |
|
37 |
'--collection-id', |
|
38 |
dest='collection_id', |
|
39 |
default=False, |
|
40 |
help='insert extracted data into the specified collection instead of trying to load a collection fixture', |
|
41 |
) |
|
42 |
parser.add_argument( |
|
43 |
'--no-jpg-conversion', |
|
44 |
dest='no-jpg-conversion', |
|
45 |
default=False, |
|
46 |
help='use this option if you only want the image copied and not converted' |
|
47 |
) |
|
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 |
|
|
69 |
def handle(self, *args, **options): |
|
70 |
|
|
71 |
print('# Logging with logger '+logger.name) |
|
72 |
logger.debug('# Initializing command with args: %r', options) |
|
73 |
|
|
74 |
self.source_dir = options.get('source_dir') |
|
75 |
|
|
76 |
if options.get('collection_id'): |
|
77 |
print('## Finding collection with id ' + |
|
78 |
options.get('collection_id')) |
|
79 |
try: |
|
80 |
collection = Collection.objects.get( |
|
81 |
pk=options.get('collection_id')) |
|
82 |
except Collection.DoesNotExist: |
|
83 |
raise ValueError('!!! Collection with primary key ' + |
|
84 |
options.get('collection_id')+' was not found, aborting !!!') |
|
85 |
else: |
|
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 |
|
|
92 |
print( |
|
93 |
'## 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')) |
|
95 |
|
|
96 |
for dirname, dirs, files in os.walk(self.source_dir): |
|
97 |
for filename in files: |
|
98 |
filename_without_extension, extension = os.path.splitext(filename) |
|
99 |
if imghdr.what(os.path.join(dirname, filename)) is None: |
|
100 |
continue |
|
101 |
|
|
102 |
json_path = os.path.join(dirname, filename_without_extension + ".json") |
|
103 |
if not os.path.isfile(json_path): |
|
104 |
continue |
|
105 |
|
|
106 |
with open(json_path) as json_data: |
|
107 |
eso_data = json.load(json_data) |
|
108 |
eso_object = eso_data['object'] |
|
109 |
eso_image = eso_data['image'] |
|
110 |
|
|
111 |
path_images = os.path.join(filename_without_extension, filename) |
|
112 |
image_list = [path_images] |
|
113 |
image_dir = filename_without_extension |
|
114 |
|
|
115 |
natural_key = ItemMetadata.get_natural_key(collection, eso_image['id']) |
|
116 |
|
|
117 |
if ItemMetadata.objects.filter(item__collection=collection, natural_key=natural_key).exists(): |
|
118 |
print('#### An item with ' + |
|
119 |
natural_key +' for natural key, already exists in database in the import collection') |
|
120 |
else: |
|
121 |
try: |
|
122 |
os.mkdir(os.path.join(settings.MEDIA_ROOT,'uploads', image_dir)) |
|
123 |
print(image_dir, "directory created") |
|
124 |
except FileExistsError: |
|
125 |
print(image_dir, "directory already exists") |
|
126 |
|
|
127 |
self.create_item_and_metadata( |
|
128 |
natural_key, collection, eso_data, image_list, options, self.source_dir) |
|
129 |
|
|
130 |
print('# All done!') |