From bf3e97a80263e185fe1b757ddf7a128dbfb9fbb5 Mon Sep 17 00:00:00 2001 From: Cannon Lock <49032265+CannonLock@users.noreply.github.com> Date: Wed, 17 Apr 2024 14:42:45 -0500 Subject: [PATCH] Pull Institutions from Canonical API (#3796) * Pull Institutions from Canonical API * Move Institutions API endpoint to config * Move Institutions API endpoint to config --- src/app.py | 10 ++++++++-- src/webapp/forms.py | 8 +++++++- src/webapp/models.py | 1 + 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/app.py b/src/app.py index 2c1a4903e..68fbfbd8d 100755 --- a/src/app.py +++ b/src/app.py @@ -823,6 +823,12 @@ def render_form(**kwargs): @app.route("/generate_project_yaml", methods=["GET", "POST"]) def generate_project_yaml(): + institution_api_data = requests.get(f"{global_data.config.get('INSTITUTIONS_API')}/institution_ids").json() + institution_short_names = {x[1]: x[0] for x in global_data.get_mappings().project_institution.items()} + institutions = [] + for institution in institution_api_data: + institutions.append((institution['id'], institution['name'], institution_short_names.get(institution['name'], ""))) + def render_form(**kwargs): institutions = list(global_data.get_mappings().project_institution.items()) session.pop("form_data", None) @@ -871,7 +877,7 @@ def validate_project_name(form, field): # Gather necessary data create_pr_response = create_file_pr( file_path=f"projects/{request.values['project_name']}.yaml", - file_content=form.get_yaml(), + file_content=form.get_yaml(institution_api_data), branch=f"add-project-{request.values['project_name']}", message=f"Add Project {request.values['project_name']}", committer=GithubUser.from_token(session["github_login"]['access_token']), @@ -904,7 +910,7 @@ def validate_project_name(form, field): # Generate the yaml for manual addition if request.method == "POST" and "manual_submit" in request.form: - form.yaml_output.data = form.get_yaml() + form.yaml_output.data = form.get_yaml(institution_api_data) return render_form(form_complete=True) return render_form() diff --git a/src/webapp/forms.py b/src/webapp/forms.py index 002128715..c2cb96282 100644 --- a/src/webapp/forms.py +++ b/src/webapp/forms.py @@ -1,5 +1,7 @@ import datetime +from typing import Dict +import requests import yaml from flask_wtf import FlaskForm from wtforms import SelectField, SelectMultipleField, StringField, \ @@ -356,13 +358,17 @@ def validate_project_name(form, field): intersection = set(field.data).intersection(set('/<>:\"\\|?* ')) raise ValidationError(f"Must be valid filename, invalid chars: {','.join(intersection)}") - def get_yaml(self) -> str: + def get_yaml(self, institution_api_data: Dict) -> str: + + institutions_name_mapped = {i["name"]: i["id"] for i in institution_api_data} + return yaml.dump({ "Description": self.description.data, "FieldOfScience": self.field_of_science.data, "Department": self.pi_department_or_organization.data, "Organization": self.pi_institution.data, "PIName": f"{self.pi_first_name.data} {self.pi_last_name.data}", + "InstitutionID": institutions_name_mapped.get(self.pi_institution.data, 'Unknown'), "FieldOfScienceID": self.field_of_science_id.data, }) diff --git a/src/webapp/models.py b/src/webapp/models.py index cc8d13429..d9288586d 100644 --- a/src/webapp/models.py +++ b/src/webapp/models.py @@ -67,6 +67,7 @@ def __init__(self, config=None, strict=False): config = {} config.setdefault("TOPOLOGY_DATA_DIR", ".") config.setdefault("CONTACT_DATA_DIR", None) + config.setdefault("INSTITUTIONS_API", "https://topology-institutions.osg-htc.org/api") config.setdefault("CILOGON_LDAP_URL", "ldaps://ldap.cilogon.org") config.setdefault("CILOGON_LDAP_USER", "uid=readonly_user,ou=system,o=OSG,o=CO,dc=cilogon,dc=org")