def check_cache(code, plugin_name): """ check meta info cache :param code: verify code :param plugin_name: plugin name :return: meta data cache list """ lock = threading.Lock() meta_info_cache = [] lock.acquire() try: search_query = { 'code': code, 'plugin_name': plugin_name } collection = get_collection('meta_cache') meta_infos = collection.find(search_query) if meta_infos is not None: for meta_info in meta_infos: meta_info.get('meta_data').update({'cache_id': str(meta_info.get('_id'))}) meta_info.pop('_id') meta_info_cache.append(meta_info['meta_data']) finally: lock.release() return meta_info_cache
def get_user_by_name(name): """ get user by name """ collection = get_collection("user") user_info = collection.find_one({"name": name}) return user_info
def delete_user_setting(username): """ delete user setting : param username """ collection = get_collection("modules_alert") collection.delete_one({"user": username})
def libraries_detail(user_name, libraries): """ get user libraries settings :param libraries: search libraries :param user_name: user name :return: list settings """ collection = get_collection('user_libraries') query = {"user_name": user_name, "libraries": libraries} settings = collection.find_one(query) all_plugin = get_all_plugin_name() if settings: result = { 'libraries': settings.get('libraries'), 'active': settings.get('active'), 'disable': list( set(all_plugin.split(',')).difference( set(settings.get('active')))) } return result return {'libraries': '', 'active': [], 'disable': all_plugin.split(',')}
def get_all_modules_setting(): """ get all modules settings :return: settings in mongo list """ collection = get_collection('modules_alert') settings = collection.find() return settings
def get_all_modules_setting_enable(): """ get modules settings which is enabled :return: settings in mongo list """ collection = get_collection('modules_alert') settings = collection.find({'setting.enable': True}) return settings
def set_node_info(node_info): """ set node info : param node_info : node info """ collection = get_collection('cluster_info') collection.delete_many({}) collection.insert(node_info)
def get_cache_by_id(cache_id): """ get cache by cache id :param cache_id: cache id :return: cache """ collection = get_collection('meta_cache') cache = collection.find_one({"_id": ObjectId(cache_id)}) return cache
def get_setting_by_username(username): """ get user modules alert setting by username : param username :return: settings in list """ collection = get_collection("modules_alert") setting = collection.find_one({'user': username}) return setting
def del_plugin_setting_by_username(name): """ del setting by username :param name username :return: """ collection = get_collection("plugin_setting") collection.delete_one({"name": name}) return True
def del_user_by_username(name): """ del user by username :param name username :return: """ collection = get_collection("user") collection.delete_one({"name": name}) return True
def get_user_by_token(token): """ use plugin token to get use info :param token: plugin token :return: user info """ collection = get_collection("user") user_info = collection.find_one({"token": token}) return user_info
def del_libraries_setting_by_username(name): """ del setting by username :param name username :return: """ collection = get_collection("user_libraries") collection.delete_many({"user_name": name}) return True
def get_password_from_db(user_info): """ get user password from database :param user_info: user info :return: password """ collection = get_collection("user") user_from_db = collection.find_one({'name': user_info['name']}) return user_from_db.get('password')
def del_libraries_settings(user_name, settings): """ delete user libraries settings :param settings: list settings :param user_name: user name :return: """ collection = get_collection("user_libraries") query = {'_id': ObjectId(settings['id']), 'user_name': user_name} collection.delete_one(query)
def get_user(name, password): """ get user by name and password :param name: user name :param password: password :return: user info """ collection = get_collection("user") user_info = collection.find_one({"name": name, "password": get_password(name, password)}) return user_info
def get_node_info(): """ get node info :return: current node info """ collection = get_collection('cluster_info') node_info = collection.find_one({}) if node_info: node_info.pop('_id') check_node_slave_status(node_info) return node_info
def get_all_user_info(): """ get all user info :return: user info in list """ collection = get_collection("user") user_infos = list(collection.find()) for user_info in user_infos: user_info.pop('_id') user_info.pop('password') return user_infos
def init_data(): """ init data (insert user admin if no exist) :return: """ collection = get_collection('user') if collection.find_one({"name": "admin"}) is None: admin_user = app_config["ADMIN"] admin_user["password"] = sha1_encode(admin_user["name"] + admin_user["password"]) admin_user["roles"] = "admin" collection.insert(admin_user)
def update_password(user_info): """ update password :param user_info: :return: """ collection = get_collection("user") if user_info.get('password') and user_info.get('password') != '': user_info['password'] = get_password(user_info['name'], user_info['password']) log('info', 'password saved') return update(user_info)
def update(user_info): """ update user info in database :param user_info: dict user info :return: insert or update """ collection = get_collection("user") if collection.find_one({"name": user_info['name']}): collection.update({"name": user_info['name']}, user_info) return False else: collection.insert(user_info) return True
def save_user_setting(username, data): """ save (insert or update) user modules alert setting : param username : param data user modules alert setting :return: """ collection = get_collection("modules_alert") setting = collection.find_one({'user': username}) data = {'user': username, 'setting': data} if setting: collection.update({'user': username}, data) else: collection.insert(data)
def get_user_plugin_setting(plugin_name, user_info): """ get user plugin setting from database :param plugin_name: str plugin name :param user_info: dict user info :return: dict user plugin setting """ collection = get_collection("plugin_setting") query = {'name': user_info["name"], 'plugin': plugin_name} plugin_setting = collection.find_one(query) if plugin_setting: return plugin_setting.get('data') else: return None
def save_libraries_settings(user_name, settings): """ save user libraries settings :param settings: list settings :param user_name: user name :return: """ collection = get_collection("user_libraries") if settings.get('id'): query = { '_id': ObjectId(settings['id']), } collection.delete_one(query) settings['user_name'] = user_name collection.insert(settings)
def user_sign_up(user_info): """ user sign up :param user_info: dict user info :return: """ collection = get_collection("user") if collection.find_one({'name': user_info.get('name')}): return False collection.insert( { 'name': user_info.get('name'), 'password': get_password(user_info.get('name'), user_info.get('password')), 'email': user_info.get('email'), 'roles': 'poweruser' } ) return True
def check_item_exist(item, plugin_name): """ chech caceh by hash code :param item: use this item to generate hash code :param plugin_name: plugin name :return: meta data cache """ search_query = { 'hash_code': make_hash(item), 'plugin_name': plugin_name } collection = get_collection('meta_cache') meta_info = collection.find_one(search_query) if meta_info: meta_info.get('meta_data').update({'cache_id': str(meta_info.get('_id'))}) meta_info.pop('_id') return meta_info['meta_data'] return None
def check_cache(code, plugin_name): """ check meta info cache :param code: verify code :param plugin_name: plugin name :return: meta data """ lock = threading.Lock() meta_info = None lock.acquire() try: search_query = {'code': code, 'plugin_name': plugin_name} collection = get_collection('meta_cache') meta_info = collection.find_one(search_query) if meta_info is not None: meta_info = meta_info['meta_data'] finally: lock.release() return meta_info
def set_cache(code, meta_data, plugin_name): """ set meta info cache :param code: verify code :param meta_data: meta_data :param plugin_name: plugin name :return: """ lock = threading.Lock() lock.acquire() cache_data = {} try: cache_data.update({'code': code}) cache_data.update({'hash_code': make_hash(meta_data)}) cache_data.update({'plugin_name': plugin_name}) cache_data.update({'meta_data': meta_data}) collection = get_collection('meta_cache') return str(collection.insert(cache_data)) finally: lock.release()
def set_cache(code, meta_data, plugin_name): """ set meta info cache :param code: verify code :param plugin_name: plugin name :return: """ lock = threading.Lock() lock.acquire() cache_data = {} try: cache_data.update({'code': code}) cache_data.update({'plugin_name': plugin_name}) cache_data.update({'meta_data': meta_data}) search_query = {'code': code, 'plugin_name': plugin_name} collection = get_collection('meta_cache') meta_info = collection.find_one(search_query) if meta_info is None: collection.insert(cache_data) finally: lock.release()
def save_plugin_setting(plugin_name, plugin_setting, user_info): """ save(insert or update) user plugin setting to database :param plugin_name: str plugin name :param plugin_setting: dict user plugin setting :param user_info: dict user info :return: """ collection = get_collection("plugin_setting") query = { 'name': user_info["name"], 'plugin': plugin_name, } setting = { 'name': user_info["name"], 'plugin': plugin_name, 'data': plugin_setting, } if collection.find_one(query): collection.update(query, setting) else: collection.insert(setting)