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

Commit b324992

Browse files
authored
feat: add command to share/unshare group virtual folders to users (#159)
* feat: add command/func to share/unshare group virtual folders to users * Add news fragment
1 parent e03d72b commit b324992

File tree

3 files changed

+76
-5
lines changed

3 files changed

+76
-5
lines changed

changes/159.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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.

src/ai/backend/client/cli/vfolder.py

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -406,18 +406,17 @@ def ls(name, path):
406406
@click.argument('name', type=str)
407407
@click.argument('emails', type=str, nargs=-1, required=True)
408408
@click.option('-p', '--perm', metavar='PERMISSION', type=str, default='rw',
409-
help='Permission to give. "ro" (read-only) / "rw" (read-write).')
409+
help='Permission to give. "ro" (read-only) / "rw" (read-write) / "wd" (write-delete).')
410410
def invite(name, emails, perm):
411-
"""Invite other users to access the virtual folder.
411+
"""Invite other users to access a user-type virtual folder.
412412
413413
\b
414414
NAME: Name of a virtual folder.
415-
EMAIL: Emails to invite.
415+
EMAILS: Emails to invite.
416416
"""
417417
with Session() as session:
418418
try:
419-
assert perm in ['rw', 'ro'], \
420-
'Invalid permission: {}'.format(perm)
419+
assert perm in ['rw', 'ro', 'wd'], 'Invalid permission: {}'.format(perm)
421420
result = session.VFolder(name).invite(perm, emails)
422421
invited_ids = result.get('invited_ids', [])
423422
if len(invited_ids) > 0:
@@ -488,6 +487,59 @@ def invitations():
488487
sys.exit(1)
489488

490489

490+
@vfolder.command()
491+
@click.argument('name', type=str)
492+
@click.argument('emails', type=str, nargs=-1, required=True)
493+
@click.option('-p', '--perm', metavar='PERMISSION', type=str, default='rw',
494+
help='Permission to give. "ro" (read-only) / "rw" (read-write) / "wd" (write-delete).')
495+
def share(name, emails, perm):
496+
"""Share a group folder to users with overriding permission.
497+
498+
\b
499+
NAME: Name of a (group-type) virtual folder.
500+
EMAILS: Emails to share.
501+
"""
502+
with Session() as session:
503+
try:
504+
assert perm in ['rw', 'ro', 'wd'], 'Invalid permission: {}'.format(perm)
505+
result = session.VFolder(name).share(perm, emails)
506+
shared_emails = result.get('shared_emails', [])
507+
if len(shared_emails) > 0:
508+
print('Shared with {} permission to:'.format(perm))
509+
for _email in shared_emails:
510+
print('\t- ' + _email)
511+
else:
512+
print('No users found. Folder is not shared.')
513+
except Exception as e:
514+
print_error(e)
515+
sys.exit(1)
516+
517+
518+
@vfolder.command()
519+
@click.argument('name', type=str)
520+
@click.argument('emails', type=str, nargs=-1, required=True)
521+
def unshare(name, emails):
522+
"""Unshare a group folder from users.
523+
524+
\b
525+
NAME: Name of a (group-type) virtual folder.
526+
EMAILS: Emails to share.
527+
"""
528+
with Session() as session:
529+
try:
530+
result = session.VFolder(name).unshare(emails)
531+
unshared_emails = result.get('unshared_emails', [])
532+
if len(unshared_emails) > 0:
533+
print('Unshared from:')
534+
for _email in unshared_emails:
535+
print('\t- ' + _email)
536+
else:
537+
print('No users found. Folder is not unshared.')
538+
except Exception as e:
539+
print_error(e)
540+
sys.exit(1)
541+
542+
491543
@vfolder.command()
492544
@click.argument('name', type=str)
493545
def leave(name):

src/ai/backend/client/func/vfolder.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,24 @@ async def umount_host(cls, name: str, edit_fstab: bool = False):
385385
async with rqst.fetch() as resp:
386386
return await resp.json()
387387

388+
@api_function
389+
async def share(self, perm: str, emails: Sequence[str]):
390+
rqst = Request('POST', '/folders/{}/share'.format(self.name))
391+
rqst.set_json({
392+
'permission': perm, 'emails': emails,
393+
})
394+
async with rqst.fetch() as resp:
395+
return await resp.json()
396+
397+
@api_function
398+
async def unshare(self, emails: Sequence[str]):
399+
rqst = Request('DELETE', '/folders/{}/unshare'.format(self.name))
400+
rqst.set_json({
401+
'emails': emails,
402+
})
403+
async with rqst.fetch() as resp:
404+
return await resp.json()
405+
388406
@api_function
389407
async def leave(self):
390408
rqst = Request('POST', '/folders/{}/leave'.format(self.name))

0 commit comments

Comments
 (0)