Example #1
0
    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("./")
Example #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}
Example #3
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')
Example #4
0
def clone_form(user_lab, id_object):
    '''
    to clone dynamic measurement form
    '''
    lab = DBSession.query(Labs).filter(Labs.name == user_lab).first()
    #static list    ]
    list_static = [twf.HiddenField(id="IDselected", label_text="ID selected :"),
                    twf.TextField(id="name", label_text="Name :", placeholder="Measurement name...", validator=twc.Validator(required=True)),
                    twf.TextArea(id="description", label_text="Description :"),
                    twf.MultipleSelectField(id="samples", label_text="Your samples : ",
                    help_text="You can add some of your existing data to this project."),
                    twf.CheckBox(id="status_type", label_text="Privacy : ",
                    help_text="Check to have it available from outside EPFL (required for UCSC visualisation)"),
                    twf.CheckBox(id="type", label_text="Raw data : ", help_text="Check if raw data"),
                    #twf.MultipleSelectField(id="parents", label_text="Parents : ", help_text="Parent(s) of this measurement."),
                    twd.HidingRadioButtonList(id="upload_way", label_text='Upload my file via...', options=('my computer', 'a Vital-IT path', 'a URL'),
        mapping={
            'my computer': ['upload'],
            'a Vital-IT path': ['vitalit_path'],
            'a URL': ['url_path', 'url_up'],
        }),
    twf.FileField(id="upload", help_text='Please provide a data'),
    twf.TextField(id="vitalit_path", label_text="Scratch path", placeholder="/scratch/el/biorepo/dropbox/"),
    twf.TextField(id="url_path", label_text="File's url", placeholder="http://www..."),
    twf.CheckBox(id="url_up", label_text="I want to upload the file from this URL : ", help_text="tick it if you want to download it in BioRepo")
    ]

    list_dynamic = []
    if lab is None:
        print "----- no dynamic fields detected ---------"
        list_fields = [list_static, list_dynamic]
        return list_fields
    else:
        to_clone = DBSession.query(Measurements).filter(Measurements.id == int(id_object)).first()
        tag = "measurements"

        if to_clone is not None:
            list_dynamic_attributes = to_clone.attributs
            for att in list_dynamic_attributes:
                if att.deprecated == False:
                    twf_type = convert_widget(att.widget)
                    twf_type.id = att.key
                    list_a_values = att.values

                    if att.widget == "textfield" or att.widget == "textarea" or att.widget == "hiding_textfield" or att.widget == "hiding_textarea":
                        for v in list_a_values:
                            if hasattr(v, tag):
                                value_object = getattr(v, tag)
                            if to_clone in value_object:
                                twf_type.value = v.value
                        list_dynamic.append(twf_type)
                    elif att.widget == "checkbox" or att.widget == "hiding_checkbox":
                        for v in list_a_values:
                            if hasattr(v, tag):
                                value_object = getattr(v, tag)
                            if to_clone in value_object:
                                #dynamic boolean are stored in varchar in the db, we have to cast them in boolean for the display
                                value_2_display = check_boolean(v.value)
                                twf_type.value = value_2_display
                        list_dynamic.append(twf_type)
                    elif att.widget == "multipleselectfield" or att.widget == "hiding_multipleselectfield":
                        list_possible_values = []
                        for v in list_a_values:
                            list_possible_values.append(v.value)
                        twf_type.options = list_possible_values
                        selected_values = []
                        for v in list_a_values:
                            if hasattr(v, tag):
                                value_object = getattr(v, tag)
                            if to_clone in value_object:
                                selected_values.append(v.value)
                        twf_type.value = selected_values
                        list_dynamic.append(twf_type)

                    elif att.widget == "singleselectfield" or att.widget == "hiding_singleselectfield":
                        list_possible_values = []
                        for v in list_a_values:
                            if v.value not in list_possible_values:
                                list_possible_values.append(v.value)
                        twf_type.options = list_possible_values
                        for v in list_a_values:
                            if hasattr(v, tag):
                                value_object = getattr(v, tag)
                            if to_clone in value_object:
                                twf_type.value = v.value
                        list_dynamic.append(twf_type)

        else:
            print "Your measurement was not found. ID problem. id :", id_object
            raise

        list_fields = [list_static, list_dynamic]
        return list_fields
Example #5
0
def edit_form(user_lab, owner, id_object):
    '''
    to edit dynamic form
    '''
    lab = DBSession.query(Labs).filter(Labs.name == user_lab).first()
    #static lists
    list_static_samples = [twf.HiddenField(id="IDselected", label_text="ID selected :"),
                    twf.SingleSelectField(id="project", label_text="Your projects : ",
                    help_text="the project which contains your sample is selected", prompt_text=None),
                    twf.TextField(id="name", label_text="Name :", validator=twc.Required),
                    twf.SingleSelectField(id="type", label_text="Type : ",
                    help_text="What technique do you use ?", prompt_text=None),
                    twf.TextArea(id="protocole", label_text="Protocole :",),
                    twf.MultipleSelectField(id="measurements", label_text="Attached measurement(s) : ")
                    ]
    list_static_measurements = [
                    twf.HiddenField(id="IDselected", label_text="ID selected :"),
                    twf.TextField(id="name", label_text="Name :", placeholder="Measurement name...", validator=twc.Required),
                    twf.TextArea(id="description", label_text="Description :"),
                    twf.MultipleSelectField(id="samples", label_text="Your samples : ",
                    help_text="You can add some of your existing data to this project."),
                    twf.CheckBox(id="status_type", label_text="Privacy : ",
                    help_text="Check to have it available from outside EPFL (required for UCSC visualisation)"),
                    twf.CheckBox(id="type", label_text="Raw data : ", help_text="Check if raw data"),
                    twf.MultipleSelectField(id="parents", label_text="Parents : ", help_text="Parent(s) of this measurement."),
                    twf.LabelField(id="uploaded", help_text="is attached to this measurement. If you want to change, it's better to delete this measurement and create a new one."),
                    twf.TextField(id="url_path", help_text="If you want to add a new URL, your old URL will be stored into the description", placeholder="http://www...")
                    #twf.CheckBox(id="url_up", label_text="I want to upload the file from this URL : ",
                    #help_text="tick it if you want to download it in BioRepo")
                    ]

    list_dynamic_samples = []
    list_dynamic_measurements = []
    if lab is None:
        print "----- no dynamic fields detected ---------"
        list_fields = [list_static_samples, list_dynamic_samples, list_static_measurements, list_dynamic_measurements]
        return list_fields
    else:
        if owner == "sample":
            object_edited = DBSession.query(Samples).filter(Samples.id == int(id_object)).first()
            list_dynamic = list_dynamic_samples
            tag = "samples"
        elif owner == "meas":
            object_edited = DBSession.query(Measurements).filter(Measurements.id == int(id_object)).first()
            list_dynamic = list_dynamic_measurements
            tag = "measurements"
        else:
            print "----------------- owner error : ", owner, " <----owner --------------------"
            raise

        if object_edited is not None:
            list_dynamic_attributes = object_edited.attributs
            for att in list_dynamic_attributes:
                if att.deprecated == False:
                    twf_type = convert_widget(att.widget)
                    twf_type.id = att.key
                    list_a_values = att.values

                    if att.widget == "textfield" or att.widget == "textarea" or att.widget == "hiding_textfield" or att.widget == "hiding_textarea":
                        for v in list_a_values:
                            if hasattr(v, tag):
                                value_object = getattr(v, tag)
                            if object_edited in value_object:
                                twf_type.value = v.value
                        list_dynamic.append(twf_type)
                    elif att.widget == "checkbox" or att.widget == "hiding_checkbox":
                        for v in list_a_values:
                            if hasattr(v, tag):
                                value_object = getattr(v, tag)
                            if object_edited in value_object:
                                #dynamic boolean are stored in varchar in the db, we have to cast them in boolean for the display
                                value_2_display = check_boolean(v.value)
                                twf_type.value = value_2_display
                        list_dynamic.append(twf_type)
                    elif att.widget == "multipleselectfield" or att.widget == "hiding_multipleselectfield":
                        list_possible_values = []
                        for v in list_a_values:
                            list_possible_values.append(v.value)
                        twf_type.options = list_possible_values
                        selected_values = []
                        for v in list_a_values:
                            if hasattr(v, tag):
                                value_object = getattr(v, tag)
                            if object_edited in value_object:
                                selected_values.append(v.value)
                        twf_type.value = selected_values
                        list_dynamic.append(twf_type)

                    elif att.widget == "singleselectfield" or att.widget == "hiding_singleselectfield":
                        list_possible_values = []
                        for v in list_a_values:
                            if v.value not in list_possible_values:
                                list_possible_values.append(v.value)
                        twf_type.options = list_possible_values
                        for v in list_a_values:
                            if hasattr(v, tag):
                                value_object = getattr(v, tag)
                            if object_edited in value_object:
                                twf_type.value = v.value
                        list_dynamic.append(twf_type)

        else:
            print "Your ", owner, " was not found. ID problem. id :", id_object
            raise

        list_fields = [list_static_samples, list_dynamic_samples, list_static_measurements, list_dynamic_measurements]
        return list_fields
Example #6
0
File: root.py Project: bbcf/biorepo
    def get_meas_from_sample(self, mail, key, s_id):
        '''
        Get a JSON with all measurements from a given sample
        input : mail and biorepo key, s_id (sample id)
        output : {"sample id": [{"measurement id": {"name": "my measurement name", "status": "public/private",
        "type": "raw/processed", "description": "my description", "URL(only if public)": "http...",
         "dynamic field": "my dynamic field, ..."}}, ...]}
        '''
        user = DBSession.query(User).filter(User._email == mail).first()
        user_lab = user.labs
        list_measurements = []
        dico_final = {}
        target = DBSession.query(Samples).filter(Samples.id == s_id).first()
        if target is None:
            return {'ERROR': "This sample ID does not exist."}
        sample_project = DBSession.query(Projects).filter(Projects.id == target.project_id).first()
        lab_target = sample_project.labs[0]
        #check if the project is owned by the user or his lab
        access_ok = False
        for l in user_lab:
            if l.id == lab_target.id:
                access_ok = True
        if access_ok:
            measurements = target.measurements
            if len(measurements) == 0:
                return {'ERROR': 'This project id : ' + str(target.id) + ' is empty.'}
            for m in measurements:
                dico_meas = {}
                dico_dynamic = {}
                meas_attributs = m.attributs
                meas_a_values = m.a_values
                parent_id = []
                if len(m.parents) > 0:
                    for p in m.parents:
                        parent_id.append(p.id)
                children_id = []
                if len(m.children) > 0:
                    for c in m.children:
                        children_id.append(c.id)
                for att in meas_attributs:
                    att_id = att.id
                    att_key = att.key
                    for val in meas_a_values:
                        value = val.value
                        if val.attribut_id == att_id:
                            #for the true weird checkbox
                            if value == "true":
                                dico_dynamic[att_key] = att_key
                            else:
                                dico_dynamic[att_key] = value
                #check the weird checkbox widget with "false" value
                if len(meas_attributs) != len(dico_dynamic.keys()):
                    for att in meas_attributs:
                        att_key = att.key
                        att_widget = att.widget
                        if att_key not in dico_dynamic.keys() and att_widget == "checkbox":
                            dico_dynamic[att_key] = "Not " + str(att_key)
                        elif att_key not in dico_dynamic.keys() and att_widget != "checkbox":
                            dico_dynamic[att_key] = "Not specified"

                m_type = "processed data"
                if m.type:
                    m_type = "raw data"
                m_status = "private"
                if m.status_type:
                    m_status = "public"

                dico_meas = {"name": m.name, "status": m_status, "type": m_type, "description": m.description, "parent_id": parent_id, "children_id": children_id}
                if check_boolean(m.status_type):
                    if len(m.fus) > 0:
                        for fu in m.fus:
                            sha1 = fu.sha1
                            filename = fu.filename
                        dico_meas["URL"] = "/biorepo/public/public_link?m_id=" + str(m.id) + "&sha1=" + str(sha1)
                        dico_meas["filename"] = filename
                dico_meas.update(dico_dynamic)
                list_measurements.append({m.id: dico_meas})
            dico_final[s_id] = list_measurements
            return json.dumps(dico_final)

        else:
            return json.dumps({'ERROR': "This sample is not a sample from your lab, you cannot access to it."})
Example #7
0
def clone_form(user_lab, id_object):
    '''
    to clone dynamic measurement form
    '''
    lab = DBSession.query(Labs).filter(Labs.name == user_lab).first()
    #static list    ]
    list_static = [
        twf.HiddenField(id="IDselected", label_text="ID selected :"),
        twf.TextField(id="name",
                      label_text="Name :",
                      placeholder="Measurement name...",
                      validator=twc.Validator(required=True)),
        twf.TextArea(id="description", label_text="Description :"),
        twf.MultipleSelectField(
            id="samples",
            label_text="Your samples : ",
            help_text="You can add some of your existing data to this project."
        ),
        twf.CheckBox(
            id="status_type",
            label_text="Privacy : ",
            help_text=
            "Check to have it available from outside EPFL (required for UCSC visualisation)"
        ),
        twf.CheckBox(id="type",
                     label_text="Raw data : ",
                     help_text="Check if raw data"),
        #twf.MultipleSelectField(id="parents", label_text="Parents : ", help_text="Parent(s) of this measurement."),
        twd.HidingRadioButtonList(id="upload_way",
                                  label_text='Upload my file via...',
                                  options=('my computer', 'a Vital-IT path',
                                           'a URL'),
                                  mapping={
                                      'my computer': ['upload'],
                                      'a Vital-IT path': ['vitalit_path'],
                                      'a URL': ['url_path', 'url_up'],
                                  }),
        twf.FileField(id="upload", help_text='Please provide a data'),
        twf.TextField(id="vitalit_path",
                      label_text="Scratch path",
                      placeholder="/scratch/el/biorepo/dropbox/"),
        twf.TextField(id="url_path",
                      label_text="File's url",
                      placeholder="http://www..."),
        twf.CheckBox(id="url_up",
                     label_text="I want to upload the file from this URL : ",
                     help_text="tick it if you want to download it in BioRepo")
    ]

    list_dynamic = []
    if lab is None:
        print "----- no dynamic fields detected ---------"
        list_fields = [list_static, list_dynamic]
        return list_fields
    else:
        to_clone = DBSession.query(Measurements).filter(
            Measurements.id == int(id_object)).first()
        tag = "measurements"

        if to_clone is not None:
            list_dynamic_attributes = to_clone.attributs
            for att in list_dynamic_attributes:
                if att.deprecated == False:
                    twf_type = convert_widget(att.widget)
                    twf_type.id = att.key
                    list_a_values = att.values

                    if att.widget == "textfield" or att.widget == "textarea" or att.widget == "hiding_textfield" or att.widget == "hiding_textarea":
                        for v in list_a_values:
                            if hasattr(v, tag):
                                value_object = getattr(v, tag)
                            if to_clone in value_object:
                                twf_type.value = v.value
                        list_dynamic.append(twf_type)
                    elif att.widget == "checkbox" or att.widget == "hiding_checkbox":
                        for v in list_a_values:
                            if hasattr(v, tag):
                                value_object = getattr(v, tag)
                            if to_clone in value_object:
                                #dynamic boolean are stored in varchar in the db, we have to cast them in boolean for the display
                                value_2_display = check_boolean(v.value)
                                twf_type.value = value_2_display
                        list_dynamic.append(twf_type)
                    elif att.widget == "multipleselectfield" or att.widget == "hiding_multipleselectfield":
                        list_possible_values = []
                        for v in list_a_values:
                            list_possible_values.append(v.value)
                        twf_type.options = list_possible_values
                        selected_values = []
                        for v in list_a_values:
                            if hasattr(v, tag):
                                value_object = getattr(v, tag)
                            if to_clone in value_object:
                                selected_values.append(v.value)
                        twf_type.value = selected_values
                        list_dynamic.append(twf_type)

                    elif att.widget == "singleselectfield" or att.widget == "hiding_singleselectfield":
                        list_possible_values = []
                        for v in list_a_values:
                            if v.value not in list_possible_values:
                                list_possible_values.append(v.value)
                        twf_type.options = list_possible_values
                        for v in list_a_values:
                            if hasattr(v, tag):
                                value_object = getattr(v, tag)
                            if to_clone in value_object:
                                twf_type.value = v.value
                        list_dynamic.append(twf_type)

        else:
            print "Your measurement was not found. ID problem. id :", id_object
            raise

        list_fields = [list_static, list_dynamic]
        return list_fields
Example #8
0
def edit_form(user_lab, owner, id_object):
    '''
    to edit dynamic form
    '''
    lab = DBSession.query(Labs).filter(Labs.name == user_lab).first()
    #static lists
    list_static_samples = [
        twf.HiddenField(id="IDselected", label_text="ID selected :"),
        twf.SingleSelectField(
            id="project",
            label_text="Your projects : ",
            help_text="the project which contains your sample is selected",
            prompt_text=None),
        twf.TextField(id="name", label_text="Name :", validator=twc.Required),
        twf.SingleSelectField(id="type",
                              label_text="Type : ",
                              help_text="What technique do you use ?",
                              prompt_text=None),
        twf.TextArea(
            id="protocole",
            label_text="Protocole :",
        ),
        twf.MultipleSelectField(id="measurements",
                                label_text="Attached measurement(s) : ")
    ]
    list_static_measurements = [
        twf.HiddenField(id="IDselected", label_text="ID selected :"),
        twf.TextField(id="name",
                      label_text="Name :",
                      placeholder="Measurement name...",
                      validator=twc.Required),
        twf.TextArea(id="description", label_text="Description :"),
        twf.MultipleSelectField(
            id="samples",
            label_text="Your samples : ",
            help_text="You can add some of your existing data to this project."
        ),
        twf.CheckBox(
            id="status_type",
            label_text="Privacy : ",
            help_text=
            "Check to have it available from outside EPFL (required for UCSC visualisation)"
        ),
        twf.CheckBox(id="type",
                     label_text="Raw data : ",
                     help_text="Check if raw data"),
        twf.MultipleSelectField(id="parents",
                                label_text="Parents : ",
                                help_text="Parent(s) of this measurement."),
        twf.LabelField(
            id="uploaded",
            help_text=
            "is attached to this measurement. If you want to change, it's better to delete this measurement and create a new one."
        ),
        twf.TextField(
            id="url_path",
            help_text=
            "If you want to add a new URL, your old URL will be stored into the description",
            placeholder="http://www...")
        #twf.CheckBox(id="url_up", label_text="I want to upload the file from this URL : ",
        #help_text="tick it if you want to download it in BioRepo")
    ]

    list_dynamic_samples = []
    list_dynamic_measurements = []
    if lab is None:
        print "----- no dynamic fields detected ---------"
        list_fields = [
            list_static_samples, list_dynamic_samples,
            list_static_measurements, list_dynamic_measurements
        ]
        return list_fields
    else:
        if owner == "sample":
            object_edited = DBSession.query(Samples).filter(
                Samples.id == int(id_object)).first()
            list_dynamic = list_dynamic_samples
            tag = "samples"
        elif owner == "meas":
            object_edited = DBSession.query(Measurements).filter(
                Measurements.id == int(id_object)).first()
            list_dynamic = list_dynamic_measurements
            tag = "measurements"
        else:
            print "----------------- owner error : ", owner, " <----owner --------------------"
            raise

        if object_edited is not None:
            list_dynamic_attributes = object_edited.attributs
            for att in list_dynamic_attributes:
                if att.deprecated == False:
                    twf_type = convert_widget(att.widget)
                    twf_type.id = att.key
                    list_a_values = att.values

                    if att.widget == "textfield" or att.widget == "textarea" or att.widget == "hiding_textfield" or att.widget == "hiding_textarea":
                        for v in list_a_values:
                            if hasattr(v, tag):
                                value_object = getattr(v, tag)
                            if object_edited in value_object:
                                twf_type.value = v.value
                        list_dynamic.append(twf_type)
                    elif att.widget == "checkbox" or att.widget == "hiding_checkbox":
                        for v in list_a_values:
                            if hasattr(v, tag):
                                value_object = getattr(v, tag)
                            if object_edited in value_object:
                                #dynamic boolean are stored in varchar in the db, we have to cast them in boolean for the display
                                value_2_display = check_boolean(v.value)
                                twf_type.value = value_2_display
                        list_dynamic.append(twf_type)
                    elif att.widget == "multipleselectfield" or att.widget == "hiding_multipleselectfield":
                        list_possible_values = []
                        for v in list_a_values:
                            list_possible_values.append(v.value)
                        twf_type.options = list_possible_values
                        selected_values = []
                        for v in list_a_values:
                            if hasattr(v, tag):
                                value_object = getattr(v, tag)
                            if object_edited in value_object:
                                selected_values.append(v.value)
                        twf_type.value = selected_values
                        list_dynamic.append(twf_type)

                    elif att.widget == "singleselectfield" or att.widget == "hiding_singleselectfield":
                        list_possible_values = []
                        for v in list_a_values:
                            if v.value not in list_possible_values:
                                list_possible_values.append(v.value)
                        twf_type.options = list_possible_values
                        for v in list_a_values:
                            if hasattr(v, tag):
                                value_object = getattr(v, tag)
                            if object_edited in value_object:
                                twf_type.value = v.value
                        list_dynamic.append(twf_type)

        else:
            print "Your ", owner, " was not found. ID problem. id :", id_object
            raise

        list_fields = [
            list_static_samples, list_dynamic_samples,
            list_static_measurements, list_dynamic_measurements
        ]
        return list_fields