diff --git a/seahub/api2/endpoints/copy_move_task.py b/seahub/api2/endpoints/copy_move_task.py index cf5201ba7b..3a177c6363 100644 --- a/seahub/api2/endpoints/copy_move_task.py +++ b/seahub/api2/endpoints/copy_move_task.py @@ -13,7 +13,6 @@ from django.utils.html import escape from seahub.api2.throttling import UserRateThrottle from seahub.api2.authentication import TokenAuthentication from seahub.api2.utils import api_error -from seahub.signals import rename_dirent_successful from seahub.views import check_folder_permission from seahub.utils import check_filename_with_rename @@ -140,10 +139,6 @@ class CopyMoveTaskView(APIView): new_dirent_name, replace=False, username=username, need_progress=1) is_dir = True if dirent_type == 'dir' else False - rename_dirent_successful.send(sender=None, src_repo_id=src_repo_id, - src_parent_dir=src_parent_dir, src_filename=src_dirent_name, - dst_repo_id=dst_repo_id, dst_parent_dir=dst_parent_dir, - dst_filename=new_dirent_name, is_dir=is_dir) except Exception as e: logger.error(e) error_msg = 'Internal Server Error' diff --git a/seahub/api2/endpoints/dir.py b/seahub/api2/endpoints/dir.py index e2d25ff76f..8e41878a78 100644 --- a/seahub/api2/endpoints/dir.py +++ b/seahub/api2/endpoints/dir.py @@ -14,7 +14,6 @@ from seahub.api2.authentication import TokenAuthentication from seahub.api2.utils import api_error from seahub.api2.views import get_dir_recursively, \ get_dir_entrys_by_id -from seahub.signals import rename_dirent_successful from seahub.views import check_folder_permission from seahub.utils import check_filename_with_rename, is_valid_dirent_name, \ @@ -215,10 +214,6 @@ class DirView(APIView): # rename dir seafile_api.rename_file(repo_id, parent_dir, old_dir_name, new_dir_name, username) - rename_dirent_successful.send(sender=None, src_repo_id=repo_id, - src_parent_dir=parent_dir, src_filename=old_dir_name, - dst_repo_id=repo_id, dst_parent_dir=parent_dir, - dst_filename=new_dir_name, is_dir=True) new_dir_path = posixpath.join(parent_dir, new_dir_name) dir_info = self.get_dir_info(repo_id, new_dir_path) diff --git a/seahub/api2/endpoints/file.py b/seahub/api2/endpoints/file.py index fa1af6fc8b..e44000ddac 100644 --- a/seahub/api2/endpoints/file.py +++ b/seahub/api2/endpoints/file.py @@ -15,7 +15,6 @@ from django.utils.translation import ugettext as _ from seahub.api2.throttling import UserRateThrottle from seahub.api2.authentication import TokenAuthentication from seahub.api2.utils import api_error -from seahub.signals import rename_dirent_successful from seahub.utils import check_filename_with_rename, is_pro_version, \ gen_file_upload_url, is_valid_dirent_name @@ -246,10 +245,6 @@ class FileView(APIView): try: seafile_api.rename_file(repo_id, parent_dir, oldname, new_file_name, username) - rename_dirent_successful.send(sender=None, src_repo_id=repo_id, - src_parent_dir=parent_dir, src_filename=oldname, - dst_repo_id=repo_id, dst_parent_dir=parent_dir, - dst_filename=new_file_name, is_dir=False) except SearpcError as e: logger.error(e) error_msg = 'Internal Server Error' @@ -320,10 +315,6 @@ class FileView(APIView): seafile_api.move_file(src_repo_id, src_dir, filename, dst_repo_id, dst_dir, new_file_name, replace=False, username=username, need_progress=0, synchronous=1) - rename_dirent_successful.send(sender=None, src_repo_id=src_repo_id, - src_parent_dir=src_dir, src_filename=filename, - dst_repo_id=dst_repo_id, dst_parent_dir=dst_dir, - dst_filename=new_file_name, is_dir=False) except SearpcError as e: logger.error(e) error_msg = 'Internal Server Error' diff --git a/seahub/api2/endpoints/file_comments_counts.py b/seahub/api2/endpoints/file_comments_counts.py index f96026685a..c83ae91efd 100644 --- a/seahub/api2/endpoints/file_comments_counts.py +++ b/seahub/api2/endpoints/file_comments_counts.py @@ -32,22 +32,24 @@ class FileCommentsCounts(APIView): return api_error(status.HTTP_400_BAD_REQUEST, 'Wrong path.') try: - obj_id = seafile_api.get_dir_id_by_path(repo_id, + dir_id = seafile_api.get_dir_id_by_path(repo_id, path) except SearpcError as e: logger.error(e) return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, 'Internal error.') - if not obj_id: - return api_error(status.HTTP_404_NOT_FOUND, 'Parent dir not found.') + if not dir_id: + return api_error(status.HTTP_404_NOT_FOUND, 'Floder %s not found.' % dir_id) + + qs = FileComment.objects.get_by_parent_path(repo_id, path).values( + 'uuid__filename').annotate(total=Count('uuid__filename')) + ''' + mysql exec: + SELECT `tags_fileuuidmap`.`filename`, COUNT(`tags_fileuuidmap`.`filename`) AS `total` FROM `base_filecomment` INNER JOIN `tags_fileuuidmap` ON ( `base_filecomment`.`uuid_id` = `tags_fileuuidmap`.`uuid` ) WHERE (`base_filecomment`.`uuid_id`) IN (SELECT U0.`uuid` FROM `tags_fileuuidmap` U0 WHERE (U0.`parent_path` = '/' AND U0.`repo_id` = '4674c2bb-3702-4dd0-b768-8952db27ac87')) GROUP BY `tags_fileuuidmap`.`filename` ORDER BY NULL LIMIT 21 + ''' ret = [] - qs = FileComment.objects.get_by_parent_path(repo_id, path).values( - 'item_name').annotate(total=Count('item_name')) for e in qs: - ret.append({e['item_name']: e['total']}) + ret.append({e['uuid__filename']: e['total']}) + return Response(ret) -''' ->>> print qs.query -SELECT "base_filecomment"."item_name", COUNT("base_filecomment"."item_name") AS "total" FROM "base_filecomment" WHERE "base_filecomment"."repo_id_parent_path_md5" = c80beeeb8e48566a394d000f6c8492ac GROUP BY "base_filecomment"."item_name" -''' diff --git a/seahub/api2/endpoints/group_discussions.py b/seahub/api2/endpoints/group_discussions.py index 6e7d27cb1e..85013eae89 100644 --- a/seahub/api2/endpoints/group_discussions.py +++ b/seahub/api2/endpoints/group_discussions.py @@ -71,7 +71,7 @@ class GroupDiscussions(APIView): "group_id": group_id, "user_name": info["name"], "user_email": info["email"], - "user_login_id": info["login_id"], + "user_contact_email": info["contact_email"], "avatar_url": request.build_absolute_uri(info["avatar_url"]), "content": msg.message, "created_at": isoformat_timestr @@ -113,7 +113,7 @@ class GroupDiscussions(APIView): "group_id": group_id, "user_name": info["name"], "user_email": info["email"], - "user_login_id": info["login_id"], + "user_contact_email": info["contact_email"], "avatar_url": request.build_absolute_uri(info["avatar_url"]), "content": msg.message, "created_at": isoformat_timestr diff --git a/seahub/api2/utils.py b/seahub/api2/utils.py index 85f8212bd2..0f2c1a2090 100644 --- a/seahub/api2/utils.py +++ b/seahub/api2/utils.py @@ -21,7 +21,7 @@ from pysearpc import SearpcError from seahub.base.accounts import User from seahub.base.templatetags.seahub_tags import email2nickname, \ - translate_seahub_time, file_icon_filter + translate_seahub_time, file_icon_filter, email2contact_email from seahub.group.models import GroupMessage, MessageReply, \ MessageAttachment, PublicGroup from seahub.group.views import is_group_staff @@ -424,17 +424,11 @@ def get_user_common_info(email, avatar_size=AVATAR_DEFAULT_SIZE): logger.error(e) avatar_url = get_default_avatar_url() - p = Profile.objects.get_profile_by_user(email) - if p: - login_id = p.login_id if p.login_id else '' - else: - login_id = '' - return { "email": email, "name": email2nickname(email), - "avatar_url": avatar_url, - "login_id": login_id + "contact_email": email2contact_email(email), + "avatar_url": avatar_url } def user_to_dict(email, request=None, avatar_size=AVATAR_DEFAULT_SIZE): @@ -446,6 +440,6 @@ def user_to_dict(email, request=None, avatar_size=AVATAR_DEFAULT_SIZE): return { 'user_name': d['name'], 'user_email': d['email'], - 'user_login_id': d['login_id'], + 'user_contact_email': d['contact_email'], 'avatar_url': avatar_url, } diff --git a/seahub/base/models.py b/seahub/base/models.py index 603e9943f9..10ad6fa9aa 100644 --- a/seahub/base/models.py +++ b/seahub/base/models.py @@ -13,6 +13,7 @@ from seahub.auth.signals import user_logged_in from seahub.group.models import GroupMessage from seahub.utils import calc_file_path_hash, within_time_range from seahub.utils.timeutils import datetime_to_isoformat_timestr +from seahub.tags.models import FileUUIDMap from fields import LowerCaseCharField @@ -38,8 +39,11 @@ class FileDiscuss(models.Model): class FileCommentManager(models.Manager): def add(self, repo_id, parent_path, item_name, author, comment): - c = self.model(repo_id=repo_id, parent_path=parent_path, - item_name=item_name, author=author, comment=comment) + fileuuidmap = FileUUIDMap.objects.get_or_create_fileuuidmap(repo_id, + parent_path, + item_name, + False) + c = self.model(uuid=fileuuidmap, author=author, comment=comment) c.save(using=self._db) return c @@ -53,21 +57,18 @@ class FileCommentManager(models.Manager): def get_by_file_path(self, repo_id, file_path): parent_path = os.path.dirname(file_path) item_name = os.path.basename(file_path) - repo_id_parent_path_md5 = self.model.md5_repo_id_parent_path( - repo_id, parent_path) + uuid = FileUUIDMap.objects.get_fileuuidmap_by_path(repo_id, parent_path, + item_name, False) objs = super(FileCommentManager, self).filter( - repo_id_parent_path_md5=repo_id_parent_path_md5, - item_name=item_name) + uuid=uuid) return objs def get_by_parent_path(self, repo_id, parent_path): - repo_id_parent_path_md5 = self.model.md5_repo_id_parent_path( - repo_id, parent_path) - - objs = super(FileCommentManager, self).filter( - repo_id_parent_path_md5=repo_id_parent_path_md5) + uuids = FileUUIDMap.objects.get_fileuuidmaps_by_parent_path(repo_id, + parent_path) + objs = super(FileCommentManager, self).filter(uuid__in=uuids) return objs @@ -75,40 +76,24 @@ class FileComment(models.Model): """ Model used to record file comments. """ - repo_id = models.CharField(max_length=36, db_index=True) - parent_path = models.TextField() - repo_id_parent_path_md5 = models.CharField(max_length=100, db_index=True) - item_name = models.TextField() + uuid = models.ForeignKey(FileUUIDMap, on_delete=models.CASCADE) author = LowerCaseCharField(max_length=255, db_index=True) comment = models.TextField() created_at = models.DateTimeField(default=timezone.now) updated_at = models.DateTimeField(default=timezone.now) objects = FileCommentManager() - @classmethod - def md5_repo_id_parent_path(cls, repo_id, parent_path): - parent_path = parent_path.rstrip('/') if parent_path != '/' else '/' - return hashlib.md5((repo_id + parent_path).encode('utf-8')).hexdigest() - @classmethod def normalize_path(self, path): return path.rstrip('/') if path != '/' else '/' - def save(self, *args, **kwargs): - self.parent_path = self.normalize_path(self.parent_path) - if not self.repo_id_parent_path_md5: - self.repo_id_parent_path_md5 = self.md5_repo_id_parent_path( - self.repo_id, self.parent_path) - - super(FileComment, self).save(*args, **kwargs) - def to_dict(self): o = self return { 'id': o.pk, - 'repo_id': o.repo_id, - 'parent_path': o.parent_path, - 'item_name': o.item_name, + 'repo_id': o.uuid.repo_id, + 'parent_path': o.uuid.parent_path, + 'item_name': o.uuid.filename, 'comment': o.comment, 'created_at': datetime_to_isoformat_timestr(o.created_at), } diff --git a/seahub/signals.py b/seahub/signals.py index e2abd5c63b..098d9a9b35 100644 --- a/seahub/signals.py +++ b/seahub/signals.py @@ -6,7 +6,4 @@ repo_created = django.dispatch.Signal(providing_args=["org_id", "creator", "repo repo_deleted = django.dispatch.Signal(providing_args=["org_id", "usernames", "repo_owner", "repo_id", "repo_name"]) upload_file_successful = django.dispatch.Signal(providing_args=["repo_id", "file_path", "owner"]) comment_file_successful = django.dispatch.Signal(providing_args=["repo", "file_path", "comment", "author", "notify_users"]) -rename_dirent_successful = django.dispatch.Signal(providing_args=["src_repo_id", "src_parent_dir", - "src_filename", "dst_repo_id", - "dst_parent_dir", "dst_filename", "is_dir"]) institution_deleted = django.dispatch.Signal(providing_args=["inst_name"]) diff --git a/seahub/tags/models.py b/seahub/tags/models.py index ef545eb5c8..21d9c99f59 100644 --- a/seahub/tags/models.py +++ b/seahub/tags/models.py @@ -54,6 +54,13 @@ class FileUUIDMapManager(models.Manager): else: return None + def get_fileuuidmaps_by_parent_path(self, repo_id, parent_path): + parent_path = FileUUIDMap.normalize_path(parent_path) + uuids = super(FileUUIDMapManager, self).filter( + repo_id=repo_id, parent_path=parent_path + ) + return uuids + class TagsManager(models.Manager): def get_or_create_tag(self, tagname): @@ -196,26 +203,3 @@ class FileTag(models.Model): def to_dict(self): return {'id': self.tag.id, 'name': self.tag.name, 'creator': self.username} - - -########## handle signals -from django.dispatch import receiver -from seahub.signals import rename_dirent_successful - - -@receiver(rename_dirent_successful) -def update_fileuuidmap(sender, **kwargs): - src_repo_id = kwargs.get('src_repo_id') - src_parent_dir = kwargs.get('src_parent_dir') - src_filename = kwargs.get('src_filename') - dst_repo_id = kwargs.get('dst_repo_id') - dst_parent_dir = kwargs.get('dst_parent_dir') - dst_filename = kwargs.get('dst_filename') - is_dir = kwargs.get('is_dir') - src_fileuuidmap = FileUUIDMap.objects.get_fileuuidmap_by_path(src_repo_id, src_parent_dir, src_filename, is_dir) - if src_fileuuidmap: - src_fileuuidmap.repo_id = dst_repo_id - src_fileuuidmap.parent_dir = dst_parent_dir - src_fileuuidmap.filename = dst_filename - src_fileuuidmap.is_dir = is_dir - src_fileuuidmap.save() diff --git a/tests/api/endpoints/test_file_comments_counts.py b/tests/api/endpoints/test_file_comments_counts.py index 621909022a..dead6a20df 100644 --- a/tests/api/endpoints/test_file_comments_counts.py +++ b/tests/api/endpoints/test_file_comments_counts.py @@ -44,3 +44,17 @@ class FileCommentsCountsTest(BaseTestCase): if d.keys()[0] == 'test2.txt': assert d['test2.txt'] == 1 + + def test_can_get_file(self): + FileComment.objects.add_by_file_path(repo_id=self.repo.id, + file_path=self.file2, + author=self.user.username, + comment='test comment on other file') + + FileComment.objects.add_by_file_path(repo_id=self.repo.id, + file_path=self.file2, + author=self.user.username, + comment='test comment on other file123') + self.file_request= reverse('api2-file-comments-counts', args=[self.repo.id]) + '?p=' + self.file2 + resp = self.client.get(self.file_request) + self.assertEqual(404, resp.status_code) diff --git a/tests/seahub/base/test_models.py b/tests/seahub/base/test_models.py index cf27c55920..10f1a9718b 100644 --- a/tests/seahub/base/test_models.py +++ b/tests/seahub/base/test_models.py @@ -2,6 +2,7 @@ import hashlib from seahub.base.models import FileComment from seahub.test_utils import BaseTestCase +from seahub.tags.models import FileUUIDMap class FileCommentManagerTest(BaseTestCase): @@ -12,7 +13,7 @@ class FileCommentManagerTest(BaseTestCase): item_name='test.txt', author=self.user.username, comment='test comment') - assert o.parent_path == '/foo/bar' + assert o.uuid.parent_path == '/foo/bar' assert len(FileComment.objects.all()) == 1 def test_add_by_file_path(self): @@ -22,7 +23,7 @@ class FileCommentManagerTest(BaseTestCase): repo_id='xxx', file_path='/foo/bar/test.txt', author=self.user.username, comment='test comment') - assert o.parent_path == '/foo/bar' + assert o.uuid.parent_path == '/foo/bar' assert len(FileComment.objects.all()) == 1 def test_get_by_file_path(self): @@ -51,13 +52,13 @@ class FileCommentManagerTest(BaseTestCase): class FileCommentTest(BaseTestCase): def test_md5_repo_id_parent_path(self): - md5 = FileComment.md5_repo_id_parent_path('xxx', '/') + md5 = FileUUIDMap.md5_repo_id_parent_path('xxx', '/') assert md5 == hashlib.md5('xxx' + '/').hexdigest() - md5 = FileComment.md5_repo_id_parent_path('xxx', '/foo') + md5 = FileUUIDMap.md5_repo_id_parent_path('xxx', '/foo') assert md5 == hashlib.md5('xxx' + '/foo').hexdigest() - md5 = FileComment.md5_repo_id_parent_path('xxx', '/foo/') + md5 = FileUUIDMap.md5_repo_id_parent_path('xxx', '/foo/') assert md5 == hashlib.md5('xxx' + '/foo').hexdigest() def test_normalize_path(self): @@ -65,13 +66,12 @@ class FileCommentTest(BaseTestCase): item_name='test.txt', author=self.user.username, comment='test comment') - assert o.parent_path == '/foo/bar' + assert o.uuid.parent_path == '/foo/bar' def test_can_save(self): assert len(FileComment.objects.all()) == 0 - - FileComment(repo_id='xxx', parent_path='/foo/bar/', - item_name='test.txt', author=self.user.username, + uuid = FileUUIDMap.objects.get_or_create_fileuuidmap('xxx', '/foo/bar/', 'test.txt', False) + FileComment(uuid=uuid, author=self.user.username, comment='test comment').save() assert len(FileComment.objects.all()) == 1