diff --git a/seahub/api2/utils.py b/seahub/api2/utils.py index bd23de1612..b0a0b87e64 100644 --- a/seahub/api2/utils.py +++ b/seahub/api2/utils.py @@ -8,6 +8,7 @@ import re from collections import defaultdict from functools import wraps +from seahub import settings from django.core.paginator import EmptyPage, InvalidPage from django.http import HttpResponse @@ -554,3 +555,6 @@ def to_python_boolean(string): if string in ('f', 'false', '0'): return False raise ValueError("Invalid boolean value: '%s'" % string) + +def is_seafile_pro(): + return any(['seahub_extra' in app for app in settings.INSTALLED_APPS]) diff --git a/seahub/api2/views.py b/seahub/api2/views.py index 4640fafc26..5d6e390832 100644 --- a/seahub/api2/views.py +++ b/seahub/api2/views.py @@ -38,7 +38,7 @@ from .utils import is_repo_writable, is_repo_accessible, \ get_groups, get_group_and_contacts, prepare_events, \ get_person_msgs, api_group_check, get_email, get_timestamp, \ get_group_message_json, get_group_msgs, get_group_msgs_json, get_diff_details, \ - json_response, to_python_boolean + json_response, to_python_boolean, is_seafile_pro from seahub.avatar.templatetags.avatar_tags import api_avatar_url, avatar from seahub.avatar.templatetags.group_avatar_tags import api_grp_avatar_url, \ grp_avatar @@ -2192,28 +2192,40 @@ class DirView(APIView): username = request.user.username operation = request.POST.get('operation', '') + parent_dir = os.path.dirname(path) + parent_dir_utf8 = parent_dir.encode('utf-8') if operation.lower() == 'mkdir': if not is_repo_writable(repo.id, username): return api_error(status.HTTP_403_FORBIDDEN, 'You do not have permission to create folder.') - parent_dir = os.path.dirname(path) if check_folder_permission(request, repo_id, parent_dir) != 'rw': return api_error(status.HTTP_403_FORBIDDEN, 'Forbid to access this folder.') - parent_dir_utf8 = parent_dir.encode('utf-8') - new_dir_name = os.path.basename(path) - new_dir_name_utf8 = check_filename_with_rename_utf8(repo_id, - parent_dir, - new_dir_name) - - try: - seafile_api.post_dir(repo_id, parent_dir, - new_dir_name_utf8, username) - except SearpcError, e: - return api_error(HTTP_520_OPERATION_FAILED, - 'Failed to make directory.') + create_parents = request.POST.get('create_parents', '').lower() in ('true', '1') + if not create_parents: + new_dir_name = os.path.basename(path) + new_dir_name_utf8 = check_filename_with_rename_utf8(repo_id, + parent_dir, + new_dir_name) + try: + seafile_api.post_dir(repo_id, parent_dir, + new_dir_name_utf8, username) + except SearpcError, e: + return api_error(HTTP_520_OPERATION_FAILED, + 'Failed to make directory.') + else: + if not is_seafile_pro(): + return api_error(HTTP_400_BAD_REQUEST, + 'Feature not supported.') + try: + seafile_api.mkdir_with_parents(repo_id, '/', + path[1:], username) + except SearpcError, e: + return api_error(HTTP_520_OPERATION_FAILED, + 'Failed to make directory.') + new_dir_name_utf8 = os.path.basename(path).encode('utf-8') if request.GET.get('reloaddir', '').lower() == 'true': resp = reloaddir(request, repo, parent_dir) diff --git a/seahub/api2/views_misc.py b/seahub/api2/views_misc.py index 0dc14a4f44..4754425a7b 100644 --- a/seahub/api2/views_misc.py +++ b/seahub/api2/views_misc.py @@ -1,6 +1,6 @@ from rest_framework.views import APIView -from seahub.api2.utils import json_response +from seahub.api2.utils import json_response, is_seafile_pro from seahub import settings from seahub.utils import HAS_OFFICE_CONVERTER, HAS_FILE_SEARCH try: @@ -21,7 +21,7 @@ class ServerInfoView(APIView): features = ['seafile-basic'] - if any(['seahub_extra' in app for app in settings.INSTALLED_APPS]): + if is_seafile_pro(): features.append('seafile-pro') if HAS_OFFICE_CONVERTER: diff --git a/tests/api/test_files.py b/tests/api/test_files.py index e32f38f3b9..15b9c396c9 100644 --- a/tests/api/test_files.py +++ b/tests/api/test_files.py @@ -5,6 +5,7 @@ Test file/dir operations. import random import re +import pytest from urllib import urlencode, quote, quote from tests.common.utils import randstring, urljoin @@ -221,3 +222,24 @@ class FilesApiTest(ApiTestBase): } res = self.post(share_dir_url, data=data) self.assertEqual(res.text, u'{}') + + @pytest.mark.xfail + def test_create_dir_with_parents(self): + with self.get_tmp_repo() as repo: + path = u'/level1/level 2/level_3/目录4' + self.create_dir_with_parents(repo, path) + + def create_dir_with_parents(self, repo, path): + data = {'operation': 'mkdir', 'create_parents': 'true'} + durl = repo.get_dirpath_url(path.encode('utf-8')) + self.post(durl, data=data, expected=201) + curpath = '' + # check the parents are created along the way + parts = path.split('/') + for i, name in enumerate(parts): + curpath += '/' + name + url = repo.get_dirpath_url(curpath.encode('utf-8')) + if i < len(parts) - 1: + assert self.get(url).json()[0]['name'] == parts[i+1] + else: + assert self.get(url).json() == []