Esempio n. 1
0
def generic_save_view(args_dict={}, document_type=''):
    '''
    Takes what's in request.args, adds it to what's in args dict and saves it.
    Also will guarantee that the type of document that's saved is of type document_type
    Returns a response object
    '''
    try:
        if request.headers['Content-Type'] == 'application/json':
            for k in request.json.keys():
                args_dict[k] = request.json[k]
            if '_id' not in args_dict:
                args_dict['_id'] = cast_uuid_to_string(uuid.uuid4())
            if 'type' not in args_dict:
                args_dict['type'] = document_type
            save_generic(args_dict, document_type)
            return Response(json.dumps(args_dict),
                            status=200,
                            mimetype='application/json')
        else:
            error_msg = 'problem with saving {0}: content type is not application/json'.format(
                document_type)
            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')
Esempio n. 2
0
def call_merge_images():
    '''
    Merges two images into a third one
    Accepts merge_type as an optional parameter, see here for valid merge types: http://www.effbot.org/imagingbook/imagechops.htm
    '''
    try:
        args_dict = gather_and_enforce_request_args([{'name': 'img1_id', 'required': True},
                                                     {'name': 'img2_id', 'required': True},
                                                     {'name': 'merge_type'}])
        img1_id = args_dict['img1_id']
        img2_id = args_dict['img2_id']

        test_input_parameters_for_valid_image_ids(args_dict)
        merge_type = check_for_merge_type(args_dict)
        result_id = cast_uuid_to_string(uuid.uuid4())

        merge_images_task.delay(
            img1_primary_id_in=img1_id,
            img1_alternate_id_in=uuid.uuid4(),
            img2_id_in=img2_id,
            img_id_out=result_id,
            group_id='current',
            merge_type=merge_type)
        accept_json = {'result_id': result_id}
        return Response(json.dumps(accept_json), status=202, mimetype='application/json')
    except Exception as e:
        return Response(json.dumps(e.message), status=e.status_code, mimetype='application/json')
Esempio n. 3
0
def update_generic(document_in, document_type):
    '''
    requires document to have at least _id and type
    '''
    if '_id' in document_in:
        the_id = cast_uuid_to_string(document_in['_id'])
        if not item_exists(the_id, 'any'):
            raise DocumentConfigurationError('trying to update {0} when no document exists for that id'.format(the_id))
        if not item_exists(the_id, document_type):
            raise DocumentConfigurationError('trying to alter document type for id {0} during update'.format(the_id))
        save_document(document_in)
    else:
         raise DocumentConfigurationError('trying to update a document with no id')
Esempio n. 4
0
def create_distortion_pair():
    try:
        if 'distortion_set_id' not in request.json:
            distortion_set_id = cast_uuid_to_string(uuid.uuid4())
            request.json['distortion_set_id'] = distortion_set_id
        else:
            distortion_set_id = request.json['distortion_set_id']

        if not item_exists(distortion_set_id, 'distortion_set'):
            distortion_set_dict = {'_id': distortion_set_id, 'type': 'distortion_set'}
            save_generic(distortion_set_dict, 'distortion_set')

        # TODO add a lot more tests to the request json, we need start_x, y, end_x, y and they need to be ints, range tests, etc
        #  ^^^^^ have this be a validation function which is optionally passed to save_generic
        return_value = generic_save_view(document_type='distortion_pair')
        return return_value
    except Exception as e:
        return Response(json.dumps(e.message), status=e.status_code, mimetype='application/json')
Esempio n. 5
0
def generic_save_view(args_dict={}, document_type=''):
    '''
    Takes what's in request.args, adds it to what's in args dict and saves it.
    Also will guarantee that the type of document that's saved is of type document_type
    Returns a response object
    '''
    try:
        if request.headers['Content-Type'] == 'application/json':
            for k in request.json.keys():
                args_dict[k] = request.json[k]
            if '_id' not in args_dict:
                args_dict['_id'] = cast_uuid_to_string(uuid.uuid4())
            if 'type' not in args_dict:
                args_dict['type'] = document_type
            save_generic(args_dict, document_type)
            return Response(json.dumps(args_dict), status=200, mimetype='application/json')
        else:
            error_msg = 'problem with saving {0}: content type is not application/json'.format(document_type)
            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')
Esempio n. 6
0
def create_distortion_pair():
    try:
        if 'distortion_set_id' not in request.json:
            distortion_set_id = cast_uuid_to_string(uuid.uuid4())
            request.json['distortion_set_id'] = distortion_set_id
        else:
            distortion_set_id = request.json['distortion_set_id']

        if not item_exists(distortion_set_id, 'distortion_set'):
            distortion_set_dict = {
                '_id': distortion_set_id,
                'type': 'distortion_set'
            }
            save_generic(distortion_set_dict, 'distortion_set')

        # TODO add a lot more tests to the request json, we need start_x, y, end_x, y and they need to be ints, range tests, etc
        #  ^^^^^ have this be a validation function which is optionally passed to save_generic
        return_value = generic_save_view(document_type='distortion_pair')
        return return_value
    except Exception as e:
        return Response(json.dumps(e.message),
                        status=e.status_code,
                        mimetype='application/json')