Skip to content

Commit

Permalink
Pull Institutions from Canonical API (#3796)
Browse files Browse the repository at this point in the history
* Pull Institutions from Canonical API

* Move Institutions API endpoint to config

* Move Institutions API endpoint to config
  • Loading branch information
CannonLock authored Apr 17, 2024
1 parent 835a6c1 commit bf3e97a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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']),
Expand Down Expand Up @@ -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()
Expand Down
8 changes: 7 additions & 1 deletion src/webapp/forms.py
Original file line number Diff line number Diff line change
@@ -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, \
Expand Down Expand Up @@ -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,
})

Expand Down
1 change: 1 addition & 0 deletions src/webapp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit bf3e97a

Please sign in to comment.