mirror of
https://github.com/haiwen/seahub.git
synced 2025-09-02 23:48:47 +00:00
@@ -8,6 +8,8 @@ from rest_framework.response import Response
|
||||
from rest_framework.views import APIView
|
||||
from seaserv import seafile_api
|
||||
from pysearpc import SearpcError
|
||||
from django.db.models import Count
|
||||
from django.core.urlresolvers import reverse
|
||||
|
||||
from seahub.api2.authentication import TokenAuthentication
|
||||
from seahub.api2.permissions import IsRepoAccessible
|
||||
@@ -17,6 +19,7 @@ from seahub.avatar.settings import AVATAR_DEFAULT_SIZE
|
||||
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
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -36,19 +39,31 @@ class FileCommentsView(APIView):
|
||||
try:
|
||||
avatar_size = int(request.GET.get('avatar_size',
|
||||
AVATAR_DEFAULT_SIZE))
|
||||
page = int(request.GET.get('page', '1'))
|
||||
per_page = int(request.GET.get('per_page', '25'))
|
||||
except ValueError:
|
||||
avatar_size = AVATAR_DEFAULT_SIZE
|
||||
page = 1
|
||||
per_page = 25
|
||||
|
||||
start = (page - 1) * per_page
|
||||
end = page * per_page
|
||||
|
||||
total_count = FileComment.objects.get_by_file_path(repo_id, path).count()
|
||||
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)[start: end]:
|
||||
comment = o.to_dict()
|
||||
comment.update(user_to_dict(o.author, request=request,
|
||||
avatar_size=avatar_size))
|
||||
comments.append(comment)
|
||||
|
||||
return Response({
|
||||
"comments": comments,
|
||||
})
|
||||
result = {'comments': comments, 'total_count': total_count}
|
||||
resp = Response(result)
|
||||
base_url = reverse('api2-file-comments', args=[repo_id])
|
||||
links_header = generate_links_header_for_paginator(base_url, page,
|
||||
per_page, total_count)
|
||||
resp['Links'] = links_header
|
||||
return resp
|
||||
|
||||
def post(self, request, repo_id, format=None):
|
||||
"""Post a comments of a file.
|
||||
|
@@ -20,18 +20,24 @@ class FileCommentsTest(BaseTestCase):
|
||||
self.remove_user(self.tmp_user.email)
|
||||
|
||||
def test_can_list(self):
|
||||
for i in xrange(10):
|
||||
o = FileComment.objects.add_by_file_path(repo_id=self.repo.id,
|
||||
file_path=self.file,
|
||||
author=self.tmp_user.username,
|
||||
comment='test comment')
|
||||
resp = self.client.get(self.endpoint)
|
||||
comment='test comment'+str(i))
|
||||
resp = self.client.get(self.endpoint + '&page=2&per_page=5')
|
||||
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 len(resp._headers.get('links')) == 2
|
||||
assert resp._headers.get('links')[0] == 'Links'
|
||||
link = reverse('api2-file-comments', args=[self.repo.id]) + '?per_page=5&page=1'
|
||||
assert link in resp._headers.get('links')[1]
|
||||
assert len(json_resp['comments']) == 5
|
||||
assert json_resp['comments'][0]['comment'] == 'test comment5'
|
||||
assert json_resp['comments'][0]['user_email'] == self.tmp_user.email
|
||||
assert 'avatars' in json_resp['comments'][0]['avatar_url']
|
||||
assert json_resp['total_count'] == 10
|
||||
|
||||
def test_can_list_with_avatar_size(self):
|
||||
o = FileComment.objects.add_by_file_path(repo_id=self.repo.id,
|
||||
@@ -46,6 +52,7 @@ class FileCommentsTest(BaseTestCase):
|
||||
assert json_resp['comments'][0]['comment'] == o.comment
|
||||
assert json_resp['comments'][0]['user_email'] == self.tmp_user.email
|
||||
assert 'avatars' in json_resp['comments'][0]['avatar_url']
|
||||
assert json_resp['total_count'] == 1
|
||||
|
||||
def test_can_post(self):
|
||||
resp = self.client.post(self.endpoint, {
|
||||
|
Reference in New Issue
Block a user