Skip to content

Commit

Permalink
Merge pull request #5729 from haiwen/SdocRepoTagsView
Browse files Browse the repository at this point in the history
SdocRepoTagsView
  • Loading branch information
shuntian authored Nov 4, 2023
2 parents b2a766c + 8cb385b commit c3dc1d0
Show file tree
Hide file tree
Showing 3 changed files with 342 additions and 0 deletions.
28 changes: 28 additions & 0 deletions seahub/file_tags/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@ def get_file_tag_by_path(self, repo_id, file_path):

return file_tags

def list_file_tags_by_file_uuid(self, file_uuid):
file_tag_list = super(FileTagsManager, self).filter(
file_uuid=file_uuid).select_related('repo_tag')

file_tags = list()
for file_tag in file_tag_list:
tag_dict = dict()
tag_dict['file_tag_id'] = file_tag.pk
tag_dict['repo_tag_id'] = file_tag.repo_tag.pk
tag_dict['tag_name'] = file_tag.repo_tag.name
tag_dict['tag_color'] = file_tag.repo_tag.color
file_tags.append(tag_dict)

return file_tags

def get_file_tag_by_id(self, file_tag_id):
try:
return super(FileTagsManager, self).get(pk=file_tag_id)
Expand All @@ -51,6 +66,13 @@ def get_file_tag(self, repo_id, repo_tag_id, file_path):
except self.model.DoesNotExist:
return None

def get_file_tag_by_file_uuid(self, file_uuid, repo_tag_id):
try:
return super(FileTagsManager, self).get(
repo_tag_id=repo_tag_id, file_uuid=file_uuid)
except self.model.DoesNotExist:
return None

def add_file_tag(self, repo_id, repo_tag_id, file_path):
file_path = normalize_file_path(file_path)
filename = os.path.basename(file_path)
Expand All @@ -62,6 +84,12 @@ def add_file_tag(self, repo_id, repo_tag_id, file_path):
file_tag.save()
return file_tag

def add_file_tag_by_file_uuid(self, file_uuid, repo_tag_id):
repo_tag = RepoTags.objects.get_repo_tag_by_id(repo_tag_id)
file_tag = self.model(repo_tag=repo_tag, file_uuid=file_uuid)
file_tag.save()
return file_tag

def delete_file_tag(self, file_tag_id):
try:
file_tag = super(FileTagsManager, self).get(pk=file_tag_id)
Expand Down
309 changes: 309 additions & 0 deletions seahub/seadoc/apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
from seahub.file_participants.models import FileParticipant
from seahub.base.accounts import User
from seahub.avatar.settings import AVATAR_DEFAULT_SIZE
from seahub.repo_tags.models import RepoTags
from seahub.file_tags.models import FileTags


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -1174,6 +1176,313 @@ def put(self, request, file_uuid, comment_id, reply_id):
return Response(data)


class SdocRepoTagsView(APIView):
authentication_classes = (SdocJWTTokenAuthentication, TokenAuthentication, SessionAuthentication)
permission_classes = (IsAuthenticated,)
throttle_classes = (UserRateThrottle,)

def get(self, request, file_uuid):
"""list all repo_tags by repo_id.
"""
# resource check
uuid_map = FileUUIDMap.objects.get_fileuuidmap_by_uuid(file_uuid)
if not uuid_map:
error_msg = 'seadoc uuid %s not found.' % file_uuid
return api_error(status.HTTP_404_NOT_FOUND, error_msg)
repo_id = uuid_map.repo_id

# permission check
if not check_folder_permission(request, repo_id, '/'):
error_msg = 'Permission denied.'
return api_error(status.HTTP_403_FORBIDDEN, error_msg)

# get files tags
repo_tags = []
try:
repo_tag_list = RepoTags.objects.get_all_by_repo_id(repo_id)
except Exception as e:
logger.error(e)
error_msg = 'Internal Server Error'
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)

for repo_tag in repo_tag_list:
res = repo_tag.to_dict()
repo_tags.append(res)

return Response({"repo_tags": repo_tags}, status=status.HTTP_200_OK)

def post(self, request, file_uuid):
"""add one repo_tag.
"""
# argument check
tag_name = request.data.get('name')
if not tag_name:
error_msg = 'name invalid.'
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)

tag_color = request.data.get('color')
if not tag_color:
error_msg = 'color invalid.'
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)

# resource check
uuid_map = FileUUIDMap.objects.get_fileuuidmap_by_uuid(file_uuid)
if not uuid_map:
error_msg = 'seadoc uuid %s not found.' % file_uuid
return api_error(status.HTTP_404_NOT_FOUND, error_msg)
repo_id = uuid_map.repo_id

repo_tag = RepoTags.objects.get_repo_tag_by_name(repo_id, tag_name)
if repo_tag:
error_msg = 'repo tag %s already exist.' % tag_name
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)

# permission check
if check_folder_permission(request, repo_id, '/') != PERMISSION_READ_WRITE:
error_msg = 'Permission denied.'
return api_error(status.HTTP_403_FORBIDDEN, error_msg)

try:
repo_tag = RepoTags.objects.create_repo_tag(repo_id, tag_name, tag_color)
except Exception as e:
logger.error(e)
error_msg = 'Internal Server Error'
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)

return Response({"repo_tag": repo_tag.to_dict()}, status=status.HTTP_201_CREATED)

def put(self, request, file_uuid):
"""bulk add repo_tags.
"""
# argument check
tags = request.data.get('tags')
if not tags:
error_msg = 'tags invalid.'
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)

# resource check
uuid_map = FileUUIDMap.objects.get_fileuuidmap_by_uuid(file_uuid)
if not uuid_map:
error_msg = 'seadoc uuid %s not found.' % file_uuid
return api_error(status.HTTP_404_NOT_FOUND, error_msg)
repo_id = uuid_map.repo_id

# permission check
if check_folder_permission(request, repo_id, '/') != PERMISSION_READ_WRITE:
error_msg = 'Permission denied.'
return api_error(status.HTTP_403_FORBIDDEN, error_msg)

tag_objs = list()
try:
for tag in tags:
name = tag.get('name' ,'')
color = tag.get('color', '')
if name and color:
obj = RepoTags(repo_id=repo_id, name=name, color=color)
tag_objs.append(obj)
except Exception as e:
logger.error(e)
error_msg = 'tags invalid.'
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)

try:
repo_tag_list = RepoTags.objects.bulk_create(tag_objs)
except Exception as e:
logger.error(e)
error_msg = 'Internal Server Error'
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)

repo_tags = list()
for repo_tag in repo_tag_list:
res = repo_tag.to_dict()
repo_tags.append(res)

return Response({"repo_tags": repo_tags}, status=status.HTTP_200_OK)


class SdocRepoTagView(APIView):
authentication_classes = (SdocJWTTokenAuthentication, TokenAuthentication, SessionAuthentication)
permission_classes = (IsAuthenticated,)
throttle_classes = (UserRateThrottle,)

def put(self, request, file_uuid, repo_tag_id):
"""update one repo_tag
"""
# argument check
tag_name = request.data.get('name')
if not tag_name:
error_msg = 'name invalid.'
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)

tag_color = request.data.get('color')
if not tag_color:
error_msg = 'color invalid.'
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)

# resource check
uuid_map = FileUUIDMap.objects.get_fileuuidmap_by_uuid(file_uuid)
if not uuid_map:
error_msg = 'seadoc uuid %s not found.' % file_uuid
return api_error(status.HTTP_404_NOT_FOUND, error_msg)
repo_id = uuid_map.repo_id

repo_tag = RepoTags.objects.get_repo_tag_by_id(repo_tag_id)
if not repo_tag:
error_msg = 'repo_tag not found.'
return api_error(status.HTTP_404_NOT_FOUND, error_msg)

# permission check
if check_folder_permission(request, repo_id, '/') != PERMISSION_READ_WRITE:
error_msg = 'Permission denied.'
return api_error(status.HTTP_403_FORBIDDEN, error_msg)

try:
repo_tag.name = tag_name
repo_tag.color = tag_color
repo_tag.save()
except Exception as e:
logger.error(e)
error_msg = 'Internal Server Error'
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)

return Response({"repo_tag": repo_tag.to_dict()}, status=status.HTTP_200_OK)

def delete(self, request, file_uuid, repo_tag_id):
"""delete one repo_tag
"""
# resource check
uuid_map = FileUUIDMap.objects.get_fileuuidmap_by_uuid(file_uuid)
if not uuid_map:
error_msg = 'seadoc uuid %s not found.' % file_uuid
return api_error(status.HTTP_404_NOT_FOUND, error_msg)
repo_id = uuid_map.repo_id

repo_tag = RepoTags.objects.get_repo_tag_by_id(repo_tag_id)
if not repo_tag:
error_msg = 'repo_tag not found.'
return api_error(status.HTTP_404_NOT_FOUND, error_msg)

# permission check
if check_folder_permission(request, repo_id, '/') != PERMISSION_READ_WRITE:
error_msg = 'Permission denied.'
return api_error(status.HTTP_403_FORBIDDEN, error_msg)

try:
RepoTags.objects.delete_repo_tag(repo_tag_id)
except Exception as e:
logger.error(e)
error_msg = 'Internal Server Error'
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)

return Response({'success': True}, status=status.HTTP_200_OK)



class SdocRepoFileTagsView(APIView):

authentication_classes = (SdocJWTTokenAuthentication, TokenAuthentication, SessionAuthentication)
permission_classes = (IsAuthenticated,)
throttle_classes = (UserRateThrottle,)

def get(self, request, file_uuid):
"""list all tags of a file.
"""
# resource check
uuid_map = FileUUIDMap.objects.get_fileuuidmap_by_uuid(file_uuid)
if not uuid_map:
error_msg = 'seadoc uuid %s not found.' % file_uuid
return api_error(status.HTTP_404_NOT_FOUND, error_msg)
repo_id = uuid_map.repo_id

# permission check
if not check_folder_permission(request, repo_id, '/'):
error_msg = 'Permission denied.'
return api_error(status.HTTP_403_FORBIDDEN, error_msg)

try:
file_tags = FileTags.objects.list_file_tags_by_file_uuid(uuid_map)
except Exception as e:
logger.error(e)
error_msg = 'Internal Server Error'
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)

return Response({"file_tags": file_tags}, status=status.HTTP_200_OK)

def post(self, request, file_uuid):
"""add a tag for a file.
"""
# argument check
repo_tag_id = request.data.get('repo_tag_id')
if not repo_tag_id:
error_msg = 'repo_tag_id invalid.'
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)

# resource check
uuid_map = FileUUIDMap.objects.get_fileuuidmap_by_uuid(file_uuid)
if not uuid_map:
error_msg = 'seadoc uuid %s not found.' % file_uuid
return api_error(status.HTTP_404_NOT_FOUND, error_msg)
repo_id = uuid_map.repo_id

repo_tag = RepoTags.objects.get_repo_tag_by_id(repo_tag_id)
if not repo_tag:
error_msg = 'repo_tag not found.'
return api_error(status.HTTP_404_NOT_FOUND, error_msg)

file_tag = FileTags.objects.get_file_tag_by_file_uuid(uuid_map, repo_tag_id)
if file_tag:
error_msg = 'file tag %s already exist.' % repo_tag_id
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)

# permission check
if check_folder_permission(request, repo_id, '/') != PERMISSION_READ_WRITE:
error_msg = 'Permission denied.'
return api_error(status.HTTP_403_FORBIDDEN, error_msg)
try:
file_tag = FileTags.objects.add_file_tag_by_file_uuid(uuid_map, repo_tag_id)
except Exception as e:
logger.error(e)
error_msg = 'Internal Server Error'
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)

return Response({"file_tag": file_tag.to_dict()}, status=status.HTTP_201_CREATED)


class SdocRepoFileTagView(APIView):

authentication_classes = (SdocJWTTokenAuthentication, TokenAuthentication, SessionAuthentication)
permission_classes = (IsAuthenticated,)
throttle_classes = (UserRateThrottle,)

def delete(self, request, file_uuid, file_tag_id):
"""delete a tag from a file
"""
# resource check
uuid_map = FileUUIDMap.objects.get_fileuuidmap_by_uuid(file_uuid)
if not uuid_map:
error_msg = 'seadoc uuid %s not found.' % file_uuid
return api_error(status.HTTP_404_NOT_FOUND, error_msg)
repo_id = uuid_map.repo_id

file_tag = FileTags.objects.get_file_tag_by_id(file_tag_id)
if not file_tag:
error_msg = 'file_tag %s not found.' % file_tag_id
return api_error(status.HTTP_404_NOT_FOUND, error_msg)

# permission check
if check_folder_permission(request, repo_id, '/') != PERMISSION_READ_WRITE:
error_msg = 'Permission denied.'
return api_error(status.HTTP_403_FORBIDDEN, error_msg)
try:
FileTags.objects.delete_file_tag(file_tag_id)
except Exception as e:
logger.error(e)
error_msg = 'Internal Server Error'
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)

return Response({'success': True}, status=status.HTTP_200_OK)


class SeadocStartRevise(APIView):
# sdoc editor use jwt token
authentication_classes = (SdocJWTTokenAuthentication, TokenAuthentication, SessionAuthentication)
Expand Down
5 changes: 5 additions & 0 deletions seahub/seadoc/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
SeadocUploadImage, SeadocDownloadImage, SeadocAsyncCopyImages, SeadocQueryCopyMoveProgressView, SeadocCopyHistoryFile, SeadocHistory, SeadocDrafts, SeadocMaskAsDraft, \
SeadocCommentsView, SeadocCommentView, SeadocStartRevise, SeadocPublishRevision, SeadocRevisionsCount, SeadocRevisions, \
SeadocCommentRepliesView, SeadocCommentReplyView, SeadocFileView, SeadocFileUUIDView, SeadocDirView, SdocRevisionBaseVersionContent, SeadocRevisionView, \
SdocRepoTagsView, SdocRepoTagView, SdocRepoFileTagsView, SdocRepoFileTagView, \
SeadocFilesInfoView, DeleteSeadocOtherRevision, SeadocPublishedRevisionContent, SdocParticipantsView, SdocParticipantView, SdocRelatedUsers,SeadocEditorCallBack

# api/v2.1/seadoc/
Expand All @@ -23,6 +24,10 @@
re_path(r'^comment/(?P<file_uuid>[-0-9a-f]{36})/(?P<comment_id>\d+)/$', SeadocCommentView.as_view(), name='seadoc_comment'),
re_path(r'^comment/(?P<file_uuid>[-0-9a-f]{36})/(?P<comment_id>\d+)/replies/$', SeadocCommentRepliesView.as_view(), name='seadoc_comment_replies'),
re_path(r'^comment/(?P<file_uuid>[-0-9a-f]{36})/(?P<comment_id>\d+)/replies/(?P<reply_id>\d+)/$', SeadocCommentReplyView.as_view(), name='seadoc_comment_reply'),
re_path(r'^repo-tags/(?P<file_uuid>[-0-9a-f]{36})/$', SdocRepoTagsView.as_view(), name='seadoc_repo_tags'),
re_path(r'^repo-tags/(?P<file_uuid>[-0-9a-f]{36})/(?P<repo_tag_id>\d+)/$', SdocRepoTagView.as_view(), name='seadoc_repo_tag'),
re_path(r'^file-tags/(?P<file_uuid>[-0-9a-f]{36})/$', SdocRepoFileTagsView.as_view(), name='seadoc_file_tags'),
re_path(r'^file-tags/(?P<file_uuid>[-0-9a-f]{36})/(?P<file_tag_id>\d+)/$', SdocRepoFileTagView.as_view(), name='seadoc_file_tag'),
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'),
Expand Down

0 comments on commit c3dc1d0

Please sign in to comment.