def cat_curator_upload_handler(request): profile = request.get_user().catprofile curator_album_select_form = CatTaggerAlbumSelectionForm(request.POST) curator_album_create_form = CatCuratorAlbumAddForm(request.POST) selection_json = request.POST.get('selection') or None selection = None if selection_json is not None: # Query again to block p**n parsed_selection = loads(selection_json) ids = [k for k, v in parsed_selection.iteritems()] response = _curator_get_records_by_ids(ids) parsed_response = loads(response.text)['result'] parsed_kv = {} for each in parsed_response: parsed_kv[each['id']] = each for k, v in parsed_selection.iteritems(): for sk, sv in parsed_kv[k].iteritems(): parsed_selection[k][sk] = sv selection = parsed_selection ret = { 'photos': {} } if selection and len(selection) > 0 and profile is not None \ and (curator_album_select_form.is_valid() or curator_album_create_form.is_valid()): if curator_album_select_form.is_valid(): album = curator_album_select_form.cleaned_data['album'] else: album = CatAlbum( title=curator_album_create_form.cleaned_data['title'], subtitle=curator_album_create_form.cleaned_data['subtitle'], ) album.save() ret['album_id'] = album.id for k, v in selection.iteritems(): upload_form = CatCuratorPhotoUploadForm(v) if upload_form.is_valid(): if upload_form.cleaned_data['institution']: upload_form.cleaned_data['institution'] = upload_form.cleaned_data['institution'].split(',')[0] source = Source.objects.filter(description=upload_form.cleaned_data['institution']).first() if not source: source = Source( name=upload_form.cleaned_data['institution'], description=upload_form.cleaned_data['institution'] ) source.save() else: source = Source.objects.get(name='AJP') if upload_form.cleaned_data['id'] and upload_form.cleaned_data['id'] != '': incoming_muis_id = upload_form.cleaned_data['id'] if '_' in incoming_muis_id: muis_id = incoming_muis_id.split('_')[0] muis_media_id = incoming_muis_id.split('_')[1] else: muis_id = incoming_muis_id muis_media_id = None if muis_media_id: existing_photo = CatPhoto.objects.filter(source=source, muis_id=muis_id, muis_media_id=muis_media_id).first() else: existing_photo = CatPhoto.objects.filter(source=source, muis_id=muis_id).first() if not existing_photo: new_photo = None try: new_photo = CatPhoto( title=upload_form.cleaned_data['title'].rstrip().encode('utf-8'), author=upload_form.cleaned_data['creators'].encode('utf-8'), source=source, source_key=upload_form.cleaned_data['identifyingNumber'], source_url=upload_form.cleaned_data['urlToRecord'], muis_id=upload_form.cleaned_data['id'].split('_')[0], muis_media_id=upload_form.cleaned_data['id'].split('_')[1] if len(upload_form.cleaned_data['id'].split('_')) > 1 else None, flip=upload_form.cleaned_data['flip'], invert=upload_form.cleaned_data['invert'], stereo=upload_form.cleaned_data['stereo'], rotated=upload_form.cleaned_data['rotated'] ) new_photo.save() opener = urllib2.build_opener() opener.addheaders = [('User-Agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.137 Safari/537.36')] img_response = opener.open(upload_form.cleaned_data['imageUrl']) new_photo.image.save('cat-muis.jpg', ContentFile(img_response.read())) photo_path = MEDIA_ROOT + '/' + str(new_photo.image) if new_photo.invert: img = Image.open(photo_path) inverted_grayscale_image = ImageOps.invert(img).convert('L') inverted_grayscale_image.save(photo_path) if new_photo.rotated > 0: img = Image.open(photo_path) rot = img.rotate(new_photo.rotated, expand=1) rot.save(photo_path) new_photo.width = rot.size[0] new_photo.height = rot.size[1] new_photo.save() if new_photo.flip: img = Image.open(photo_path) flipped = img.transpose(Image.FLIP_LEFT_RIGHT) flipped.save(photo_path) ret['photos'][k] = {} ret['photos'][k]['message'] = _('OK') new_photo.save() if album: album.photos.add(new_photo) ret['photos'][k]['success'] = True except: if new_photo: new_photo.image.delete() new_photo.delete() ret['photos'][k] = {} ret['photos'][k]['error'] = _('Error uploading file') else: if album: album.photos.add(existing_photo) ret['photos'][k] = {} ret['photos'][k]['success'] = True ret['photos'][k]['message'] = _('Photo already exists in Sift') if album: album.save() else: if not selection or len(selection) == 0: error = _('Please add photos to your album') else: error = _('Not enough data submitted') ret = { 'error': error } return HttpResponse(dumps(ret), content_type='application/json')