示例#1
0
文件: sample.py 项目: bbcf/biorepo
    def edit(self, *args, **kw):

        user = handler.user.get_user_in_session(request)
        sample = DBSession.query(Samples).filter(Samples.id == args[0]).first()
        admin = isAdmin(user)
        user_lab = session.get("current_lab", None)
        if user_lab:
            lab = DBSession.query(Labs).filter(Labs.name == user_lab).first()

        if admin:
            projects = DBSession.query(Projects).all()
            measurements = DBSession.query(Measurements).all()
        else:
            projects = DBSession.query(Projects).filter(Projects.user_id == user.id).all()
            for p in projects:
                if p not in lab.projects:
                    projects.remove(p)
            attributs = DBSession.query(Attributs).filter(and_(Attributs.lab_id == lab.id, Attributs.deprecated == False)).all()
            measurements = []
            for a in attributs:
                for m in a.measurements:
                    if m not in measurements and m.user_id == user.id:
                        measurements.append(m)

        if sample.get_userid == user.id or admin:

            edit_form = build_form("edit", "sample", sample.id)(action=url('/samples/post_edit'))
            edit_form.child.children[0].value = sample.id
            projects_list = [(p.id, '%s' % p.project_name) for p in projects]
            edit_form.child.children[1].options = projects_list
            id_project = DBSession.query(Projects.id).filter(Projects.id == sample.project_id).first()
            #measurement(s) attached to the sample
            list_unselected = [m for m in measurements if m not in sample.measurements]
            meas_selected = [(meas.id, '%s' % (meas.name)) for meas in list_unselected] + [(meas.id, '%s' % (meas.name), {'selected': True}) for meas in sample.measurements]
            edit_form.child.children[1].value = id_project[0]
            edit_form.child.children[2].value = sample.name
            edit_form.child.children[3].options = get_list_types(user_lab)
            edit_form.child.children[3].value = sample.type
            edit_form.child.children[4].value = sample.protocole
            edit_form.child.children[5].options = meas_selected

            return dict(page='samples', widget=edit_form.req(), value=kw)
        else:
            flash("It is not your sample -> you are not allowed to edit this sample", 'error')
            raise redirect('/samples')
示例#2
0
文件: sample.py 项目: bbcf/biorepo
    def new(self, *args, **kw):
        #take the logged user
        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()
        projects = DBSession.query(Projects).filter(Projects.user_id == user.id).all()
        for p in projects:
            if p not in lab.projects:
                projects.remove(p)

        new_form = build_form("new", "sample", None)(action=url('/samples/post')).req()
        #static fields
        new_form.child.children[0].options = [(project.id, '%s' % project.project_name) for project in projects]
        new_form.child.children[1].placeholder = "Your sample name..."
        new_form.child.children[2].options = get_list_types(user_lab)
        new_form.child.children[3].placeholder = "Your protocole here..."

        return dict(page='samples', widget=new_form)
示例#3
0
文件: search.py 项目: bbcf/biorepo
def get_sample_types():
    user_lab = session.get("current_lab", None)
    return [('%s' % type) for type in get_list_types(user_lab) if type != None]
示例#4
0
文件: search.py 项目: bbcf/biorepo
def get_sample_types():
    user_lab = session.get("current_lab", None)
    return [('%s' % type) for type in get_list_types(user_lab) if type != None]
示例#5
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}
示例#6
0
文件: root.py 项目: bbcf/biorepo
    def get_my_fields(self, mail, key):
        '''
        Get a JSON with all the Projects/Samples/Measurements fields by lab
        input : mail and biorepo key
        output : {"lab": {"Projects": [field1, field2], "Samples": [field1, field2, field3],
        "Measurements": [field1, field2, field3, field4]}}
        '''
        dico_by_labs = {}
        dico_fields = {}
        user = DBSession.query(User).filter(User._email == mail).first()
        if user is None:
            return {'ERROR': "User " + mail + " not in BioRepo."}
        else:
            user_labs = user.labs
            if len(user_labs) > 0:
                for lab in user_labs:
                    lab_name = lab.name
                    fields_projects = {'id': 'auto assigned id', 'user_id': 'auto assigned id', 'project_name': 'free text', 'description': 'free text'}
                    fields_samples = {'id': 'auto assigned id', 'project_id': 'auto assigned id', 'name': 'free text', 'type': get_list_types(lab_name), 'protocole': 'free text'}
                    fields_meas = {'id': 'auto assigned id', 'user_id': 'auto assigned id', 'name': 'free text', 'description': 'free text', 'status_type': ['Public', 'Private'], 'type': ['Raw', 'Processed']}
                    lab_id = lab.id
                    sample_attributs = DBSession.query(Attributs).filter(and_(Attributs.lab_id == lab_id, Attributs.owner == "sample", Attributs.deprecated == False)).all()
                    meas_attributs = DBSession.query(Attributs).filter(and_(Attributs.lab_id == lab_id, Attributs.owner == "measurement", Attributs.deprecated == False)).all()
                    for s_att in sample_attributs:
                        if not s_att.deprecated and (s_att.widget == "textfield" or s_att.widget == "textarea" or s_att.widget == "hiding_textfield" or s_att.widget == "hiding_area"):
                            fields_samples[s_att.key] = "free text"
                        elif not s_att.deprecated and (s_att.widget == "singleselectfield" or s_att.widget == "multiselectfield" or s_att.widget == "hiding_singleselectfield" or s_att.widget == "hiding_multiselectfield"):
                            att_id = s_att.id
                            values = DBSession.query(Attributs_values).filter(and_(Attributs_values.attribut_id == att_id, Attributs_values.deprecated == False)).all()
                            list_v = []
                            for v in values:
                                if v.value not in list_v:
                                    list_v.append(v.value)
                            fields_samples[s_att.key] = list_v
                        elif not s_att.deprecated and (s_att.widget == "checkbox" or s_att.widget == "hiding_checkbox"):
                            fields_samples[s_att.key] = [s_att.key, "Not " + str(s_att.key)]

                    for m_att in meas_attributs:
                        if not m_att.deprecated and (m_att.widget == "textfield" or m_att.widget == "textarea" or m_att.widget == "hiding_textfield" or m_att.widget == "hiding_area"):
                            fields_meas[m_att.key] = "free text"
                        elif not m_att.deprecated and (m_att.widget == "singleselectfield" or m_att.widget == "multiselectfield" or m_att.widget == "hiding_singleselectfield" or m_att.widget == "hiding_multiselectfield"):
                            att_id = m_att.id
                            values = DBSession.query(Attributs_values).filter(and_(Attributs_values.attribut_id == att_id, Attributs_values.deprecated == False)).all()
                            list_v = []
                            for v in values:
                                if v.value not in list_v:
                                    list_v.append(v.value)
                            fields_meas[m_att.key] = list_v
                        elif not m_att.deprecated and (m_att.widget == "checkbox" or m_att.widget == "hiding_checkbox"):
                            fields_meas[s_att.key] = [m_att.key, "Not " + str(m_att.key)]
                    dico_fields["Projects"] = fields_projects
                    dico_fields["Samples"] = fields_samples
                    fields_samples = {}
                    dico_fields["Measurements"] = fields_meas
                    fields_meas = {}
                    dico_by_labs[str(lab.name) + '(' + str(lab.id) + ')'] = dico_fields
                    dico_fields = {}
                return dico_by_labs

            else:
                return {'ERROR': "This user " + mail + " has no lab. Contact the administrator please."}