author | ymh <ymh.work@gmail.com> |
Fri, 15 Jun 2018 16:45:22 +0200 | |
changeset 7 | 023dbfdc9f19 |
parent 5 | cfd40849d24c |
child 8 | 4b9587be651f |
permissions | -rw-r--r-- |
5
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
1 |
# -*- coding: UTF-8 -*- |
7 | 2 |
import csv |
3 |
import json |
|
4 |
import logging |
|
5 |
import os |
|
6 |
import pprint |
|
7 |
import re |
|
8 |
import shutil |
|
9 |
||
10 |
from django.conf import settings |
|
5
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
11 |
from django.core.management.base import BaseCommand, CommandError |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
12 |
from PIL import Image as ImagePIL |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
13 |
from sorl.thumbnail import get_thumbnail |
7 | 14 |
|
15 |
from iconolab.management.commands.importimages import BaseImportImagesCommand |
|
16 |
from iconolab.models import (Collection, Folder, Image, ImageStats, Item, |
|
17 |
ItemMetadata, MetaCategory) |
|
5
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
18 |
|
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
19 |
if settings.IMPORT_LOGGER_NAME and settings.LOGGING['loggers'].get(settings.IMPORT_LOGGER_NAME, ''): |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
20 |
logger = logging.getLogger(settings.IMPORT_LOGGER_NAME) |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
21 |
else: |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
22 |
logger = logging.getLogger(__name__) |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
23 |
|
7 | 24 |
|
25 |
class Command(BaseImportImagesCommand): |
|
5
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
26 |
help = 'import images from a directory into the media folder and creates item and image objects' |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
27 |
|
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
28 |
def add_arguments(self, parser): |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
29 |
parser.add_argument('csv_path') |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
30 |
parser.add_argument( |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
31 |
'--jpeg-quality', |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
32 |
dest='jpeg_quality', |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
33 |
default=settings.IMG_JPG_DEFAULT_QUALITY, |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
34 |
help='Jpeg default quality' |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
35 |
|
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
36 |
) |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
37 |
parser.add_argument( |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
38 |
'--encoding', |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
39 |
dest='encoding', |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
40 |
default='utf-8', |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
41 |
help='CSV file encoding' |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
42 |
|
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
43 |
) |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
44 |
parser.add_argument( |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
45 |
'--collection-json', |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
46 |
dest='collection_json', |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
47 |
default=False, |
7 | 48 |
help='creates a new collection from a json file, must be an object with fields : ' + |
49 |
'"name" (identifier), ' + |
|
50 |
'"verbose_name" (proper title name), ' + |
|
51 |
'"description" (description on homepage, html is supported), ' + |
|
52 |
'"image" (image on homepages, must be "uploads/<imgname>"), ' + |
|
5
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
53 |
'"height" and "width" (height and width of the image)', |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
54 |
) |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
55 |
parser.add_argument( |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
56 |
'--collection-id', |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
57 |
dest='collection_id', |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
58 |
default=False, |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
59 |
help='insert extracted data into the specified collection instead of trying to load a collection fixture', |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
60 |
) |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
61 |
parser.add_argument( |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
62 |
'--metacategories-json', |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
63 |
dest='metacategories_json', |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
64 |
default=False, |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
65 |
help='add metacategories to the collection from a json file (json must be a list of object with "label" and "triggers_notifications" fields)', |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
66 |
) |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
67 |
parser.add_argument( |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
68 |
'--delimiter', |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
69 |
dest='csv_delimiter', |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
70 |
default=';', |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
71 |
help='csv file delimiter' |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
72 |
) |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
73 |
parser.add_argument( |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
74 |
'--no-jpg-conversion', |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
75 |
dest='no-jpg-conversion', |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
76 |
default=False, |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
77 |
help='use this option if you only want the image copied and not converted' |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
78 |
) |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
79 |
parser.add_argument( |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
80 |
'--img-filename-identifier', |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
81 |
dest='img_filename_identifier', |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
82 |
default=settings.IMPORT_DEFAULT_FIELD_TO_FILENAME_IDENTIFIER, |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
83 |
help='codename of the csv field we\'ll try to match to find the related image to a given object' |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
84 |
) |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
85 |
parser.add_argument( |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
86 |
'--filename-regexp-prefix', |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
87 |
dest='filename_regexp_prefix', |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
88 |
default=r'.*', |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
89 |
help='regexp prefix to properly parse image names with info from csv. The pattern should describe the part before the filename identifier string, default is .*' |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
90 |
) |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
91 |
parser.add_argument( |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
92 |
'--filename-regexp-suffix', |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
93 |
dest='filename_regexp_suffix', |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
94 |
default=r'[\.\-_].*', |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
95 |
help='regexp suffix to properly parse image names with info from csv. The pattern should describe the part after the filename identifier string, default is [\.\-_].*' |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
96 |
) |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
97 |
parser.add_argument( |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
98 |
'--folders', |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
99 |
dest='import_folders', |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
100 |
default=False, |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
101 |
action='store_const', |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
102 |
const=True, |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
103 |
help='option to create folders' |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
104 |
) |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
105 |
parser.add_argument( |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
106 |
'--folders-regexp', |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
107 |
dest='folders_regexp', |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
108 |
default=False, |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
109 |
help='regexp used to extract the folder name/number' |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
110 |
) |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
111 |
parser.add_argument( |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
112 |
'--folders-metadata', |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
113 |
dest='folders_metadata', |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
114 |
default='REF', |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
115 |
help='metadata from which to extract the folder name/number' |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
116 |
) |
7 | 117 |
|
5
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
118 |
def handle(self, *args, **options): |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
119 |
""" |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
120 |
Step-by-step for import: |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
121 |
|
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
122 |
1) Argument checks for file existence and database state to check that everything can proceed without issue before reading the files |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
123 |
1) We import data from csv in a 'pivot' list of dicts 'cleaned_row_data' with the following logic: |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
124 |
* in the settings, there is value "IMPORT_FIELDS_DICT" that is a dict where each key is an identifier for the metadatas |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
125 |
to which we associate a list of column header that will identified as that metadata |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
126 |
* The cleaned_row_data list will associate the identifier with the actual value for its related column |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
127 |
2) Once we have cleaned_row_data, we filter out rows that don't have any associated image into a 'filtered_row_data' list, and add a key "SRC_IMG_FILES" that contains the list of images associated |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
128 |
to each row for the filtered data. |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
129 |
3) At this point we have a list of all the items that will be created into the database and the related images to import, so we create the collection object if necessary |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
130 |
4) For each item: |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
131 |
We create the object in the database |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
132 |
* Metadatas are extracted from the filtered_csv_data using the pivot identifiers from settings.IMPORT_FIELD_DICT |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
133 |
We copy/convert the image into the MEDIA_ROOT/uploads/ dir: thumbnails size listed in settings.PREGENERATE_THUMBNAIL_SIZES are pre-generated for each image |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
134 |
|
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
135 |
Note: each unused row and each unused image in the import folder is kept track of in no_data_images, no_image_rows and duplicate_rows lists and logged at the end of the command. |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
136 |
""" |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
137 |
try: |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
138 |
print('# Logging with logger '+logger.name) |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
139 |
logger.debug('# Initializing command with args: %r', options) |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
140 |
# Check we have a collection to store data into: |
7 | 141 |
self.source_dir = os.path.dirname( |
142 |
os.path.realpath(options.get('csv_path'))) |
|
5
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
143 |
print('# Checking collection args') |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
144 |
if options.get('collection_json'): |
7 | 145 |
print('## Finding collection json data in '+self.source_dir) |
146 |
collection_json_path = os.path.join( |
|
147 |
self.source_dir, options.get('collection_json')) |
|
5
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
148 |
if not os.path.isfile(collection_json_path): |
7 | 149 |
print('### No '+options.get('collection_json') + |
150 |
'.json file was found in the source directory') |
|
151 |
raise ValueError('!!! Json file ' + |
|
152 |
collection_json_path+' was not found !!!') |
|
5
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
153 |
try: |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
154 |
with open(collection_json_path) as json_fixture_file: |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
155 |
collection_data = json.loads(json_fixture_file.read()) |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
156 |
for key in ['name', 'verbose_name', 'description', 'image', 'height', 'width']: |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
157 |
if not key in collection_data.keys(): |
7 | 158 |
print('!!! Json file '+collection_json_path + |
159 |
' has no '+key+' field !!!') |
|
5
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
160 |
raise ValueError() |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
161 |
if not collection_data.get('name', ''): |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
162 |
print('!!! Collection data key "name" is empty') |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
163 |
raise ValueError() |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
164 |
if Collection.objects.filter(name=collection_data.get('name')).exists(): |
7 | 165 |
print( |
166 |
'!!! A Collection with the provided name already exists!') |
|
5
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
167 |
raise ValueError() |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
168 |
if collection_data.get('image', '') and not (collection_data.get('width', 0) and collection_data.get('height', 0)): |
7 | 169 |
print( |
170 |
'!!! Collection data has an image but no height and width') |
|
5
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
171 |
raise ValueError() |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
172 |
except ValueError as e: |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
173 |
raise ValueError('!!! JSON Data is invalid. !!!') |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
174 |
elif options.get('collection_id'): |
7 | 175 |
print('## Finding collection with id ' + |
176 |
options.get('collection_id')) |
|
5
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
177 |
try: |
7 | 178 |
collection = Collection.objects.get( |
179 |
pk=options.get('collection_id')) |
|
5
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
180 |
except Collection.DoesNotExist: |
7 | 181 |
raise ValueError('!!! Collection with primary key ' + |
182 |
options.get('collection_id')+' was not found, aborting !!!') |
|
5
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
183 |
else: |
7 | 184 |
raise ValueError( |
185 |
'!!! No collection fixture or collection id, aborting because we can\'t properly generate data. !!!') |
|
5
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
186 |
|
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
187 |
if options.get('metacategories_json'): |
7 | 188 |
print('## Finding metacategories fixture json data in '+self.source_dir) |
189 |
metacategories_json_path = os.path.join( |
|
190 |
self.source_dir, options.get('metacategories_json')) |
|
5
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
191 |
if not os.path.isfile(metacategories_json_path): |
7 | 192 |
print('### No '+options.get('metacategories_json') + |
193 |
'.json file was found in the source directory') |
|
194 |
raise ValueError( |
|
195 |
'!!! Fixture file '+metacategories_json_path+' was not found !!!') |
|
5
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
196 |
with open(metacategories_json_path) as metacategories_json_file: |
7 | 197 |
metacategories_data = json.loads( |
198 |
metacategories_json_file.read()) |
|
5
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
199 |
for metacategory in metacategories_data: |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
200 |
if metacategory.get('label', None) is None: |
7 | 201 |
raise ValueError( |
202 |
'!!! Metacategory without label !!!') |
|
5
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
203 |
|
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
204 |
if options['import_folders'] and not options['folders_regexp']: |
7 | 205 |
raise ValueError( |
206 |
'!!! No regexp specified to extract folder name !!!') |
|
5
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
207 |
|
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
208 |
# We read the csv |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
209 |
delimiter = options.get('csv_delimiter') |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
210 |
if delimiter == '#9': |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
211 |
delimiter = chr(9) |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
212 |
if delimiter == '#29': |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
213 |
delimiter = chr(29) |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
214 |
if delimiter == '#30': |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
215 |
delimiter = chr(30) |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
216 |
if delimiter == '#31': |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
217 |
delimiter = chr(31) |
7 | 218 |
csvreader = csv.DictReader(open(options.get( |
219 |
'csv_path'), encoding=options.get('encoding')), delimiter=delimiter) |
|
5
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
220 |
print('# Extracting data from csv file and storing it in standardized format') |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
221 |
# We store data using the Jocondelab keys, as defined in settings.IMPORT_FIELDS_DICT |
7 | 222 |
cleaned_csv_data = [] |
223 |
duplicate_rows = [] |
|
5
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
224 |
for row in csvreader: |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
225 |
cleaned_row_data = {} |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
226 |
for key in settings.IMPORT_FIELDS_DICT.keys(): |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
227 |
cleaned_row_data[key] = '' |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
228 |
for row_key in row.keys(): |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
229 |
if row_key in settings.IMPORT_FIELDS_DICT[key]: |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
230 |
if key == 'REF': |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
231 |
ref_number, _, _ = row[row_key].partition(';') |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
232 |
cleaned_row_data[key] = ref_number.rstrip() |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
233 |
else: |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
234 |
cleaned_row_data[key] = row[row_key] |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
235 |
break |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
236 |
if cleaned_row_data[options.get('img_filename_identifier')] in [row[options.get('img_filename_identifier')] for row in cleaned_csv_data]: |
7 | 237 |
print("## We already have "+options.get('img_filename_identifier')+" value " + |
238 |
cleaned_row_data[options.get('img_filename_identifier')]+" in the data to import, ignoring duplicate line") |
|
5
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
239 |
duplicate_rows.append(cleaned_row_data) |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
240 |
else: |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
241 |
cleaned_csv_data.append(cleaned_row_data) |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
242 |
# Listing image files in csv directory |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
243 |
image_list = [ |
7 | 244 |
f for f in os.listdir(self.source_dir) |
245 |
if os.path.isfile(os.path.join(self.source_dir, f)) |
|
5
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
246 |
and (f.endswith('.jpg') or f.endswith('.tif') or f.endswith('.bmp') or f.endswith('.png')) |
7 | 247 |
] # Maybe check if image another way |
5
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
248 |
filtered_csv_data = [] |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
249 |
no_image_rows = [] |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
250 |
no_data_images = [] |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
251 |
assigned_images = [] |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
252 |
# Now we trim the cleaned_csv_data dict to keep only entries that have at least one image |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
253 |
for item in cleaned_csv_data: |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
254 |
item['SRC_IMG_FILES'] = [] |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
255 |
has_image = False |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
256 |
for image in image_list: |
7 | 257 |
img_name_pattern = options.get('filename_regexp_prefix')+re.escape( |
258 |
item[options.get('img_filename_identifier')])+options.get('filename_regexp_suffix') |
|
5
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
259 |
if re.match(img_name_pattern, image): |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
260 |
item['SRC_IMG_FILES'].append(image) |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
261 |
assigned_images.append(image) |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
262 |
has_image = True |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
263 |
if has_image: |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
264 |
filtered_csv_data.append(item) |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
265 |
else: |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
266 |
# We keep track of the entries that don't have any corresponding image |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
267 |
no_image_rows.append(item) |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
268 |
# We keep track of the images that don't have any corresponding entry |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
269 |
for image in image_list: |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
270 |
if image not in assigned_images: |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
271 |
no_data_images.append(image) |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
272 |
|
7 | 273 |
print('## found ' + str(len(filtered_csv_data)) + |
274 |
' items with at least one image') |
|
5
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
275 |
print('# Importing data into Iconolab') |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
276 |
if options.get('collection_json'): |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
277 |
print('## Loading collection json') |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
278 |
collection = Collection.objects.create( |
7 | 279 |
name=collection_data.get('name'), |
280 |
verbose_name=collection_data.get('verbose_name', ''), |
|
281 |
description=collection_data.get('description', ''), |
|
282 |
image=collection_data.get('image', ''), |
|
283 |
height=collection_data.get('height', 0), |
|
284 |
width=collection_data.get('width', 0), |
|
5
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
285 |
) |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
286 |
if collection.image: |
7 | 287 |
collection_image_path = os.path.join( |
288 |
settings.MEDIA_ROOT, str(collection.image)) |
|
5
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
289 |
if not os.path.isfile(collection_image_path): |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
290 |
print('### Moving collection image') |
7 | 291 |
_, collection_image_name = os.path.split( |
292 |
collection_image_path) |
|
5
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
293 |
try: |
7 | 294 |
col_im = ImagePIL.open(os.path.join( |
295 |
self.source_dir, collection_image_name)) |
|
296 |
print('##### Generating or copying jpeg for ' + |
|
297 |
collection_image_name) |
|
5
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
298 |
col_im.thumbnail(col_im.size) |
7 | 299 |
col_im.save(collection_image_path, 'JPEG', quality=options.get( |
300 |
'jpeg_quality', settings.IMG_JPG_DEFAULT_QUALITY)) |
|
5
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
301 |
except Exception as e: |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
302 |
print(e) |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
303 |
if options.get('metacategories_json'): |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
304 |
for metacategory in metacategories_data: |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
305 |
MetaCategory.objects.create( |
7 | 306 |
collection=collection, |
307 |
label=metacategory.get('label'), |
|
308 |
triggers_notifications=metacategory.get( |
|
309 |
'triggers_notifications', 0) |
|
5
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
310 |
) |
7 | 311 |
print( |
312 |
'## Converting image and moving it to static dir, creating Image and Item objects') |
|
313 |
self.target_dir = os.path.join(settings.MEDIA_ROOT, 'uploads') |
|
314 |
print('### Images will be stored in '+self.target_dir) |
|
5
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
315 |
for item in filtered_csv_data: |
7 | 316 |
print('#### Computing metadatas for item ' + |
317 |
item['REF']+' (natural key)') |
|
5
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
318 |
if not item['REF']: |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
319 |
print('#### No Natural key, skipping') |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
320 |
continue |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
321 |
item_authors = item['AUTR'] |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
322 |
item_school = item['ECOLE'] |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
323 |
item_designation = '' |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
324 |
if item.get('TITR', ''): |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
325 |
item_designation = item['TITR'] |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
326 |
elif item.get('DENO', ''): |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
327 |
item_designation = item['DENO'] |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
328 |
elif item.get('APPL', ''): |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
329 |
item_designation = item['APPL'] |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
330 |
item_datation = '' |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
331 |
if item.get('PERI', ''): |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
332 |
item_datation = item['PERI'] |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
333 |
elif item.get('MILL', ''): |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
334 |
item_datation = item['MILL'] |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
335 |
elif item.get('EPOQ', ''): |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
336 |
item_datation = item['EPOQ'] |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
337 |
item_technics = item['TECH'] |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
338 |
item_field = item['DOM'] |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
339 |
item_measurements = item['DIMS'] |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
340 |
item_create_or_usage_location = item['LIEUX'] |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
341 |
item_discovery_context = item['DECV'] |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
342 |
item_conservation_location = item['LOCA'] |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
343 |
item_photo_credits = item['PHOT'] |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
344 |
item_inventory_number = item['INV'] |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
345 |
item_joconde_ref = item['REF'] |
7 | 346 |
if ItemMetadata.objects.filter(item__collection=collection, natural_key=item_joconde_ref).exists(): |
347 |
print('#### An item with ' + |
|
348 |
item['REF']+' for natural key, already exists in database in the import collection') |
|
5
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
349 |
|
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
350 |
if options['import_folders']: |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
351 |
|
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
352 |
# Extract folder name from natural key |
7 | 353 |
m = re.search( |
354 |
options['folders_regexp'], item[options['folders_metadata']]) |
|
5
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
355 |
folder_id = m.group(1) |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
356 |
|
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
357 |
if not Folder.objects.filter(original_id=folder_id).exists(): |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
358 |
print('#### Creating folder "'+folder_id+'"') |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
359 |
folder = Folder.objects.create( |
7 | 360 |
collection=collection, |
361 |
name='Dossier '+folder_id, |
|
362 |
original_id=folder_id |
|
5
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
363 |
) |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
364 |
else: |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
365 |
print('#### Folder "'+folder_id+'" already exists') |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
366 |
folder = Folder.objects.get(original_id=folder_id) |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
367 |
|
7 | 368 |
item_metadata = ItemMetadata.objects.get( |
369 |
item__collection=collection, natural_key=item_joconde_ref) |
|
5
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
370 |
item = item_metadata.item |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
371 |
|
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
372 |
item.folders.add(folder) |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
373 |
|
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
374 |
else: |
7 | 375 |
self.create_item_and_metadata( |
376 |
item_joconde_ref, collection, new_metadata, item['SRC_IMG_FILES'], options, self.source_dir, self.target_dir) |
|
5
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
377 |
|
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
378 |
print('# All done!') |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
379 |
|
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
380 |
logger.debug('# Recap for import command: ') |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
381 |
print('# Images without data: ') |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
382 |
logger.debug('## Checking images left without data') |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
383 |
collection_image_file = os.path.split(str(collection.image))[1] |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
384 |
if no_data_images and collection_image_file in no_data_images: |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
385 |
no_data_images.remove(collection_image_file) |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
386 |
|
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
387 |
if no_data_images: |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
388 |
for image in no_data_images: |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
389 |
logger.debug('### %r', image) |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
390 |
print('## '+image) |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
391 |
else: |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
392 |
print('## Each image has one corresponding row!') |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
393 |
logger.debug('### Each image has one corresponding row!') |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
394 |
print('# CSV Items without image') |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
395 |
logger.debug('## Checking csv rows left without image') |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
396 |
if no_image_rows: |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
397 |
for item in no_image_rows: |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
398 |
logger.debug('### %r', item['REF']) |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
399 |
print('## Natural key: '+item['REF']) |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
400 |
else: |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
401 |
print('## Each row found at least one corresponding image!') |
7 | 402 |
logger.debug( |
403 |
'### Each row found at least one corresponding image!') |
|
5
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
404 |
print('# Duplicate rows in csv') |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
405 |
logger.debug('## Checking duplicate rows in csv') |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
406 |
if duplicate_rows: |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
407 |
for item in no_image_rows: |
7 | 408 |
logger.debug('### %r: %r', options.get( |
409 |
'img_filename_identifier'), item[options.get('img_filename_identifier')]) |
|
410 |
print('## '+options.get('img_filename_identifier') + |
|
411 |
': '+item[options.get('img_filename_identifier')]) |
|
5
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
412 |
else: |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
413 |
print('## Each row found at least one corresponding image!') |
7 | 414 |
logger.debug( |
415 |
'### Each row found at least one corresponding image!') |
|
5
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
416 |
except FileNotFoundError: |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
417 |
print('!!! File '+options.get('csv_path')+' does not exist. !!!') |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
418 |
except ValueError as e: |
cfd40849d24c
Turning iconolab-mcc into App to add specific import commands
Riwad Salim
parents:
diff
changeset
|
419 |
print(str(e)) |