Esempio n. 1
0
    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')
Esempio n. 2
0
    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}