From a9200e8fa25c00a1f44e2f8d8f308ad71f333b63 Mon Sep 17 00:00:00 2001 From: wangjianhui Date: Wed, 10 Oct 2018 21:52:22 +0800 Subject: [PATCH] comment-detail --- seahub/api2/endpoints/file_comment.py | 27 +++++++++++++++------- seahub/api2/endpoints/file_comments.py | 32 +++++++++++++++++--------- seahub/base/models.py | 10 ++++---- 3 files changed, 46 insertions(+), 23 deletions(-) diff --git a/seahub/api2/endpoints/file_comment.py b/seahub/api2/endpoints/file_comment.py index 3ac9690f82..dc6df59dc8 100644 --- a/seahub/api2/endpoints/file_comment.py +++ b/seahub/api2/endpoints/file_comment.py @@ -69,14 +69,15 @@ class FileCommentView(APIView): """Update a comment, only comment author or repo owner can perform this op 1.Change resolved of comment + 2.Add comment_detail """ # argument check resolved = request.data.get('resolved') - if resolved not in ('true', 'false'): + if resolved not in ('true', 'false', None): error_msg = 'resolved invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) - comment_resolved = to_python_boolean(resolved) + detail = request.data.get('detail') # resource check try: @@ -91,12 +92,22 @@ class FileCommentView(APIView): error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) - try: - file_comment.resolved = comment_resolved - file_comment.save() - except Exception as e: - logger.error(e) - return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, 'Internal error.') + if resolved is not None: + comment_resolved = to_python_boolean(resolved) + try: + file_comment.resolved = comment_resolved + file_comment.save() + except Exception as e: + logger.error(e) + return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, 'Internal error.') + + if detail is not None: + try: + file_comment.detail = detail + file_comment.save() + except Exception as e: + logger.error(e) + return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, 'Internal error.') try: avatar_size = int(request.GET.get('avatar_size', AVATAR_DEFAULT_SIZE)) diff --git a/seahub/api2/endpoints/file_comments.py b/seahub/api2/endpoints/file_comments.py index 9cfac2cfa0..607471f778 100644 --- a/seahub/api2/endpoints/file_comments.py +++ b/seahub/api2/endpoints/file_comments.py @@ -20,6 +20,7 @@ from seahub.base.models import FileComment from seahub.utils.repo import get_repo_owner from seahub.signals import comment_file_successful from seahub.api2.endpoints.utils import generate_links_header_for_paginator +from seahub.views import check_folder_permission logger = logging.getLogger(__name__) @@ -41,6 +42,10 @@ class FileCommentsView(APIView): error_msg = 'resolved invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) + # permission check + if check_folder_permission(request, repo_id, '/') is None: + return api_error(status.HTTP_403_FORBIDDEN, 'Permission denied.') + try: avatar_size = int(request.GET.get('avatar_size', AVATAR_DEFAULT_SIZE)) @@ -79,33 +84,39 @@ class FileCommentsView(APIView): def post(self, request, repo_id, format=None): """Post a comments of a file. """ + # argument check path = request.GET.get('p', '/').rstrip('/') if not path: return api_error(status.HTTP_400_BAD_REQUEST, 'Wrong path.') + comment = request.data.get('comment', '') + if not comment: + return api_error(status.HTTP_400_BAD_REQUEST, 'Comment can not be empty.') + try: avatar_size = int(request.GET.get('avatar_size', AVATAR_DEFAULT_SIZE)) except ValueError: avatar_size = AVATAR_DEFAULT_SIZE + # resource check try: - obj_id = seafile_api.get_file_id_by_path(repo_id, - path) + file_id = seafile_api.get_file_id_by_path(repo_id, path) except SearpcError as e: logger.error(e) return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, 'Internal error.') - if not obj_id: + if not file_id: return api_error(status.HTTP_404_NOT_FOUND, 'File not found.') - comment = request.data.get('comment', '') - if not comment: - return api_error(status.HTTP_400_BAD_REQUEST, 'Comment can not be empty.') + # permission check + if check_folder_permission(request, repo_id, '/') is None: + return api_error(status.HTTP_403_FORBIDDEN, 'Permission denied.') + detail = request.data.get('detail', '') username = request.user.username - o = FileComment.objects.add_by_file_path( - repo_id=repo_id, file_path=path, author=username, comment=comment) + file_comment = FileComment.objects.add_by_file_path( + repo_id=repo_id, file_path=path, author=username, comment=comment, detail=detail) repo = seafile_api.get_repo(repo_id) repo_owner = get_repo_owner(request, repo.id) comment_file_successful.send(sender=None, @@ -115,7 +126,6 @@ class FileCommentsView(APIView): comment=comment, author=username) - comment = o.to_dict() - comment.update(user_to_dict(request.user.username, request=request, - avatar_size=avatar_size)) + comment = file_comment.to_dict() + comment.update(user_to_dict(username, request=request, avatar_size=avatar_size)) return Response(comment, status=201) diff --git a/seahub/base/models.py b/seahub/base/models.py index c33cdcd8d8..767216eaa8 100644 --- a/seahub/base/models.py +++ b/seahub/base/models.py @@ -55,21 +55,21 @@ class FileDiscuss(models.Model): class FileCommentManager(models.Manager): - def add(self, repo_id, parent_path, item_name, author, comment): + def add(self, repo_id, parent_path, item_name, author, comment, detail): fileuuidmap = FileUUIDMap.objects.get_or_create_fileuuidmap(repo_id, parent_path, item_name, False) - c = self.model(uuid=fileuuidmap, author=author, comment=comment) + c = self.model(uuid=fileuuidmap, author=author, comment=comment, detail=detail) c.save(using=self._db) return c - def add_by_file_path(self, repo_id, file_path, author, comment): + def add_by_file_path(self, repo_id, file_path, author, comment, detail): file_path = self.model.normalize_path(file_path) parent_path = os.path.dirname(file_path) item_name = os.path.basename(file_path) - return self.add(repo_id, parent_path, item_name, author, comment) + return self.add(repo_id, parent_path, item_name, author, comment, detail) def get_by_file_path(self, repo_id, file_path): parent_path = os.path.dirname(file_path) @@ -100,6 +100,7 @@ class FileComment(models.Model): updated_at = models.DateTimeField(default=timezone.now) objects = FileCommentManager() resolved = models.BooleanField(default=False) + detail = models.TextField() @classmethod def normalize_path(self, path): @@ -115,6 +116,7 @@ class FileComment(models.Model): 'comment': o.comment, 'created_at': datetime_to_isoformat_timestr(o.created_at), 'resolved': o.resolved, + 'detail': o.detail, }