diff --git a/seahub/seadoc/apis.py b/seahub/seadoc/apis.py index 3706560f940..7fd8f897525 100644 --- a/seahub/seadoc/apis.py +++ b/seahub/seadoc/apis.py @@ -879,73 +879,12 @@ def put(self, request, file_uuid, comment_id): return Response(comment) -class SeadocRevisions(APIView): - +class SeadocStartRevise(APIView): # sdoc editor use jwt token authentication_classes = (SdocJWTTokenAuthentication, TokenAuthentication, SessionAuthentication) permission_classes = (IsAuthenticated,) throttle_classes = (UserRateThrottle, ) - def get(self, request): - """list - """ - username = request.user.username - # argument check - origin_file_uuid = request.GET.get('doc_uuid') - repo_id = request.GET.get('repo_id') - try: - page = int(request.GET.get('page', '1')) - per_page = int(request.GET.get('per_page', '25')) - except ValueError: - page = 1 - per_page = 25 - start = (page - 1) * per_page - limit = per_page + 1 - - if origin_file_uuid: - origin_uuid_map = FileUUIDMap.objects.get_fileuuidmap_by_uuid(origin_file_uuid) - if not origin_uuid_map: - error_msg = 'seadoc uuid %s not found.' % origin_file_uuid - return api_error(status.HTTP_404_NOT_FOUND, error_msg) - repo_id = origin_uuid_map.repo_id - username = request.user.username - path = posixpath.join(origin_uuid_map.parent_path, origin_uuid_map.filename) - # permission check - if not check_folder_permission(request, repo_id, path): - error_msg = 'Permission denied.' - return api_error(status.HTTP_403_FORBIDDEN, error_msg) - - revision_queryset = SeadocRevision.objects.list_by_origin_doc_uuid(origin_uuid_map.uuid, start, limit) - count = SeadocRevision.objects.filter(origin_doc_uuid=origin_uuid_map.uuid).count() - elif repo_id: - # resource check - repo = seafile_api.get_repo(repo_id) - if not repo: - error_msg = 'Library %s not found.' % repo_id - return api_error(status.HTTP_404_NOT_FOUND, error_msg) - # permission check - permission = check_folder_permission(request, repo_id, '/') - if not permission: - error_msg = 'Permission denied.' - return api_error(status.HTTP_403_FORBIDDEN, error_msg) - - revision_queryset = SeadocRevision.objects.list_by_repo_id(repo_id, start, limit) - count = SeadocRevision.objects.filter(repo_id=repo_id).count() - else: - # owned - revision_queryset = SeadocRevision.objects.list_by_username(username, start, limit) - count = SeadocRevision.objects.filter(username=username).count() - - uuid_set = set() - for item in revision_queryset: - uuid_set.add(item.doc_uuid) - uuid_set.add(item.origin_doc_uuid) - - fileuuidmap_queryset = FileUUIDMap.objects.filter(uuid__in=list(uuid_set)) - revisions = [revision.to_dict(fileuuidmap_queryset) for revision in revision_queryset] - - return Response({'revisions': revisions, 'count': count}) - def post(self, request): """create """ @@ -1043,6 +982,74 @@ def post(self, request): return Response(revision.to_dict()) +class SeadocRevisionsCount(APIView): + + # sdoc editor use jwt token + authentication_classes = (SdocJWTTokenAuthentication, TokenAuthentication, SessionAuthentication) + permission_classes = (IsAuthenticated,) + throttle_classes = (UserRateThrottle, ) + + def get(self, request, file_uuid): + if not file_uuid: + error_msg = 'file_uuid %s not found.' % file_uuid + return api_error(status.HTTP_404_NOT_FOUND, error_msg) + + uuid_map = FileUUIDMap.objects.get_fileuuidmap_by_uuid(file_uuid) + + if not uuid_map: + error_msg = 'file %s uuid_map not found.' % file_uuid + return api_error(status.HTTP_404_NOT_FOUND, error_msg) + + count = SeadocRevision.objects.filter(origin_doc_uuid=uuid_map.uuid).count() + return Response({ + 'count': count + }) + + +class SeadocRevisions(APIView): + # sdoc editor use jwt token + authentication_classes = (SdocJWTTokenAuthentication, TokenAuthentication, SessionAuthentication) + permission_classes = (IsAuthenticated,) + throttle_classes = (UserRateThrottle, ) + + def get(self, request, file_uuid): + if not file_uuid: + error_msg = 'file_uuid %s not found.' % file_uuid + return api_error(status.HTTP_404_NOT_FOUND, error_msg) + + uuid_map = FileUUIDMap.objects.get_fileuuidmap_by_uuid(file_uuid) + + if not uuid_map: + error_msg = 'file %s uuid_map not found.' % file_uuid + return api_error(status.HTTP_404_NOT_FOUND, error_msg) + + count = SeadocRevision.objects.filter(origin_doc_uuid=uuid_map.uuid).count() + revision_queryset = SeadocRevision.objects.list_all_by_origin_doc_uuid(uuid_map.uuid) + + try: + page = int(request.GET.get('page', '1')) + per_page = int(request.GET.get('per_page', '25')) + except ValueError: + page = 1 + per_page = 25 + start = (page - 1) * per_page + limit = page * per_page + + revisions_queryset= revision_queryset[start:limit] + uuid_set = set() + for item in revisions_queryset: + uuid_set.add(item.doc_uuid) + uuid_set.add(item.origin_doc_uuid) + + fileuuidmap_queryset = FileUUIDMap.objects.filter(uuid__in=list(uuid_set)) + revisions = [revision.to_dict(fileuuidmap_queryset) for revision in revisions_queryset] + + return Response({ + 'count': count, + 'revisions': revisions + }) + + class SeadocPublishRevision(APIView): authentication_classes = (SdocJWTTokenAuthentication, TokenAuthentication, SessionAuthentication) permission_classes = (IsAuthenticated, ) diff --git a/seahub/seadoc/models.py b/seahub/seadoc/models.py index 639bb79f78e..d352521c4d3 100644 --- a/seahub/seadoc/models.py +++ b/seahub/seadoc/models.py @@ -109,6 +109,10 @@ def list_by_doc_uuids(self, doc_uuid_list): def list_by_origin_doc_uuid(self, origin_doc_uuid, start, limit): return self.filter( origin_doc_uuid=origin_doc_uuid, is_published=False).order_by('-id')[start:limit] + + def list_all_by_origin_doc_uuid(self, origin_doc_uuid): + return self.filter( + origin_doc_uuid=origin_doc_uuid, is_published=False).order_by('revision_id') def list_by_username(self, username, start, limit): return self.filter( diff --git a/seahub/seadoc/urls.py b/seahub/seadoc/urls.py index e6ced1831ce..4a9d59f8bd5 100644 --- a/seahub/seadoc/urls.py +++ b/seahub/seadoc/urls.py @@ -1,9 +1,10 @@ from django.urls import re_path from .apis import SeadocAccessToken, SeadocUploadLink, SeadocDownloadLink, SeadocRevisionDownloadLinks, SeadocUploadFile, \ SeadocUploadImage, SeadocDownloadImage, SeadocCopyHistoryFile, SeadocHistory, SeadocDrafts, SeadocMaskAsDraft, \ - SeadocCommentsView, SeadocCommentView, SeadocRevisions, SeadocPublishRevision + SeadocCommentsView, SeadocCommentView, SeadocStartRevise, SeadocPublishRevision, SeadocRevisionsCount, SeadocRevisions +# api/v2.1/seadoc/ urlpatterns = [ re_path(r'^access-token/(?P[-0-9a-f]{36})/$', SeadocAccessToken.as_view(), name='seadoc_access_token'), re_path(r'^upload-file/(?P[-0-9a-f]{36})/$', SeadocUploadFile.as_view(), name='seadoc_upload_file'), @@ -18,6 +19,8 @@ re_path(r'^mark-as-draft/(?P[-0-9a-f]{36})/$', SeadocMaskAsDraft.as_view(), name='seadoc_mark_as_draft'), re_path(r'^comments/(?P[-0-9a-f]{36})/$', SeadocCommentsView.as_view(), name='seadoc_comments'), re_path(r'^comment/(?P[-0-9a-f]{36})/(?P\d+)/$', SeadocCommentView.as_view(), name='seadoc_comment'), - re_path(r'^revisions/$', SeadocRevisions.as_view(), name='seadoc_revisions'), + re_path(r'^start-revise/$', SeadocStartRevise.as_view(), name='seadoc_start_revise'), re_path(r'^publish-revision/(?P[-0-9a-f]{36})/$', SeadocPublishRevision.as_view(), name='seadoc_publish_revision'), + re_path(r'^revisions-count/(?P[-0-9a-f]{36})/$', SeadocRevisionsCount.as_view(), name='seadoc_revisions_count'), + re_path(r'^revisions/(?P[-0-9a-f]{36})/$', SeadocRevisions.as_view(), name='seadoc_revisions'), ]