def change_project_owner(self, p_id, new_owner_id, mail, key): ''' Allow to change the owner of one given project (include its sample(s) and measurement(s)) ''' try: p_target = DBSession.query(Projects).filter(Projects.id == p_id).first() #just one lab by project, so the first is the good one project_lab = p_target.labs[0] new_owner = DBSession.query(User).filter(User.id == new_owner_id).first() if project_lab in new_owner.labs: list_samples = p_target.samples for s in list_samples: list_meas = s.measurements for m in list_meas: m.user_id = new_owner.id DBSession.add(m) DBSession.flush() p_target.user_id = new_owner.id DBSession.add(p_target) DBSession.flush() print "Update done." else: raise Exception("The new owner is not a member of this lab project. Impossible to continue the operation.") except: print_traceback()
def change_bis_sample(self, sending_s, reception_s): ''' Allow to move all the measurements of one sample to an other one and delete the sending sample after the operation. Usefull for the "_bis" samples in spreadsheet. @param principal : sending_s (sample id sending the measurements), reception_s (sample id receptioning the measurements) ''' try: # samples queries from_sample = DBSession.query(Samples).filter(Samples.id == int(sending_s)).first() to_sample = DBSession.query(Samples).filter(Samples.id == int(reception_s)).first() # get the measurements lists from_att = from_sample.attributs to_att = to_sample.attributs # lab checking if from_att[0].lab_id != to_att[0].lab_id: raise Exception("Samples from different labs. Impossible to move these measurements.") # get list of measurements objects meas_to_move = from_sample.measurements meas_in_place = to_sample.measurements # move the measurements for m in meas_to_move: if m not in meas_in_place: (to_sample.measurements).append(m) DBSession.delete(from_sample) DBSession.add(to_sample) DBSession.flush() print "---> Sample " + sending_s + " was deleted and its measurements are now into the sample " + reception_s except: print_traceback()
def multi_meas_delete(self, p_id, s_id, mail, key): ''' deleted ALL the measurements for a given sample /!\ IRREVERSIBLE /!\ ''' try: project = DBSession.query(Projects).filter(Projects.id == p_id).first() sample = DBSession.query(Samples).filter(Samples.id == s_id).first() user = DBSession.query(User).filter(User._email == mail).first() #checking print "--- Check your inputs... ---" if project is None: print "Project " + str(p_id) + " not found." if sample is None: print "Sample " + str(s_id) + " not found." if user is None: print "Your mail " + mail + " is not recorded in BioRepo." if project.id == sample.project_id and user.id == project.user_id: print "--- Begin the purge... ---" list_meas = sample.measurements print "Today, " + str(len(list_meas)) + " will die..." print "--------------------------" for m in list_meas: list_fus = m.fus for f in list_fus: #delete the file on the server only if it is not used by anyone else anymore if len(f.measurements) == 1 and not (f.path).startswith(HTS_path_data()) and not (f.path).startswith(HTS_path_archive()): path_fu = f.path + "/" + f.sha1 mail = user._email mail_tmp = mail.split('@') path_mail = "AT".join(mail_tmp) path_symlink = f.path + "/" + path_mail + "/" + f.sha1 DBSession.delete(f) path_symlink = f.path + "/" + path_mail + "/" + f.sha1 try: os.remove(path_symlink) except: print "---- path_symlink deleted yet ----" pass os.remove(path_fu) elif (f.path).startswith(HTS_path_data()) or (f.path).startswith(HTS_path_archive()): DBSession.delete(f) #TODO send back something to hts to notify that it's not into biorepo anymore print str(m.name) + "(" + str(m.id) + ") ... Sorry ... PAN." DBSession.delete(m) DBSession.flush() print "--- They are all died T_T ---" else: print "It's not your project/sample. The FBI was notified. Run." except: print_traceback() print "Something went wrong...Please, don't cry."
def delete(self, *args, **kw): user = handler.user.get_user_in_session(request) project = DBSession.query(Projects).filter(Projects.id == args[0]).first() admin = isAdmin(user) if project.user_id == user.id or admin: try: flash("Your project " + str(project.project_name) + " has been deleted with success") except: flash("Your project " + (project.project_name) + " has been deleted with success") DBSession.delete(project) DBSession.flush() raise redirect('/projects') else: flash("It is not your project -> you are not allowed to delete it", 'error') raise redirect('/projects')
def post(self, *args, **kw): user = handler.user.get_user_in_session(request) user_lab = session.get("current_lab", None) if user_lab: lab = DBSession.query(Labs).filter(Labs.name == user_lab).first() p = Projects() if kw['project_name'] == '': flash("Bad Project : Your project must have a name", "error") raise redirect("./new") p.project_name = kw['project_name'] p.user_id = user.id p.description = kw.get('description', None) (p.labs).append(lab) DBSession.add(p) DBSession.flush() flash("Project created !") raise redirect('/projects')
def create_gone_user(self, mail, key, lab_id, firstname, name, user_mail): #utilisation (only for admins) : #wget --post-data "key=xxxxxxxxxxxxxxxxxxxxx&[email protected]&lab_id=lab_id&firstname=jean-michel&name=michel&[email protected]" \ # http://yourdomain.com/biorepo/create_gone_user try: user = User() user.firstname = firstname.capitalize() user.name = name.capitalize() lab = DBSession.query(Labs).filter(Labs.id == lab_id).first() user.labs.append(lab) user._email = user_mail group = DBSession.query(Group).filter(Group.id == 1).first() user.groups.append(group) DBSession.add(user) DBSession.flush() print "Gone/Exterior user created :", user except: print_traceback() print "Gone/Exterior user NOT created --> ERROR"
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 create(self, *args, **kw): user = handler.user.get_user_in_session(request) project = Projects() name = kw.get('project_name', None) if name is None: return {'ERROR': "You have to give a name to your project"} project.project_name = name lab_id = kw['lab'] if lab_id is None: return {'ERROR': "We need to know the user's lab"} lab = DBSession.query(Labs).filter(Labs.id == lab_id).first() (project.labs).append(lab) #test if user is an admin admin = isAdmin(user) if admin: project.user_id = kw.get('user_id', user.id) else: project.user_id = user.id project.description = kw.get('description', None) get_samples = kw.get('samples', None) l = [] if get_samples is None: project.samples = l else: for x in get_samples.split(','): sam = DBSession.query(Samples).join(Projects).join(User).filter(Samples.id == x).first() l.append(sam) project.samples = l #checking print print project, " building project with wget" DBSession.add(project) DBSession.flush() return {"user_id": user.id, "user_name": user.name, "project_id": project.id, "project_name": project.project_name, "description": project.description}
def delete(self, *args, **kw): user = handler.user.get_user_in_session(request) sample = DBSession.query(Samples).filter(Samples.id == args[0]).first() admin = isAdmin(user) if sample.get_userid == user.id or admin: try: flash("Your sample " + str(sample.name) + " has been deleted with success") except: flash("Your sample " + (sample.name) + " has been deleted with success") DBSession.delete(sample) DBSession.flush() raise redirect("/samples") #TO CHECK : check if sample get already an user as owner elif sample.get_userid == None or admin: DBSession.delete(sample) DBSession.flush() flash("Your sample has been deleted") raise redirect("/samples") else: flash("It is not your sample -> you are not allowed to delete it", 'error') raise redirect('/samples')
def add_lab_4_user(self, mail, key, user_mail, lab2add): ''' Allow to bypass Shibboleth. You can add here a registered lab for a registered user. Warning : Don't use it for someone who is not from EPFL. ''' #utilisation (only for admins) : #wget --post-data "[email protected]&key=xxxxxxxxxxx&[email protected]&lab2add=bbcf" http://yourdomain.com/biorepo/add_lab_4_user user = DBSession.query(User).filter(User._email == user_mail).first() if user is None: print "User not found." lab = DBSession.query(Labs).filter(Labs.name == lab2add).first() if lab is None: print "Lab not found." registred = False if lab is not None and lab in user.labs: print "This lab is already registered for this user." registred = True if user is not None and lab is not None and not registred: (user.labs).append(lab) DBSession.flush() else: print "Error, lab was not added to the user"
def manage_fu_from_HTS(existing_fu, meas, filename, sha1, url_path, hts_path): #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 FROM HTS--------" meas.fus.append(existing_fu) DBSession.add(meas) DBSession.flush() return existing_fu ########################################################################################### else: #new files_up building fu = Files_up() fu.path = hts_path #save the filename and the extension to the database fu.filename = filename if '.' in filename: extension = filename.split('.')[-1] fu.extension = extension else: fu.extension = "not specified" fu.sha1 = sha1 fu.url_path = url_path #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() return fu
def manage_fu_from_HTS(existing_fu, meas, filename, sha1, url_path, hts_path): #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 FROM HTS--------" meas.fus.append(existing_fu) DBSession.add(meas) DBSession.flush() return existing_fu ########################################################################################### else: #new files_up building fu = Files_up() fu.path = hts_path #save the filename and the extension to the database fu.filename = filename if '.' in filename: extension = filename.split('.')[-1] fu.extension = extension else: fu.extension = "not specified" fu.sha1 = sha1 fu.url_path = url_path #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() return fu
def check_value_addition(self, list_type_att, dict_att_values_type, dict_att_values_db, lab_id): ''' Checking for value(s) addition @param principal @return an updated db ''' for key_type in list_type_att: for i in dict_att_values_type[key_type]: #get the attribut and its value(s) i_att = DBSession.query(Attributs).filter( and_(Attributs.lab_id == lab_id, Attributs.key == key_type)).first() #warning : i_att_value is a list ! i_att_value = DBSession.query(Attributs_values).filter( and_(Attributs_values.attribut_id == i_att.id, Attributs_values.value == i)).all() try: flag = False if i not in dict_att_values_db[key_type] and i != "None": for existant in i_att_value: if existant.value == i and existant.deprecated == True: existant.deprecated = False DBSession.flush() flag = True if existant.value is None: existant.deprecated = True DBSession.flush() if flag == False: new_value = Attributs_values() new_value.attribut_id = i_att.id new_value.value = i new_value.deprecated = False DBSession.add(new_value) DBSession.flush() except Exception as e: import sys, traceback a, b, c = sys.exc_info() traceback.print_exception(a, b, c) print "--- error login.py check_value_addition() ---" print "i --->", i print "key_type --->", key_type print "list_type_att --->", list_type_att print "dict_att_values_type[key_type] -->", dict_att_values_type[ key_type] print "i_att --> ", i_att print "i_att_value", i_att_value
def check_value_addition(self, list_type_att, dict_att_values_type, dict_att_values_db, lab_id): ''' Checking for value(s) addition @param principal @return an updated db ''' for key_type in list_type_att: for i in dict_att_values_type[key_type]: #get the attribut and its value(s) i_att = DBSession.query(Attributs).filter(and_(Attributs.lab_id == lab_id, Attributs.key == key_type)).first() #warning : i_att_value is a list ! i_att_value = DBSession.query(Attributs_values).filter(and_(Attributs_values.attribut_id == i_att.id, Attributs_values.value == i)).all() try: flag = False if i not in dict_att_values_db[key_type] and i != "None": for existant in i_att_value: if existant.value == i and existant.deprecated == True: existant.deprecated = False DBSession.flush() flag = True if existant.value is None: existant.deprecated = True DBSession.flush() if flag == False: new_value = Attributs_values() new_value.attribut_id = i_att.id new_value.value = i new_value.deprecated = False DBSession.add(new_value) DBSession.flush() except Exception as e: import sys, traceback a, b, c = sys.exc_info() traceback.print_exception(a, b, c) print "--- error login.py check_value_addition() ---" print "i --->", i print "key_type --->", key_type print "list_type_att --->", list_type_att print "dict_att_values_type[key_type] -->", dict_att_values_type[key_type] print "i_att --> ", i_att print "i_att_value", i_att_value
def auth(self, came_from='/', **kw): ''' Fetch user back from tequila. Validate the key from tequila. Log user. ''' if not kw.has_key('key'): raise redirect(came_from) # take parameters key = kw.get('key') environ = request.environ authentication_plugins = environ['repoze.who.plugins'] identifier = authentication_plugins['ticket'] secret = identifier.secret cookiename = identifier.cookie_name remote_addr = environ['REMOTE_ADDR'] # get user principal = tequila.validate_key(key, 'tequila.epfl.ch') if principal is None: raise redirect('./login') #in case of user gets several labs try: if session["first_passage"] == False: #second passage tmp_user = session["tmp_user"] tmp_lab = session['tmp_lab'] except: #first passage session["first_passage"] = True session["principal_tequila"] = principal session.save() tmp_user, tmp_lab = self.build_user(principal) try: mail = tmp_user.email except: flash( "Sorry, you've been disconnected. You can try to relog yourself now", 'error') raise redirect('/login/out') # log or create him user = DBSession.query(User).filter( User.email == tmp_user.email).first() if user is None: user_group = DBSession.query(Group).filter( Group.name == gl.group_users).first() user_group.users.append(tmp_user) DBSession.add(tmp_user) DBSession.flush() user = DBSession.query(User).filter(User.email == mail).first() flash(u'Your account has been created %s' % (user.firstname + ' ' + user.name, )) DBSession.flush() print "######################" print "key user :"******"######################" print "key user :"******"######################" print "key user :"******"lab created : ", lab else: if lab not in user.labs: lab.users.append(user) DBSession.flush() print "lab existing : ", lab #create attributs / check existing attributs attributs = DBSession.query(Attributs).filter( Attributs.lab_id == lab.id).all() if len(attributs) == 0: attributs = None lab_id = lab.id #parsing "unit".ini config = ConfigParser.RawConfigParser() config.read(path_conf_unit(lab.name)) list_sample_att = (config.get('samples_attributs:main', 'keys')).split(',') list_sample_hiding = (config.get('samples_hiding:main', 'keys')).split(',') if len(list_sample_hiding) == 1 and list_sample_hiding[0] == '': list_sample_hiding = '' list_measurement_att = (config.get('meas_attributs:main', 'keys')).split(',') list_meas_hiding = (config.get('meas_hiding:main', 'keys')).split(',') if len(list_meas_hiding) == 1 and list_meas_hiding[0] == '': list_meas_hiding = '' list_searchable = (config.get('searchable_attributs:main', 'keys')).split(',') list_deprecated = (config.get('deprecated_attributs:main', 'keys')).split(',') dict_att_values_sample = {} dict_att_values_meas = {} dict_widgets_sample_att = {} dict_widgets_meas_att = {} dict_hiding_s_att = {} dict_hiding_m_att = {} for x in list_sample_att: dict_att_values_sample[x] = (config.get('samples_attributs:' + x, x)).split(',') dict_widgets_sample_att[x] = (config.get('samples_attributs:' + x, 'widget')).split(',') for x in list_measurement_att: dict_att_values_meas[x] = (config.get('meas_attributs:' + x, x)).split(',') dict_widgets_meas_att[x] = (config.get('meas_attributs:' + x, 'widget')).split(',') #to build the search page search_fields_to_display = (config.get('search_grid_fields:main', 'keys')).split(',') session["search_grid_fields"] = search_fields_to_display #hidingradiobutton lists #samples if isinstance(list_sample_hiding, list): for x in list_sample_hiding: dic_mapping = {} list_possibilities = (config.get('samples_attributs:' + x, x)).split(',') for p in list_possibilities: attribs_by_poss = (config.get('samples_attributs:' + x, p + "_mapping")).split(',') dic_mapping[p] = attribs_by_poss dict_hiding_s_att[x] = dic_mapping if len(dict_hiding_s_att.keys()) > 0: session["hiding_sample"] = dict_hiding_s_att else: session["hiding_sample"] = {} #measurements if isinstance(list_meas_hiding, list): for x in list_meas_hiding: dic_mapping = {} list_possibilities = (config.get('meas_attributs:' + x, x)).split(',') for p in list_possibilities: attribs_by_poss = (config.get('meas_attributs:' + x, p + "_mapping")).split(',') dic_mapping[p] = attribs_by_poss dict_hiding_m_att[x] = dic_mapping if len(dict_hiding_m_att.keys()) > 0: session["hiding_meas"] = dict_hiding_m_att else: session["hiding_meas"] = {} session.save() #creating fixed values list list_fixed_values_samples = [] list_fixed_values_meas = [] fixed_value_case = [ 'singleselectfield', 'multiselectfield', 'hiding_singleselectfield', 'hiding_multiselectfield' ] for fix_s in list_sample_att: for i in dict_att_values_sample[fix_s]: if i != "None": if fix_s not in list_fixed_values_samples: list_fixed_values_samples.append(fix_s) for fix_m in list_measurement_att: for i in dict_att_values_meas[fix_m]: if i != "None": if fix_m not in list_fixed_values_meas: list_fixed_values_meas.append(fix_m) print list_fixed_values_samples, "<--- fixed sample" print list_fixed_values_meas, "<---- fixed meas" if attributs is None: ######################################### ###### creating samples attributs ####### ######################################### for s in list_sample_att: #TODO virer cast str widget = str(dict_widgets_sample_att[s]) owner_widget = "sample" attribut = self.build_attribut(s, lab_id, list_fixed_values_samples, list_searchable, list_deprecated, widget, owner_widget) DBSession.add(attribut) DBSession.flush() if attribut.fixed_value == False: #if Attribut do not get fixed values att_value = self.build_None_attribut_value(attribut.id) DBSession.add(att_value) DBSession.flush() ######################################### #### creating measurements attributs #### ######################################### for m in list_measurement_att: widget = str(dict_widgets_meas_att[m]) owner_widget = "measurement" attribut = self.build_attribut(m, lab_id, list_fixed_values_meas, list_searchable, list_deprecated, widget, owner_widget) DBSession.add(attribut) DBSession.flush() if attribut.fixed_value == False: #if Attribut do not get fixed values att_value = self.build_None_attribut_value(attribut.id) DBSession.add(att_value) DBSession.flush() ######################################### ####### creating attributs values ####### ######################################### ####### for samples ####### ######################################### dict_fixed_values_samples = {} list_attributs_samples_values = self.build_attribut_value( 'samples_attributs:', lab_id, dict_fixed_values_samples, list_fixed_values_samples, config) for att_v in list_attributs_samples_values: DBSession.add(att_v) DBSession.flush() ######################################### ###### for measurements ####### ######################################### dict_fixed_values_meas = {} list_attributs_meas_values = self.build_attribut_value( 'meas_attributs:', lab_id, dict_fixed_values_meas, list_fixed_values_meas, config) for att_v in list_attributs_meas_values: DBSession.add(att_v) DBSession.flush() #check if there is a new key (or several...) in the config file of the lab else: list_existant_keys = [] print list_searchable, "-------------searchable" for k in attributs: list_existant_keys.append(str(k.key)) for att_s in list_sample_att: att_s = unicode(att_s) if att_s not in list_existant_keys: ######################################### ###### creating samples attributs ####### ######################################### widget = str(dict_widgets_sample_att[att_s]) owner_widget = "sample" new_sample_attribut = self.build_attribut( att_s, lab_id, list_fixed_values_samples, list_searchable, list_deprecated, widget, owner_widget) print new_sample_attribut DBSession.add(new_sample_attribut) DBSession.flush() if new_sample_attribut.fixed_value == False: #if Attribut do not get fixed values att_value = self.build_None_attribut_value( new_sample_attribut.id) DBSession.add(att_value) DBSession.flush() ######################################### ####### creating attributs values ####### ######################################### ####### for samples ####### ######################################### dict_fixed_values_samples = {} list_attributs_samples_values = self.build_attribut_value( 'samples_attributs:', lab_id, dict_fixed_values_samples, list_fixed_values_samples, config) for att_v in list_attributs_samples_values: DBSession.add(att_v) DBSession.flush() #check widgets type att_2_check = DBSession.query(Attributs).filter( and_(Attributs.lab_id == lab_id, Attributs.key == att_s, Attributs.owner == "sample")).first() wid_sample_tmp = dict_widgets_sample_att[att_s] for w_s in wid_sample_tmp: if w_s != att_2_check.widget: in_db_before = att_2_check.widget in_db_now = w_s att_2_check.widget = w_s if in_db_before in fixed_value_case and in_db_now not in fixed_value_case: att_2_check.fixed_value = False elif in_db_before not in fixed_value_case and in_db_now in fixed_value_case: att_2_check.fixed_value = True DBSession.flush() #check and update search buttons if att_2_check is None: print att_s, "not in db" if att_2_check is not None and not att_2_check.searchable and att_2_check.key in list_searchable: att_2_check.searchable = True DBSession.flush() elif att_2_check is not None and att_2_check.searchable and att_2_check.key not in list_searchable: att_2_check.searchable = False DBSession.flush() for att_m in list_measurement_att: att_m = unicode(att_m) if att_m not in list_existant_keys: ######################################### #### creating measurements attributs #### ######################################### #TODO virer cast str widget = str(dict_widgets_meas_att[att_m]) owner_widget = "measurement" new_meas_attribut = self.build_attribut( att_m, lab_id, list_fixed_values_meas, list_searchable, list_deprecated, widget, owner_widget) DBSession.add(new_meas_attribut) DBSession.flush() if new_meas_attribut.fixed_value == False: #if Attribut do not get fixed values att_value = self.build_None_attribut_value( new_meas_attribut.id) DBSession.add(att_value) DBSession.flush() ######################################### ####### creating attributs values ####### ######################################### ###### for measurements ####### ######################################### dict_fixed_values_meas = {} list_attributs_meas_values = self.build_attribut_value( 'meas_attributs:', lab_id, dict_fixed_values_meas, list_fixed_values_meas, config) for att_v in list_attributs_meas_values: DBSession.add(att_v) DBSession.flush() #check the widgets att_2_check = DBSession.query(Attributs).filter( and_(Attributs.lab_id == lab_id, Attributs.key == att_m, Attributs.owner == "measurement")).first() wid_meas_tmp = dict_widgets_meas_att[att_m] for w_m in wid_meas_tmp: if w_m != att_2_check.widget: in_db_before = att_2_check.widget in_db_now = w_m att_2_check.widget = w_m if in_db_before in fixed_value_case and in_db_now not in fixed_value_case: att_2_check.fixed_value = False elif in_db_before not in fixed_value_case and in_db_now in fixed_value_case: att_2_check.fixed_value = True DBSession.flush() #check and update search buttons if att_2_check is None: print att_m, "not in db" if att_2_check is not None and not att_2_check.searchable and att_2_check.key in list_searchable: att_2_check.searchable = True DBSession.flush() elif att_2_check is not None and att_2_check.searchable and att_2_check.key not in list_searchable: att_2_check.searchable = False DBSession.flush() #if lab choose to delete an attributs (or undelete a key...) #Attributs obj have to stay into the db but user can not add others Attributs() with this key --> deprecated == True not_deprecated_in_db = DBSession.query(Attributs).filter( and_(Attributs.lab_id == lab_id, Attributs.deprecated == False)).all() for k in not_deprecated_in_db: if k.key in list_deprecated: k.deprecated = True DBSession.add(k) DBSession.flush() deprecated_in_db = DBSession.query(Attributs).filter( and_(Attributs.lab_id == lab_id, Attributs.deprecated == True)).all() for k in deprecated_in_db: if k.key not in list_deprecated: k.deprecated = False DBSession.add(k) DBSession.flush() #test if deleted attributs_values #build a dictionnary to fast check what is into db dict_att_values_db = {} for a in attributs: #dict_att_values_db[a.key] = empty_list list_tmp = [] for v in a.values: if v.deprecated == False: try: list_tmp.append(str(v.value)) except: list_tmp.append(v.value) if len(list_tmp) == 0: list_tmp = [None] dict_att_values_db[str(a.key)] = list_tmp #You have to check deletion BEFORE addition #checking attribut value(s) deletion(s) in samples attributs self.check_value_deletion(list_sample_att, dict_att_values_sample, dict_att_values_db, lab_id) #checking attribut value(s) deletion(s) in measurements attributs self.check_value_deletion(list_measurement_att, dict_att_values_meas, dict_att_values_db, lab_id) #checking attribut value(s) addition in sample attributs self.check_value_addition(list_sample_att, dict_att_values_sample, dict_att_values_db, lab_id) #checking attribut value(s) addition in measurements attributs self.check_value_addition(list_measurement_att, dict_att_values_meas, dict_att_values_db, lab_id) # look if an user is admin or not admins = tg.config.get('admin.mails') group_admins = DBSession.query(Group).filter( Group.name == gl.group_admins).first() if user.email in admins: user not in group_admins.users and group_admins.users.append(user) else: user in group_admins.users and group_admins.users.remove(user) DBSession.flush() # create the authentication ticket user = DBSession.query(User).filter(User.email == mail).first() userdata = str(user.id) ticket = auth_tkt.AuthTicket(secret, user.email, remote_addr, tokens=token, user_data=userdata, time=None, cookie_name=cookiename, secure=True) val = ticket.cookie_value() # set it in the cookies response.set_cookie(cookiename, value=val, max_age=None, path='/', domain=None, secure=False, httponly=False, comment=None, expires=None, overwrite=False) #transaction.commit() extern_meas = session.get("extern_meas", False) check_tequila = session.get("check_tequila", False) if extern_meas is False and check_tequila is False: raise redirect(came_from) elif extern_meas is False and check_tequila: raise redirect("/search") else: del session["extern_meas"] raise redirect(url('/measurements/external_add'))
def post_edit(self, *args, **kw): id_sample = kw['IDselected'] sample = DBSession.query(Samples).filter(Samples.id == id_sample).first() try: project_id = kw.get("project") if project_id is None or project_id == "": flash("Edition rejected : Your sample must be in a project", 'error') raise redirect("./") sample.project_id = project_id except: flash("Your sample must be in a project", 'error') raise redirect("./") if kw['name'] == '' or kw['name'] is None: flash("Bad Sample : you have to give a name to your sample", "error") raise redirect("./edit/" + id_sample) sample.name = kw.get("name", None) sample.protocole = kw.get("protocole", None) sample.type = kw.get("type", None) meas_ids = kw.get("measurements", None) if meas_ids is not None: if not isinstance(meas_ids, (list, tuple)): meas_ids = [int(meas_ids)] else: #from unicode to integer for comparison list_tmp = [] for i in meas_ids: i = int(i) list_tmp.append(i) meas_ids = list_tmp else: meas_ids = [] list_meas = [] for m in meas_ids: measurement = DBSession.query(Measurements).filter(Measurements.id == m).first() list_meas.append(measurement) sample.measurements = list_meas #DYNAMICITY list_static = ['project', 'name', 'type', 'protocole', 'IDselected', 'measurements'] list_attributs = [] list_a_values = sample.a_values for a in sample.attributs: if a.deprecated == False: list_attributs.append(a) for x in kw: if x not in list_static: for a in list_attributs: if x == a.key: val_kw = kw[x] object_2_delete = None #search if the field was edited for v in list_a_values: v_value = v.value if a.widget == "checkbox" or a.widget == "hiding_checkbox": val_kw = check_boolean(kw[x]) v_value = check_boolean(v.value) if v.attribut_id == a.id and v_value != val_kw and a.widget != "multipleselectfield" and a.widget != "hiding_multipleselectfield": object_2_delete = v if a.widget == "textfield" or a.widget == "hiding_textfield" or a.widget == "textarea" or a.widget == "hiding_textarea": if object_2_delete: object_2_delete.value = kw[x] elif a.widget == "checkbox" or a.widget == "hiding_checkbox": if len(a.values) < 3: for old_v in a.values: if old_v.value is not None and old_v.value != '': list_a_values.remove(old_v) av = Attributs_values() av.attribut_id = a.id av.value = True av.deprecated = False DBSession.add(av) list_a_values.append(av) DBSession.flush() elif len(a.values) == 3: if object_2_delete: list_a_values.remove(object_2_delete) v = object_2_delete.value for val in a.values: val_to_avoid = [None, ""] if v not in val_to_avoid: val_to_avoid.append(v) if val.value not in val_to_avoid: list_a_values.append(val) DBSession.flush() else: print "----- BOOLEAN ERROR -----" print str(a.id), " attributs id" print "boolean with more than 2 values" raise elif a.widget == "singleselectfield" or a.widget == "hiding_singleselectfield": #edition : delete the connexion to the older a_value, make the connexion between the new a_value and the sample if object_2_delete: list_a_values.remove(object_2_delete) list_possible = a.values for p in list_possible: if p.value == kw[x]: list_a_values.append(p) #if the value was "None", just add the new value edited elif object_2_delete is None: list_possible = a.values for p in list_possible: if p.value == kw[x]: list_a_values.append(p) elif a.widget == "multipleselectfield" or a.widget == "hiding_multipleselectfield": #!!! NOT TESTED !!! list_objects_2_delete = [] for v in list_a_values: #warning : types of i and v.value have to be similar... for i in kw[x]: if v.attribut_id == a.id and v.value != i: list_objects_2_delete.append(v) if len(list_objects_2_delete) > 0: for v in list_objects_2_delete: list_a_values.remove(v) if a.fixed_value == True: to_add = DBSession.query(Attributs_values).filter(and_(Attributs_values.value == i, Attributs_values.attribut_id == a.id)).first() list_a_values.append(to_add) else: #mutliple selected field can't be not a fixed value. print "something wrong happenned - illogical - controller sample post_edit()" raise #special case for checkbox because of the "on" and None value of TW2 for True and False... (Here it's False) lab = session.get('current_lab', None) labo = DBSession.query(Labs).filter(Labs.name == lab).first() lab_id = labo.id dynamic_booleans = DBSession.query(Attributs).filter(and_(Attributs.lab_id == lab_id, Attributs.deprecated == False, Attributs.owner == "sample", or_(Attributs.widget == "checkbox", Attributs.widget == "hiding_checkbox"))).all() if len(dynamic_booleans) > 0: for b in dynamic_booleans: if b.key not in kw: list_value = b.values #2 cases possibles #1 : values are None and (True or False) if len(list_value) == 2: for v in list_value: #1.1 : None and True if v.value is not None: val = check_boolean(v.value) else: val = None if val == True: list_a_values.remove(v) av = Attributs_values() av.attribut_id = b.id av.value = False av.deprecated = False DBSession.add(av) list_a_values.append(av) DBSession.flush() break #1.2 : None and False elif val == False: #because nothing was edited for the field pass #2 : values are None, True and False elif len(list_value) == 3: for v in list_value: if v.value is not None: val = check_boolean(v.value) else: val = None if val == True: try: list_a_values.remove(v) except: pass elif val == False: list_a_values.append(v) elif len(list_value) > 3: print "----- ERROR -----" print str(b.key), " gets more than 3 values." print "-----------------" flash("Sample edited !") raise redirect("./")
def post(self, *args, **kw): user_lab = session.get("current_lab", None) lab = DBSession.query(Labs).filter(Labs.name == user_lab).first() lab_id = lab.id s = Samples() list_static = ['project', 'name', 'type', 'protocole'] list_dynamic = [] #new sample object if 'project' not in kw: flash("You have to choose a project to attach to your new sample, retry please", "error") raise redirect('./') s.project_id = kw['project'] #TODO : make a correct validator for NewSample if kw['name'] == '': flash("Bad Sample : you have to give a name to your sample", "error") raise redirect('./new') s.name = kw['name'] s.type = kw.get('type', None) s.protocole = kw.get('protocole', None) DBSession.add(s) DBSession.flush() #link the new sample to the attributs object for x in kw: #exclude the static fields belonging to Samples() if x not in list_static: list_dynamic.append(x) #get the attribut a = DBSession.query(Attributs).filter(and_(Attributs.lab_id == lab_id, Attributs.key == x, Attributs.deprecated == False, Attributs.owner == "sample")).first() if a is not None: #get its value(s) (s.attributs).append(a) #if values of the attribute are fixed if a.fixed_value == True and kw[x] is not None and kw[x] != '' and a.widget != "checkbox" and a.widget != "hiding_checkbox": value = kw[x] list_value = DBSession.query(Attributs_values).filter(Attributs_values.attribut_id == a.id).all() for v in list_value: #if the keyword value is in the value list, the attributs_values object is saved in the cross table if v.value == value and v not in s.a_values: (s.a_values).append(v) DBSession.flush() #if values of the attribute are free elif a.fixed_value == False and a.widget != "checkbox" and a.widget != "hiding_checkbox": av = Attributs_values() av.attribut_id = a.id av.value = kw.get(x, None) av.deprecated = False DBSession.add(av) DBSession.flush() (s.a_values).append(av) DBSession.flush() #special case for checkbox because of the "on" and None value of TW2 for True and False...(here it's True) elif a.widget == "checkbox" or a.widget == "hiding_checkbox": found = False for v in a.values: if check_boolean(v.value) and v.value is not None: (s.a_values).append(v) found = True if not found: av = Attributs_values() av.attribut_id = a.id av.value = True av.deprecated = False DBSession.add(av) DBSession.flush() (s.a_values).append(av) DBSession.flush() #special case for checkbox because of the "on" and None value of TW2 for True and False...(here it's False) dynamic_booleans = DBSession.query(Attributs).filter(and_(Attributs.lab_id == lab_id, Attributs.deprecated == False, Attributs.owner == "sample", or_(Attributs.widget == "checkbox", Attributs.widget == "hiding_checkbox"))).all() if len(dynamic_booleans) > 0: for d in dynamic_booleans: if d.key not in list_dynamic: if d.widget == "checkbox" or d.widget == "hiding_checkbox": found = False for v in d.values: if not check_boolean(v.value) and v.value is not None: (s.attributs).append(d) (s.a_values).append(v) found = True #to avoid IntegrityError in the db break if not found: av = Attributs_values() av.attribut_id = d.id av.value = False av.deprecated = False DBSession.add(av) DBSession.flush() (s.attributs).append(d) (s.a_values).append(av) DBSession.flush() flash("Sample created !") raise redirect('/samples')
def create(self, *args, **kw): user = handler.user.get_user_in_session(request) kw['user'] = user.id lab = kw.get("lab", None) if lab is None: return {"ERROR": "We need to know the lab of the user..."} sample = Samples() if not kw.has_key('project_id'): return {"ERROR": "project_id missing"} type_ = kw.get('type', None) sample.project_id = kw['project_id'] sample.name = kw.get('name', 'Give me a name please') if type_ is not None: try: ret1 = list_lower(type_, get_list_types(lab)) sample.type = ret1 except: return {"ERROR": "your " + type_ + " is not known in types list"} elif type_ is None: sample.type = type_ sample.protocole = kw.get('protocole', None) get_meas = kw.get('measurements', None) l = [] if get_meas is None: sample.measurements = l else: for x in get_meas.split(','): meas = DBSession.query(Measurements).filter(Measurements.id == x).first() l.append(meas) sample.measurements = l #print server print sample, "building sample with wget" #dynamicity list_static = ['project', 'name', 'type', 'protocole', 'measurements', 'lab', 'user', 'key', 'mail', 'project_id'] list_dynamic = [] labo = DBSession.query(Labs).filter(Labs.name == lab).first() lab_id = labo.id #save the attributs of the lab for final comparison dynamic_keys = [] lab_attributs = DBSession.query(Attributs).filter(and_(Attributs.lab_id == lab_id, Attributs.deprecated == False, Attributs.owner == "sample")).all() for i in lab_attributs: dynamic_keys.append(i.key) for x in kw: #exclude the static fields belonging to Samples() if x not in list_static: list_dynamic.append(x) #get the attribut a = DBSession.query(Attributs).filter(and_(Attributs.lab_id == lab_id, Attributs.key == x, Attributs.deprecated == False, Attributs.owner == "sample")).first() if a is not None: #get its value(s) (sample.attributs).append(a) #if values of the attribute are fixed if a.fixed_value == True and kw[x] is not None and kw[x] != '' and a.widget != "checkbox" and a.widget != "hiding_checkbox": value = kw[x] list_value = DBSession.query(Attributs_values).filter(and_(Attributs_values.attribut_id == a.id, Attributs_values.deprecated == False)).all() for v in list_value: #if the keyword value is in the value list, the attributs_values object is saved in the cross table if (v.value).lower() == value.lower() and v not in sample.a_values: (sample.a_values).append(v) DBSession.flush() #if values of the attribute are free elif a.fixed_value == False and a.widget != "checkbox" and a.widget != "hiding_checkbox": av = Attributs_values() av.attribut_id = a.id av.value = kw.get(x, None) av.deprecated = False DBSession.add(av) DBSession.flush() (sample.a_values).append(av) DBSession.flush() #special case for checkbox because of the "on" and None value of TW2 for True and False...(here it's True) elif a.widget == "checkbox" or a.widget == "hiding_checkbox": #Why 3 ? Because 3 cases max registred : True, False and None ---> so <3 if len(a.values) < 3: av = Attributs_values() av.attribut_id = a.id #for True value, Attribut key and value have to be similar into the excel sheet... if (kw[x]).lower() == x.lower(): av.value = True #...and different for the False :) else: av.value = False av.deprecated = False DBSession.add(av) DBSession.flush() (sample.a_values).append(av) DBSession.flush() else: if (kw[x]).lower() == x.lower(): for v in a.values: if check_boolean(v.value) and v.value is not None: (sample.a_values).append(v) else: for v in a.values: if check_boolean(v.value) == False and v.value is not None: (sample.a_values).append(v) DBSession.flush() #to take in account the empty dynamic fields in the excel sheet for k in dynamic_keys: if k not in list_dynamic: print k, " -------------------- NOT FOUND IN SAMPLE DESCRIPTION EXCEL SHEET" a = DBSession.query(Attributs).filter(and_(Attributs.lab_id == lab_id, Attributs.key == k, Attributs.deprecated == False, Attributs.owner == "sample")).first() (sample.attributs).append(a) DBSession.flush() DBSession.add(sample) DBSession.flush() return {"sample": sample, "measurements": l}
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 auth(self, came_from='/', **kw): ''' Fetch user back from tequila. Validate the key from tequila. Log user. ''' if not kw.has_key('key'): raise redirect(came_from) # take parameters key = kw.get('key') environ = request.environ authentication_plugins = environ['repoze.who.plugins'] identifier = authentication_plugins['ticket'] secret = identifier.secret cookiename = identifier.cookie_name remote_addr = environ['REMOTE_ADDR'] # get user principal = tequila.validate_key(key, 'tequila.epfl.ch') if principal is None: raise redirect('./login') #in case of user gets several labs try: if session["first_passage"] == False: #second passage tmp_user = session["tmp_user"] tmp_lab = session['tmp_lab'] except: #first passage session["first_passage"] = True session["principal_tequila"] = principal session.save() tmp_user, tmp_lab = self.build_user(principal) try: mail = tmp_user.email except: flash("Sorry, you've been disconnected. You can try to relog yourself now", 'error') raise redirect('/login/out') # log or create him user = DBSession.query(User).filter(User.email == tmp_user.email).first() if user is None: user_group = DBSession.query(Group).filter(Group.name == gl.group_users).first() user_group.users.append(tmp_user) DBSession.add(tmp_user) DBSession.flush() user = DBSession.query(User).filter(User.email == mail).first() flash(u'Your account has been created %s' % (user.firstname + ' ' + user.name, )) DBSession.flush() print "######################" print "key user :"******"######################" print "key user :"******"######################" print "key user :"******"lab created : ", lab else: if lab not in user.labs: lab.users.append(user) DBSession.flush() print "lab existing : ", lab #create attributs / check existing attributs attributs = DBSession.query(Attributs).filter(Attributs.lab_id == lab.id).all() if len(attributs) == 0: attributs = None lab_id = lab.id #parsing "unit".ini config = ConfigParser.RawConfigParser() config.read(path_conf_unit(lab.name)) list_sample_att = (config.get('samples_attributs:main', 'keys')).split(',') list_sample_hiding = (config.get('samples_hiding:main', 'keys')).split(',') if len(list_sample_hiding) == 1 and list_sample_hiding[0] == '': list_sample_hiding = '' list_measurement_att = (config.get('meas_attributs:main', 'keys')).split(',') list_meas_hiding = (config.get('meas_hiding:main', 'keys')).split(',') if len(list_meas_hiding) == 1 and list_meas_hiding[0] == '': list_meas_hiding = '' list_searchable = (config.get('searchable_attributs:main', 'keys')).split(',') list_deprecated = (config.get('deprecated_attributs:main', 'keys')).split(',') dict_att_values_sample = {} dict_att_values_meas = {} dict_widgets_sample_att = {} dict_widgets_meas_att = {} dict_hiding_s_att = {} dict_hiding_m_att = {} for x in list_sample_att: dict_att_values_sample[x] = (config.get('samples_attributs:' + x, x)).split(',') dict_widgets_sample_att[x] = (config.get('samples_attributs:' + x, 'widget')).split(',') for x in list_measurement_att: dict_att_values_meas[x] = (config.get('meas_attributs:' + x, x)).split(',') dict_widgets_meas_att[x] = (config.get('meas_attributs:' + x, 'widget')).split(',') #to build the search page search_fields_to_display = (config.get('search_grid_fields:main', 'keys')).split(',') session["search_grid_fields"] = search_fields_to_display #hidingradiobutton lists #samples if isinstance(list_sample_hiding, list): for x in list_sample_hiding: dic_mapping = {} list_possibilities = (config.get('samples_attributs:' + x, x)).split(',') for p in list_possibilities: attribs_by_poss = (config.get('samples_attributs:' + x, p + "_mapping")).split(',') dic_mapping[p] = attribs_by_poss dict_hiding_s_att[x] = dic_mapping if len(dict_hiding_s_att.keys()) > 0: session["hiding_sample"] = dict_hiding_s_att else: session["hiding_sample"] = {} #measurements if isinstance(list_meas_hiding, list): for x in list_meas_hiding: dic_mapping = {} list_possibilities = (config.get('meas_attributs:' + x, x)).split(',') for p in list_possibilities: attribs_by_poss = (config.get('meas_attributs:' + x, p + "_mapping")).split(',') dic_mapping[p] = attribs_by_poss dict_hiding_m_att[x] = dic_mapping if len(dict_hiding_m_att.keys()) > 0: session["hiding_meas"] = dict_hiding_m_att else: session["hiding_meas"] = {} session.save() #creating fixed values list list_fixed_values_samples = [] list_fixed_values_meas = [] fixed_value_case = ['singleselectfield', 'multiselectfield', 'hiding_singleselectfield', 'hiding_multiselectfield'] for fix_s in list_sample_att: for i in dict_att_values_sample[fix_s]: if i != "None": if fix_s not in list_fixed_values_samples: list_fixed_values_samples.append(fix_s) for fix_m in list_measurement_att: for i in dict_att_values_meas[fix_m]: if i != "None": if fix_m not in list_fixed_values_meas: list_fixed_values_meas.append(fix_m) print list_fixed_values_samples, "<--- fixed sample" print list_fixed_values_meas, "<---- fixed meas" if attributs is None: ######################################### ###### creating samples attributs ####### ######################################### for s in list_sample_att: #TODO virer cast str widget = str(dict_widgets_sample_att[s]) owner_widget = "sample" attribut = self.build_attribut(s, lab_id, list_fixed_values_samples, list_searchable, list_deprecated, widget, owner_widget) DBSession.add(attribut) DBSession.flush() if attribut.fixed_value == False: #if Attribut do not get fixed values att_value = self.build_None_attribut_value(attribut.id) DBSession.add(att_value) DBSession.flush() ######################################### #### creating measurements attributs #### ######################################### for m in list_measurement_att: widget = str(dict_widgets_meas_att[m]) owner_widget = "measurement" attribut = self.build_attribut(m, lab_id, list_fixed_values_meas, list_searchable, list_deprecated, widget, owner_widget) DBSession.add(attribut) DBSession.flush() if attribut.fixed_value == False: #if Attribut do not get fixed values att_value = self.build_None_attribut_value(attribut.id) DBSession.add(att_value) DBSession.flush() ######################################### ####### creating attributs values ####### ######################################### ####### for samples ####### ######################################### dict_fixed_values_samples = {} list_attributs_samples_values = self.build_attribut_value('samples_attributs:', lab_id, dict_fixed_values_samples, list_fixed_values_samples, config) for att_v in list_attributs_samples_values: DBSession.add(att_v) DBSession.flush() ######################################### ###### for measurements ####### ######################################### dict_fixed_values_meas = {} list_attributs_meas_values = self.build_attribut_value('meas_attributs:', lab_id, dict_fixed_values_meas, list_fixed_values_meas, config) for att_v in list_attributs_meas_values: DBSession.add(att_v) DBSession.flush() #check if there is a new key (or several...) in the config file of the lab else: list_existant_keys = [] print list_searchable, "-------------searchable" for k in attributs: list_existant_keys.append(str(k.key)) for att_s in list_sample_att: att_s = unicode(att_s) if att_s not in list_existant_keys: ######################################### ###### creating samples attributs ####### ######################################### widget = str(dict_widgets_sample_att[att_s]) owner_widget = "sample" new_sample_attribut = self.build_attribut(att_s, lab_id, list_fixed_values_samples, list_searchable, list_deprecated, widget, owner_widget) print new_sample_attribut DBSession.add(new_sample_attribut) DBSession.flush() if new_sample_attribut.fixed_value == False: #if Attribut do not get fixed values att_value = self.build_None_attribut_value(new_sample_attribut.id) DBSession.add(att_value) DBSession.flush() ######################################### ####### creating attributs values ####### ######################################### ####### for samples ####### ######################################### dict_fixed_values_samples = {} list_attributs_samples_values = self.build_attribut_value('samples_attributs:', lab_id, dict_fixed_values_samples, list_fixed_values_samples, config) for att_v in list_attributs_samples_values: DBSession.add(att_v) DBSession.flush() #check widgets type att_2_check = DBSession.query(Attributs).filter(and_(Attributs.lab_id == lab_id, Attributs.key == att_s, Attributs.owner == "sample")).first() wid_sample_tmp = dict_widgets_sample_att[att_s] for w_s in wid_sample_tmp: if w_s != att_2_check.widget: in_db_before = att_2_check.widget in_db_now = w_s att_2_check.widget = w_s if in_db_before in fixed_value_case and in_db_now not in fixed_value_case: att_2_check.fixed_value = False elif in_db_before not in fixed_value_case and in_db_now in fixed_value_case: att_2_check.fixed_value = True DBSession.flush() #check and update search buttons if att_2_check is None: print att_s, "not in db" if att_2_check is not None and not att_2_check.searchable and att_2_check.key in list_searchable: att_2_check.searchable = True DBSession.flush() elif att_2_check is not None and att_2_check.searchable and att_2_check.key not in list_searchable: att_2_check.searchable = False DBSession.flush() for att_m in list_measurement_att: att_m = unicode(att_m) if att_m not in list_existant_keys: ######################################### #### creating measurements attributs #### ######################################### #TODO virer cast str widget = str(dict_widgets_meas_att[att_m]) owner_widget = "measurement" new_meas_attribut = self.build_attribut(att_m, lab_id, list_fixed_values_meas, list_searchable, list_deprecated, widget, owner_widget) DBSession.add(new_meas_attribut) DBSession.flush() if new_meas_attribut.fixed_value == False: #if Attribut do not get fixed values att_value = self.build_None_attribut_value(new_meas_attribut.id) DBSession.add(att_value) DBSession.flush() ######################################### ####### creating attributs values ####### ######################################### ###### for measurements ####### ######################################### dict_fixed_values_meas = {} list_attributs_meas_values = self.build_attribut_value('meas_attributs:', lab_id, dict_fixed_values_meas, list_fixed_values_meas, config) for att_v in list_attributs_meas_values: DBSession.add(att_v) DBSession.flush() #check the widgets att_2_check = DBSession.query(Attributs).filter(and_(Attributs.lab_id == lab_id, Attributs.key == att_m, Attributs.owner == "measurement")).first() wid_meas_tmp = dict_widgets_meas_att[att_m] for w_m in wid_meas_tmp: if w_m != att_2_check.widget: in_db_before = att_2_check.widget in_db_now = w_m att_2_check.widget = w_m if in_db_before in fixed_value_case and in_db_now not in fixed_value_case: att_2_check.fixed_value = False elif in_db_before not in fixed_value_case and in_db_now in fixed_value_case: att_2_check.fixed_value = True DBSession.flush() #check and update search buttons if att_2_check is None: print att_m, "not in db" if att_2_check is not None and not att_2_check.searchable and att_2_check.key in list_searchable: att_2_check.searchable = True DBSession.flush() elif att_2_check is not None and att_2_check.searchable and att_2_check.key not in list_searchable: att_2_check.searchable = False DBSession.flush() #if lab choose to delete an attributs (or undelete a key...) #Attributs obj have to stay into the db but user can not add others Attributs() with this key --> deprecated == True not_deprecated_in_db = DBSession.query(Attributs).filter(and_(Attributs.lab_id == lab_id, Attributs.deprecated == False)).all() for k in not_deprecated_in_db: if k.key in list_deprecated: k.deprecated = True DBSession.add(k) DBSession.flush() deprecated_in_db = DBSession.query(Attributs).filter(and_(Attributs.lab_id == lab_id, Attributs.deprecated == True)).all() for k in deprecated_in_db: if k.key not in list_deprecated: k.deprecated = False DBSession.add(k) DBSession.flush() #test if deleted attributs_values #build a dictionnary to fast check what is into db dict_att_values_db = {} for a in attributs: #dict_att_values_db[a.key] = empty_list list_tmp = [] for v in a.values: if v.deprecated == False: try: list_tmp.append(str(v.value)) except: list_tmp.append(v.value) if len(list_tmp) == 0: list_tmp = [None] dict_att_values_db[str(a.key)] = list_tmp #You have to check deletion BEFORE addition #checking attribut value(s) deletion(s) in samples attributs self.check_value_deletion(list_sample_att, dict_att_values_sample, dict_att_values_db, lab_id) #checking attribut value(s) deletion(s) in measurements attributs self.check_value_deletion(list_measurement_att, dict_att_values_meas, dict_att_values_db, lab_id) #checking attribut value(s) addition in sample attributs self.check_value_addition(list_sample_att, dict_att_values_sample, dict_att_values_db, lab_id) #checking attribut value(s) addition in measurements attributs self.check_value_addition(list_measurement_att, dict_att_values_meas, dict_att_values_db, lab_id) # look if an user is admin or not admins = tg.config.get('admin.mails') group_admins = DBSession.query(Group).filter(Group.name == gl.group_admins).first() if user.email in admins: user not in group_admins.users and group_admins.users.append(user) else: user in group_admins.users and group_admins.users.remove(user) DBSession.flush() # create the authentication ticket user = DBSession.query(User).filter(User.email == mail).first() userdata = str(user.id) ticket = auth_tkt.AuthTicket( secret, user.email, remote_addr, tokens=token, user_data=userdata, time=None, cookie_name=cookiename, secure=True) val = ticket.cookie_value() # set it in the cookies response.set_cookie( cookiename, value=val, max_age=None, path='/', domain=None, secure=False, httponly=False, comment=None, expires=None, overwrite=False) #transaction.commit() extern_meas = session.get("extern_meas", False) check_tequila = session.get("check_tequila", False) if extern_meas is False and check_tequila is False: raise redirect(came_from) elif extern_meas is False and check_tequila: raise redirect("/search") else: del session["extern_meas"] raise redirect(url('/measurements/external_add'))
def check_value_deletion(self, list_type_att, dict_att_values_type, dict_att_values_db, lab_id): ''' Checking for value(s) deletion(s) @param principal : list_type_att and dict_att_values_type (where "type" == sample or "measurement"), dict_att_values_db, lab_id @return an updated db ''' for key_type in list_type_att: j_att = DBSession.query(Attributs).filter(and_(Attributs.lab_id == lab_id, Attributs.key == key_type)).first() j_att_value = DBSession.query(Attributs_values).filter(Attributs_values.attribut_id == j_att.id).all() if "None" in dict_att_values_type[key_type] and (dict_att_values_db.has_key(key_type) == False or dict_att_values_db[key_type] != ['None']): #all the value in db are now deprecated #warning : LOOKS USELESS NOW, but it's maybe a mistake --- to be continued ... :/ pass # for v in j_att_value: # print v.value # v.deprecated = True # DBSession.flush() # #creation of the new None value # new_value_None = Attributs_values() # new_value_None.attribut_id = j_att.id # new_value_None.value = None # new_value_None.deprecated = False # DBSession.add(new_value_None) # j_att.fixed_value = False # DBSession.flush() else: if key_type in dict_att_values_db.keys(): for j in dict_att_values_db[key_type]: if j == "None": j = None j_att_value_exact = DBSession.query(Attributs_values).filter(and_(Attributs_values.attribut_id == j_att.id, Attributs_values.value == j)).first() if j is None: j = "None" # #for the boolean mess... # if j is not None and j != "None": # j = check_boolean(j) try: #test presence of value from db in conf file if j not in dict_att_values_type[key_type] and j != "None": j_att_value_exact.deprecated = True DBSession.flush() #if value None in db but not None in conf file elif j not in dict_att_values_type[key_type] and j == "None": if j_att.fixed_value != True: j_att.fixed_value = True j_att_value_exact.deprecated = True DBSession.flush() DBSession.flush() except Exception as e: import sys, traceback a, b, c = sys.exc_info() traceback.print_exception(a, b, c) print "---- error login.py check_value_deletion() ----" print "j ---> ", j print "key_type ---> ", key_type print "list_type_att -->", list_type_att print "dict_att_values_type[key_type] -->", dict_att_values_type[key_type] print "j_att --> ", j_att print "j_att_value", j_att_value else: print key_type + " not found in db during check deletion"
def check_value_deletion(self, list_type_att, dict_att_values_type, dict_att_values_db, lab_id): ''' Checking for value(s) deletion(s) @param principal : list_type_att and dict_att_values_type (where "type" == sample or "measurement"), dict_att_values_db, lab_id @return an updated db ''' for key_type in list_type_att: j_att = DBSession.query(Attributs).filter( and_(Attributs.lab_id == lab_id, Attributs.key == key_type)).first() j_att_value = DBSession.query(Attributs_values).filter( Attributs_values.attribut_id == j_att.id).all() if "None" in dict_att_values_type[key_type] and ( dict_att_values_db.has_key(key_type) == False or dict_att_values_db[key_type] != ['None']): #all the value in db are now deprecated #warning : LOOKS USELESS NOW, but it's maybe a mistake --- to be continued ... :/ pass # for v in j_att_value: # print v.value # v.deprecated = True # DBSession.flush() # #creation of the new None value # new_value_None = Attributs_values() # new_value_None.attribut_id = j_att.id # new_value_None.value = None # new_value_None.deprecated = False # DBSession.add(new_value_None) # j_att.fixed_value = False # DBSession.flush() else: if key_type in dict_att_values_db.keys(): for j in dict_att_values_db[key_type]: if j == "None": j = None j_att_value_exact = DBSession.query( Attributs_values).filter( and_(Attributs_values.attribut_id == j_att.id, Attributs_values.value == j)).first() if j is None: j = "None" # #for the boolean mess... # if j is not None and j != "None": # j = check_boolean(j) try: #test presence of value from db in conf file if j not in dict_att_values_type[ key_type] and j != "None": j_att_value_exact.deprecated = True DBSession.flush() #if value None in db but not None in conf file elif j not in dict_att_values_type[ key_type] and j == "None": if j_att.fixed_value != True: j_att.fixed_value = True j_att_value_exact.deprecated = True DBSession.flush() DBSession.flush() except Exception as e: import sys, traceback a, b, c = sys.exc_info() traceback.print_exception(a, b, c) print "---- error login.py check_value_deletion() ----" print "j ---> ", j print "key_type ---> ", key_type print "list_type_att -->", list_type_att print "dict_att_values_type[key_type] -->", dict_att_values_type[ key_type] print "j_att --> ", j_att print "j_att_value", j_att_value else: print key_type + " not found in db during check deletion"
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