def create_ext_lab(self, mail, key, lab_name): #utilisation (only for admins) : #wget --post-data "[email protected]&key=xxxxxxxxxxx&lab_name=bbcf" http://yourdomain.com/biorepo/create_ext_lab lab_test = DBSession.query(Labs).filter(Labs.name == lab_name).first() if lab_test is None: try: lab = Labs() lab.name = lab_name lab.path_raw = path_raw(lab_name) lab.path_processed = path_processed(lab_name) lab.path_tmp = path_tmp(lab_name) DBSession.add(lab) DBSession.flush() print "Exterior lab created :", lab_name except: print_traceback() print "Exterior lab NOT created --> ERROR" else: print "This lab : ", str(lab_name), " is in the db yet. --> ERROR"
def manage_fu(existing_fu, meas, public_dirname, filename, sha1, up_data, url_path, url_up, dest_raw, dest_processed, tmp_path, lab): tmpdir_to_delete = os.path.abspath(os.path.join(tmp_path, os.path.pardir)) #fixing bug str "true", str "false" for meas.type if isinstance(meas.type, basestring): bool_type = str2bool(meas.type) meas.type = bool_type if existing_fu: print "-------- EXISTING FILE --------" #create symbolic link source = existing_fu.path + "/" + sha1 if meas.type: dest = dest_raw + sha1 #test symlin existance if os.path.islink(dest): symlink_e = "This file was already in your personal folder" else: symlink_e = "This file was added to your personal folder" os.symlink(source, dest) else: dest = dest_processed + sha1 #test symlink existance if os.path.islink(dest): symlink_e = "This file was already in your personal folder" else: symlink_e = "This file was added to your personal folder" os.symlink(source, dest) meas.fus.append(existing_fu) DBSession.add(meas) DBSession.flush() flash(symlink_e + ", measurement created") #remove the tmp file if it didn't come from HTSStation if not tmpdir_to_delete.startswith('/data'): shutil.rmtree(tmpdir_to_delete) #raise redirect("./") return existing_fu ########################################################################################### else: #new files_up building fu = Files_up() #raw test for path orientation if meas.type: fu.path = path_raw(lab) data_dirname = os.path.join(public_dirname, fu.path) else: fu.path = path_processed(lab) data_dirname = os.path.join(public_dirname, fu.path) #save the filename and the extension to the database fu.filename = filename if '.' in filename: extension = filename.split('.')[-1] fu.extension = extension # root, ext = os.path.splitext(filename) # DOUBLE_EXTENSIONS = ['tar.gz','tar.bz2','bedGraph.gz','bed.gz'] # if any([filename.endswith(x) for x in DOUBLE_EXTENSIONS]): # root, first_ext = os.path.splitext(root) # ext = first_ext + ext # fu.extension = ext # else: # fu.extension = ext else: fu.extension = "not specified" data_path = os.path.join(data_dirname, str(sha1)) fu.sha1 = sha1 fu.url_path = url_path #writing the file into the server HD #version browser try: with open(data_path, "w") as d: if up_data is not None: d.write(up_data.value) elif url_path is not None and url_up: u = urllib2.urlopen(url_path) while True: buffer = u.read(8192) if not buffer: break d.write(buffer) else: print "ERROR" #version commandline except: shutil.move(tmp_path, data_path) #symlink source = data_path if meas.type: dest = dest_raw + sha1 if os.path.islink(dest): symlink_e = "This file was already in your personal folder" else: symlink_e = "This file was added to your personal folder" os.symlink(source, dest) else: dest = dest_processed + sha1 if os.path.islink(dest): symlink_e = "This file was already in your personal folder" else: symlink_e = "This file was added to your personal folder" os.symlink(source, dest) #add to the crossing table (measurement <-> file uploaded) meas.fus.append(fu) #adding new measurement and new files_up to the db DBSession.add(meas) DBSession.add(fu) DBSession.flush() if meas.type: flash(symlink_e + ", raw data was successfully created") else: flash(symlink_e + ", processed data was successfully created") #remove the tmp file if it didn't come from HTSStation if not tmpdir_to_delete.startswith('/data'): shutil.rmtree(tmpdir_to_delete) return fu
def build_user(self, principal): ''' Build an User and his/her Lab(s) from a principal hash from Tequila @param principal: the hash from Tequila @return: an User, his/her Lab(s) ''' hash = dict(item.split('=') for item in principal.split('\n') if len(item.split('=')) > 1) user = User() lab = Labs() #initialize the configparser configp = ConfigParser.RawConfigParser() print "################# NEW CONNEXION ###############" print hash, "--- connexion" now2 = datetime.datetime.now() print "############## TIME #############" print now2.ctime() if(hash.has_key('firstname')): user.firstname = hash.get('firstname').decode("utf-8") if(hash.has_key('name')): user.name = hash.get('name').decode("utf-8") if(hash.has_key('email')): user.email = hash.get('email') #testing unit tmp_u = hash['allunits'].lower() list_units = tmp_u.split(',') print list_units, "list_units" #possibility to add one or several external lab(s) to a collaborator without Shibboleth agreement test_user = DBSession.query(User).filter(User._email == user.email).first() #if it is not the first connexion for the user if test_user is not None: #get his/her lab(s) registered test_labs = test_user.labs #check if we get a Shibboleth bypass for this one if len(test_labs) > 0: for l in test_labs: if str(l.name) not in list_units: print "Shibboleth bypass for : " + str(l.name) list_units.append(str(l.name)) #parsing conf file labs.ini configp.read(path_conf_labs()) list_labs = (configp.get('LABS:main', 'keys')).split(',') print list_labs, "list_labs in labs.ini" valid = False # try: # test = session['current_lab'] # exist = True # except: # exist = False if len(list_units) > 1 and session['first_passage'] == True: cpt_labs = len(list_units) cpt = 0 for u in list_units: if u in list_labs: raise redirect('choose_lab', {'list_units': list_units}) else: cpt += 1 pass if cpt == cpt_labs: flash("Sorry, your lab is not registered in BioRepo, please contact the administrator", 'error') raise redirect('/') elif len(list_units) > 1 and session['first_passage'] == False: valid = True u = session["current_lab"] lab.name = u lab.path_raw = path_raw(u) lab.path_processed = path_processed(u) lab.path_tmp = path_tmp(u) session["tmp_user"] = user session["tmp_lab"] = lab session.save() #the user is affiliated to one lab elif len(list_units) < 2 and list_units != ['unsupported']: for u in list_units: if u in list_labs: #creating the Labs keys valid = True lab.name = u lab.path_raw = path_raw(u) lab.path_processed = path_processed(u) lab.path_tmp = path_tmp(u) session['current_lab'] = u session.save() else: flash("Sorry, your lab is not registered in BioRepo, please contact the administrator to do it", 'error') raise redirect('/') #the user is an external collaborator, not from EPFL elif len(list_units) == 1 and list_units == ['unsupported']: print "---- OUTSIDE USER DETECTED ----" valid = True mail = user.email user_tocheck = DBSession.query(User).filter(User._email == mail).first() if user_tocheck is None or len(user_tocheck.labs) == 0 or len(user_tocheck.labs) > 2: valid = False #ext_users have only one lab else: for l in user_tocheck.labs: lab_name = str(l.name) lab.name = lab_name lab.path_raw = path_raw(lab_name) lab.path_processed = path_processed(lab_name) lab.path_tmp = path_tmp(lab_name) session['current_lab'] = lab_name session.save() #IMPORTANT : where you have to put your name if you are a super admin if valid == True or hash['user'] == 'mouscaz' or hash['user'] == 'fdavid': return user, lab else: flash("Sorry, your lab is not registered in BioRepo", 'error') raise redirect('/')
def build_user(self, principal): ''' Build an User and his/her Lab(s) from a principal hash from Tequila @param principal: the hash from Tequila @return: an User, his/her Lab(s) ''' hash = dict( item.split('=') for item in principal.split('\n') if len(item.split('=')) > 1) user = User() lab = Labs() #initialize the configparser configp = ConfigParser.RawConfigParser() print "################# NEW CONNEXION ###############" print hash, "--- connexion" now2 = datetime.datetime.now() print "############## TIME #############" print now2.ctime() if (hash.has_key('firstname')): user.firstname = hash.get('firstname').decode("utf-8") if (hash.has_key('name')): user.name = hash.get('name').decode("utf-8") if (hash.has_key('email')): user.email = hash.get('email') #testing unit tmp_u = hash['allunits'].lower() list_units = tmp_u.split(',') print list_units, "list_units" #possibility to add one or several external lab(s) to a collaborator without Shibboleth agreement test_user = DBSession.query(User).filter( User._email == user.email).first() #if it is not the first connexion for the user if test_user is not None: #get his/her lab(s) registered test_labs = test_user.labs #check if we get a Shibboleth bypass for this one if len(test_labs) > 0: for l in test_labs: if str(l.name) not in list_units: print "Shibboleth bypass for : " + str(l.name) list_units.append(str(l.name)) #parsing conf file labs.ini configp.read(path_conf_labs()) list_labs = (configp.get('LABS:main', 'keys')).split(',') print list_labs, "list_labs in labs.ini" valid = False # try: # test = session['current_lab'] # exist = True # except: # exist = False if len(list_units) > 1 and session['first_passage'] == True: cpt_labs = len(list_units) cpt = 0 for u in list_units: if u in list_labs: raise redirect('choose_lab', {'list_units': list_units}) else: cpt += 1 pass if cpt == cpt_labs: flash( "Sorry, your lab is not registered in BioRepo, please contact the administrator", 'error') raise redirect('/') elif len(list_units) > 1 and session['first_passage'] == False: valid = True u = session["current_lab"] lab.name = u lab.path_raw = path_raw(u) lab.path_processed = path_processed(u) lab.path_tmp = path_tmp(u) session["tmp_user"] = user session["tmp_lab"] = lab session.save() #the user is affiliated to one lab elif len(list_units) < 2 and list_units != ['unsupported']: for u in list_units: if u in list_labs: #creating the Labs keys valid = True lab.name = u lab.path_raw = path_raw(u) lab.path_processed = path_processed(u) lab.path_tmp = path_tmp(u) session['current_lab'] = u session.save() else: flash( "Sorry, your lab is not registered in BioRepo, please contact the administrator to do it", 'error') raise redirect('/') #the user is an external collaborator, not from EPFL elif len(list_units) == 1 and list_units == ['unsupported']: print "---- OUTSIDE USER DETECTED ----" valid = True mail = user.email user_tocheck = DBSession.query(User).filter( User._email == mail).first() if user_tocheck is None or len( user_tocheck.labs) == 0 or len(user_tocheck.labs) > 2: valid = False #ext_users have only one lab else: for l in user_tocheck.labs: lab_name = str(l.name) lab.name = lab_name lab.path_raw = path_raw(lab_name) lab.path_processed = path_processed(lab_name) lab.path_tmp = path_tmp(lab_name) session['current_lab'] = lab_name session.save() #IMPORTANT : where you have to put your name if you are a super admin if valid == True or hash['user'] == 'mouscaz' or hash[ 'user'] == 'fdavid': return user, lab else: flash("Sorry, your lab is not registered in BioRepo", 'error') raise redirect('/')