def clean_up_files(snap_id, group_id): ''' Deletes all picture files for a given snap if those pictures sources are designated in the group to be deleted after processing ''' group_document = get_group_document(group_id) if 'image_sources_to_delete' in group_document: image_sources_to_delete = group_document['image_sources_to_delete'].split(',') pictures = search_generic(document_type='picture', args_dict={'snap_id': str(snap_id)}) clean_up_files = True if len(pictures): snap_document = get_generic(snap_id, 'snap') if snap_document['clean_up_files'] == False: # TODO make sure this comparison works clean_up_files = False if clean_up_files: for pic_id in pictures.keys(): if pictures[pic_id]['source'] in image_sources_to_delete: os.remove(pictures[pic_id]['uri']) pictures[pic_id]['uri'] = '' pictures[pic_id]['deleted'] = True else: shutil.move(pictures[pic_id]['uri'], current_app.config['PICTURE_SAVE_DIRECTORY']) pictures[pic_id]['uri'] = os.path.join(current_app.config['PICTURE_SAVE_DIRECTORY'], pictures[pic_id]['filename']) update_generic(pictures[pic_id], 'picture') os.rmdir(os.path.join(current_app.config['PICTURE_SAVE_DIRECTORY'], str(snap_id))) snap_document['files_have_been_cleaned_up'] = True update_generic(snap_document, 'snap')
def clean_up_files(snap_id, group_id): """ Deletes all picture files for a given snap if those pictures sources are designated in the group to be deleted after processing """ group_document = get_group_document(group_id) if "image_sources_to_delete" in group_document: image_sources_to_delete = group_document["image_sources_to_delete"].split(",") pictures = search_generic(document_type="picture", args_dict={"snap_id": str(snap_id)}) clean_up_files = True if len(pictures): snap_document = get_generic(snap_id, "snap") if snap_document["clean_up_files"] == False: # TODO make sure this comparison works clean_up_files = False if clean_up_files: for pic_id in pictures.keys(): if pictures[pic_id]["source"] in image_sources_to_delete: os.remove(pictures[pic_id]["uri"]) pictures[pic_id]["uri"] = "" pictures[pic_id]["deleted"] = True else: shutil.move(pictures[pic_id]["uri"], current_app.config["PICTURE_SAVE_DIRECTORY"]) pictures[pic_id]["uri"] = os.path.join( current_app.config["PICTURE_SAVE_DIRECTORY"], pictures[pic_id]["filename"] ) update_generic(pictures[pic_id], "picture") os.rmdir(os.path.join(current_app.config["PICTURE_SAVE_DIRECTORY"], str(snap_id))) snap_document["files_have_been_cleaned_up"] = True update_generic(snap_document, "snap")
def generic_update_view(item_id='', document_type=''): try: item_dict = get_document_with_exception(item_id, document_type) if request.headers['Content-Type'] == 'application/json': for k in request.json.keys(): if doc_attribute_can_be_set(k): item_dict[k] = request.json[k] update_generic(item_dict, document_type) return Response(json.dumps(item_dict), status=200, mimetype='application/json') error_msg = 'problem with update: content type is not application/json' return Response(json.dumps(error_msg), status=409, mimetype='application/json') except Exception as e: return Response(json.dumps(e.message), status=e.status_code, mimetype='application/json')
def update_group(group_id): ''' Updates group record ''' try: group_dict = get_group_document(group_id) if request.headers['Content-Type'] == 'application/json': for k in request.json.keys(): if doc_attribute_can_be_set(k): group_dict[k] = request.json[k] update_generic(group_dict, 'group') return Response(json.dumps(group_dict), status=200, mimetype='application/json') return Response(json.dumps('problem with request data'), status=409, mimetype='application/json') except Exception as e: return Response(json.dumps(e.message), status=e.status_code, mimetype='application/json')
def update_settings(): ''' Updates the settings document ''' try: settings = get_settings_document() if request.headers['Content-Type'] == 'application/json': for k in request.json.keys(): if doc_attribute_can_be_set(k): settings[k] = request.json[k] update_generic(settings, 'settings') return Response(json.dumps(settings), status=200, mimetype='application/json') err_msg = 'no valid settings parameters supplied' return Response(json.dumps(err_msg), status=409, mimetype='application/json') except Exception as e: return Response(json.dumps(e.message), status=e.status_code, mimetype='application/json')
def test_update_generic_throws_error_when_no_doc_of_the_right_type_exists_and_is_specified_in_call(self): the_id = str(uuid.uuid4()) the_document = {'_id': the_id, 'type': 'ringo'} current_app.db[the_id] = the_document with pytest.raises(DocumentConfigurationError) as exception_info: ret_val = ts.update_generic(the_document, 'paul') assert 'trying to alter document type for id {0} during update'.format(str(the_id)) in str(exception_info.value)
def test_update_generic_throws_error_when_no_doc_exists(self, ts_item_exists): ts_item_exists.return_value = False item_id = uuid.uuid4() the_document = {'_id': item_id} with pytest.raises(DocumentConfigurationError) as exception_info: ret_val = ts.update_generic(the_document, 'whatever') assert 'trying to update {0} when no document exists for that id'.format(str(item_id)) in str(exception_info.value)
def test_update_generic_throws_error_when_no_doc_of_the_right_type_exists_and_is_specified_in_call( self): the_id = str(uuid.uuid4()) the_document = {'_id': the_id, 'type': 'ringo'} current_app.db[the_id] = the_document with pytest.raises(DocumentConfigurationError) as exception_info: ret_val = ts.update_generic(the_document, 'paul') assert 'trying to alter document type for id {0} during update'.format( str(the_id)) in str(exception_info.value)
def test_update_generic_throws_error_when_no_doc_exists( self, ts_item_exists): ts_item_exists.return_value = False item_id = uuid.uuid4() the_document = {'_id': item_id} with pytest.raises(DocumentConfigurationError) as exception_info: ret_val = ts.update_generic(the_document, 'whatever') assert 'trying to update {0} when no document exists for that id'.format( str(item_id)) in str(exception_info.value)
def upload_files_to_s3(snap_id, group_id): """ Uploads pictures for a given snap to s3 Takes a snap id and group id, if the group is configured to use a gallery it looks for all pictures whose values for source matches those are specified in the group to use for the gallery """ group_document = get_group_document(group_id) if group_document["use_gallery"]: image_sources_for_gallery = group_document["image_sources_for_gallery"].split(",") pictures = search_generic(document_type="picture", args_dict={"snap_id": str(snap_id)}) # TODO the following assumes s3 and internet are working fine, make it more robust, with py.test tests too conn = boto.connect_s3(current_app.config["S3_ACCESS_KEY_ID"], current_app.config["S3_SECRET_ACCESS_KEY"]) bucket = conn.get_bucket(current_app.config["S3_BUCKET_NAME"]) for pic_id in pictures.keys(): if pictures[pic_id]["source"] in image_sources_for_gallery: destination = boto.s3.key.Key(bucket) destination.key = pictures[pic_id]["filename"] destination.set_contents_from_filename(pictures[pic_id]["uri"]) destination.make_public() pic_gallery_url = destination.generate_url(expires_in=0, query_auth=False) pictures[pic_id]["gallery_url"] = pic_gallery_url update_generic(pictures[pic_id], "picture")
def test_update_generic_calls_expected_functions(self, ts_save_document, ts_item_exists, ts_cast_uuid_to_string): ts_item_exists.return_value = True ts_cast_uuid_to_string.return_value = 'abc' item_id = uuid.uuid4() the_document = {'_id': item_id} ret_val = ts.update_generic(the_document, 'plastic') ts_cast_uuid_to_string.assert_called_once_with(item_id) ts_item_exists.assert_has_calls([call('abc', 'any'), call('abc', 'plastic')]) ts_save_document.assert_called_once_with(the_document)
def test_update_generic_calls_expected_functions(self, ts_save_document, ts_item_exists, ts_cast_uuid_to_string): ts_item_exists.return_value = True ts_cast_uuid_to_string.return_value = 'abc' item_id = uuid.uuid4() the_document = {'_id': item_id} ret_val = ts.update_generic(the_document, 'plastic') ts_cast_uuid_to_string.assert_called_once_with(item_id) ts_item_exists.assert_has_calls( [call('abc', 'any'), call('abc', 'plastic')]) ts_save_document.assert_called_once_with(the_document)
def upload_files_to_s3(snap_id, group_id): ''' Uploads pictures for a given snap to s3 Takes a snap id and group id, if the group is configured to use a gallery it looks for all pictures whose values for source matches those are specified in the group to use for the gallery ''' group_document = get_group_document(group_id) if group_document['use_gallery']: image_sources_for_gallery = group_document['image_sources_for_gallery'].split(',') pictures = search_generic(document_type='picture', args_dict={'snap_id': str(snap_id)}) # TODO the following assumes s3 and internet are working fine, make it more robust, with py.test tests too conn = boto.connect_s3(current_app.config['S3_ACCESS_KEY_ID'], current_app.config['S3_SECRET_ACCESS_KEY']) bucket = conn.get_bucket(current_app.config['S3_BUCKET_NAME']) for pic_id in pictures.keys(): if pictures[pic_id]['source'] in image_sources_for_gallery: destination = boto.s3.key.Key(bucket) destination.key = pictures[pic_id]['filename'] destination.set_contents_from_filename(pictures[pic_id]['uri']) destination.make_public() pic_gallery_url = destination.generate_url(expires_in=0, query_auth=False) pictures[pic_id]['gallery_url'] = pic_gallery_url update_generic(pictures[pic_id], 'picture')
def test_update_generic_throws_error_when_no_doc_id(self): the_document = {'insect': 'beetle'} with pytest.raises(DocumentConfigurationError) as exception_info: ret_val = ts.update_generic(the_document, 'color') assert 'trying to update a document with no id' in str(exception_info.value)
def test_update_generic_throws_error_when_no_doc_id(self): the_document = {'insect': 'beetle'} with pytest.raises(DocumentConfigurationError) as exception_info: ret_val = ts.update_generic(the_document, 'color') assert 'trying to update a document with no id' in str( exception_info.value)