diff --git a/seahub/api2/views.py b/seahub/api2/views.py index f92b9c6b3c..c9bfcf22f3 100644 --- a/seahub/api2/views.py +++ b/seahub/api2/views.py @@ -67,6 +67,7 @@ from seahub.utils import gen_file_get_url, gen_token, gen_file_upload_url, \ gen_block_get_url, get_file_type_and_ext, HAS_FILE_SEARCH, \ gen_file_share_link, gen_dir_share_link, is_org_context, gen_shared_link, \ get_org_user_events, calculate_repos_last_modify +from seahub.utils.repo import get_sub_repo_abbrev_origin_path from seahub.utils.star import star_file, unstar_file from seahub.utils.file_types import IMAGE, DOCUMENT from seahub.utils.timeutils import utc_to_local @@ -559,18 +560,11 @@ class Repos(APIView): repos_json.append(repo) if filter_by['sub']: - def get_abbrev_origin_path(repo_name, path): - if len(path) > 20: - abbrev_path = path[-20:] - return repo_name + '/...' + abbrev_path - else: - return repo_name + path - # compose abbrev origin path for display sub_repos = [] sub_repos = get_virtual_repos_by_owner(request) for repo in sub_repos: - repo.abbrev_origin_path = get_abbrev_origin_path( + repo.abbrev_origin_path = get_sub_repo_abbrev_origin_path( repo.origin_repo_name, repo.origin_path) sub_repos.sort(lambda x, y: cmp(y.last_modify, x.last_modify)) diff --git a/seahub/utils/repo.py b/seahub/utils/repo.py index 14a6ed5736..787ae2a1fd 100644 --- a/seahub/utils/repo.py +++ b/seahub/utils/repo.py @@ -74,3 +74,15 @@ def check_group_folder_perm_args(from_user, repo_id, path, group_id, perm = None return {'success': True} +def get_sub_repo_abbrev_origin_path(repo_name, origin_path): + """Return abbrev path for sub repo based on `repo_name` and `origin_path`. + + Arguments: + - `repo_id`: + - `origin_path`: + """ + if len(origin_path) > 20: + abbrev_path = origin_path[-20:] + return repo_name + '/...' + abbrev_path + else: + return repo_name + origin_path diff --git a/seahub/views/ajax.py b/seahub/views/ajax.py index 220e07a607..4a2c902ef2 100644 --- a/seahub/views/ajax.py +++ b/seahub/views/ajax.py @@ -49,7 +49,7 @@ from seahub.utils import check_filename_with_rename, EMPTY_SHA1, \ get_org_user_events, get_user_events, get_file_type_and_ext, \ is_valid_username, send_perm_audit_msg from seahub.utils.repo import check_group_folder_perm_args, \ - check_user_folder_perm_args + check_user_folder_perm_args, get_sub_repo_abbrev_origin_path from seahub.utils.star import star_file, unstar_file from seahub.base.accounts import User from seahub.thumbnail.utils import get_thumbnail_src, allow_generate_thumbnail @@ -1255,17 +1255,35 @@ def sub_repo(request, repo_id): check if a dir has a corresponding sub_repo if it does not have, create one ''' + username = request.user.username content_type = 'application/json; charset=utf-8' result = {} + if not request.user.permissions.can_add_repo(): + result['error'] = _(u"You do not have permission to create library") + return HttpResponse(json.dumps(result), status=403, + content_type=content_type) + + origin_repo = seafile_api.get_repo(repo_id) + if origin_repo is None: + result['error'] = _('Repo not found.') + return HttpResponse(json.dumps(result), status=400, + content_type=content_type) + + # perm check, only repo owner can create sub repo + repo_owner = seafile_api.get_repo_owner(origin_repo.id) + is_repo_owner = True if username == repo_owner else False + if not is_repo_owner: + result['error'] = _(u"You do not have permission to create library") + return HttpResponse(json.dumps(result), status=403, + content_type=content_type) + path = request.GET.get('p') if not path: result['error'] = _('Argument missing') return HttpResponse(json.dumps(result), status=400, content_type=content_type) name = os.path.basename(path) - username = request.user.username - # check if the sub-lib exist try: if is_org_context(request): @@ -1275,7 +1293,8 @@ def sub_repo(request, repo_id): else: sub_repo = seafile_api.get_virtual_repo(repo_id, path, username) except SearpcError as e: - result['error'] = e.msg + logger.error(e) + result['error'] = _('Failed to create sub library, please try again later.') return HttpResponse(json.dumps(result), status=500, content_type=content_type) if sub_repo: @@ -1294,8 +1313,12 @@ def sub_repo(request, repo_id): username) result['sub_repo_id'] = sub_repo_id result['name'] = name - except SearpcError, e: - result['error'] = e.msg + result['abbrev_origin_path'] = get_sub_repo_abbrev_origin_path( + origin_repo.name, path) + + except SearpcError as e: + logger.error(e) + result['error'] = _('Failed to create sub library, please try again later.') return HttpResponse(json.dumps(result), status=500, content_type=content_type) return HttpResponse(json.dumps(result), content_type=content_type) diff --git a/static/scripts/app/views/myhome-sub-repos.js b/static/scripts/app/views/myhome-sub-repos.js index 8ebb5bf008..efde9d07f4 100644 --- a/static/scripts/app/views/myhome-sub-repos.js +++ b/static/scripts/app/views/myhome-sub-repos.js @@ -125,7 +125,7 @@ define([ 'name': data["name"], 'origin_repo_id': ori_repo_id, 'origin_path': path, - 'abbrev_origin_path': '', // TODO + 'abbrev_origin_path': data["abbrev_origin_path"], 'mtime': new Date().getTime() / 1000, 'mtime_relative': gettext("Just now") }, {prepend: true});