def funding_award_select(filename=None, project_node_id=None): form = AwardSelectForm(filename=filename) # Process POST if request.method == 'POST': form_value = request.form form_dict = form_value.to_dict(flat=False) node_id = None new_page = PAGE_FUNDING_AWARD_SELECT if form_dict: for key in form_dict: val = form_dict[key][0] # value is the first list element if val == BTN_SAVE_AND_CONTINUE: new_page = PAGE_PROJECT elif val[0:4] == 'Back': new_page = PAGE_PROJECT elif val == BTN_EDIT: new_page = PAGE_FUNDING_AWARD node_id = key elif val == BTN_REMOVE: new_page = PAGE_FUNDING_AWARD_SELECT node_id = key eml_node = load_eml(filename=filename) remove_child(node_id=node_id) save_both_formats(filename=filename, eml_node=eml_node) elif val == UP_ARROW: new_page = PAGE_FUNDING_AWARD_SELECT node_id = key process_up_button(filename, node_id) elif val == DOWN_ARROW: new_page = PAGE_FUNDING_AWARD_SELECT node_id = key process_down_button(filename, node_id) elif val[0:3] == 'Add': new_page = PAGE_FUNDING_AWARD node_id = '1' else: new_page = check_val_for_hidden_buttons( val, new_page, PAGE_FUNDING_AWARD_SELECT) if form.validate_on_submit(): if node_id and project_node_id: url = url_for(new_page, filename=filename, node_id=node_id, project_node_id=project_node_id) elif project_node_id: url = url_for(new_page, filename=filename, project_node_id=project_node_id) else: url = url_for(new_page, filename=filename, node_id=node_id) return redirect(url) # Process GET return funding_award_select_get(filename=filename, form=form, project_node_id=project_node_id)
def load_geo_coverage(filename): form = LoadDataForm() document = current_user.get_filename() uploads_folder = get_document_uploads_folder_name() # Process POST if request.method == 'POST' and BTN_CANCEL in request.form: url = url_for(PAGE_GEOGRAPHIC_COVERAGE_SELECT, filename=filename) return redirect(url) if request.method == 'POST' and form.validate_on_submit(): form_value = request.form form_dict = form_value.to_dict(flat=False) new_page = None if form_dict: for key in form_dict: val = form_dict[key][0] # value is the first list element new_page = check_val_for_hidden_buttons(val, new_page, PAGE_LOAD_GEO_COVERAGE) if new_page: url = url_for(new_page, filename=filename) return redirect(url) # Check if the post request has the file part if 'file' not in request.files: flash('No file part', 'error') return redirect(request.url) file = request.files['file'] if file: filename = secure_filename(file.filename) if filename is None or filename == '': flash('No selected file', 'error') elif allowed_data_file(filename): filepath = os.path.join(uploads_folder, filename) file.save(filepath) data_file = filename data_file_path = f'{uploads_folder}/{data_file}' try: load_geo_coverage_from_csv(data_file_path, document) log_usage(actions['LOAD_GEOGRAPHIC_COVERAGE'], filename) flash(f'Loaded {data_file}') except ValueError as ex: flash(f'Load CSV file failed. {ex.args[0]}', 'error') return redirect(url_for(PAGE_GEOGRAPHIC_COVERAGE_SELECT, filename=document)) # Process GET help = [get_help('geographic_coverages_csv_file')] return render_template('load_geo_coverage.html', form=form, help=help)
def related_project_select_post(filename=None, form=None, form_dict=None, method=None, this_page=None, back_page=None, next_page=None, edit_page=None): project_node_id = None new_page = None if form_dict: for key in form_dict: val = form_dict[key][0] # value is the first list element if val == BTN_BACK: new_page = back_page elif val == BTN_NEXT or val == BTN_SAVE_AND_CONTINUE: new_page = next_page elif val == BTN_EDIT: new_page = edit_page project_node_id = key elif val == BTN_REMOVE: new_page = this_page project_node_id = key remove_related_project(filename, project_node_id) elif val == UP_ARROW: new_page = this_page node_id = key process_up_button(filename, node_id) elif val == DOWN_ARROW: new_page = this_page node_id = key process_down_button(filename, node_id) elif val[0:3] == BTN_ADD: new_page = edit_page project_node_id = '1' elif val[0:4] == BTN_BACK: new_page = edit_page project_node_id = None else: new_page = check_val_for_hidden_buttons( val, new_page, this_page) if form.validate_on_submit(): if new_page == edit_page: return url_for(new_page, filename=filename, project_node_id=project_node_id, title='Related Project') else: return url_for(new_page, filename=filename)
def select_new_page(back_page=None, next_page=None, edit_page=None): form_value = request.form form_dict = form_value.to_dict(flat=False) new_page = back_page if form_dict: for key in form_dict: val = form_dict[key][0] # value is the first list element if val == BTN_BACK: new_page = back_page break elif val in (BTN_NEXT, BTN_SAVE_AND_CONTINUE): new_page = next_page break else: new_page = check_val_for_hidden_buttons( val, new_page, back_page) return new_page
def taxonomic_coverage(filename=None, node_id=None, taxon=None): form = TaxonomicCoverageForm(filename=filename) # Process POST if request.method == 'POST' and BTN_CANCEL in request.form: url = url_for(PAGE_TAXONOMIC_COVERAGE_SELECT, filename=filename) return redirect(url) if request.method == 'POST' and form.validate_on_submit(): save = False if is_dirty_form(form): save = True # flash(f'save: {save}') form_value = request.form have_links = False if 'Fill' in form_value: source = form.taxonomic_authority.data if source == 'ITIS': source_type = TaxonomySourceEnum.ITIS elif source == 'NCBI': source_type = TaxonomySourceEnum.NCBI elif source == "WORMS": source_type = TaxonomySourceEnum.WORMS try: source_name = '' for choice in form.taxonomic_authority.choices: if choice[0] == source: source_name = choice[1] break hierarchy = fill_taxonomic_coverage(form.taxon_value.data, source_type, source_name) if hierarchy: log_usage(actions['FILL_TAXONOMIC_HIERARCHY'], form.taxon_value.data, source_name) # set the taxon rank dropdown appropriately rank = hierarchy[0][0].capitalize() if (rank, rank) in form.taxon_rank.choices: form.taxon_rank.data = rank # see if we should display a Links column for taxon in hierarchy: if taxon[4]: have_links = True break except ValueError as e: flash(str(e), 'error') hierarchy = [(form.taxon_rank.data, form.taxon_value.data, '', '')] form.hierarchy.data = hierarchy form.hidden_taxon_rank.data = form.taxon_rank.data form.hidden_taxon_value.data = form.taxon_value.data form.hidden_taxonomic_authority.data = form.taxonomic_authority.data help = get_helps(['taxonomic_coverage_fill_hierarchy']) return render_template('taxonomic_coverage.html', title='Taxonomic Coverage', form=form, hierarchy=hierarchy, taxon_rank=form.taxon_rank.data, taxon_value=form.taxon_value.data, taxonomic_authority=form.taxonomic_authority.data, help=help, have_links=have_links) form_dict = form_value.to_dict(flat=False) new_page = PAGE_TAXONOMIC_COVERAGE_SELECT this_page = PAGE_TAXONOMIC_COVERAGE if form_dict: for key in form_dict: val = form_dict[key][0] # value is the first list element new_page = check_val_for_hidden_buttons(val, new_page, this_page) if save: if not form.taxon_value.data and not form.taxon_rank.data: return redirect(url_for(new_page, filename=filename)) submitted_hierarchy = form_value.get('hierarchy') if isinstance(form_value.get('hierarchy'), str) and form_value.get('hierarchy'): # convert hierarchy string to list submitted_hierarchy = ast.literal_eval(form_value.get('hierarchy')) # if we're fixing up a failed hierarchy, we take the entered values as gospel if len(submitted_hierarchy) == 1: hierarchy = list(submitted_hierarchy[0]) if form.taxon_rank.data: hierarchy[0] = form.taxon_rank.data if form.taxon_value.data: hierarchy[1] = form.taxon_value.data submitted_hierarchy = [tuple(hierarchy)] form.hierarchy.data = submitted_hierarchy # if we're saving after doing 'Fill Hierarchy', fill in the values we've been passed if form_value.get('hidden_taxon_rank'): form.taxon_rank.data = form_value.get('hidden_taxon_rank') form.taxon_value.data = form_value.get('hidden_taxon_value') form.taxonomic_authority.data = form_value.get('hidden_taxonomic_authority') elif not submitted_hierarchy: # we don't have a hierarchy, so construct a fake hierarchy to be used by create_taxonomic_coverage() form.hierarchy.data = [( form_value.get('taxon_rank'), form_value.get('taxon_value'), '', '', '', '' )] if not form_value.get('taxon_rank'): flash('Taxon Rank is required.', 'error') return redirect(url_for(PAGE_TAXONOMIC_COVERAGE, filename=filename, node_id=node_id, taxon=form.taxon_value.data)) eml_node = load_eml(filename=filename) dataset_node = eml_node.find_child(names.DATASET) if not dataset_node: dataset_node = Node(names.DATASET) coverage_node = dataset_node.find_child(names.COVERAGE) if not coverage_node: coverage_node = Node(names.COVERAGE, parent=dataset_node) add_child(dataset_node, coverage_node) txc_node = Node(names.TAXONOMICCOVERAGE, parent=coverage_node) create_taxonomic_coverage( txc_node, form.general_taxonomic_coverage.data, form.hierarchy.data, form.taxonomic_authority.data) if node_id and len(node_id) != 1: old_txc_node = Node.get_node_instance(node_id) if old_txc_node: coverage_parent_node = old_txc_node.parent coverage_parent_node.replace_child(old_txc_node, txc_node) else: msg = f"No node found in the node store with node id {node_id}" raise Exception(msg) else: add_child(coverage_node, txc_node) save_both_formats(filename=filename, eml_node=eml_node) return redirect(url_for(new_page, filename=filename)) # Process GET have_links = False if node_id == '1': form.init_md5() if taxon: form.taxon_value.data = taxon else: eml_node = load_eml(filename=filename) dataset_node = eml_node.find_child(names.DATASET) if dataset_node: coverage_node = dataset_node.find_child(names.COVERAGE) if coverage_node: txc_nodes = coverage_node.find_all_children(names.TAXONOMICCOVERAGE) if txc_nodes: for txc_node in txc_nodes: if node_id == txc_node.id: have_links = populate_taxonomic_coverage_form(form, txc_node) help = get_helps(['taxonomic_coverage_fill_hierarchy']) set_current_page('taxonomic_coverage') return render_template('taxonomic_coverage.html', title='Taxonomic Coverage', form=form, hierarchy=form.hierarchy.data, have_links=have_links, help=help)
def temporal_coverage(filename=None, node_id=None): form = TemporalCoverageForm(filename=filename) tc_node_id = node_id # Process POST if request.method == 'POST' and BTN_CANCEL in request.form: url = url_for(PAGE_TEMPORAL_COVERAGE_SELECT, filename=filename) return redirect(url) if request.method == 'POST' and form.validate_on_submit(): save = False if is_dirty_form(form): save = True form_value = request.form form_dict = form_value.to_dict(flat=False) new_page = PAGE_TEMPORAL_COVERAGE_SELECT this_page = PAGE_TEMPORAL_COVERAGE if form_dict: for key in form_dict: val = form_dict[key][0] # value is the first list element new_page = check_val_for_hidden_buttons(val, new_page, this_page) url = url_for(new_page, filename=filename) if save: eml_node = load_eml(filename=filename) dataset_node = eml_node.find_child(names.DATASET) if not dataset_node: dataset_node = Node(names.DATASET) coverage_node = dataset_node.find_child(names.COVERAGE) if not coverage_node: coverage_node = Node(names.COVERAGE, parent=dataset_node) add_child(dataset_node, coverage_node) tc_node = Node(names.TEMPORALCOVERAGE, parent=coverage_node) begin_date_str = form.begin_date.data end_date_str = form.end_date.data create_temporal_coverage(tc_node, begin_date_str, end_date_str) if node_id and len(node_id) != 1: old_tc_node = Node.get_node_instance(node_id) if old_tc_node: coverage_parent_node = old_tc_node.parent coverage_parent_node.replace_child(old_tc_node, tc_node) else: msg = f"No node found in the node store with node id {node_id}" raise Exception(msg) else: add_child(coverage_node, tc_node) tc_node_id = tc_node.id flash_msg = compare_begin_end_dates(begin_date_str, end_date_str) if flash_msg: flash(flash_msg, 'error') url = (url_for(PAGE_TEMPORAL_COVERAGE, filename=filename, node_id=tc_node_id)) save_both_formats(filename=filename, eml_node=eml_node) return redirect(url) # Process GET if node_id == '1': form.init_md5() else: eml_node = load_eml(filename=filename) dataset_node = eml_node.find_child(names.DATASET) if dataset_node: coverage_node = dataset_node.find_child(names.COVERAGE) if coverage_node: tc_nodes = coverage_node.find_all_children(names.TEMPORALCOVERAGE) if tc_nodes: for tc_node in tc_nodes: if node_id == tc_node.id: populate_temporal_coverage_form(form, tc_node) set_current_page('temporal_coverage') return render_template('temporal_coverage.html', title='Temporal Coverage', form=form)
def geographic_coverage(filename=None, node_id=None): form = GeographicCoverageForm(filename=filename) # Process POST if request.method == 'POST' and BTN_CANCEL in request.form: url = url_for(PAGE_GEOGRAPHIC_COVERAGE_SELECT, filename=filename) return redirect(url) if request.method == 'POST' and form.validate_on_submit(): submit_type = None if is_dirty_form(form): submit_type = 'Save Changes' form_value = request.form form_dict = form_value.to_dict(flat=False) new_page = PAGE_GEOGRAPHIC_COVERAGE_SELECT this_page = PAGE_GEOGRAPHIC_COVERAGE if form_dict: for key in form_dict: val = form_dict[key][0] # value is the first list element new_page = check_val_for_hidden_buttons(val, new_page, this_page) url = url_for(new_page, filename=filename) if submit_type == 'Save Changes': eml_node = load_eml(filename=filename) dataset_node = eml_node.find_child(names.DATASET) if not dataset_node: dataset_node = Node(names.DATASET) coverage_node = dataset_node.find_child(names.COVERAGE) if not coverage_node: coverage_node = Node(names.COVERAGE, parent=dataset_node) add_child(dataset_node, coverage_node) geographic_description = form.geographic_description.data wbc = form.wbc.data if form.wbc.data is not None else '' ebc = form.ebc.data if form.ebc.data is not None else '' nbc = form.nbc.data if form.nbc.data is not None else '' sbc = form.sbc.data if form.sbc.data is not None else '' amin = form.amin.data if form.amin.data is not None else '' amax = form.amax.data if form.amax.data is not None else '' aunits = form.aunits.data if form.aunits.data is not None else '' gc_node = Node(names.GEOGRAPHICCOVERAGE, parent=coverage_node) create_geographic_coverage( gc_node, geographic_description, wbc, ebc, nbc, sbc, amin, amax, aunits) if node_id and len(node_id) != 1: old_gc_node = Node.get_node_instance(node_id) if old_gc_node: coverage_parent_node = old_gc_node.parent coverage_parent_node.replace_child(old_gc_node, gc_node) else: msg = f"No node found in the node store with node id {node_id}" raise Exception(msg) else: add_child(coverage_node, gc_node) if nbc and sbc and nbc < sbc: flash('North should be greater than or equal to South', 'error') url = (url_for(PAGE_GEOGRAPHIC_COVERAGE, filename=filename, node_id=gc_node.id)) if ebc and wbc and ebc < wbc: flash('East should be greater than or equal to West', 'error') url = (url_for(PAGE_GEOGRAPHIC_COVERAGE, filename=filename, node_id=gc_node.id)) save_both_formats(filename=filename, eml_node=eml_node) return redirect(url) # Process GET if node_id == '1': form.init_md5() else: eml_node = load_eml(filename=filename) dataset_node = eml_node.find_child(names.DATASET) if dataset_node: coverage_node = dataset_node.find_child(names.COVERAGE) if coverage_node: gc_nodes = coverage_node.find_all_children(names.GEOGRAPHICCOVERAGE) if gc_nodes: for gc_node in gc_nodes: if node_id == gc_node.id: populate_geographic_coverage_form(form, gc_node) set_current_page('geographic_coverage') help = get_helps(['geographic_coverages', 'geographic_description', 'bounding_coordinates', 'bounding_altitudes']) return render_template('geographic_coverage.html', title='Geographic Coverage', form=form, help=help)
def keyword(filename=None, node_id=None): eml_node = load_eml(filename=filename) dataset_node = eml_node.find_child(names.DATASET) if not dataset_node: dataset_node = Node(names.DATASET, parent=eml_node) add_child(eml_node, dataset_node) form = KeywordForm(filename=filename, node_id=node_id) form.init_keywords() # Process POST if request.method == 'POST' and BTN_CANCEL in request.form: url = url_for(PAGE_KEYWORD_SELECT, filename=filename) return redirect(url) # if request.method == 'POST' and form.validate_on_submit(): if request.method == 'POST': form_value = request.form form_dict = form_value.to_dict(flat=False) new_page = PAGE_KEYWORD_SELECT if form_dict: for key in form_dict: val = form_dict[key][0] # value is the first list element new_page = check_val_for_hidden_buttons( val, new_page, new_page) submit_type = None if is_dirty_form(form): submit_type = 'Save Changes' # flash(f'submit_type: {submit_type}') if submit_type == 'Save Changes': keyword = form.keyword.data keyword_type = form.keyword_type.data keyword_thesaurus = form.keyword_thesaurus.data # If so thesaurus was specified, see if the LTER Controlled Vocabulary applies if not keyword_thesaurus: lter_keywords = get_keywords('LTER') if keyword in lter_keywords: keyword_thesaurus = 'LTER Controlled Vocabulary' keyword_set_nodes = [] eml_node.find_all_descendants(names.KEYWORDSET, keyword_set_nodes) keyword_set_node = None for kws_node in keyword_set_nodes: keyword_thesaurus_node = kws_node.find_child( names.KEYWORDTHESAURUS) if keyword_thesaurus_node and keyword_thesaurus_node.content == keyword_thesaurus: keyword_set_node = kws_node break if not keyword_thesaurus_node and not keyword_thesaurus: keyword_set_node = kws_node break if not keyword_set_node: keyword_set_node = Node(names.KEYWORDSET, parent=dataset_node) add_child(dataset_node, keyword_set_node) if keyword_thesaurus: keyword_thesaurus_node = Node(names.KEYWORDTHESAURUS, parent=keyword_set_node) keyword_thesaurus_node.content = keyword_thesaurus keyword_set_node.children.append(keyword_thesaurus_node) keyword_node = Node(names.KEYWORD, parent=keyword_set_node) create_keyword(keyword_node, keyword, keyword_type) if node_id and len(node_id) != 1: old_keyword_node = Node.get_node_instance(node_id) if old_keyword_node: keyword_parent_node = old_keyword_node.parent keyword_parent_node.replace_child(old_keyword_node, keyword_node) else: msg = f"No keyword node found in the node store with node id {node_id}" raise Exception(msg) else: add_child(keyword_set_node, keyword_node) save_both_formats(filename=filename, eml_node=eml_node) url = url_for(new_page, filename=filename) return redirect(url) # Process GET if node_id == '1': form.init_md5() else: keyword_set_nodes = [] eml_node.find_all_descendants(names.KEYWORDSET, keyword_set_nodes) found = False for keyword_set_node in keyword_set_nodes: keyword_nodes = keyword_set_node.find_all_children(names.KEYWORD) keyword_thesaurus_node = keyword_set_node.find_child( names.KEYWORDTHESAURUS) if keyword_nodes: for kw_node in keyword_nodes: if node_id == kw_node.id: populate_keyword_form(form, kw_node, keyword_thesaurus_node) found = True break if found: break set_current_page('keyword') help = [get_help('keywords')] return render_template('keyword.html', title='Keyword', form=form, filename=filename, help=help)
def method_step_select(filename=None): form = MethodStepSelectForm(filename=filename) # Process POST if request.method == 'POST': form_value = request.form form_dict = form_value.to_dict(flat=False) node_id = '' new_page = '' url = '' this_page = PAGE_METHOD_STEP_SELECT edit_page = PAGE_METHOD_STEP back_page = PAGE_PUBLICATION_INFO next_page = PAGE_PROJECT if form_dict: for key in form_dict: val = form_dict[key][0] # value is the first list element if val == BTN_BACK: new_page = back_page elif val in [BTN_NEXT, BTN_SAVE_AND_CONTINUE]: new_page = next_page elif val == BTN_EDIT: new_page = edit_page node_id = key elif val == BTN_REMOVE: new_page = this_page node_id = key eml_node = load_eml(filename=filename) remove_child(node_id=node_id) save_both_formats(filename=filename, eml_node=eml_node) elif val == UP_ARROW: new_page = this_page node_id = key process_up_button(filename, node_id) elif val == DOWN_ARROW: new_page = this_page node_id = key process_down_button(filename, node_id) elif val[0:3] == 'Add': new_page = edit_page node_id = '1' elif val == '[ ]': new_page = this_page node_id = key else: new_page = check_val_for_hidden_buttons( val, new_page, this_page) if form.validate_on_submit(): if new_page in [edit_page, this_page]: url = url_for(new_page, filename=filename, node_id=node_id) else: url = url_for(new_page, filename=filename) return redirect(url) # Process GET method_step_list = [] title = 'Method Steps' eml_node = load_eml(filename=filename) if eml_node: dataset_node = eml_node.find_child(names.DATASET) if dataset_node: method_step_list = list_method_steps(dataset_node) set_current_page('method_step') help = [get_help('methods')] return render_template('method_step_select.html', title=title, filename=filename, method_step_list=method_step_list, form=form, help=help)
def method_step(filename=None, node_id=None): eml_node = load_eml(filename=filename) dataset_node = eml_node.find_child(names.DATASET) if dataset_node: methods_node = dataset_node.find_child(names.METHODS) else: dataset_node = Node(names.DATASET, parent=eml_node) add_child(eml_node, dataset_node) if not methods_node: methods_node = Node(names.METHODS, parent=dataset_node) add_child(dataset_node, methods_node) form = MethodStepForm(filename=filename, node_id=node_id) # Process POST if request.method == 'POST' and BTN_CANCEL in request.form: url = url_for(PAGE_METHOD_STEP_SELECT, filename=filename) return redirect(url) if request.method == 'POST' and form.validate_on_submit(): new_page = PAGE_METHOD_STEP_SELECT # Save or Back sends us back to the list of method steps form_value = request.form form_dict = form_value.to_dict(flat=False) if form_dict: for key in form_dict: val = form_dict[key][0] # value is the first list element new_page = check_val_for_hidden_buttons( val, new_page, PAGE_METHOD_STEP) submit_type = None if is_dirty_form(form): submit_type = 'Save Changes' if submit_type == 'Save Changes': description = form.description.data valid, msg = is_valid_xml_fragment(description, names.MAINTENANCE) if not valid: flash(invalid_xml_error_message(msg, False, names.DESCRIPTION), 'error') return render_get_method_step_page(eml_node, form, filename) instrumentation = form.instrumentation.data data_sources = form.data_sources.data method_step_node = Node(names.METHODSTEP, parent=methods_node) create_method_step(method_step_node, description, instrumentation, data_sources, data_sources_marker_begin, data_sources_marker_end) if node_id and len(node_id) != 1: old_method_step_node = Node.get_node_instance(node_id) if old_method_step_node: method_step_parent_node = old_method_step_node.parent method_step_parent_node.replace_child( old_method_step_node, method_step_node) else: msg = f"No methodStep node found in the node store with node id {node_id}" raise Exception(msg) else: add_child(methods_node, method_step_node) save_both_formats(filename=filename, eml_node=eml_node) url = url_for(new_page, filename=filename) return redirect(url) # Process GET if node_id == '1': form.init_md5() else: method_step_nodes = methods_node.find_all_children(names.METHODSTEP) if method_step_nodes: for ms_node in method_step_nodes: if node_id == ms_node.id: populate_method_step_form(form, ms_node) break return render_get_method_step_page(eml_node, form, filename)