From 420cffee7652f7fbe5a5a3e71cf054e2c466f8bc Mon Sep 17 00:00:00 2001 From: Verena Chung <9377970+vpchung@users.noreply.github.com> Date: Mon, 1 May 2023 12:54:34 -0700 Subject: [PATCH] Feature: create new team for grant project (#27) * add feat: create designated team for new project * update docs * use defined permissions * remove redundant unicode string indication --- portal_tables/sync_grants.py | 70 ++++++++++++++++++++++++++++-------- 1 file changed, 56 insertions(+), 14 deletions(-) diff --git a/portal_tables/sync_grants.py b/portal_tables/sync_grants.py index 6293bdb9..4d17a488 100644 --- a/portal_tables/sync_grants.py +++ b/portal_tables/sync_grants.py @@ -2,12 +2,33 @@ This script will sync over new grants and its annotations to the Grants portal table. A Synapse Project with pre-filled Wikis and -Folders will also be created for each new grant found. +Folders will also be created for each new grant found, as well as +a Synapse team. """ import argparse import synapseclient +from synapseclient import Table, Project, Wiki, Folder, Team + +PERMISSIONS = { + "view": ["READ"], + "download": ["READ", "DOWNLOAD"], + "edit": ["READ", "DOWNLOAD", "CREATE", "UPDATE"], + "edit_delete": ["READ", "DOWNLOAD", "CREATE", "UPDATE", "DELETE"], + "admin": [ + "READ", + "DOWNLOAD", + "CREATE", + "UPDATE", + "DELETE", + "MODERATE", + "CHANGE_SETTINGS", + "CHANGE_PERMISSIONS", + ], +} + + def _syn_prettify(name): """Prettify a name that will conform to Synapse naming rules. @@ -18,6 +39,17 @@ def _syn_prettify(name): return name.translate(valid) +def _join_listlike_col(col, join_by="_", delim=","): + """Join list-like column values by specified value. + + Expects a list, but if string is given, then split (and strip + whitespace) by delimiter first. + """ + if isinstance(col, str): + col = [el.strip() for el in col.split(delim)] + return join_by.join(col).replace("'", "") + + def get_args(): """Set up command-line interface and get arguments.""" parser = argparse.ArgumentParser(description="Add new grants to the CCKP") @@ -87,14 +119,24 @@ def create_folders(syn, project_id): syn.store(Folder(name, parent=project_id)) -def syn_prettify(name): - """Prettify a name that will conform to Synapse naming rules. - - Names can only contain letters, numbers, spaces, underscores, hyphens, - periods, plus signs, apostrophes, and parentheses. - """ - valid = {38: 'and', 58: '-', 59: '-', 47: '_'} - return name.translate(valid) +def create_team(syn, project_id, grant, access_type="edit"): + """Create team for new grant project.""" + consortia = _join_listlike_col(grant["grantConsortiumName"]) + center = _join_listlike_col(grant["grantInstitutionAlias"]) + team_name = f"{consortia} {center} {grant['grantType']} {grant['grantNumber']}" + try: + new_team = Team(name=team_name, canPublicJoin=False) + new_team = syn.store(new_team) + syn.setPermissions( + project_id, + principalId=new_team.id, + accessType=PERMISSIONS.get(access_type) + ) + except ValueError as err: + if err.__context__.response.status_code == 409: + print(f"Team already exists: {team_name}") + else: + print(f"Something went wrong! Team: {team_name}") def create_grant_projects(syn, grants): @@ -109,9 +151,9 @@ def create_grant_projects(syn, grants): project = Project(name) project = syn.store(project) syn.setPermissions( - project.id, principalId=3450948, - accessType=['CREATE', 'READ', 'UPDATE', 'DELETE', 'DOWNLOAD', - 'CHANGE_PERMISSIONS', 'CHANGE_SETTINGS', 'MODERATE'], + project.id, + principalId=3450948, + accessType=PERMISSIONS.get("admin") ) # Update grants table with new synId @@ -119,10 +161,10 @@ def create_grant_projects(syn, grants): create_wiki_pages(syn, project.id, row) create_folders(syn, project.id) + create_team(syn, project.id, row) except synapseclient.core.exceptions.SynapseHTTPError: print(f"Skipping: {name}") grants.at[_, "grantId"] = "" - return grants @@ -185,7 +227,7 @@ def main(): else: print(f"{len(new_grants)} new grants found!\n") if args.dryrun: - print(u"\u26A0", "WARNING:", + print("\u26A0", "WARNING:", "dryrun is enabled (no updates will be done)\n") print(new_grants) else: