コード例 #1
0
ファイル: login.py プロジェクト: bbcf/biorepo
 def build_None_attribut_value(self, att_id):
     '''
     build a None value Attribut value
     @param principal = Attributs.id
     @return an Attribut value
     '''
     att_val = Attributs_values()
     att_val.attribut_id = att_id
     att_val.value = None
     att_val.deprecated = False
     return att_val
コード例 #2
0
ファイル: login.py プロジェクト: bbcf/biorepo
 def build_None_attribut_value(self, att_id):
     '''
     build a None value Attribut value
     @param principal = Attributs.id
     @return an Attribut value
     '''
     att_val = Attributs_values()
     att_val.attribut_id = att_id
     att_val.value = None
     att_val.deprecated = False
     return att_val
コード例 #3
0
ファイル: login.py プロジェクト: bbcf/biorepo
    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
コード例 #4
0
ファイル: login.py プロジェクト: bbcf/biorepo
    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
コード例 #5
0
ファイル: login.py プロジェクト: bbcf/biorepo
    def build_attribut_value(self, s, lab_id, dict_fixed_values_type, list_fixed_values_type, config):
        '''
        build Attribut values
        @return a list of Attribut values
        '''
        list_objects_value = []
        for att in list_fixed_values_type:
            #build a dictionnary of fixed values : {'key':'[value1, value2, value3, ...}'}
            list_values = []
            list_values = (config.get(s + att, att)).split(',')
            dict_fixed_values_type[att] = list_values

        for k, list_values in dict_fixed_values_type.iteritems():
            att_tmp = DBSession.query(Attributs).filter(and_(Attributs.key == k), Attributs.lab_id == lab_id).first()
            for v in list_values:
                attributs_v = Attributs_values()
                attributs_v.attribut_id = att_tmp.id
                attributs_v.value = v
                attributs_v.deprecated = False
                list_objects_value.append(attributs_v)

        return list_objects_value
コード例 #6
0
ファイル: login.py プロジェクト: bbcf/biorepo
    def build_attribut_value(self, s, lab_id, dict_fixed_values_type,
                             list_fixed_values_type, config):
        '''
        build Attribut values
        @return a list of Attribut values
        '''
        list_objects_value = []
        for att in list_fixed_values_type:
            #build a dictionnary of fixed values : {'key':'[value1, value2, value3, ...}'}
            list_values = []
            list_values = (config.get(s + att, att)).split(',')
            dict_fixed_values_type[att] = list_values

        for k, list_values in dict_fixed_values_type.iteritems():
            att_tmp = DBSession.query(Attributs).filter(
                and_(Attributs.key == k), Attributs.lab_id == lab_id).first()
            for v in list_values:
                attributs_v = Attributs_values()
                attributs_v.attribut_id = att_tmp.id
                attributs_v.value = v
                attributs_v.deprecated = False
                list_objects_value.append(attributs_v)

        return list_objects_value
コード例 #7
0
ファイル: sample.py プロジェクト: bbcf/biorepo
    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("./")
コード例 #8
0
ファイル: sample.py プロジェクト: bbcf/biorepo
    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')
コード例 #9
0
ファイル: sample.py プロジェクト: bbcf/biorepo
    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}