From fe999b7559ac3c6b9e502bec43196e4da8a9c5b4 Mon Sep 17 00:00:00 2001 From: zhengxie Date: Sat, 21 May 2016 18:25:09 +0800 Subject: [PATCH] [api2] Add user info to file comment obj --- seahub/api2/endpoints/file_comment.py | 16 ++++++++++++--- seahub/api2/endpoints/file_comments.py | 25 ++++++++++++++++++++--- seahub/api2/utils.py | 15 +++++++++++++- seahub/base/models.py | 1 - tests/api/endpoints/test_file_comment.py | 10 +++++++++ tests/api/endpoints/test_file_comments.py | 25 +++++++++++++++++++++++ 6 files changed, 84 insertions(+), 8 deletions(-) diff --git a/seahub/api2/endpoints/file_comment.py b/seahub/api2/endpoints/file_comment.py index d34abe3a72..05c1e85064 100644 --- a/seahub/api2/endpoints/file_comment.py +++ b/seahub/api2/endpoints/file_comment.py @@ -6,12 +6,12 @@ from rest_framework.permissions import IsAuthenticated from rest_framework.response import Response from rest_framework.views import APIView from seaserv import seafile_api -from pysearpc import SearpcError from seahub.api2.authentication import TokenAuthentication from seahub.api2.permissions import IsRepoAccessible from seahub.api2.throttling import UserRateThrottle -from seahub.api2.utils import api_error +from seahub.api2.utils import api_error, user_to_dict +from seahub.avatar.settings import AVATAR_DEFAULT_SIZE from seahub.base.models import FileComment logger = logging.getLogger(__name__) @@ -30,7 +30,17 @@ class FileCommentView(APIView): except FileComment.DoesNotExist: return api_error(status.HTTP_400_BAD_REQUEST, 'Wrong comment id') - return Response(o.to_dict()) + try: + avatar_size = int(request.GET.get('avatar_size', + AVATAR_DEFAULT_SIZE)) + except ValueError: + avatar_size = AVATAR_DEFAULT_SIZE + + comment = o.to_dict() + comment.update(user_to_dict(request.user.username, request=request, + avatar_size=avatar_size)) + + return Response(comment) def delete(self, request, repo_id, pk, format=None): """Delete a comment, only comment author or repo owner can perform diff --git a/seahub/api2/endpoints/file_comments.py b/seahub/api2/endpoints/file_comments.py index 4311800e73..9e8a2e03f1 100644 --- a/seahub/api2/endpoints/file_comments.py +++ b/seahub/api2/endpoints/file_comments.py @@ -11,7 +11,8 @@ from pysearpc import SearpcError from seahub.api2.authentication import TokenAuthentication from seahub.api2.permissions import IsRepoAccessible from seahub.api2.throttling import UserRateThrottle -from seahub.api2.utils import api_error +from seahub.api2.utils import api_error, user_to_dict +from seahub.avatar.settings import AVATAR_DEFAULT_SIZE from seahub.base.models import FileComment logger = logging.getLogger(__name__) @@ -29,9 +30,18 @@ class FileCommentsView(APIView): if not path: return api_error(status.HTTP_400_BAD_REQUEST, 'Wrong path.') + try: + avatar_size = int(request.GET.get('avatar_size', + AVATAR_DEFAULT_SIZE)) + except ValueError: + avatar_size = AVATAR_DEFAULT_SIZE + comments = [] for o in FileComment.objects.get_by_file_path(repo_id, path): - comments.append(o.to_dict()) + comment = o.to_dict() + comment.update(user_to_dict(request.user.username, request=request, + avatar_size=avatar_size)) + comments.append(comment) return Response({ "comments": comments, @@ -44,6 +54,12 @@ class FileCommentsView(APIView): if not path: return api_error(status.HTTP_400_BAD_REQUEST, 'Wrong path.') + try: + avatar_size = int(request.GET.get('avatar_size', + AVATAR_DEFAULT_SIZE)) + except ValueError: + avatar_size = AVATAR_DEFAULT_SIZE + try: obj_id = seafile_api.get_file_id_by_path(repo_id, path) @@ -61,4 +77,7 @@ class FileCommentsView(APIView): username = request.user.username o = FileComment.objects.add_by_file_path( repo_id=repo_id, file_path=path, author=username, comment=comment) - return Response(o.to_dict(), status=201) + comment = o.to_dict() + comment.update(user_to_dict(request.user.username, request=request, + avatar_size=avatar_size)) + return Response(comment, status=201) diff --git a/seahub/api2/utils.py b/seahub/api2/utils.py index 6ccd984a80..bac044734e 100644 --- a/seahub/api2/utils.py +++ b/seahub/api2/utils.py @@ -29,7 +29,7 @@ from seahub.group.views import is_group_staff from seahub.message.models import UserMessage, UserMsgAttachment from seahub.notifications.models import UserNotification from seahub.utils import api_convert_desc_link, get_file_type_and_ext, \ - gen_file_get_url, is_org_context + gen_file_get_url, is_org_context, get_site_scheme_and_netloc from seahub.utils.paginator import Paginator from seahub.utils.file_types import IMAGE from seahub.api2.models import Token, TokenV2, DESKTOP_PLATFORMS @@ -651,3 +651,16 @@ def get_user_common_info(email, avatar_size=AVATAR_DEFAULT_SIZE): "avatar_url": avatar_url, "login_id": login_id } + +def user_to_dict(email, request=None, avatar_size=AVATAR_DEFAULT_SIZE): + d = get_user_common_info(email, avatar_size) + if request is None: + avatar_url = '%s%s' % (get_site_scheme_and_netloc(), d['avatar_url']) + else: + avatar_url = request.build_absolute_uri(d['avatar_url']) + return { + 'user_name': d['name'], + 'user_email': d['email'], + 'user_login_id': d['login_id'], + 'avatar_url': avatar_url, + } diff --git a/seahub/base/models.py b/seahub/base/models.py index 551ecce87f..a260afcd2b 100644 --- a/seahub/base/models.py +++ b/seahub/base/models.py @@ -109,7 +109,6 @@ class FileComment(models.Model): 'repo_id': o.repo_id, 'parent_path': o.parent_path, 'item_name': o.item_name, - 'author': o.author, 'comment': o.comment, 'created_at': datetime_to_isoformat_timestr(o.created_at), } diff --git a/tests/api/endpoints/test_file_comment.py b/tests/api/endpoints/test_file_comment.py index 132a603f21..d712210c18 100644 --- a/tests/api/endpoints/test_file_comment.py +++ b/tests/api/endpoints/test_file_comment.py @@ -25,6 +25,16 @@ class FileCommentTest(BaseTestCase): json_resp = json.loads(resp.content) assert json_resp['parent_path'] == '/' assert json_resp['item_name'] == 'test.txt' + assert 'avatars' in json_resp['avatar_url'] + + def test_can_get_with_avatar_size(self): + resp = self.client.get(self.endpoint + '&avatar_size=20') + self.assertEqual(200, resp.status_code) + + json_resp = json.loads(resp.content) + assert json_resp['parent_path'] == '/' + assert json_resp['item_name'] == 'test.txt' + assert 'avatars' in json_resp['avatar_url'] def test_can_delete(self): assert len(FileComment.objects.all()) == 1 diff --git a/tests/api/endpoints/test_file_comments.py b/tests/api/endpoints/test_file_comments.py index 5e62362bc9..31b21743de 100644 --- a/tests/api/endpoints/test_file_comments.py +++ b/tests/api/endpoints/test_file_comments.py @@ -24,6 +24,20 @@ class FileCommentsTest(BaseTestCase): json_resp = json.loads(resp.content) assert len(json_resp['comments']) == 1 assert json_resp['comments'][0]['comment'] == o.comment + assert 'avatars' in json_resp['comments'][0]['avatar_url'] + + def test_can_list_with_avatar_size(self): + o = FileComment.objects.add_by_file_path(repo_id=self.repo.id, + file_path=self.file, + author=self.user.username, + comment='test comment') + resp = self.client.get(self.endpoint + '&avatar_size=20') + self.assertEqual(200, resp.status_code) + + json_resp = json.loads(resp.content) + assert len(json_resp['comments']) == 1 + assert json_resp['comments'][0]['comment'] == o.comment + assert 'avatars' in json_resp['comments'][0]['avatar_url'] def test_can_post(self): resp = self.client.post(self.endpoint, { @@ -33,6 +47,17 @@ class FileCommentsTest(BaseTestCase): json_resp = json.loads(resp.content) assert json_resp['comment'] == 'new comment' + assert 'avatars' in json_resp['avatar_url'] + + def test_can_post_with_avatar_size(self): + resp = self.client.post(self.endpoint + '&avatar_size=20', { + 'comment': 'new comment' + }) + self.assertEqual(201, resp.status_code) + + json_resp = json.loads(resp.content) + assert json_resp['comment'] == 'new comment' + assert 'avatars' in json_resp['avatar_url'] def test_invalid_user(self): self.logout()