Commit 1524a6a8 authored by Celine Noirot's avatar Celine Noirot
Browse files

Handle create user id

parent 5dc412aa
......@@ -33,9 +33,11 @@ class NG6Workflow (Workflow):
def __init__(self, args={}, id=None, function= "process", parameters_section="parameters", add_run_params=True):
Workflow.__init__(self, args, id, function, parameters_section)
# admin login is required for all workflow
self.parameters.append(Parameter("admin_login", "Who is the project administrator", "--admin-login", required=True, type=str))
if add_run_params:
self.parameters.extend(self._add_run_parameters())
self.args = self._extend_and_format_args(self.parameters, args)
self.args = self._extend_and_format_args(self.parameters, args)
def _add_run_parameters(self):
orequired = []
......@@ -54,27 +56,35 @@ class NG6Workflow (Workflow):
self.project = None
self.runobj = None
# if starting from an existing project
if self.args.has_key("project_id"):
if self.args.has_key("project_id") and self.args["project_id"] is not None:
self.project = Project.get_from_id(self.args["project_id"])
self.metadata.append("project_id="+str(self.args["project_id"]))
# if user is not allowed to add data on project (is not admin)
if self.project is not None and not self.project.is_admin(self.args["admin_login"]):
raise ValueError( "The user login '" + self.args["admin_login"] + "' is not allowed to add data on project '" + self.project.name + "'." )
# if starting from a brand new project
elif self.args.has_key("project_name") and self.args.has_key("project_description") and self.args.has_key("admin_login"):
self.project = Project(self.args["project_name"], self.args["project_description"], self.args["admin_login"])
self.metadata.append("project_id="+str(self.project.id))
# if starting from an existing run
if self.args.has_key("run_id"):
if self.args.has_key("run_id") and self.args["run_id"] is not None:
if self.project is None :
project = Project.get_from_run_id(self.args["run_id"])
# if user is not allowed to add data on project (is not admin)
if not project.is_admin(self.args["admin_login"]):
raise ValueError( "The user login '" + self.args["admin_login"] + "' is not allowed to add data on project '" + project.name + "'." )
self.runobj = Run.get_from_id(self.args["run_id"])
self.metadata.append("run_id="+str(self.args["run_id"]))
# if starting from a brand new run
elif self.args.has_key("run_date") and self.args.has_key("run_name") and self.args.has_key("species") and self.args.has_key("data_nature") \
and self.args.has_key("type") and self.args.has_key("run_description") and self.args.has_key("sequencer") and self.args.has_key("project_id"):
# build the project
self.project = Project.get_from_id(self.args["project_id"])
# and the run
# build the run
self.runobj = Run(self.args["run_name"], self.args["run_date"], self.args["species"], self.args["data_nature"],
self.args["type"], self.args["run_description"], self.args["sequencer"])
self.runobj.admin_login=self.args["admin_login"]
# then add the run to the project
self.project.add_run(self.runobj)
# if it's a run with some index, let's write down description
......@@ -103,7 +113,10 @@ class NG6Workflow (Workflow):
# add parent and project/run information
cmpt_object.parent = parent
if addto == "project": cmpt_object.project = self.project
elif addto == "run": cmpt_object.run = self.runobj
elif addto == "run": cmpt_object.run = self.runobj
# link analysis with ots create user
cmpt_object.admin_login = self.args["admin_login"]
# add the component
self.components.append(cmpt_object)
# if this one require a dynamic pipeline, execute the first part
......
......@@ -51,16 +51,63 @@ class t3MySQLdb(object):
self.passwd = passwd
self.db = db
#--------------------------------------------------------------------
# User functions
def get_user_id(self, login):
"""
Select the user id from login
@return : tthe user id
@param login : the login name
"""
conn = connect(self.host, self.user, self.passwd, self.db)
curs = conn.cursor()
req = "SELECT uid FROM fe_users WHERE username = '" + str(login) + "'"
curs.execute(req)
try:
id = curs.fetchall()[0][0]
except:
raise Exception('t3MySQLdb', 'The login ' + login + ' does not exist.')
curs.close()
conn.close()
return id
def get_users (self, project_id, right):
"""
Select the users ids for a project and a level of rigth
@return : [user ids]
@param project_id : project id
@param rigth : right (string)
"""
conn = connect(self.host, self.user, self.passwd, self.db)
curs = conn.cursor()
req = "SELECT r.fe_user_id FROM fe_rights_levels l , fe_rights r WHERE \
l.right_level_label = '" + right + "' AND \
r.project_id=" + str(project_id) + " AND \
l.right_level_id = r.right_id;"
curs.execute(req)
users_ids=[]
try:
result_set = curs.fetchall()
for user_id in result_set:
users_ids.append(user_id[0])
logging.getLogger("t3MySQLdb.get_users").debug(str(users_ids))
except:
raise Exception('t3MySQLdb', 'Unable to retrieve right for level ' + right + ' and project '+ str(project_id))
return users_ids
#--------------------------------------------------------------------
# Project functions
def add_project(self, name, pdescription, admin_login, hidden=1):
def add_project(self, name, pdescription, cruser_login, hidden=1):
"""
Add to the database a new Project
@return : the Project ID added
@param name : the project name
@param pdescription : the project description
@param admin_login : the administrator login
@param cruser_login : the administrator login
@param hidden : is the project hidden
"""
exists = False
......@@ -74,15 +121,8 @@ class t3MySQLdb(object):
except:
crdate = time.mktime(datetime.date.today().timetuple())
if admin_login: # If the administrator login is specified
req = "SELECT uid FROM fe_users WHERE username = '" + str(admin_login) + "'"
curs.execute(req)
try:
admin_id = curs.fetchall()[0][0]
except:
raise Exception('t3MySQLdb', 'The administration login ' + admin_login + ' does not exist.')
if cruser_login: # If the administrator login is specified
admin_id=self.get_user_id(cruser_login)
req = "SELECT right_level_id FROM fe_rights_levels WHERE right_level_label = 'administrator'"
curs.execute(req)
try:
......@@ -90,7 +130,7 @@ class t3MySQLdb(object):
except:
raise Exception('t3MySQLdb', 'The administration value does not exist in the fe_rights_levels table.')
req = "INSERT INTO tx_nG6_project (pid, tstamp, crdate, description, name, hidden) VALUES ('" + str(self.cfg_reader.get_pid()) + "','" + str(crdate) + "','" + str(crdate) + "','" + pdescription.replace("'", "\'") + "','" + name.replace("'", "\'") + "','" + str(hidden) + "')"
req = "INSERT INTO tx_nG6_project (pid, tstamp, crdate, cruser_id, description, name, hidden) VALUES ('" + str(self.cfg_reader.get_pid()) + "','" + str(crdate) + "','" + str(crdate) + "','" + str(admin_id) + "','" + pdescription.replace("'", "\'") + "','" + name.replace("'", "\'") + "','" + str(hidden) + "')"
curs.execute(req)
project_id = conn.insert_id()
req = "INSERT INTO fe_rights (fe_user_id, project_id, right_id) VALUES ('" + str(admin_id) + "','" + str(project_id) + "','" + str(right_level_id) + "')"
......@@ -180,7 +220,7 @@ class t3MySQLdb(object):
# Run functions
def add_run(self, name, date, directory, species, data_nature,
type, nb_sequences, full_seq_size, description, sequencer, hidden=1):
type, nb_sequences, full_seq_size, description, sequencer, cruser_login, hidden=1):
"""
Add to the database a new run
@param name : the run name
......@@ -193,14 +233,17 @@ class t3MySQLdb(object):
@param full_seq_size : the full sequences size in the run
@param description : the run description
@param sequencer : the sequencer used
@param cruser_login : the create user login
@param hidden : is the analysis hidden
"""
cruser_id=self.get_user_id(cruser_login)
conn = connect(self.host, self.user, self.passwd, self.db)
curs = conn.cursor()
crdate = str(time.mktime(datetime.date.today().timetuple())).split(".")[0]
req = "INSERT INTO tx_nG6_run (pid,tstamp,crdate,name,date,directory,species,data_nature,"
req = "INSERT INTO tx_nG6_run (pid,tstamp,crdate,cruser_id,name,date,directory,species,data_nature,"
req += "type,nb_sequences,full_seq_size,description,hidden,sequencer) VALUES ('" + str(self.cfg_reader.get_pid())+ "','"
req += str(crdate) + "'," + "'" + str(crdate) + "','" + name.replace("'", "\'") + "','" + str(time.mktime(date.timetuple())) + "','"
req += str(crdate) + "'," + "'" + str(crdate) + "'," + str(cruser_id)+ ",'" + name.replace("'", "\'") + "','" + str(time.mktime(date.timetuple())) + "','"
req += directory + "','" + species.replace("'", "\'") + "','" + data_nature.replace("'", "\'") + "','" + type.replace("'", "\'") + "','"
req += str(nb_sequences) + "','" + str(full_seq_size) + "','" + str(description.replace("'", "\'")) + "','" + str(hidden) + "','"
req += sequencer.replace("'", "\'") + "')"
......@@ -235,6 +278,24 @@ class t3MySQLdb(object):
curs.close()
conn.close()
def select_project_id_from_run_id(self, run_id):
"""
Return the project id
@param run_id : the run id to select
@return: project_id : the project_id
"""
logging.getLogger("t3MySQLdb.select_project_id_from_run_id").debug("Selecting run id=" + str(run_id))
conn = connect(self.host, self.user, self.passwd, self.db)
curs = conn.cursor()
req = "SELECT distinct(project_id) FROM tx_nG6_project_run WHERE tx_nG6_project_run.run_id ='" + str(run_id) + "'"
logging.getLogger("t3MySQLdb.select_run").debug(req)
curs.execute(req)
res = curs.fetchall()[0]
curs.close()
conn.close()
logging.getLogger("t3MySQLdb.select_run").debug("Returning " + str(res[0]))
return res[0]
def select_run(self, run_id):
"""
Return the run infos
......@@ -384,34 +445,39 @@ class t3MySQLdb(object):
#--------------------------------------------------------------------
# Analyze functions
def add_analysis(self, aclass, name, description, date, directory, software, version, params, is_editable, hidden=1, parent_uid=0):
def add_analysis(self, aclass, name, description, cruser_login, date, directory, software, version, params, is_editable, hidden=1, parent_uid=0):
"""
Update an Analyze
@param aclass : the class of the analysis
@param name : the analysis name
@param description : the analysis description
@param date : the analysis date in datetime format
@param directory : the directory where analysis results are stored
@param software : the software name used for the analysis
@param version : the software version used for the analysis
@param params : the software params used
@param hidden : is the analysis hidden
@param parent_uid : the analysis parent uid
@param aclass : the class of the analysis
@param name : the analysis name
@param description : the analysis description
@param cruser_login : the user login
@param date : the analysis date in datetime format
@param directory : the directory where analysis results are stored
@param software : the software name used for the analysis
@param version : the software version used for the analysis
@param params : the software params used
@param hidden : is the analysis hidden
@param parent_uid : the analysis parent uid
@param is_editable : True if analysis has been added by an user
"""
cruser_id=self.get_user_id(cruser_login)
conn = connect(self.host, self.user, self.passwd, self.db)
curs = conn.cursor()
crdate = str(time.mktime(datetime.date.today().timetuple())).split(".")[0]
if is_editable : analysis_is_editable = 1
else : analysis_is_editable = 0
req = "INSERT INTO tx_nG6_analyze (pid,tstamp,crdate,class,name,description,date,directory,software,version,"
req = "INSERT INTO tx_nG6_analyze (pid,tstamp,crdate,cruser_id,class,name,description,date,directory,software,version,"
req += "hidden, params, is_editable, parent_uid) VALUES ('" + str(self.cfg_reader.get_pid())+ "','" + str(crdate) + "','"
req += str(crdate) + "','" + aclass + "','" + name.replace("'", "\'") + "','" + description.replace("'", "\'") + "','"
req += str(crdate) + "'," + str(cruser_id) + ",'" + aclass + "','" + name.replace("'", "\'") + "','" + description.replace("'", "\'") + "','"
req += str(crdate) + "','" + directory + "','" + software.replace("'", "\'") + "','"
req += version.replace("'", "\'") + "','" + str(hidden) + "','" + params.replace("'", "\'") + "','"
req += str(analysis_is_editable) + "','" + str(parent_uid) + "')"
curs.execute(req)
analysis_id = conn.insert_id()
req = "SELECT directory FROM tx_nG6_analyze WHERE uid=" + str(analysis_id)
curs.execute(req)
analysis_directory = curs.fetchone()
......
......@@ -42,8 +42,3 @@ project_description.name = Project description
project_description.flag = --description
project_description.help = Give a description to your project
project_description.required = True
admin_login.name = Admin login
admin_login.flag = --admin-login
admin_login.help = Who is the project administrator
admin_login.required = True
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment