Skip to content

Commit

Permalink
feat: list sdoc revisions in edit page (#5559)
Browse files Browse the repository at this point in the history
* feat: list sdoc revisions in edit page

* feat: optimize code

* feat: optimzie code
  • Loading branch information
YangGuoXuan-0503 authored Jul 28, 2023
1 parent f3bf3df commit 2975f45
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 64 deletions.
131 changes: 69 additions & 62 deletions seahub/seadoc/apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
Expand Down Expand Up @@ -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, )
Expand Down
4 changes: 4 additions & 0 deletions seahub/seadoc/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
7 changes: 5 additions & 2 deletions seahub/seadoc/urls.py
Original file line number Diff line number Diff line change
@@ -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<repo_id>[-0-9a-f]{36})/$', SeadocAccessToken.as_view(), name='seadoc_access_token'),
re_path(r'^upload-file/(?P<file_uuid>[-0-9a-f]{36})/$', SeadocUploadFile.as_view(), name='seadoc_upload_file'),
Expand All @@ -18,6 +19,8 @@
re_path(r'^mark-as-draft/(?P<repo_id>[-0-9a-f]{36})/$', SeadocMaskAsDraft.as_view(), name='seadoc_mark_as_draft'),
re_path(r'^comments/(?P<file_uuid>[-0-9a-f]{36})/$', SeadocCommentsView.as_view(), name='seadoc_comments'),
re_path(r'^comment/(?P<file_uuid>[-0-9a-f]{36})/(?P<comment_id>\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<file_uuid>[-0-9a-f]{36})/$', SeadocPublishRevision.as_view(), name='seadoc_publish_revision'),
re_path(r'^revisions-count/(?P<file_uuid>[-0-9a-f]{36})/$', SeadocRevisionsCount.as_view(), name='seadoc_revisions_count'),
re_path(r'^revisions/(?P<file_uuid>[-0-9a-f]{36})/$', SeadocRevisions.as_view(), name='seadoc_revisions'),
]

0 comments on commit 2975f45

Please sign in to comment.