def add_record(request): """ Function simulates the add new element into Cache Tree. INPUT POST parameters: 1) [optional] data -- current content of Cache Tree in JSON format; 2) [optional] parent_xpath -- full path of parent element where we'll add a child; 3) [mandatory] parent_xpath -- full path of parent element where we'll add a child. :returns updated Cache Tree in JSON format. """ data = get_str_param(request, 'data') parent_key = get_str_param(request, 'parent_key') tag_name = get_str_param(request, 'tag_name') logger.info("Request received:\n tag = {}; \nparent_key={};\nCurrent Cache data: {} " .format(tag_name, parent_key, data)) cache_tree = CacheTree(data) logger.info("Cache tree has been built.") if tag_name != "": cache_tree.add_new_node_by_key(parent_key, tag_name) logger.info("New note {} has been added.".format(parent_key)) else: logger.info("New note {} has not been added cuz tag name is blank.".format(parent_key)) tree_json = cache_tree.get_json() logger.debug("Response JSON has been built: {}".format(tree_json)) return send_json(tree_json)
def cache_after_save(request): """ Function simply unmark edit and add nodes. """ cache_data = get_str_param(request, 'data') cache_tree = CacheTree(cache_data) logger.debug("Save cache request received:\n Current Cache data: {} ".format(cache_data)) cache_tree.update_after_save() tree_json = cache_tree.get_json() logger.debug("Response JSON has been built: {}".format(tree_json)) return send_json(tree_json)
def get_db_content(request): """ Function which simply queries the DB Tree content. If POST data parameter is blank then it build default DB. Otherwise function builds DB Tree from JSON. :returns the DB Tree content in JSON format. """ #data = get_str_param(request, 'data') #logger.info("Request received:\n data: {}".format(data)) #if data == "": tree = DBTree("cfg/db_content.xml") #else: # tree = DBTree("", data) tree_json = tree.get_json() return send_json(tree_json)
def save_cache_changes(request): """ Function simulates the save Cache Tree changes into DB Tree. INPUT POST parameters: 1) [mandatory] cache_data -- current content of Cache Tree in JSON format; 2) [mandatory] db_data -- current content of DB Tree in JSON format. :returns updated DB Tree in JSON format. """ cache_data = get_str_param(request, 'cache_data') db_data = get_str_param(request, 'db_data') logger.debug("Save request received:\n Current DB data={};\nCurrent Cache data: {} ".format(db_data, cache_data)) db_tree = DBTree("", db_data) cache_tree = CacheTree(cache_data) cache_tree.save_by_key(db_tree) logger.info("Cache Tree changes has been saved.") tree_json = db_tree.get_json() logger.debug("Response JSON has been built: {}".format(tree_json)) return send_json(tree_json)
def remove_record(request): """ Function simulates the 'remove' element from Cache Tree. NOTE: it doesn't remove element from Cache Tree it just marks it and recursively all children as deleted nodes. INPUT POST parameters: 1) [optional] data -- current content of Cache Tree in JSON format; 2) [optional] xpath -- full path of element to be deleted; 3) [mandatory] parent_xpath -- full path of parent element where we'll add a child. :returns updated Cache Tree in JSON format. """ data = get_str_param(request, 'data') key = get_str_param(request, 'key') logger.info("Remove request received:\n xpath={};\nCurrent Cache data: {} ".format(key, data)) cache_tree = CacheTree(data) logger.info("Cache tree has been built.") cache_tree.delete_node_by_key(key) logger.info("Note {} has been removed.".format(key)) tree_json = cache_tree.get_json() logger.debug("Response JSON has been built: {}".format(tree_json)) return send_json(tree_json)
def cache_record(request): """ Function simulates the DB record caching process. INPUT POST parameters: 1) [optional] data -- current content of Cache Tree in JSON format; 2) [mandatory] xpath -- full path of caching element. :returns updated Cache Tree in JSON format. """ data = get_str_param(request, 'data') title = get_str_param(request, 'title') #xpath = get_str_param(request, 'xpath') key = get_str_param(request, 'key') parent_key = get_str_param(request, 'parent_key') logger.info("Request received:\n xpath = {}; title: {}; parent_key: {}; \nCurrent Cache data: {} ".format(key, title, parent_key, data)) cache_tree = CacheTree(data) logger.info("Cache tree has been built.") cache_tree.cache_node_by_key(key, parent_key, title) logger.info("Note {} has been inserted.".format(key)) tree_json = cache_tree.get_json() logger.debug("Response JSON has been built: {}".format(tree_json)) return send_json(tree_json)
def get_cache_content(request): """ Stub function. For now it just retrieve empty JSON. Used to clear Cache Tree. """ return send_json('[]')