Ejemplo n.º 1
0
def migrate_meta():
    from app.models import User, Role, Permission, \
    IUCNStatus, ESAStatus, OrganismType, GrowthFormRaunkiaer, ReproductiveRepetition, \
    DicotMonoc, AngioGymno, SpandExGrowthType, SourceType, Database, Purpose, MissingData, ContentEmail, Ecoregion, Continent, InvasiveStatusStudy, InvasiveStatusElsewhere, StageTypeClass, \
    TransitionType, MatrixComposition, StartSeason, StudiedSex, Captivity, Species, Taxonomy, Trait, \
    Publication, Study, AuthorContact, AdditionalSource, Population, Stage, StageType, Treatment, \
    MatrixStage, MatrixValue, Matrix, Interval, Fixed, Small, CensusTiming, PurposeEndangered, PurposeWeed, Institute

    print "Migrating Meta Tables..."
    try:
        Role.insert_roles()
        Species.migrate()
        Taxonomy.migrate()
        Trait.migrate()
        Publication.migrate()
        AuthorContact.migrate()
        Population.migrate()
        StageType.migrate()
        MatrixValue.migrate()
        Matrix.migrate()
        Fixed.migrate()
        Study.migrate()
        User.migrate()
        Version.migrate()
        Institute.migrate()
    except:
        "Error migrating metadata"
    finally:
        "Done + good"
   
    return
Ejemplo n.º 2
0
 def study_init(row):
     study = Study()
     num = Study.query.filter_by(name=row['Nom']).count()
     if num > 0:
         Study.query.delete()
     study.name = row['Nom']
     study.description = row['Description']
     return study
Ejemplo n.º 3
0
def add():
    form = StudyForm()
    if form.validate_on_submit():
        study = Study(name=form.name.data, description=form.description.data)
        db.session.add(study)
        db.session.commit()
        flash(_('Nouvelle etude ajouté avec succèss!'))
        return redirect(url_for('study.detail', id=study.id))
    return render_template('study/form.html', form=form)
Ejemplo n.º 4
0
def add_fake_data(number_users):
    """
    Adds fake data to the database.
    """
    study = Study()
    study.name = 'test_study'
    study.patients = 10
    study.variables = 10
    db.session.add(study)
    db.session.commit()
    variables = [Variable() for i in range(10)]
    for i, v in enumerate(variables):
        v.code = 'variable' + str(i)
        v.path = '\\\\folder1\\folder2\\variable' + str(i)
        v.type = 'NUMERIC'
        v.study_id = study.id
        db.session.add(v)
    db.session.commit()
Ejemplo n.º 5
0
def study():
    form = StudyForm()
    if form.validate_on_submit():
        study = Study(id_study=form.id_study.data, date_study=form.date_study.data, result_study=form.result_study.data)
        
        #save the model to the database
        db.session.add(study)
        db.session.commit()

        flash('Congratulations, you are now a registered study!')
        return redirect(url_for('index'))
    return render_template('study.html', title='validate', form=form)
Ejemplo n.º 6
0
def create_study(
    client,
    name="Test Study",
    description="",
    image="",
    data_values=0,
    number_of_columns=4,
    number_of_rows=4,
    start_date=date.today(),
    end_date=date.today() + timedelta(days=3),
    mail_sent=False,
    creator=None,
    user_group=None,
    card_set_x=None,
    card_set_y=None,
    data_value_labels=None,
):
    with client.application.test_request_context():
        if not creator:
            creator = create_admin(client)
        if not user_group:
            user_group = create_user_group(client, creator=creator)
        if not card_set_x:
            card_set_x = create_card_set(client, creator=creator)
        if not card_set_y:
            card_set_y = create_card_set(client, creator=creator)
        if not data_value_labels:
            data_value_labels = [
                create_data_value_label(client, creator=creator, label="You get the benefit (e.g. Personalised Service)"),
                create_data_value_label(client, creator=creator, label="Data Consumer gets the benefit (e.g. Supply Chain gets the benefit)")
            ]
        study = Study(
            name=name,
            description=description,
            image=image,
            data_values=data_values,
            number_of_columns=number_of_columns,
            number_of_rows=number_of_rows,
            start_date=start_date,
            end_date=end_date,
            mail_sent=mail_sent,
            creator=creator,
            user_group=user_group,
            card_set_x=card_set_x,
            card_set_y=card_set_y,
            data_value_labels=data_value_labels,
        )
        db.session.add(study)
        db.session.commit()

        return study
Ejemplo n.º 7
0
def submit_new(data):
    species = Species.query.filter_by(species_accepted=data["species_accepted"]).first()

    if species == None:
        species = Species(species_accepted=data["species_accepted"])        
        species.gbif_taxon_key = data["species_gbif_taxon_key"]
        species.species_author = data["species_author"]
        species.species_accepted = data["species_accepted"]        
        species.species_esa_status = ESAStatus.query.filter_by(status_code=data["species_esa_status_id"]).first()        
        species.species_common = data["species_common"]
        species.species_iucn_status = IUCNStatus.query.filter_by(status_code=data["species_iucn_status_id"]).first()

        db.session.add(species)
        db.session.commit()

        ''' Species Version '''
        species_version = Version()
        species_version.version_number = 0
        species_version.species = species
        db.session.add(species_version) 
        db.session.commit()  
        species_version.version_of_id = species_version.id
        species_version.checked = True
        species_version.checked_count = 1
        species_version.statuses = Status.query.filter_by(status_name="Green").first()
        species_version.user = User.query.filter_by(username="******").first()
        species_version.database = Database.query.filter_by(database_name="COMPADRE 4").first()

        db.session.add(species_version)
        db.session.commit()

    ''' Publication '''    
    if data["publication_DOI_ISBN"] == None:
        publication = Publication.query.filter_by(authors=data["publication_authors"]).filter_by(year=data["publication_year"]).filter_by(name=data["publication_journal_name"]).first()
    else: 
        publication = Publication.query.filter_by(DOI_ISBN=data["publication_DOI_ISBN"]).first()    
    if publication == None:
        publication = Publication()
        publication.authors = data["publication_authors"]
        publication.year = data["publication_year"]
        publication.DOI_ISBN = data["publication_DOI_ISBN"]
        publication.additional_source_string = data["publication_additional_source_string"]
        publication.name = data["publication_journal_name"]

        db.session.add(publication)
        db.session.commit()

        ''' Publication Version '''
        publication_version = Version()
        publication_version.version_number = 0
        publication_version.publication = publication
        db.session.add(publication_version) 
        db.session.commit()  
        publication_version.version_of_id = publication_version.id
        publication_version.checked = True
        publication_version.checked_count = 1
        publication_version.statuses = Status.query.filter_by(status_name="Green").first()
        publication_version.user = User.query.filter_by(username="******").first()
        publication_version.database = Database.query.filter_by(database_name="COMPADRE 4").first()

        db.session.add(publication_version)
        db.session.commit()

    ''' Trait '''
    spand_ex_growth_type = SpandExGrowthType.query.filter_by(type_name=data["trait_spand_ex_growth_type_id"]).first()
    dicot_monoc = DicotMonoc.query.filter_by(dicot_monoc_name=data["trait_dicot_monoc_id"]).first()
    growth_form_raunkiaer = GrowthFormRaunkiaer.query.filter_by(form_name=data["trait_growth_form_raunkiaer_id"]).first()
    organism_type = OrganismType.query.filter_by(type_name=data["trait_organism_type_id"]).first()
    angio_gymno = AngioGymno.query.filter_by(angio_gymno_name=data["trait_angio_gymno_id"]).first()

    trait = Trait.query.filter_by(species_id=species.id).first()

    if trait == None:
        trait = Trait()
        trait.species_id = species.id
        trait.organism_type = organism_type
        trait.dicot_monoc = dicot_monoc
        trait.angio_gymno = angio_gymno
        trait.spand_ex_growth_type = spand_ex_growth_type
        trait.growth_form_raunkiaer = growth_form_raunkiaer

        db.session.add(trait)
        db.session.commit()

        ''' Trait Version '''
        trait_version = Version()
        trait_version.version_number = 0
        trait_version.trait = trait
        db.session.add(trait_version) 
        db.session.commit()  
        trait_version.version_of_id = trait_version.id
        trait_version.checked = True
        trait_version.checked_count = 1
        trait_version.statuses = Status.query.filter_by(status_name="Green").first()
        trait_version.user = User.query.filter_by(username="******").first()
        trait_version.database = Database.query.filter_by(database_name="COMPADRE 4").first()

        db.session.add(trait_version)
        db.session.commit()

    ''' Study '''
    # What if all none? Will they be grouped together?
    purpose_endangered = PurposeEndangered.query.filter_by(purpose_name=data["study_purpose_endangered_id"]).first()
    purpose_weed = PurposeWeed.query.filter_by(purpose_name="study_purpose_weed_id").first()

    study = Study.query.filter_by(publication_id=publication.id, study_start=data["study_start"], study_end=data["study_end"]).first()
    if study == None:
        study = Study()
        study.study_duration = data["study_duration"]
        study.study_start = data["study_start"]
        study.study_end = data["study_end"]
        study.publication_id = publication.id
        study.number_populations = data["study_number_populations"]

        db.session.add(study)
        db.session.commit()

        ''' Study Version '''
        study_version = Version()
        study_version.version_number = 0
        study_version.study = study
        db.session.add(study_version) 
        db.session.commit()  
        study_version.version_of_id = study_version.id
        study_version.checked = True
        study_version.checked_count = 1
        study_version.statuses = Status.query.filter_by(status_name="Green").first()
        study_version.user = User.query.filter_by(username="******").first()
        study_version.database = Database.query.filter_by(database_name="COMPADRE 4").first()

        db.session.add(study_version)
        db.session.commit()


    ''' Population '''
    invasive_status_study = InvasiveStatusStudy.query.filter_by(status_name=data["population_invasive_status_study_id"]).first()
    invasive_status_elsewhere = InvasiveStatusStudy.query.filter_by(status_name=data["population_invasive_status_elsewhere_id"]).first()
    ecoregion = Ecoregion.query.filter_by(ecoregion_code=data["population_ecoregion_id"]).first()
    continent = Continent.query.filter_by(continent_name=data["population_continent_id"]).first()

    pop = Population.query.filter_by(name=data["population_name"], species_id=species.id, publication_id=publication.id).first()



    if pop == None:
        pop = Population()
        pop.species_author = data["species_author"]
        pop.name = data["population_name"]
        pop.species_id = species.id
        pop.publication_id = publication.id
        pop.study_id = study.id

        pop.longitude = data["population_longitude"]
        pop.latitude = data["population_latitude"]
        pop.altitude = data["population_altitude"]
        pop.pop_size = data["population_pop_size"]
        pop.country = data["population_country"]

        pop.invasive_status_study = invasive_status_study
        pop.invasive_status_elsewhere = invasive_status_elsewhere
        pop.ecoregion = ecoregion
        pop.continent = continent

        db.session.add(pop)
        db.session.commit()

        ''' Population Version '''
        population_version = Version()
        population_version.version_number = 0
        population_version.population = pop
        db.session.add(population_version) 
        db.session.commit()  
        population_version.version_of_id = population_version.id
        population_version.checked = True
        population_version.checked_count = 1
        population_version.statuses = Status.query.filter_by(status_name="Green").first()
        population_version.user = User.query.filter_by(username="******").first()
        population_version.database = Database.query.filter_by(database_name="COMPADRE 4").first()

        db.session.add(population_version)
        db.session.commit()

    ''' Taxonomy '''
    tax = Taxonomy.query.filter_by(species_id=species.id).first()
    if tax == None:
        tax = Taxonomy()
        tax.species_author = species.species_author
        tax.species_accepted = species.species_accepted
        tax.authority = None
        tax.tpl_version = None
        tax.infraspecies_accepted = None
        tax.species_epithet_accepted = None
        tax.genus_accepted = data["taxonomy_genus_accepted"]
        tax.genus = data["taxonomy_genus"]
        tax.family = data["taxonomy_family"]
        tax.tax_order = data["taxonomy_order"]
        tax.tax_class = data["taxonomy_class"]
        tax.phylum = data["taxonomy_phylum"]
        tax.kingdom = data["taxonomy_kingdom"]
        tax.species = species
        tax.publication = publication
        tax.col_check_date = data["taxonomy_col_check_date"]
        tax.col_check_ok = data["taxonomy_col_check_ok"]

        db.session.add(tax)
        db.session.commit()

        ''' Taxonomy Version '''
        taxonomy_version = Version()
        taxonomy_version.version_number = 0
        taxonomy_version.taxonomy = tax
        db.session.add(taxonomy_version) 
        db.session.commit()  
        taxonomy_version.version_of_id = taxonomy_version.id
        taxonomy_version.checked = True
        taxonomy_version.checked_count = 1
        taxonomy_version.statuses = Status.query.filter_by(status_name="Green").first()
        taxonomy_version.user = User.query.filter_by(username="******").first()
        taxonomy_version.database = Database.query.filter_by(database_name="COMPADRE 4").first()

        db.session.add(taxonomy_version)
        db.session.commit()

    ''' Matrix '''
    matrix = Matrix()
    treatment = Treatment.query.filter_by(treatment_name=data["matrix_treatment_id"]).first()
    
    if treatment == None:
        treatment = Treatment(treatment_name=data["matrix_treatment_id"])
    
    matrix.treatment = treatment
    matrix.matrix_split = data["matrix_split"]
    
    composition = MatrixComposition.query.filter_by(comp_name=data["matrix_composition_id"]).first()
    

    matrix.matrix_composition = composition

    
    matrix.survival_issue = data["matrix_survival_issue"]    
    matrix.periodicity = data["matrix_periodicity"]
    matrix.matrix_criteria_size = data["matrix_criteria_size"]
    matrix.matrix_criteria_ontogeny = coerce_boolean(data["matrix_criteria_ontogeny"])
    matrix.matrix_criteria_age = coerce_boolean(data["matrix_criteria_age"]) 
    
    matrix.matrix_start_month = data["matrix_start_month"]
    matrix.matrix_end_month = data["matrix_end_month"]
    matrix.matrix_start_year = data["matrix_start_year"]
    matrix.matrix_end_year = data["matrix_end_year"]

    matrix.studied_sex = StudiedSex.query.filter_by(sex_code=data["matrix_studied_sex_id"]).first()


    start_season = StartSeason.query.filter_by(season_id=data["matrix_start_season_id"]).first()
    end_season = EndSeason.query.filter_by(season_id=data["matrix_end_season_id"]).first()

    if start_season != None:
        matrix.start_season = start_season

    if end_season != None:
        matrix.end_season = end_season
        
    matrix.matrix_fec = coerce_boolean(data["matrix_fec"])

    matrix.matrix_a_string = data["matrix_a_string"]
    matrix.matrix_f_string = data["matrix_f_string"]
    matrix.matrix_u_string = data["matrix_u_string"]
    matrix.matrix_c_string = data["matrix_c_string"]

    matrix.non_independence = data["matrix_non_independence"]
    matrix.dimension = data["matrix_dimension"]
    matrix.non_independence_author = data["matrix_non_independence_author"]
    matrix.matrix_complete = data["matrix_complete"]
    matrix.vectors_includes_na = data["matrix_vectors_includes_na"]
    matrix.class_number = data["matrix_class_number"]
    matrix.observations = data["matrix_observations"]

    captivities = Captivity.query.filter_by(cap_code=data["matrix_captivity_id"]).first()

    matrix.captivities = captivities
    matrix.class_author = data["matrix_class_author"]
    matrix.matrix_difficulty = data["matrix_difficulty"]
    matrix.independent = data["matrix_independent"]
    matrix.seasonal = data["matrix_seasonal"]

    matrix.uid = generate_uid(species, publication, pop, matrix)

    matrix.population = pop
    matrix.study = study

    db.session.add(matrix)
    db.session.commit()

    ''' matrix Version '''
    matrix_version = Version()
    matrix_version.version_number = 0
    matrix_version.matrix = matrix
    db.session.add(matrix_version) 
    db.session.commit()
    matrix_version.version_of_id = matrix_version.id
    matrix_version.checked = True
    matrix_version.checked_count = 1
    matrix_version.statuses = Status.query.filter_by(status_name="Green").first()
    matrix_version.user = User.query.filter_by(username="******").first()
    matrix_version.database = Database.query.filter_by(database_name="COMPADRE 4").first()

    db.session.add(matrix_version)
    db.session.commit()

    ''' Fixed '''

    fixed = Fixed.query.filter_by(matrix=matrix).first()

    if fixed == None:
        fixed = Fixed()
        fixed.matrix = matrix
        fixed.census_timings = CensusTiming.query.filter_by(census_name=data["fixed_census_timing_id"]).first()
        fixed.seed_stage_error = data["fixed_seed_stage_error"]
        fixed.smalls = Small.query.filter_by(small_name=data["fixed_small_id"]).first()

        db.session.add(fixed)
        db.session.commit()

        ''' fixed Version '''
        fixed_version = Version()
        fixed_version.version_number = 0
        fixed_version.fixed = fixed
        db.session.add(fixed_version) 
        db.session.commit()
        fixed_version.version_of_id = fixed_version.id
        fixed_version.checked = True
        fixed_version.checked_count = 1
        fixed_version.statuses = Status.query.filter_by(status_name="Green").first()
        fixed_version.user = User.query.filter_by(username="******").first()
        fixed_version.database = Database.query.filter_by(database_name="COMPADRE 4").first()

        db.session.add(fixed_version)
        db.session.commit()