def read_gridfs(id): file = None try: file = gfs.get(id) except Exception as e: logger.debug('--read_gridfs--' + str(e)) return file
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
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
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
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
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
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
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
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
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
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
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)
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
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
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)
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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