1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-12 21:30:39 +00:00

[api2] Add user info to file comment obj

This commit is contained in:
zhengxie
2016-05-21 18:25:09 +08:00
committed by llj
parent c19b781713
commit fe999b7559
6 changed files with 84 additions and 8 deletions

View File

@@ -6,12 +6,12 @@ from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework.views import APIView from rest_framework.views import APIView
from seaserv import seafile_api from seaserv import seafile_api
from pysearpc import SearpcError
from seahub.api2.authentication import TokenAuthentication from seahub.api2.authentication import TokenAuthentication
from seahub.api2.permissions import IsRepoAccessible from seahub.api2.permissions import IsRepoAccessible
from seahub.api2.throttling import UserRateThrottle 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 from seahub.base.models import FileComment
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -30,7 +30,17 @@ class FileCommentView(APIView):
except FileComment.DoesNotExist: except FileComment.DoesNotExist:
return api_error(status.HTTP_400_BAD_REQUEST, 'Wrong comment id') 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): def delete(self, request, repo_id, pk, format=None):
"""Delete a comment, only comment author or repo owner can perform """Delete a comment, only comment author or repo owner can perform

View File

@@ -11,7 +11,8 @@ from pysearpc import SearpcError
from seahub.api2.authentication import TokenAuthentication from seahub.api2.authentication import TokenAuthentication
from seahub.api2.permissions import IsRepoAccessible from seahub.api2.permissions import IsRepoAccessible
from seahub.api2.throttling import UserRateThrottle 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 from seahub.base.models import FileComment
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -29,9 +30,18 @@ class FileCommentsView(APIView):
if not path: if not path:
return api_error(status.HTTP_400_BAD_REQUEST, 'Wrong 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 = [] comments = []
for o in FileComment.objects.get_by_file_path(repo_id, path): 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({ return Response({
"comments": comments, "comments": comments,
@@ -44,6 +54,12 @@ class FileCommentsView(APIView):
if not path: if not path:
return api_error(status.HTTP_400_BAD_REQUEST, 'Wrong 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: try:
obj_id = seafile_api.get_file_id_by_path(repo_id, obj_id = seafile_api.get_file_id_by_path(repo_id,
path) path)
@@ -61,4 +77,7 @@ class FileCommentsView(APIView):
username = request.user.username username = request.user.username
o = FileComment.objects.add_by_file_path( o = FileComment.objects.add_by_file_path(
repo_id=repo_id, file_path=path, author=username, comment=comment) 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)

View File

@@ -29,7 +29,7 @@ from seahub.group.views import is_group_staff
from seahub.message.models import UserMessage, UserMsgAttachment from seahub.message.models import UserMessage, UserMsgAttachment
from seahub.notifications.models import UserNotification from seahub.notifications.models import UserNotification
from seahub.utils import api_convert_desc_link, get_file_type_and_ext, \ 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.paginator import Paginator
from seahub.utils.file_types import IMAGE from seahub.utils.file_types import IMAGE
from seahub.api2.models import Token, TokenV2, DESKTOP_PLATFORMS 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, "avatar_url": avatar_url,
"login_id": login_id "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,
}

View File

@@ -109,7 +109,6 @@ class FileComment(models.Model):
'repo_id': o.repo_id, 'repo_id': o.repo_id,
'parent_path': o.parent_path, 'parent_path': o.parent_path,
'item_name': o.item_name, 'item_name': o.item_name,
'author': o.author,
'comment': o.comment, 'comment': o.comment,
'created_at': datetime_to_isoformat_timestr(o.created_at), 'created_at': datetime_to_isoformat_timestr(o.created_at),
} }

View File

@@ -25,6 +25,16 @@ class FileCommentTest(BaseTestCase):
json_resp = json.loads(resp.content) json_resp = json.loads(resp.content)
assert json_resp['parent_path'] == '/' assert json_resp['parent_path'] == '/'
assert json_resp['item_name'] == 'test.txt' 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): def test_can_delete(self):
assert len(FileComment.objects.all()) == 1 assert len(FileComment.objects.all()) == 1

View File

@@ -24,6 +24,20 @@ class FileCommentsTest(BaseTestCase):
json_resp = json.loads(resp.content) json_resp = json.loads(resp.content)
assert len(json_resp['comments']) == 1 assert len(json_resp['comments']) == 1
assert json_resp['comments'][0]['comment'] == o.comment 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): def test_can_post(self):
resp = self.client.post(self.endpoint, { resp = self.client.post(self.endpoint, {
@@ -33,6 +47,17 @@ class FileCommentsTest(BaseTestCase):
json_resp = json.loads(resp.content) json_resp = json.loads(resp.content)
assert json_resp['comment'] == 'new comment' 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): def test_invalid_user(self):
self.logout() self.logout()