예제 #1
0
def read_gridfs(id):
    file = None
    try:
        file = gfs.get(id)
    except Exception as e:
        logger.debug('--read_gridfs--' + str(e))
    return file
예제 #2
0
def count_template(filter):
    count = None
    try:
        count = template_collection.count(filter)
        logger.debug('--count_template--' + dumps(count))
    except Exception as e:
        logger.debug('--count_template--' + str(e))
    return count
예제 #3
0
def count_user(filter):
    count = None
    try:
        count = user_collection.count(filter)
        logger.debug('--count_user--' + dumps(count))
    except Exception as e:
        logger.debug('--count_user--' + str(e))
    return count
예제 #4
0
def get_template(id):
    result = None
    try:
        result = template_collection.find_one({'_id': id})
        logger.debug('--result--' + dumps(result))
    except Exception as e:
        logger.debug('--create_template--' + str(e))
    return result
예제 #5
0
def exist_gridfs(id):
    result = None
    try:
        result = gfs.exists({'_id': id})
        logger.debug('--exist_gridfs--' + dumps(result))
    except Exception as e:
        logger.debug('--exist_gridfs--' + str(e))
    return result
예제 #6
0
def get_image(id):
    result = None
    try:
        result = image_collection.find_one({'_id': id})
        logger.debug('--get_image--' + dumps(result))
    except Exception as e:
        logger.debug('--get_image--' + str(e))
    return result
예제 #7
0
def count_image(id):
    count = None
    try:
        count = image_collection.count({'_id': id})
        logger.debug('--count_image--' + dumps(count))
    except Exception as e:
        logger.debug('--count_image--' + str(e))
    return count
예제 #8
0
def convert_template_list_from_mongo(results):
    objs = []
    if results:
        for result in results:
            obj = convert_template_from_mongo(result)
            objs.append(obj)
    logger.debug('--objs--' + str(objs))
    return objs
예제 #9
0
def store_fs(file):
    fullname = None
    if file:
        try:
            filename = file.filename
            fullname = os.path.join(upload_dir, filename)
            file.save(fullname)
        except Exception as e:
            logger.debug('--store_fs--' + str(e))
    return fullname
예제 #10
0
def delete_image(id):
    result = None
    if exist_image(id):
        try:
            image_collection.delete_one({'_id': id})
            if not exist_image(id):
                result = True
        except Exception as e:
            logger.debug('--delete_image--' + str(e))
    return result
예제 #11
0
def create():
    request_data = request.json
    logger.debug('--request_data--' + str(request_data))
    result = create_user(request_data)
    if not result:
        return Response(status=500)

    obj = convert_user_from_mongo(result)
    response_data = json.dumps(obj)
    logger.debug('--response_data--' + response_data)
    resp = Response(response=response_data, status=201, content_type='application/json')
    return resp
예제 #12
0
def delete():
    request_data = request.json
    logger.debug('--request_data--' + str(request_data))
    id = request_data.get('id')
    if not (id and ObjectId.is_valid(id)):
        return Response(status=400)

    result = delete_image(ObjectId(id))
    if result:
        return Response(status=204)
    else:
        return Response(status=202)
예제 #13
0
def create_image(data):
    result = None
    if data:
        try:
            obj = convert_image_from_json(data)
            insert_result = image_collection.insert_one(obj)
            id = insert_result.inserted_id
            logger.debug('--id--' + str(id))
            result = get_image(id)
        except Exception as e:
            logger.debug('--create_image--' + str(e))
    return result
예제 #14
0
def delete_gridfs(id):
    result = None
    if not exist_gridfs(id):
        result = False
    else:
        try:
            gfs.delete(id)
            if not exist_gridfs(id):
                result = True
        except Exception as e:
            logger.debug('--delete_gridfs--' + str(e))
    return result
예제 #15
0
def trim_regions():
    request_data = request.json
    logger.debug('--request_data--' + str(request_data))
    id = request_data.get('id')
    if not (id and ObjectId.is_valid(id)):
        return Response(status=400)

    result = trim_image_regions(ObjectId(id))
    if result:
        return Response(status=200)
    else:
        return Response(status=404)
예제 #16
0
def delete_template(id):
    result = None
    if not exist_template(id):
        result = False
    else:
        try:
            template_collection.delete_one({'_id': id})
            if not exist_template(id):
                result = True
        except Exception as e:
            logger.debug('--delete_template--' + str(e))
    return result
예제 #17
0
def upload():
    if 'file' not in request.files:
        return Response(status=400)

    file = request.files['file']
    obj = store_gridfs(file)
    if not obj:
        return Response(status=500)

    response_data = json.dumps(obj)
    logger.debug('--response_data--' + response_data)
    resp = Response(response=response_data, status=201, content_type='application/json')
    return resp
예제 #18
0
def create_template(data):
    result = None
    if data:
        try:
            obj = convert_template_from_json(data)
            obj['status'] = 'create'
            insert_result = template_collection.insert_one(obj)
            id = insert_result.inserted_id
            logger.debug('--id--' + str(id))
            result = get_template(id)
        except Exception as e:
            logger.debug('--create_template--' + str(e))
    return result
예제 #19
0
def update_image(data):
    result = None
    if data:
        try:
            obj = convert_image_from_json(data)
            id = obj.get('_id')
            logger.debug('--id--' + str(id))
            if exist_image(id):
                del obj['_id']
                image_collection.update_one({'_id': id}, {'$set': obj})
                result = get_image(id)
        except Exception as e:
            logger.debug('--update_image--' + str(e))
    return result
예제 #20
0
def trim_image_regions(id):
    image = get_image(id)
    if not image:
        return False

    kind = image.get('kind')
    filename = image.get('filename')
    extension = filename.split('.')[-1]

    path = os.path.join(image_dir, kind, str(id), 'regions')

    regions = image.get('regions')
    if not regions:
        return False

    for region in regions:
        logger.debug('--region--' + str(region))
        try:
            region_name = region.get('name') + '.' + extension
            src_filename = os.path.join(path, region_name)
            logger.debug('--src_filename--' + src_filename)
            trim(src_filename, src_filename)
        except Exception as e:
            logger.debug('--trim_image_regions--' + str(e))
            return False

    try:
        image_collection.update_one({'_id': id}, {'$set': {'status': 'trim'}})
    except Exception as e:
        logger.debug('--trim_image_regions--' + str(e))
        return False
    return True
예제 #21
0
def get():
    id = request.args.get('id')
    if not (id and ObjectId.is_valid(id)):
        abort(400)

    result = get_template(ObjectId(id))
    if not result:
        return Response(status=404)

    obj = convert_template_from_mongo(result)
    response_data = json.dumps(obj)
    logger.debug('--response_data--' + response_data)
    resp = Response(response=response_data, status=201, content_type='application/json')
    return resp
예제 #22
0
def sign_in_user(data):
    result = None
    if data:
        try:
            username = data.get('username')
            password = data.get('password')
            if username and password:
                existing_user = user_collection.find_one({'username': username})
                hashed_password = existing_user['password']
                if check_password(password, hashed_password):
                    result = existing_user
        except Exception as e:
            logger.debug('--sign_in_user--' + str(e))
    return result
예제 #23
0
def prepare_image_regions(id):
    image = get_image(id)
    if not image:
        return False

    kind = image.get('kind')
    if not kind:
        return False

    storage_id = image.get('storage_id')
    file = read_gridfs(storage_id)
    if not file:
        return False

    filename = file.filename
    bytes = file.read()

    template_id = image.get('template_id')
    template = get_template(template_id)
    if not template:
        return False

    regions = template.get('regions')
    if not regions:
        return False

    path = os.path.join(image_dir, kind, str(id))
    if not os.path.exists(path):
        os.makedirs(path)

    src_filename = os.path.join(path, filename)
    logger.debug('--src_filename--' + src_filename)
    f = open(src_filename, 'wb')
    f.write(bytes)
    f.close()

    try:
        image_collection.update_one({'_id': id}, {
            '$set': {
                'filename': filename,
                'regions': regions,
                'status': 'prepare'
            }
        })
    except Exception as e:
        logger.debug('--prepare_image_regions--' + str(e))
        return False
    return True
예제 #24
0
def create_user(data):
    result = None
    if data:
        try:
            obj = convert_user_from_json(data)
            password = obj['password']
            if password:
                obj['password'] = encrypt_password(password)
            obj['status'] = 'create'
            insert_result = user_collection.insert_one(obj)
            id = insert_result.inserted_id
            logger.debug('--id--' + str(id))
            result = get_user(id)
        except Exception as e:
            logger.debug('--create_user--' + str(e))
    return result
예제 #25
0
def update():
    request_data = request.json
    logger.debug('--request_data--' + str(request_data))
    id = request_data.get('id')
    if not id:
        abort(400)

    result = update_user(request_data)
    if not result:
        return Response(status=500)

    obj = convert_user_from_mongo(result)
    response_data = json.dumps(obj)
    logger.debug('--response_data--' + response_data)
    resp = Response(response=response_data, status=200, content_type='application/json')
    return resp
예제 #26
0
def create():
    request_data = request.json
    logger.debug('--request_data--' + str(request_data))
    storage_id = request_data.get('storage_id')
    if not storage_id:
        abort(400)

    result = create_template(request_data)
    if not result:
        return Response(status=500)

    obj = convert_template_from_mongo(result)
    response_data = json.dumps(obj)
    logger.debug('--response_data--' + response_data)
    resp = Response(response=response_data, status=201, content_type='application/json')
    return resp
예제 #27
0
def list_template(filter, page_request):
    offset = page_request.offset
    sort = page_request.sort
    size = page_request.size
    result = None
    cursor = template_collection.find(filter)
    try:
        if offset:
            cursor.skip(offset)
        if sort:
            cursor.sort(sort)
        if size:
            cursor.limit(size)
        result = list(cursor)
        logger.debug('--list_template--' + dumps(result))
    except Exception as e:
        logger.debug('--list_template--' + str(e))
    return result
예제 #28
0
def get(id, name):
    if not (id and ObjectId.is_valid(id) and name):
        return Response(status=400)

    file = read_gridfs(ObjectId(id))
    if not file:
        return Response(status=404)

    filename = file.filename
    response_data = file.read()
    content_type = file.content_type
    length = file.length
    logger.debug('--filename--' + filename)

    resp = Response(response=response_data, status=200, content_type=content_type)
    resp.headers['Content-Length'] = length
    resp.headers['Content-Disposition'] = "inline; filename=" + str(filename.encode('utf-8'))
    return resp
예제 #29
0
def store_gridfs(file):
    obj = None
    if file:
        try:
            filename = file.filename
            extension = filename.split('.')[-1]
            content_type = mimetypes.types_map['.' + extension]
            id = gfs.put(file, filename=filename, content_type=content_type)
            logger.debug('--id--' + str(id))
            read_file = read_gridfs(id)
            obj = {
                'id': str(id),
                'filename': read_file.filename,
                'content_type': read_file.content_type,
                'length': read_file.length
            }
        except Exception as e:
            logger.debug('--store_gridfs--' + str(e))
    return obj
예제 #30
0
def convert_image_from_json(data):
    obj = {}

    id = data.get('id')
    if id and ObjectId.is_valid(id):
        obj['_id'] = ObjectId(id)

    user_id = data.get('user_id')
    if user_id and ObjectId.is_valid(user_id):
        obj['user_id'] = ObjectId(user_id)

    kind = data.get('kind')
    if kind:
        obj['kind'] = kind

    name = data.get('name')
    if name:
        obj['name'] = name

    template_id = data.get('template_id')
    if template_id and ObjectId.is_valid(template_id):
        obj['template_id'] = ObjectId(template_id)

    regions = data.get('regions')
    if regions:
        obj['regions'] = regions

    storage_id = data.get('storage_id')
    if storage_id and ObjectId.is_valid(storage_id):
        obj['storage_id'] = ObjectId(storage_id)

    filename = data.get('filename')
    if filename:
        obj['filename'] = filename

    status = data.get('status')
    if status:
        obj['status'] = status

    logger.debug('--obj--' + str(obj))
    return obj