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