mirror of
https://github.com/haiwen/seahub.git
synced 2025-09-05 08:53:14 +00:00
add api to create a dir with parents
This commit is contained in:
@@ -8,6 +8,7 @@ import re
|
|||||||
|
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
|
from seahub import settings
|
||||||
|
|
||||||
from django.core.paginator import EmptyPage, InvalidPage
|
from django.core.paginator import EmptyPage, InvalidPage
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
@@ -554,3 +555,6 @@ def to_python_boolean(string):
|
|||||||
if string in ('f', 'false', '0'):
|
if string in ('f', 'false', '0'):
|
||||||
return False
|
return False
|
||||||
raise ValueError("Invalid boolean value: '%s'" % string)
|
raise ValueError("Invalid boolean value: '%s'" % string)
|
||||||
|
|
||||||
|
def is_seafile_pro():
|
||||||
|
return any(['seahub_extra' in app for app in settings.INSTALLED_APPS])
|
||||||
|
@@ -38,7 +38,7 @@ from .utils import is_repo_writable, is_repo_accessible, \
|
|||||||
get_groups, get_group_and_contacts, prepare_events, \
|
get_groups, get_group_and_contacts, prepare_events, \
|
||||||
get_person_msgs, api_group_check, get_email, get_timestamp, \
|
get_person_msgs, api_group_check, get_email, get_timestamp, \
|
||||||
get_group_message_json, get_group_msgs, get_group_msgs_json, get_diff_details, \
|
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.avatar_tags import api_avatar_url, avatar
|
||||||
from seahub.avatar.templatetags.group_avatar_tags import api_grp_avatar_url, \
|
from seahub.avatar.templatetags.group_avatar_tags import api_grp_avatar_url, \
|
||||||
grp_avatar
|
grp_avatar
|
||||||
@@ -2192,28 +2192,40 @@ class DirView(APIView):
|
|||||||
|
|
||||||
username = request.user.username
|
username = request.user.username
|
||||||
operation = request.POST.get('operation', '')
|
operation = request.POST.get('operation', '')
|
||||||
|
parent_dir = os.path.dirname(path)
|
||||||
|
parent_dir_utf8 = parent_dir.encode('utf-8')
|
||||||
|
|
||||||
if operation.lower() == 'mkdir':
|
if operation.lower() == 'mkdir':
|
||||||
if not is_repo_writable(repo.id, username):
|
if not is_repo_writable(repo.id, username):
|
||||||
return api_error(status.HTTP_403_FORBIDDEN,
|
return api_error(status.HTTP_403_FORBIDDEN,
|
||||||
'You do not have permission to create folder.')
|
'You do not have permission to create folder.')
|
||||||
|
|
||||||
parent_dir = os.path.dirname(path)
|
|
||||||
if check_folder_permission(request, repo_id, parent_dir) != 'rw':
|
if check_folder_permission(request, repo_id, parent_dir) != 'rw':
|
||||||
return api_error(status.HTTP_403_FORBIDDEN, 'Forbid to access this folder.')
|
return api_error(status.HTTP_403_FORBIDDEN, 'Forbid to access this folder.')
|
||||||
|
|
||||||
parent_dir_utf8 = parent_dir.encode('utf-8')
|
create_parents = request.POST.get('create_parents', '').lower() in ('true', '1')
|
||||||
new_dir_name = os.path.basename(path)
|
if not create_parents:
|
||||||
new_dir_name_utf8 = check_filename_with_rename_utf8(repo_id,
|
new_dir_name = os.path.basename(path)
|
||||||
parent_dir,
|
new_dir_name_utf8 = check_filename_with_rename_utf8(repo_id,
|
||||||
new_dir_name)
|
parent_dir,
|
||||||
|
new_dir_name)
|
||||||
try:
|
try:
|
||||||
seafile_api.post_dir(repo_id, parent_dir,
|
seafile_api.post_dir(repo_id, parent_dir,
|
||||||
new_dir_name_utf8, username)
|
new_dir_name_utf8, username)
|
||||||
except SearpcError, e:
|
except SearpcError, e:
|
||||||
return api_error(HTTP_520_OPERATION_FAILED,
|
return api_error(HTTP_520_OPERATION_FAILED,
|
||||||
'Failed to make directory.')
|
'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':
|
if request.GET.get('reloaddir', '').lower() == 'true':
|
||||||
resp = reloaddir(request, repo, parent_dir)
|
resp = reloaddir(request, repo, parent_dir)
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
from rest_framework.views import APIView
|
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 import settings
|
||||||
from seahub.utils import HAS_OFFICE_CONVERTER, HAS_FILE_SEARCH
|
from seahub.utils import HAS_OFFICE_CONVERTER, HAS_FILE_SEARCH
|
||||||
try:
|
try:
|
||||||
@@ -21,7 +21,7 @@ class ServerInfoView(APIView):
|
|||||||
|
|
||||||
features = ['seafile-basic']
|
features = ['seafile-basic']
|
||||||
|
|
||||||
if any(['seahub_extra' in app for app in settings.INSTALLED_APPS]):
|
if is_seafile_pro():
|
||||||
features.append('seafile-pro')
|
features.append('seafile-pro')
|
||||||
|
|
||||||
if HAS_OFFICE_CONVERTER:
|
if HAS_OFFICE_CONVERTER:
|
||||||
|
@@ -5,6 +5,7 @@ Test file/dir operations.
|
|||||||
|
|
||||||
import random
|
import random
|
||||||
import re
|
import re
|
||||||
|
import pytest
|
||||||
from urllib import urlencode, quote, quote
|
from urllib import urlencode, quote, quote
|
||||||
|
|
||||||
from tests.common.utils import randstring, urljoin
|
from tests.common.utils import randstring, urljoin
|
||||||
@@ -221,3 +222,24 @@ class FilesApiTest(ApiTestBase):
|
|||||||
}
|
}
|
||||||
res = self.post(share_dir_url, data=data)
|
res = self.post(share_dir_url, data=data)
|
||||||
self.assertEqual(res.text, u'{}')
|
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() == []
|
||||||
|
Reference in New Issue
Block a user