Skip to content
This repository has been archived by the owner on Sep 22, 2023. It is now read-only.

Commit

Permalink
feat: add command to share/unshare group virtual folders to users (#159)
Browse files Browse the repository at this point in the history
* feat: add command/func to share/unshare group virtual folders to users

* Add news fragment
  • Loading branch information
adrysn committed May 5, 2021
1 parent e03d72b commit b324992
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 5 deletions.
1 change: 1 addition & 0 deletions changes/159.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add commands to share/unshare a group virtual folder directly to specific users. This is to allow specified users (usually teachers with user account) can upload data/materials to a virtual folder while it is shared as read-only for other group users.
62 changes: 57 additions & 5 deletions src/ai/backend/client/cli/vfolder.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,18 +406,17 @@ def ls(name, path):
@click.argument('name', type=str)
@click.argument('emails', type=str, nargs=-1, required=True)
@click.option('-p', '--perm', metavar='PERMISSION', type=str, default='rw',
help='Permission to give. "ro" (read-only) / "rw" (read-write).')
help='Permission to give. "ro" (read-only) / "rw" (read-write) / "wd" (write-delete).')
def invite(name, emails, perm):
"""Invite other users to access the virtual folder.
"""Invite other users to access a user-type virtual folder.
\b
NAME: Name of a virtual folder.
EMAIL: Emails to invite.
EMAILS: Emails to invite.
"""
with Session() as session:
try:
assert perm in ['rw', 'ro'], \
'Invalid permission: {}'.format(perm)
assert perm in ['rw', 'ro', 'wd'], 'Invalid permission: {}'.format(perm)
result = session.VFolder(name).invite(perm, emails)
invited_ids = result.get('invited_ids', [])
if len(invited_ids) > 0:
Expand Down Expand Up @@ -488,6 +487,59 @@ def invitations():
sys.exit(1)


@vfolder.command()
@click.argument('name', type=str)
@click.argument('emails', type=str, nargs=-1, required=True)
@click.option('-p', '--perm', metavar='PERMISSION', type=str, default='rw',
help='Permission to give. "ro" (read-only) / "rw" (read-write) / "wd" (write-delete).')
def share(name, emails, perm):
"""Share a group folder to users with overriding permission.
\b
NAME: Name of a (group-type) virtual folder.
EMAILS: Emails to share.
"""
with Session() as session:
try:
assert perm in ['rw', 'ro', 'wd'], 'Invalid permission: {}'.format(perm)
result = session.VFolder(name).share(perm, emails)
shared_emails = result.get('shared_emails', [])
if len(shared_emails) > 0:
print('Shared with {} permission to:'.format(perm))
for _email in shared_emails:
print('\t- ' + _email)
else:
print('No users found. Folder is not shared.')
except Exception as e:
print_error(e)
sys.exit(1)


@vfolder.command()
@click.argument('name', type=str)
@click.argument('emails', type=str, nargs=-1, required=True)
def unshare(name, emails):
"""Unshare a group folder from users.
\b
NAME: Name of a (group-type) virtual folder.
EMAILS: Emails to share.
"""
with Session() as session:
try:
result = session.VFolder(name).unshare(emails)
unshared_emails = result.get('unshared_emails', [])
if len(unshared_emails) > 0:
print('Unshared from:')
for _email in unshared_emails:
print('\t- ' + _email)
else:
print('No users found. Folder is not unshared.')
except Exception as e:
print_error(e)
sys.exit(1)


@vfolder.command()
@click.argument('name', type=str)
def leave(name):
Expand Down
18 changes: 18 additions & 0 deletions src/ai/backend/client/func/vfolder.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,24 @@ async def umount_host(cls, name: str, edit_fstab: bool = False):
async with rqst.fetch() as resp:
return await resp.json()

@api_function
async def share(self, perm: str, emails: Sequence[str]):
rqst = Request('POST', '/folders/{}/share'.format(self.name))
rqst.set_json({
'permission': perm, 'emails': emails,
})
async with rqst.fetch() as resp:
return await resp.json()

@api_function
async def unshare(self, emails: Sequence[str]):
rqst = Request('DELETE', '/folders/{}/unshare'.format(self.name))
rqst.set_json({
'emails': emails,
})
async with rqst.fetch() as resp:
return await resp.json()

@api_function
async def leave(self):
rqst = Request('POST', '/folders/{}/leave'.format(self.name))
Expand Down

0 comments on commit b324992

Please sign in to comment.