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

[dir-api] update get dirents api

add 't' argument to control if only return file or dir list
This commit is contained in:
lian
2015-09-18 14:32:46 +08:00
parent c0b8f2df0c
commit 8f688faaee
2 changed files with 44 additions and 4 deletions

View File

@@ -1312,7 +1312,13 @@ class UpdateBlksLinkView(APIView):
url = gen_file_upload_url(token, 'update-blks-api') url = gen_file_upload_url(token, 'update-blks-api')
return Response(url) return Response(url)
def get_dir_entrys_by_id(request, repo, path, dir_id): def get_dir_entrys_by_id(request, repo, path, dir_id, request_type=None):
""" Get dirents in a dir
if request_type is 'f', only return file list,
if request_type is 'd', only return dir list,
else, return both.
"""
username = request.user.username username = request.user.username
try: try:
dirs = seafserv_threaded_rpc.list_dir_with_perm(repo.id, path, dir_id, dirs = seafserv_threaded_rpc.list_dir_with_perm(repo.id, path, dir_id,
@@ -1357,7 +1363,13 @@ def get_dir_entrys_by_id(request, repo, path, dir_id):
dir_list.sort(lambda x, y: cmp(x['name'].lower(), y['name'].lower())) dir_list.sort(lambda x, y: cmp(x['name'].lower(), y['name'].lower()))
file_list.sort(lambda x, y: cmp(x['name'].lower(), y['name'].lower())) file_list.sort(lambda x, y: cmp(x['name'].lower(), y['name'].lower()))
dentrys = dir_list + file_list
if request_type == 'f':
dentrys = file_list
elif request_type == 'd':
dentrys = dir_list
else:
dentrys = dir_list + file_list
response = HttpResponse(json.dumps(dentrys), status=200, response = HttpResponse(json.dumps(dentrys), status=200,
content_type=json_content_type) content_type=json_content_type)
@@ -2230,7 +2242,12 @@ class DirView(APIView):
response["oid"] = dir_id response["oid"] = dir_id
return response return response
else: else:
return get_dir_entrys_by_id(request, repo, path, dir_id) request_type = request.GET.get('t', None)
if request_type and request_type not in ('f', 'd'):
return api_error(status.HTTP_400_BAD_REQUEST,
"'t'(type) should be 'f' or 'd'.")
return get_dir_entrys_by_id(request, repo, path, dir_id, request_type)
def post(self, request, repo_id, format=None): def post(self, request, repo_id, format=None):
# new dir # new dir

View File

@@ -179,7 +179,30 @@ class FilesApiTest(ApiTestBase):
res = self.get(update_blks_url) res = self.get(update_blks_url)
self.assertRegexpMatches(res.text, r'"http(.*)/update-blks-api/[^/]+"') self.assertRegexpMatches(res.text, r'"http(.*)/update-blks-api/[^/]+"')
def test_list_dir(self): def test_only_list_dir(self):
with self.get_tmp_repo() as repo:
self.create_file(repo)
self.create_dir(repo)
dirents = self.get(repo.dir_url + '?t=d').json()
self.assertHasLen(dirents, 1)
for dirent in dirents:
self.assertIsNotNone(dirent['id'])
self.assertIsNotNone(dirent['name'])
self.assertEqual(dirent['type'], 'dir')
def test_only_list_file(self):
with self.get_tmp_repo() as repo:
self.create_file(repo)
self.create_dir(repo)
dirents = self.get(repo.dir_url + '?t=f').json()
self.assertHasLen(dirents, 1)
for dirent in dirents:
self.assertIsNotNone(dirent['id'])
self.assertIsNotNone(dirent['name'])
self.assertIsNotNone(dirent['size'])
self.assertEqual(dirent['type'], 'file')
def test_list_dir_and_file(self):
with self.get_tmp_repo() as repo: with self.get_tmp_repo() as repo:
self.create_file(repo) self.create_file(repo)
self.create_dir(repo) self.create_dir(repo)