def add_folder(): params = request.get_json() if not params or not params.get('id') or not params.get('folder'): return jsonify({ 'message': 'illegal param', 'code': 104000, }), 400 record_id = params.get('id') folder = params.get('folder') parent = params.get('parent') book_id = params.get('book_id') parent = parent if parent != '.' else '/' parent_path = None if parent != '/': parent_record = Playbook.find_one({ '_id': ObjectId(record_id), 'is_dir': True }) if not parent_record: return jsonify({ 'message': 'invalid params', 'code': 104001, }), 400 parent_path = parent_record.get('path') file_path = os.path.join(parent, folder) record = { 'path': file_path, 'book_id': book_id, 'parent': parent_path, 'is_dir': True, 'content': '', 'is_edit': False, 'add_by': login_user.get('username'), 'created_at': int(time.time()), 'updated_at': datetime.datetime.now().isoformat(), } meta = get_meta(file_path) record.update(meta) record['additions'] = meta check = Playbook.find_one({'book_id': book_id, 'path': record['path']}) if check: additions = check.get('additions') or {} additions.update(meta) record['additions'] = additions Playbook.update_one({'_id': check['_id']}, {'$set': record}) else: Playbook.insert_one(record) return jsonify({ 'message': 'ok', 'code': 0, })
def install_book_from_dir(dirname, book_id): wk = Workspace() bucket = wk.import_book_from_dir(dirname, book_id) playbooks = Playbook.find({'book_id': book_id}) files = map(lambda i: i['path'], bucket) paths = map(lambda i: i['path'], playbooks) diff = list(set(paths) - set(files)) for item in diff: Playbook.delete_one({'book_id': book_id, 'path': item}) mapping = {} map(lambda i: {mapping['path']: i}, playbooks) for item in bucket: record = mapping.get(item['path']) if not record: Playbook.insert_one(item) continue if record['additions']: item['additions'].update(record['additions']) Playbook.update_one({'_id': record['_id']}, {'$set': item})
def check_playbook_node(node): """ loop check playbook parent node @param {dict} node """ parent_path = os.path.dirname(node['path']) record = Playbook.find_one({ 'book_id': node['book_id'], 'path': parent_path }) if not record: current = node.copy() current['path'] = parent_path current['parent'] = None if parent_path == '/' else os.path.dirname( parent_path) Playbook.insert_one(current) return check_playbook_node(current) where = { 'book_id': node['book_id'], 'path': node['path'], } return Playbook.update_one(where, node, upsert=True)
def upload_playbook(_id): files = request.files record = Book.find_by_id(_id) if not record: return jsonify({ "message": "book not found", "code": 104004, }), 400 if not files: return jsonify({ 'message': 'invalid files params', 'code': 104001 }), 400 file = files['file'] filename = file.filename.lstrip('/') path_list = filename.split('/') filename = '/'.join(path_list[1:]) filename = '/' + filename home_path, basename = os.path.split(filename) file_list = set(_make_path(filename)) for dirname in file_list: check = Playbook.find_one({ 'book_id': _id, 'path': dirname, }) if not check: parent_path, name = os.path.split(dirname) parent_path = parent_path if parent_path != '/' else None parent = { 'path': dirname, 'is_dir': True, 'is_edit': False, 'book_id': _id, 'parent': parent_path, 'name': name, 'created_at': time.time(), } meta = get_meta(dirname) parent.update(meta) parent['additions'] = meta Playbook.insert_one(parent) data = { 'path': filename, 'is_dir': False, 'parent': home_path or None, 'book_id': _id } can_edit = is_edit(file) if not can_edit: file_id = db.save_file(filename=filename, fileobj=file) data['file_id'] = file_id else: content = file.stream.read() content = content.decode('utf-8') data['is_encrypt'] = Vault.is_encrypted(content) if data['is_encrypt']: # @todo vault password vault = Vault() data['content'] = vault.encrypt_string(content) data['md5'] = md5(content) else: data['content'] = content data['md5'] = md5(content) meta = get_meta(data['path']) data.update(meta) data['additions'] = meta data['is_edit'] = can_edit data['created_at'] = time.time() data['updated_at'] = time.time() Playbook.update_one({ 'path': filename, 'book_id': _id }, {'$set': data}, upsert=True) return jsonify({ "message": "ok", "code": 0, })
def edit_file(_id): """ edit playbook file @todo add lock :param _id: ObjectId string :return: json """ params = request.get_json() or request.form if not params: return jsonify({ 'message': 'invalid params', 'code': 154000, }), 400 edit_type = params.get('type') if edit_type == 'upload': return upload_file(_id) name = params.get('name') role = params.get('role') description = params.get('description') status = params.get('status', 1) maintainer = params.get('maintainer', []) can_edit = params.get('is_edit') is_dir = params.get('is_dir') is_encrypt = params.get('is_encrypt') folder = params.get('folder') register = params.get('register') content = params.get('content') record = Playbook.find_by_id(_id) if not record: return jsonify({ 'message': 'record not found', 'code': 154041, }), 404 data = { 'status': status, } if name: data['name'] = name if role: data['role'] = role if folder: data['folder'] = folder if content: data['content'] = content data['md5'] = md5(content) if description: data['description'] = description if maintainer: data['maintainer'] = maintainer if can_edit is not None: data['is_edit'] = bool(is_edit) data['is_encrypt'] = bool(is_encrypt) if is_dir is not None: data['is_dir'] = bool(is_dir) if register: c_ids = map(lambda i: ObjectId(i), register) where = {'_id': {'$in': c_ids}} register_config = Configuration.find(where) if not register_config: return jsonify({ 'message': 'invalid register config', 'code': 154042, }), 404 data['register'] = register Playbook.update_one({'_id': ObjectId(_id)}, {'$set': data}, upsert=True) data['_id'] = _id book = Book.find_one({'_id': ObjectId(record['book_id'])}) wk = Workspace() wk.write_book_file(book.get('name'), record) return jsonify({ 'message': 'ok', 'code': 0, 'data': data, })