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

file participant batch add (#3914)

* file participants batch add view

* file participants batch add test

* file participants batch add in exist API

* fix database query
This commit is contained in:
sniper-py
2019-07-26 11:14:08 +08:00
committed by Daniel Pan
parent 4a4e280098
commit 75318ef1f9
2 changed files with 86 additions and 44 deletions

View File

@@ -62,7 +62,7 @@ class FileParticipantsView(APIView):
return Response({'participant_list': participant_list}) return Response({'participant_list': participant_list})
def post(self, request, repo_id, format=None): def post(self, request, repo_id, format=None):
"""Post a participant of a file. """batch add participants of a file.
""" """
# argument check # argument check
path = request.data.get('path') path = request.data.get('path')
@@ -70,41 +70,67 @@ class FileParticipantsView(APIView):
return api_error(status.HTTP_400_BAD_REQUEST, 'path invalid.') return api_error(status.HTTP_400_BAD_REQUEST, 'path invalid.')
path = normalize_file_path(path) path = normalize_file_path(path)
email = request.data.get('email') emails = request.data.get('emails')
if not email or not is_valid_username(email): if not emails or not isinstance(emails, list):
return api_error(status.HTTP_400_BAD_REQUEST, 'email invalid.') return api_error(status.HTTP_400_BAD_REQUEST, 'emails invalid.')
emails = list(set(emails))
# resource check # resource check
file_id = seafile_api.get_file_id_by_path(repo_id, path) file_id = seafile_api.get_file_id_by_path(repo_id, path)
if not file_id: if not file_id:
return api_error(status.HTTP_404_NOT_FOUND, 'File %s not found.' % path) return api_error(status.HTTP_404_NOT_FOUND, 'File %s not found.' % path)
try:
user = User.objects.get(email=email)
except User.DoesNotExist:
return api_error(status.HTTP_404_NOT_FOUND, 'User %s not found.' % email)
# permission check # permission check
if not check_folder_permission(request, repo_id, '/'): if not check_folder_permission(request, repo_id, '/'):
return api_error(status.HTTP_403_FORBIDDEN, 'Permission denied.') return api_error(status.HTTP_403_FORBIDDEN, 'Permission denied.')
if not seafile_api.check_permission_by_path(repo_id, '/', user.username): # batch add
return api_error(status.HTTP_403_FORBIDDEN, _('%s Permission denied.') % email) success = list()
failed = list()
# main
try: try:
file_uuid = FileUUIDMap.objects.get_or_create_fileuuidmap_by_path(repo_id, path, False) uuid = FileUUIDMap.objects.get_or_create_fileuuidmap_by_path(repo_id, path, False)
participants_queryset = FileParticipant.objects.get_participants(uuid)
if FileParticipant.objects.get_participant(file_uuid, email):
return api_error(status.HTTP_409_CONFLICT, _('Participant %s already exists.') % email)
FileParticipant.objects.add_participant(file_uuid, email)
participant = get_user_common_info(email)
except Exception as e: except Exception as e:
logger.error(e) logger.error(e)
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, 'Internal Server Error.') return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, 'Internal Server Error.')
return Response(participant, status=201) for email in emails:
if not is_valid_username(email):
error_dic = {'email': email, 'error_msg': 'email invalid.', 'error_code': 400}
failed.append(error_dic)
continue
try:
user = User.objects.get(email=email)
except User.DoesNotExist:
error_dic = {'email': email, 'error_msg': 'User not found.', 'error_code': 404}
failed.append(error_dic)
continue
# permission check
if not seafile_api.check_permission_by_path(repo_id, '/', user.username):
error_dic = {'email': email, 'error_msg': 'Permission denied.', 'error_code': 403}
failed.append(error_dic)
continue
# main
try:
if participants_queryset.filter(uuid=uuid, username=email).count() > 0:
error_dic = {'email': email, 'error_msg': 'Participant already exists.', 'error_code': 409}
failed.append(error_dic)
continue
FileParticipant.objects.add_participant(uuid, email)
participant = get_user_common_info(email)
success.append(participant)
except Exception as e:
logger.error(e)
error_dic = {'email': email, 'error_msg': 'Internal Server Error.', 'error_code': 500}
failed.append(error_dic)
continue
return Response({'success': success, 'failed': failed})
class FileParticipantView(APIView): class FileParticipantView(APIView):

View File

@@ -49,52 +49,68 @@ class FileParticipantsTest(BaseTestCase):
self.assertEqual(403, resp.status_code) self.assertEqual(403, resp.status_code)
def test_can_post(self): def test_can_post(self):
resp = self.client.post(self.url, { data = {
'email': self.tmp_user.username, 'emails': [self.tmp_user.username, self.admin.username],
'path': self.file, 'path': self.file,
}) }
self.assertEqual(201, resp.status_code) resp = self.client.post(self.url, json.dumps(data), 'application/json')
self.assertEqual(200, resp.status_code)
json_resp = json.loads(resp.content) json_resp = json.loads(resp.content)
assert json_resp['email'] == self.tmp_user.username assert len(json_resp['success']) == 1
assert json_resp['avatar_url'] assert json_resp['success'][0]['email'] == self.tmp_user.username
assert json_resp['contact_email']
assert json_resp['name'] assert len(json_resp['failed']) == 1
assert FileParticipant.objects.filter( assert json_resp['failed'][0]['email'] == self.admin.username
uuid=self.file_uuid, username=self.tmp_user.username).count() == 1
assert FileParticipant.objects.filter(uuid=self.file_uuid).count() == 2
def test_can_not_post_by_not_exists_path(self): def test_can_not_post_by_not_exists_path(self):
invalid_path = self.file + randstring(5) invalid_path = self.file + randstring(5)
resp = self.client.post(self.url, { data = {
'email': self.tmp_user.username, 'emails': [self.tmp_user.username],
'path': invalid_path, 'path': invalid_path,
}) }
resp = self.client.post(self.url, json.dumps(data), 'application/json')
self.assertEqual(404, resp.status_code) self.assertEqual(404, resp.status_code)
def test_can_not_post_by_invalid_user_permission(self): def test_can_not_post_by_invalid_user_permission(self):
self.logout() self.logout()
self.login_as(self.admin) self.login_as(self.admin)
resp = self.client.post(self.url, { data = {
'email': self.tmp_user.username, 'emails': [self.tmp_user.username],
'path': self.file, 'path': self.file,
}) }
resp = self.client.post(self.url, json.dumps(data), 'application/json')
self.assertEqual(403, resp.status_code) self.assertEqual(403, resp.status_code)
def test_can_not_post_by_not_exists_user(self): def test_can_not_post_by_not_exists_user(self):
invalid_email = randstring(5) + '@seafile.com' invalid_email = randstring(5) + '@seafile.com'
resp = self.client.post(self.url, { data = {
'email': invalid_email, 'emails': [invalid_email],
'path': self.file, 'path': self.file,
}) }
self.assertEqual(404, resp.status_code) resp = self.client.post(self.url, json.dumps(data), 'application/json')
self.assertEqual(200, resp.status_code)
json_resp = json.loads(resp.content)
assert len(json_resp['success']) == 0
assert len(json_resp['failed']) == 1
assert json_resp['failed'][0]['email'] == invalid_email
def test_can_not_post_by_invalid_email_permission(self): def test_can_not_post_by_invalid_email_permission(self):
resp = self.client.post(self.url, { data = {
'email': self.admin.username, 'emails': [self.admin.username],
'path': self.file, 'path': self.file,
}) }
self.assertEqual(403, resp.status_code) resp = self.client.post(self.url, json.dumps(data), 'application/json')
self.assertEqual(200, resp.status_code)
json_resp = json.loads(resp.content)
assert len(json_resp['success']) == 0
assert len(json_resp['failed']) == 1
assert json_resp['failed'][0]['email'] == self.admin.username
class FileParticipantTest(BaseTestCase): class FileParticipantTest(BaseTestCase):