Skip to content

Commit

Permalink
Feature: create new team for grant project (#27)
Browse files Browse the repository at this point in the history
* add feat: create designated team for new project

* update docs

* use defined permissions

* remove redundant unicode string indication
  • Loading branch information
vpchung authored May 1, 2023
1 parent 352b9bd commit 420cffe
Showing 1 changed file with 56 additions and 14 deletions.
70 changes: 56 additions & 14 deletions portal_tables/sync_grants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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")
Expand Down Expand Up @@ -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):
Expand All @@ -109,20 +151,20 @@ 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
grants.at[_, 'grantId'] = project.id

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


Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 420cffe

Please sign in to comment.