def change_label(label_id, label_dict, torn): """ Function to change a label record's data Inputs: Label ID; Dictionary of label values; Tornado object Output: True if operation was successful, False if the operation was not Caveats: Determine if the label ID exists and whether the text exists as well as parent label """ if label_id_exists(label_id) == False: torn.set_status(404) torn.write({"message": "Label does not exist"}) return False if (label_dict["label_text"] is not None): if (label_exists(label_dict["label_text"])): torn.set_status(404) torn.write({"message": "New label text already exists"}) return False try: session.execute( update( TableEntities.Label).where(TableEntities.Label.label_id == int( label_id)).values(label_dict)) session.commit() except exc.SQLAlchemyError as Error: flhandler.log_error_to_file(Error) torn.set_status(500) return False return True
def delete_label(label_id, torn): """ Function to delete a label record from the database Inputs: Label ID; Tornado object to write any messages Output: True if operation was successful, False if the operation was not Caveats: Check if the label id exists. Nullify label ID in any other tables """ if label_id_exists(label_id) == False: torn.set_status(404) torn.write({"message": "Label does not exist"}) return False null_dict = {"label_id": None} try: # Nullify label_ids in other tables session.execute( update(TableEntities.Nodes).where(TableEntities.Nodes.label_id == int(label_id)).values(null_dict)) session.commit() # Delete the label session.execute( delete(TableEntities.Label).where( TableEntities.Label.label_id == int(label_id))) session.commit() except exc.SQLAlchemyError as Error: torn.set_status(500) flhandler.log_error_to_file(Error) return False return True
def delete_type(type_id, torn): """ Function to delete a node type record from the database table Inputs: Type ID; Tornado object to write any messages Output: True if operation was successful, False if the operation was not Caveats: Delete all depandancies of the node type """ if type_id_exists(type_id) == False: torn.set_status(404) torn.write({"message": "Node type ID does not exist"}) return False try: import utilities.node as nodeutil #Delete all objects dependant on the node type that will be deleted #Deleting a node will remove the link and metadata as well nodeutil.delete_node_with_type(type_id, torn) #Delete the label session.execute( delete(TableEntities.NodeType).where(TableEntities.NodeType.type_id == int(type_id)) ) session.commit() except exc.SQLAlchemyError as Error: torn.set_status(500) flhandler.log_error_to_file(Error) return False return True
def change_type(type_id, type_dict, torn): """ Function to change a node type's information that is stored in the database Inputs: Type ID; Dictionary of columns and values that will be ammended to the record; Tornado object Output: True if operation was successful, False if the operation was not Caveats: None """ if type_id_exists(type_id) == False: torn.set_status(404) torn.write({"message": "Node Type does not exist"}) return False if (type_dict["type"] is None): torn.set_status(400) torn.write({"message": "Empty update statement"}) return False if (type_exists(type_dict["type"])): torn.set_status(400) torn.write({"message": "New node type already exists"}) return False try: session.execute( update(TableEntities.NodeType).where(TableEntities.NodeType.type_id == int(type_id)) .values(type_dict) ) session.commit() except exc.SQLAlchemyError as Error: torn.set_status(500) flhandler.log_error_to_file(Error) return False return True
def create_user(user_dict, torn): """ Function to create a user record and to be inserted into the database Inputs: Dictionairy of user data; Tornado object to write messages Output: None if user error occured; True if the operating was successful Caveats: Password is hashed using pbkdf sha256 with 48k rounds and salt size of 64 bits """ if user_exists(user_dict["username"]): torn.set_status(400) torn.write({"message": "Username already exists"}) return False if "privilege" not in user_dict: user_dict["privilege"] = 0 user_dict["username"] = user_dict["username"].lower() #salt_size 64 bits #48k rounds user_dict["password"] = pbkdf2_sha256.hash(user_dict["password"], salt_size=64, rounds=48000) new_user = TableEntities.User(username=user_dict["username"], password=user_dict["password"], privilege=user_dict["privilege"]) try: session.add(new_user) session.commit() except exc.SQLAlchemyError as Error: torn.set_status(500) flhandler.log_error_to_file(Error) return False #sanitize variables del user_dict return True
def delete_node(node_id, torn): """ Function to delete a node record from the database table Inputs: Node ID; Tornado object to write any messages Output: True if operation was successful, False if the operation was not Caveats: Check if the node id already exists """ if node_id_exists(node_id) == False: torn.set_status(404) torn.write({"message":"Node does not exist"}) return False #Import module in function. Not allowed to cross reference import utilities.link as linkutil #Delete all links that contain the node id linkutil.delete_link_with_node(node_id) import utilities.meta as MetaUtil MetaUtil.delete_metadata_with_node(node_id) #Delete the node try: session.execute( delete(TableEntities.Nodes).where(TableEntities.Nodes.node_id == int(node_id)) ) session.commit() except exc.SQLAlchemyError as Error: torn.set_status(500) flhandler.log_error_to_file(Error) return False return True
def delete_link_with_node(node_id): """ Function to delete links with a given node Inputs: Node ID Output: True if operation was successful, False if the operation was not Caveats: Delete metadata of links """ import utilities.meta as MetaUtil links = session.query(TableEntities.Links).filter( (TableEntities.Links.node_id_1 == int(node_id)) | (TableEntities.Links.node_id_2 == int(node_id))).all() try: for link in links: MetaUtil.delete_metadata_with_link(link.link_id) session.execute( delete(TableEntities.Links).where( (TableEntities.Links.node_id_1 == int(node_id)) | (TableEntities.Links.node_id_2 == int(node_id)))) session.commit() except exc.SQLAlchemyError as Error: torn.set_status(500) flhandler.log_error_to_file(Error) return False return True
def delete_node_with_type(type_id, torn): """ Function to delete a node which depends on a node type Inputs: type; Tornado object to write any messages Output: True if operation was successful, False if the operation was not Caveats: Delete links and metadata alongside """ node_entries = session.query(TableEntities.Nodes).filter(TableEntities.Nodes.type_id == int(type_id)).all() #Import module in function. Not allowed to cross reference import utilities.link as linkutil import utilities.meta as MetaUtil for node in node_entries: linkutil.delete_link_with_node(node.node_id) MetaUtil.delete_metadata_with_node(node.node_id) try: session.execute( delete(TableEntities.Nodes).where(TableEntities.Nodes.node_id == int(node.node_id)) ) session.commit() except exc.SQLAlchemyError as Error: torn.set_status(500) flhandler.log_error_to_file(Error) return False return True
def change_relationship(relationship_id, relationship_dict, torn): """ Function to change link record data Inputs: Link ID; Dictionary of link data to be ammended; Tornado object to write messages Output: True if operation was successful, False if the operation was not Caveats: Determine if node and other FK objects needed to be changed exist; Determine if link relation already exists """ if relationship_id_exists(relationship_id) == False: torn.set_status(404) torn.write({"message": "Relationship does not exists"}) return False if relationship_exists(relationship_dict["message"]): torn.set_status(400) torn.write({"message": "New relationship message exists"}) return False try: session.execute( update(TableEntities.Relationship).where( TableEntities.Relationship.relationship_id == int(relationship_id)) .values(relationship_dict) ) session.commit() except exc.SQLAlchemyError as Error: flhandler.log_error_to_file(Error) return False return True
def delete_relationship(relationship_id, torn): """ Function to delete relationship Inputs: Relationship ID Output: True if operation was successful, False if the operation was not Caveats: Nullify relation ID columns in other tables """ if relationship_id_exists(relationship_id) == False: torn.set_status(404) torn.write({"message": "Relationship id does not exist"}) return False null_dict = {"relationship_id": None} # Create SQL statements to set relationship ID in FK _table_s to NULL try: # Nullify relationship id's in links table session.execute( update(TableEntities.Links).where( TableEntities.Links.relationship_id == int(relationship_id)) .values(null_dict) ) session.commit() # Delete relationship session.execute( delete(TableEntities.Relationship).where( TableEntities.Relationship.relationship_id == int(relationship_id)) ) session.commit() except exc.SQLAlchemyError as Error: torn.set_status(500) flhandler.log_error_to_file(Error) return False return True
def create_category(metadata_dict, torn): """ Function to create metadata category for a specified node Inputs: Dictionary of metadata; Tornado object to write messages Output: True if insert operation was successful, False if the operation was not Caveats: Check if the node and if the category for that node already exists """ if "node_id" in metadata_dict: if nodeutil.node_id_exists(metadata_dict["node_id"]) == False: torn.set_status(404) torn.write({"message": "Node does not exist"}) return False else: if linkutil.link_exists(metadata_dict["link_id"]) == False: torn.set_status(404) torn.write({"message": "Link does not exist"}) return False if "node_id" in metadata_dict: if category_node_exists(metadata_dict["category"], metadata_dict["node_id"]): torn.set_status(400) torn.write({ "message": "Metadata category for the specified node already exists" }) return False else: if category_node_exists(metadata_dict["category"], metadata_dict["link_id"]): torn.set_status(400) torn.write({ "message": "Metadata category for the specified link already exists" }) return False try: if "node_id" in metadata_dict: session.add( TableEntities.Metadata(node_id=int(metadata_dict["node_id"]), category=metadata_dict["category"], data=metadata_dict["data"])) session.commit() else: session.add( TableEntities.Metadata(link_id=int(metadata_dict["link_id"]), category=metadata_dict["category"], data=metadata_dict["data"])) session.commit() except exc.SQLAlchemyError as Error: torn.set_status(500) flhandler.log_error_to_file(Error) return False meta_id = session.query(TableEntities.Metadata).order_by( TableEntities.Metadata.meta_id.desc()).first() return meta_id.meta_id
def create_link(link_dict, torn): """ Function to create a link Inputs: Link dictionary; Tornado Object Output: Newly created link_id if operation was successful, False if the operation was not Caveats: Check if both nodes and any FK objects exists, links already exists and if the link is to and from the same node """ if check_nodes_exist(link_dict["node_id_1"], link_dict["node_id_2"]) == False: torn.write({"message": "One of nodes in the the link does not exist"}) torn.set_status(404) return False if link_dict["node_id_1"] == link_dict["node_id_2"]: torn.write( {"message": "Links from and to the same node are not allowed"}) torn.set_status(400) return False if link_relation_exists(link_dict["node_id_1"], link_dict["node_id_2"]): torn.write({"message": "Link between the two nodes already exists"}) torn.set_status(400) return False if viewutil.view_id_exists(link_dict["view_id"]) == False: torn.write({"message": "View ID does not exist"}) torn.set_status(404) return False link = TableEntities.Links(node_id_1=int(link_dict["node_id_1"]), node_id_2=int(link_dict["node_id_2"]), view_id=int(link_dict["view_id"])) if "relationship_id" in link_dict: if relationshiputil.relationship_id_exists( link_dict["relationship_id"]) == False: torn.write({"message": "Relationship does not exist"}) torn.set_status(404) return False else: link.relationship_id = int(link_dict["relationship_id"]) try: session.add(link) session.commit() except exc.SQLAlchemyError as Error: flhandler.log_error_to_file(Error) torn.set_status(500) return False link_id = session.query(TableEntities.Links).order_by( TableEntities.Links.link_id.desc()).first() return link_id.link_id
def delete_metadata_with_node(node_id): """ Function to delete all metadata records that belong to a single node Inputs: Node ID Output: None Caveats: Returns none as this function is seen as "best effort" """ try: session.execute( delete(TableEntities.Metadata).where( TableEntities.Metadata.node_id == int(node_id))) session.commit() return True except exc.SQLAlchemyError as Error: flhandler.log_error_to_file(Error) return False
def change_node(node_id, node_dict, torn): """ Function to change a node's information that is stored in the database Inputs: Node ID; Dictionary of columns and values that will be ammended to the record; Tornado object Output: True if operation was successful, False if the operation was not Caveats: Check if the node and any FK objects exists """ if node_id_exists(node_id) == False: torn.set_status(404) torn.write({'message': "Node does not exist"}) return False if "type" in node_dict: if typeutil.type_id_exists(node_dict["type"]) == False: torn.set_status(404) torn.write({'message': "Type ID does not exist"}) return False if "label_id" in node_dict: if labelutil.label_id_exists(node_dict["label_id"]) == False: torn.set_status(404) torn.write({"message": "Label id does not exist"}) return False if "view_id" in node_dict: if viewutil.view_id_exists(node_dict["view_id"]) == False: torn.set_status(404) torn.write({'message': "View ID does not exist"}) return False try: session.execute( update(TableEntities.Nodes).where(TableEntities.Nodes.node_id == int(node_id)).values(node_dict) ) session.commit() if("type_id" in node_dict): type_name = typeutil.get_type(node_dict["type_id"])["data"][0]["type"] import utilities.meta as metadatautil metadata_id = metadatautil.get_metadata_id("Type", node_id) metadata_dict = {"node_id": node_id, "category":"Type", "data": type_name} metadatautil.change_metadata_internal(metadata_id, metadata_dict) except exc.SQLAlchemyError as Error: torn.set_status(500) flhandler.log_error_to_file(Error) return False return True
def create_type_category(metadata_dict): """ Function to create metadata category called "Type" for a specified node Inputs: Dictionary of metadata Output: True if insert operation was successful, False if the operation was not Caveats: None """ try: session.add( TableEntities.Metadata(node_id=int(metadata_dict["node_id"]), category=metadata_dict["category"], data=metadata_dict["data"])) session.commit() return True except exc.SQLAlchemyError as Error: flhandler.log_error_to_file(Error) return False
def create_node(node_dict, torn): """ Function to create a node in the database Inputs: Dictionary of node data, Tornado object to write data Output: True if operation was successful, False if the operation was not Caveats: Check if the node type already exists """ if typeutil.type_id_exists(node_dict["type_id"]) == False: torn.set_status(404) torn.write({'message': "Type ID does not exist"}) return False if viewutil.view_id_exists(node_dict["view_id"]) == False: torn.set_status(404) torn.write({'message': "View ID does not exist"}) return False node = TableEntities.Nodes(type_id=node_dict["type_id"], view_id=node_dict["view_id"]) if "label_id" in node_dict: if labelutil.label_id_exists(node_dict["label_id"]) == False: torn.set_status(404) torn.write({"message": "Label ID does not exist"}) return False else: node.label_id = int(node_dict["label_id"]) if "icon" in node_dict: node.icon = node_dict["icon"] try: session.add(node) session.commit() except exc.SQLAlchemyError as Error: torn.set_status(500) flhandler.log_error_to_file(Error) return False node_id = session.query(TableEntities.Nodes).order_by(TableEntities.Nodes.node_id.desc()).first() #create metadata category for type of node type_name = typeutil.get_type(node_dict["type_id"])["data"][0]["type"] import utilities.meta as metadatautil metadata_dict = {"node_id": int(node_id.node_id), "category":"Type", "data": type_name} metadatautil.create_type_category(metadata_dict) return node_id.node_id
def change_password(user_id, new_password): """ Function to allow a user to change it's password Inputs: user_id; user's new password Output: False if user error occured; True if the operation was successful Caveats: Password is hashed using pbkdf sha256 with 48k rounds and salt size of 64 bits """ password = pbkdf2_sha256.hash(new_password, salt_size=64, rounds=48000) try: session.execute( update(TableEntities.User).where( TableEntities.User.user_id == int(user_id)).values( {"password": password})) session.commit() return True except exc.SQLAlchemyError as Error: flhandler.log_error_to_file(Error) return False
def change_metadata_internal(metadata_id, metadata_dict): """ Function to change a metadata record from the database Inputs: Metadata ID; Dictionary of metadata Output: True if operation was successful, False if the operation was not Caveats: None """ try: session.execute( update(TableEntities.Metadata).where( TableEntities.Metadata.meta_id == int(metadata_id)).values( metadata_dict)) session.commit() return True except exc.SQLAlchemyError as Error: torn.set_status(500) flhandler.log_error_to_file(Error) return False
def update_link_with_relationship(relationship_id): """ Function to update links with a given relationship id Inputs: Relationship ID Output: True if operation was successful, False if the operation was not Caveats: None """ try: session.execute( update(TableEntities.Links).where( TableEntities.Links.relationship_id == int( relationship_id)).values({"relationship_id": None})) session.commit() return True except exc.SQLAlchemyError as Error: torn.set_status(500) flhandler.log_error_to_file(Error) return False return True
def create_type(type_dict, torn): """ Function to create a node type Inputs: Dictionary of node type data, Tornado object to write data Output: True if operation was successful, False if the operation was not Caveats: Check if the node type already exists """ if type_exists(type_dict["type"]): torn.set_status(400) torn.write({'message': "Node type already exists"}) return False try: session.add(TableEntities.NodeType(type=type_dict["type"])) session.commit() except exc.SQLAlchemyError as Error: torn.set_status(500) flhandler.log_error_to_file(Error) return False return True
def delete_metadata(metadata_id, torn): """ Function to delete a metadata record from the database Inputs: Metadata ID; Tornado object to write messages Output: True if operation was successful, False if the operation was not Caveats: Determine the existance of metadata record """ if metadata_exists(metadata_id) == False: torn.set_status(404) torn.write({"message": "Node does not exist"}) return False try: session.execute( delete(TableEntities.Metadata).where( TableEntities.Metadata.meta_id == int(metadata_id))) session.commit() except exc.SQLAlchemyError as Error: torn.set_status(500) flhandler.log_error_to_file(Error) return False return True
def create_label(label_dict, torn): """ Function to create a label record Inputs: Dictionary of label data, Tornado object to write data Output: True if operation was successful, False if the operation was not Caveats: Check if the label text already exists """ if label_exists(label_dict["label_text"]): torn.set_status(400) torn.write({'message': "Label text already exists"}) return False try: session.add(TableEntities.Label(label_text=label_dict["label_text"])) session.commit() except exc.SQLAlchemyError as Error: flhandler.log_error_to_file(Error) torn.set_status(500) return False return True
def create_relationship(relationship_dict, torn): """ Function to create a relationship Inputs: Relationship dictionary; Tornado Object Output: True if operation was successful, False if the operation was not Caveats: Check if relationship message already exists """ if relationship_exists(relationship_dict["message"]): torn.set_status(400) torn.write({"message": "Relationship message already exists"}) return False try: session.add(TableEntities.Relationship( message=relationship_dict["message"])) session.commit() except exc.SQLAlchemyError as Error: torn.set_status(500) flhandler.log_error_to_file(Error) return False relationship = session.query(TableEntities.Relationship).order_by(TableEntities.Relationship.relationship_id.desc()).first() return relationship.relationship_id
def delete_link(link_id, torn): """ Function to delete a link from the database Inputs: Link ID; Tornado object Output: True if operation was successful, False if the operation was not Caveats: Check if the node and any FK objects exists """ if link_exists(link_id) == False: torn.set_status(404) torn.write({"message": "Link does not exist"}) return False try: import utilities.meta as MetaUtil MetaUtil.delete_metadata_with_link(link_id) session.execute( delete(TableEntities.Links).where( TableEntities.Links.link_id == int(link_id))) session.commit() except exc.SQLAlchemyError as Error: torn.set_status(500) flhandler.log_error_to_file(Error) return False return True
def change_link(link_id, link_dict, torn): """ Function to change link record data Inputs: Link ID; Dictionary of link data to be ammended; Tornado object to write messages Output: True if operation was successful, False if the operation was not Caveats: Determine if node and other FK objects needed to be changed exist; Determine if link relation already exists """ if link_exists(link_id) == False: torn.set_status(400) torn.write({"message": "Link does not exist"}) return False check = True if "node_id_1" in link_dict or "node_id_2" in link_dict: #Get the node id that the user didn't submit if "node_id_1" in link_dict and "node_id_2" not in link_dict: node_ids = get_node_ids_in_link(link_id) link_dict["node_id_2"] = int(node_ids[1]) elif "node_id_2" in link_dict and "node_id_1" not in link_dict: node_ids = get_node_ids_in_link(link_id) link_dict["node_id_1"] = int(node_ids[0]) else: check = False if check == True and check_nodes_exist( link_dict["node_id_1"], link_dict["node_id_2"]) == False: torn.set_status(404) torn.write( {"message": "One of nodes in the the link does not exist"}) return False if link_dict["node_id_1"] == link_dict["node_id_2"]: torn.set_status(400) torn.write( {"message": "Links from and to the same node are not allowed"}) return False if link_relation_exists(link_dict["node_id_1"], link_dict["node_id_2"]): torn.set_status(400) torn.write( {"message": "Link between the two nodes already exists"}) return False if "view_id" in link_dict: if viewutil.view_id_exists(link_dict["view_id"]) == False: torn.set_status(404) torn.write({'message': "View ID does not exist"}) return False if "relationship_id" in link_dict: if relationshiputil.relationship_id_exists( link_dict["relationship_id"]) == False: torn.set_status(404) torn.write({'message': "Relationship ID does not exist"}) return False try: session.execute( update(TableEntities.Links).where( TableEntities.Links.link_id == int(link_id)).values(link_dict)) session.commit() except exc.SQLAlchemyError as Error: torn.set_status(500) flhandler.log_error_to_file(Error) return False return True