author | ymh <ymh.work@gmail.com> |
Thu, 02 Aug 2018 16:24:30 +0200 | |
changeset 35 | 7fa549d6fc45 |
parent 30 | 7b479c7b6861 |
permissions | -rw-r--r-- |
19 | 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 |
||
30
7b479c7b6861
change the way images collection files are imported and named. upgrade iconolab dependency and increment version nb
ymh <ymh.work@gmail.com>
parents:
28
diff
changeset
|
66 |
for dirname, _, files in os.walk(self.source_dir): |
19 | 67 |
for filename in files: |
30
7b479c7b6861
change the way images collection files are imported and named. upgrade iconolab dependency and increment version nb
ymh <ymh.work@gmail.com>
parents:
28
diff
changeset
|
68 |
filename_without_extension, _ = os.path.splitext(filename) |
19 | 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: |
|
30
7b479c7b6861
change the way images collection files are imported and named. upgrade iconolab dependency and increment version nb
ymh <ymh.work@gmail.com>
parents:
28
diff
changeset
|
77 |
item_data = json.load(json_data) |
7b479c7b6861
change the way images collection files are imported and named. upgrade iconolab dependency and increment version nb
ymh <ymh.work@gmail.com>
parents:
28
diff
changeset
|
78 |
item_image = item_data['image'] |
19 | 79 |
|
28
15f63c5dfe3f
Correct path for source image in path, take the real one, not the expected one. increase version nb
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
80 |
path_images = os.path.join(dirname, filename) |
19 | 81 |
image_list = [path_images] |
82 |
||
30
7b479c7b6861
change the way images collection files are imported and named. upgrade iconolab dependency and increment version nb
ymh <ymh.work@gmail.com>
parents:
28
diff
changeset
|
83 |
natural_key = ItemMetadata.get_natural_key(collection, item_image['id']) |
19 | 84 |
|
85 |
if ItemMetadata.objects.filter( |
|
86 |
item__collection=collection, natural_key=natural_key).exists(): |
|
87 |
print('#### An item with ' + |
|
88 |
natural_key +' for natural key, already exists in database in the import collection') |
|
89 |
else: |
|
90 |
self.create_item_and_metadata( |
|
30
7b479c7b6861
change the way images collection files are imported and named. upgrade iconolab dependency and increment version nb
ymh <ymh.work@gmail.com>
parents:
28
diff
changeset
|
91 |
natural_key, collection, item_data, image_list, options) |
19 | 92 |
|
93 |
print('# All done!') |