Ejemplo n.º 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
Ejemplo n.º 2
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