Exemple #1
0
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
Exemple #2
0
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
Exemple #3
0
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
Exemple #4
0
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
Exemple #5
0
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
Exemple #6
0
def log_message(format, message_dict):
    """
	Function to log a message to the database
	Inputs: message format, dictionary of message variables
	Output: Success message
	Caveats: Variables replace {} areas within message formats
	"""
    message = _message_format_[format]
    for key in message_dict:
        if "{" + key + "}" in message:
            message = message.replace("{" + key + "}", message_dict[key])
    try:
        session.add(TableEntities.Log(message=message))
        session.commit()
    except exc.SQLAlchemyError as Error:
        print(Error)
        print("Something went wrong. <Log add>")
        return False
    return "Success"
Exemple #7
0
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
Exemple #8
0
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
Exemple #9
0
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