1 # -*- coding: UTF-8 -*- |
|
2 import json |
|
3 import logging |
|
4 import os |
|
5 import pprint |
|
6 import re |
|
7 import shutil |
|
8 |
|
9 from django.conf import settings |
|
10 from django.core.management.base import BaseCommand, CommandError |
|
11 |
|
12 from iconolab.management.commands.importimages import BaseImportImagesCommand |
|
13 from iconolab.models import (Collection, Folder, Image, ImageStats, Item, |
|
14 ItemMetadata, MetaCategory) |
|
15 |
|
16 if settings.IMPORT_LOGGER_NAME and settings.LOGGING['loggers'].get(settings.IMPORT_LOGGER_NAME, ''): |
|
17 logger = logging.getLogger(settings.IMPORT_LOGGER_NAME) |
|
18 else: |
|
19 logger = logging.getLogger(__name__) |
|
20 |
|
21 class Command(BaseImportImagesCommand): |
|
22 help = 'import metacategories files from a directory' |
|
23 |
|
24 def add_arguments(self, parser): |
|
25 parser.add_argument('source_dir') |
|
26 parser.add_argument( |
|
27 '--encoding', |
|
28 dest='encoding', |
|
29 default='utf-8', |
|
30 help='JSON file encoding' |
|
31 |
|
32 ) |
|
33 parser.add_argument( |
|
34 '--collection-id', |
|
35 dest='collection_id', |
|
36 default=False, |
|
37 help='insert extracted data into the specified collection instead of trying to load a collection fixture', |
|
38 ) |
|
39 parser.add_argument( |
|
40 '--metacategories-json', |
|
41 dest='metacategories_json', |
|
42 default=False, |
|
43 help='add metacategories to the collection from a json file (json must be a list of object with "label" and "triggers_notifications" fields)', |
|
44 ) |
|
45 |
|
46 def handle(self, *args, **options): |
|
47 |
|
48 print('# Logging with logger '+logger.name) |
|
49 logger.debug('# Initializing command with args: %r', options) |
|
50 |
|
51 self.source_dir = options.get('source_dir') |
|
52 |
|
53 if options.get('collection_id'): |
|
54 print('## Finding collection with id ' + |
|
55 options.get('collection_id')) |
|
56 try: |
|
57 collection = Collection.objects.get( |
|
58 pk=options.get('collection_id')) |
|
59 except Collection.DoesNotExist: |
|
60 raise ValueError('!!! Collection with primary key ' + |
|
61 options.get('collection_id')+' was not found, aborting !!!') |
|
62 else: |
|
63 raise ValueError( |
|
64 '!!! No collection fixture or collection id, aborting because we can\'t properly generate data. !!!') |
|
65 |
|
66 if options.get('metacategories_json'): |
|
67 print('## Finding metacategories fixture json data in '+self.source_dir) |
|
68 metacategories_json_path = os.path.join( |
|
69 self.source_dir, options.get('metacategories_json')) |
|
70 if not os.path.isfile(metacategories_json_path): |
|
71 print('### No '+options.get('metacategories_json')+ |
|
72 '.json file was found in the source directory') |
|
73 raise ValueError( |
|
74 '!!! Fixture file '+metacategories_json_path+' was not found !!!') |
|
75 with open(metacategories_json_path) as metacategories_json_file: |
|
76 metacategories_data = json.loads( |
|
77 metacategories_json_file.read()) |
|
78 for metacategory in metacategories_data: |
|
79 if metacategory.get('label', None) is None: |
|
80 raise ValueError( |
|
81 '!!! Metacategory without label !!!') |
|
82 |
|
83 if options.get('metacategories_json'): |
|
84 for metacategory in metacategories_data: |
|
85 MetaCategory.objects.create( |
|
86 collection=collection, |
|
87 label=metacategory.get('label'), |
|
88 triggers_notifications=metacategory.get( |
|
89 'triggers_notifications', 0) |
|
90 ) |
|