1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-13 13:50:07 +00:00

WIP: finish dir_shared_items

This commit is contained in:
zhengxie
2015-07-13 17:06:54 +08:00
parent 69101f41f6
commit 1c6475d7d1
5 changed files with 478 additions and 259 deletions

View File

@@ -2250,222 +2250,6 @@ class DirShareView(APIView):
return HttpResponse(json.dumps({}), status=200, content_type=json_content_type)
class DirSharedItemsView(APIView):
authentication_classes = (TokenAuthentication, SessionAuthentication)
permission_classes = (IsAuthenticated,)
throttle_classes = (UserRateThrottle, )
def list_user_shared_items(self, request, repo_id, path):
username = request.user.username
if path == '/':
share_items = seafile_api.list_repo_shared_to(username, repo_id)
else:
share_items = seafile_api.get_shared_users_for_subdir(repo_id,
path, username)
ret = []
for item in share_items:
ret.append({
"share_type": "user",
"user_info": {
"name": item.user,
"nickname": email2nickname(item.user),
},
"permission": item.perm,
})
return ret
def list_group_shared_items(self, request, repo_id, path):
return []
def add_user_shared_item(self, request, repo_id, path):
pass
def handle_shared_to_args(self, request):
share_type = request.GET.get('share_type', None)
shared_to_user = False
shared_to_group = False
if share_type:
for e in share_type.split(','):
e = e.strip()
if e not in ['user', 'group']:
continue
if e == 'user':
shared_to_user = True
if e == 'group':
shared_to_group = True
else:
shared_to_user = True
shared_to_group = True
return (shared_to_user, shared_to_group)
def get_sub_repo_by_path(self, request, repo, path):
if path == '/':
raise Exception("Invalid path")
# get or create sub repo
username = request.user.username
if is_org_context(request):
org_id = request.user.org.org_id
sub_repo = seaserv.seafserv_threaded_rpc.get_org_virtual_repo(
org_id, repo.id, path, username)
else:
sub_repo = seafile_api.get_virtual_repo(repo.id, path, username)
return sub_repo
def get_or_create_sub_repo_by_path(self, request, repo, path):
username = request.user.username
sub_repo = self.get_sub_repo_by_path(request, repo, path)
if not sub_repo:
name = os.path.basename(path)
# create a sub-lib,
# use name as 'repo_name' & 'repo_desc' for sub_repo
if is_org_context(request):
org_id = request.user.org.org_id
sub_repo_id = seaserv.seafserv_threaded_rpc.create_org_virtual_repo(
org_id, repo.id, path, name, name, username)
else:
sub_repo_id = seafile_api.create_virtual_repo(repo.id, path,
name, name, username)
sub_repo = seafile_api.get_repo(sub_repo_id)
return sub_repo
def get(self, request, repo_id, format=None):
repo = get_repo(repo_id)
if not repo:
return api_error(status.HTTP_400_BAD_REQUEST, 'Repo not found.')
shared_to_user, shared_to_group = self.handle_shared_to_args(request)
path = request.GET.get('p', '/')
if seafile_api.get_dir_id_by_path(repo.id, path) is None:
return api_error(status.HTTP_400_BAD_REQUEST, 'Directory not found.')
ret = []
if shared_to_user:
ret += self.list_user_shared_items(request, repo_id, path)
if shared_to_group:
ret += self.list_group_shared_items(request, repo_id, path)
return HttpResponse(json.dumps(ret), status=200,
content_type=json_content_type)
def post(self, request, repo_id, format=None):
return HttpResponse(json.dumps([{'foo': 'bar'}]), status=200,
content_type=json_content_type)
def put(self, request, repo_id, format=None):
username = request.user.username
repo = get_repo(repo_id)
if not repo:
return api_error(status.HTTP_400_BAD_REQUEST, 'Repo not found.')
# TODO: perm check, quota check
path = request.GET.get('p', '/')
if seafile_api.get_dir_id_by_path(repo.id, path) is None:
return api_error(status.HTTP_400_BAD_REQUEST, 'Directory not found.')
if path != '/':
try:
sub_repo = self.get_sub_repo_by_path(request, repo, path)
except SearpcError as e:
logger.error(e)
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, 'Failed to get sub repo')
else:
sub_repo = None
share_type = request.DATA.get('share_type')
if share_type != 'user' and share_type != 'group':
return api_error(status.HTTP_400_BAD_REQUEST, 'Bad share type')
permission = request.DATA.get('permission', 'r')
if permission not in ['r', 'rw']:
return api_error(status.HTTP_400_BAD_REQUEST, 'Bad permission')
shared_repo = repo if path == '/' else sub_repo
success, failed = [], []
if share_type == 'user':
share_to_users = request.DATA.getlist('username')
for to_user in share_to_users:
try:
if is_org_context(request):
org_id = request.user.org.org_id
# org_share_repo(org_id, shared_repo.id, username, to_user, permission)
else:
seafile_api.share_repo(shared_repo.repo_id, username, to_user, permission)
# send a signal when sharing repo successful
share_repo_to_user_successful.send(sender=None,
from_user=username,
to_user=to_user,
repo=shared_repo)
success.append({
"share_type": "user",
"user_info": {
"name": to_user,
"nickname": email2nickname(to_user),
},
"permission": permission
})
except SearpcError as e:
logger.error(e)
failed.append(to_user)
continue
if share_type == 'group':
pass
return HttpResponse(json.dumps({
"success": success,
"failed": failed
}), status=200, content_type=json_content_type)
def delete(self, request, repo_id, format=None):
username = request.user.username
repo = get_repo(repo_id)
if not repo:
return api_error(status.HTTP_400_BAD_REQUEST, 'Repo not found.')
shared_to_user, shared_to_group = self.handle_shared_to_args(request)
path = request.GET.get('p', '/')
if seafile_api.get_dir_id_by_path(repo.id, path) is None:
return api_error(status.HTTP_400_BAD_REQUEST, 'Directory not found.')
if path == '/':
shared_repo = repo
else:
try:
sub_repo = self.get_sub_repo_by_path(request, repo, path)
if sub_repo:
shared_repo = sub_repo
else:
return api_error(status.HTTP_400_BAD_REQUEST, 'No sub repo found')
except SearpcError as e:
logger.error(e)
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, 'Failed to get sub repo')
if shared_to_user:
shared_to = request.GET.get('username')
if shared_to is None or not is_valid_username(shared_to):
return api_error(status.HTTP_400_BAD_REQUEST, 'Bad argument.')
if is_org_context(request):
org_id = request.user.org.org_id
# org_remove_share(org_id, repo_id, from_email, shared_to)
else:
seaserv.remove_share(shared_repo.id, username, shared_to)
if shared_to_group:
pass
return HttpResponse(json.dumps({'success': True}), status=200,
content_type=json_content_type)
class DirSubRepoView(APIView):
authentication_classes = (TokenAuthentication, )
permission_classes = (IsAuthenticated,)