# HG changeset patch # User durandn # Date 1471526781 -7200 # Node ID 2c2d394904dbdafebfb50c6f996b844ef6cc1f48 # Parent 454e39dced1f0d27dbc5f32296eca4cc4068d54e corrected error with metacategories, fixed image import command to import metacategories with new collection diff -r 454e39dced1f -r 2c2d394904db src/iconolab/forms/comments.py --- a/src/iconolab/forms/comments.py Thu Aug 18 10:58:55 2016 +0200 +++ b/src/iconolab/forms/comments.py Thu Aug 18 15:26:21 2016 +0200 @@ -8,13 +8,20 @@ from django.utils.encoding import force_text from django.utils import timezone from iconolab.models import MetaCategory +import logging + +logger = logging.getLogger(__name__) class IconolabCommentForm(XtdCommentForm): - metacategories = forms.ModelMultipleChoiceField(widget=forms.CheckboxSelectMultiple, queryset=MetaCategory.objects.all(), required=False) email = forms.EmailField(required=False) - + metacategories = forms.ModelMultipleChoiceField(widget=forms.CheckboxSelectMultiple, queryset=None, required=False) + def __init__(self, *args, **kwargs): super(IconolabCommentForm, self).__init__(*args, **kwargs) + self.collection = self.target_object.image.item.collection + logger.debug(self.fields) + self.fields["metacategories"].queryset = self.collection.metacategories.all() + logger.debug(self.fields["metacategories"].queryset) self.fields.pop('email') def get_comment_create_data(self): diff -r 454e39dced1f -r 2c2d394904db src/iconolab/management/commands/importimages.py --- a/src/iconolab/management/commands/importimages.py Thu Aug 18 10:58:55 2016 +0200 +++ b/src/iconolab/management/commands/importimages.py Thu Aug 18 15:26:21 2016 +0200 @@ -30,6 +30,12 @@ default=False, help='insert extracted data into the specified collection instead of trying to load a collection fixture', ) + parser.add_argument( + '--metacategories-fixture', + dest='metacategories_fixture', + default=False, + help='add metacategories to the created collection from a fixture file', + ) def handle(self, *args, **options): pp = pprint.PrettyPrinter(indent=4) @@ -60,6 +66,22 @@ raise ValueError("!!! Collection with primary key "+options.get("collection_id")+" was not found, aborting !!!") else: raise ValueError("!!! No collection fixture or collection id, aborting because we can't properly generate data. !!!") + + if options.get("metacategories_fixture"): + print("## Finding metacategories fixture json data in "+source_dir) + metacategories_fixture_path = os.path.join(source_dir, options.get("metacategories_fixture")) + if not os.path.isfile(metacategories_fixture_path): + print("### No "+options.get("metacategories_fixture")+".json file was found in the source directory") + raise ValueError("!!! Fixture file "+metacategories_fixture_path+" was not found !!!") + with open(metacategories_fixture_path) as metacategories_fixture_file: + metacategories_data = json.loads(metacategories_fixture_file.read()) + for metacategory in metacategories_data: + if options.get("collection_fixture") and metacategory["fields"].get("collection", False) != collection_data[0].get("pk"): + print(metacategory["fields"].get("collection", False)) + raise ValueError("!!! The fixture should only contain metacategories for the imported collection !!!") + elif options.get("collection_id") and metacategory["fields"].get("collection", False) != collection.id: + raise ValueError("!!! The fixture should only contain metacategories for the imported collection !!!") + # We read the csv csvreader = csv.DictReader(open(options.get("csv_path"), encoding=options.get("encoding")), delimiter=";") print("# Extracting data from csv file and storing it in standardized format") @@ -97,6 +119,20 @@ collection = Collection.objects.get( pk = collection_data[0]["pk"] ) + if collection.image: + collection_image_path = os.path.join(settings.MEDIA_ROOT, str(collection.image)) + if not os.path.isfile(collection_image_path): + print("### Moving collection image") + _ , collection_image_name = os.path.split(collection_image_path) + try: + col_im = ImagePIL.open(os.path.join(source_dir, collection_image_name)) + print("##### Generating or copying jpeg for "+collection_image_name) + col_im.thumbnail(col_im.size) + col_im.save(collection_image_path, "JPEG", quality=100) + except Exception as e: + print(e) + if options.get("metacategories_fixture"): + call_command("loaddata", metacategories_fixture_path) print("## Converting image and moving it to static dir, creating Image and Item objects") target_dir = os.path.join(settings.MEDIA_ROOT, "uploads") print("### Images will be stored in "+target_dir)