diff --git a/seahub/tags/models.py b/seahub/tags/models.py index e2877443f5..ba4a64e795 100644 --- a/seahub/tags/models.py +++ b/seahub/tags/models.py @@ -2,9 +2,12 @@ # -*- coding: utf-8 -*- import uuid import hashlib +import posixpath from django.db import models +from seaserv import seafile_api + from seahub.base.fields import LowerCaseCharField @@ -27,6 +30,7 @@ class FileUUIDMapManager(models.Manager): return: uuid of filemap """ + repo_id, parent_path = self.model.get_origin_repo_id_and_parent_path(repo_id, parent_path) uuid = self.get_fileuuidmap_by_path(repo_id, parent_path, filename, is_dir) if not uuid: uuid = self.model(repo_id=repo_id, parent_path=parent_path, @@ -44,6 +48,7 @@ class FileUUIDMapManager(models.Manager): return: return uuid if it's exist,otherwise return None """ + repo_id, parent_path = self.model.get_origin_repo_id_and_parent_path(repo_id, parent_path) md5_repo_id_parent_path = self.model.md5_repo_id_parent_path(repo_id, parent_path) uuid = super(FileUUIDMapManager, self).filter( repo_id_parent_path_md5=md5_repo_id_parent_path, @@ -55,6 +60,7 @@ class FileUUIDMapManager(models.Manager): return None def get_fileuuidmaps_by_parent_path(self, repo_id, parent_path): + repo_id, parent_path = self.model.get_origin_repo_id_and_parent_path(repo_id, parent_path) parent_path = FileUUIDMap.normalize_path(parent_path) uuids = super(FileUUIDMapManager, self).filter( repo_id=repo_id, parent_path=parent_path @@ -181,6 +187,14 @@ class FileUUIDMap(models.Model): def normalize_path(self, path): return path.rstrip('/') if path != '/' else '/' + @classmethod + def get_origin_repo_id_and_parent_path(cls, repo_id, parent_path): + repo = seafile_api.get_repo(repo_id) + if repo.is_virtual: + repo_id = repo.origin_repo_id + parent_path = posixpath.join(repo.origin_path, parent_path.strip('/')) + return repo_id, parent_path + def save(self, *args, **kwargs): self.parent_path = self.normalize_path(self.parent_path) if not self.repo_id_parent_path_md5: diff --git a/tests/seahub/base/test_models.py b/tests/seahub/base/test_models.py index 10f1a9718b..258bce8f27 100644 --- a/tests/seahub/base/test_models.py +++ b/tests/seahub/base/test_models.py @@ -6,10 +6,17 @@ from seahub.tags.models import FileUUIDMap class FileCommentManagerTest(BaseTestCase): + + def setUp(self): + self.repo_id = self.repo.id + + def tearDown(self): + self.remove_repo() + def test_can_add(self): assert len(FileComment.objects.all()) == 0 - o = FileComment.objects.add(repo_id='xxx', parent_path='/foo/bar', + o = FileComment.objects.add(repo_id=self.repo_id, parent_path='/foo/bar', item_name='test.txt', author=self.user.username, comment='test comment') @@ -20,49 +27,56 @@ class FileCommentManagerTest(BaseTestCase): assert len(FileComment.objects.all()) == 0 o = FileComment.objects.add_by_file_path( - repo_id='xxx', file_path='/foo/bar/test.txt', + repo_id=self.repo_id, file_path='/foo/bar/test.txt', author=self.user.username, comment='test comment') assert o.uuid.parent_path == '/foo/bar' assert len(FileComment.objects.all()) == 1 def test_get_by_file_path(self): - o1 = FileComment.objects.add(repo_id='xxx', parent_path='/foo/bar/', + o1 = FileComment.objects.add(repo_id=self.repo_id, parent_path='/foo/bar/', item_name='test.txt', author=self.user.username, comment='test comment 1') - o2 = FileComment.objects.add(repo_id='xxx', parent_path='/foo/bar', + o2 = FileComment.objects.add(repo_id=self.repo_id, parent_path='/foo/bar', item_name='test.txt', author=self.user.username, comment='test comment 2') - assert len(FileComment.objects.get_by_file_path('xxx', '/foo/bar/test.txt')) == 2 + assert len(FileComment.objects.get_by_file_path(self.repo_id, '/foo/bar/test.txt')) == 2 def test_get_by_parent_path(self): - o1 = FileComment.objects.add(repo_id='xxx', parent_path='/foo/bar/', + o1 = FileComment.objects.add(repo_id=self.repo_id, parent_path='/foo/bar/', item_name='test1.txt', author=self.user.username, comment='comment 1') - o2 = FileComment.objects.add(repo_id='xxx', parent_path='/foo/bar', + o2 = FileComment.objects.add(repo_id=self.repo_id, parent_path='/foo/bar', item_name='test2.txt', author=self.user.username, comment='comment 2') - assert len(FileComment.objects.get_by_parent_path('xxx', '/foo/bar/')) == 2 - assert len(FileComment.objects.get_by_parent_path('xxx', '/foo/bar')) == 2 + assert len(FileComment.objects.get_by_parent_path(self.repo_id, '/foo/bar/')) == 2 + assert len(FileComment.objects.get_by_parent_path(self.repo_id, '/foo/bar')) == 2 class FileCommentTest(BaseTestCase): + + def setUp(self): + self.repo_id = self.repo.id + + def tearDown(self): + self.remove_repo() + def test_md5_repo_id_parent_path(self): - md5 = FileUUIDMap.md5_repo_id_parent_path('xxx', '/') - assert md5 == hashlib.md5('xxx' + '/').hexdigest() + md5 = FileUUIDMap.md5_repo_id_parent_path(self.repo_id, '/') + assert md5 == hashlib.md5(self.repo_id + '/').hexdigest() - md5 = FileUUIDMap.md5_repo_id_parent_path('xxx', '/foo') - assert md5 == hashlib.md5('xxx' + '/foo').hexdigest() + md5 = FileUUIDMap.md5_repo_id_parent_path(self.repo_id, '/foo') + assert md5 == hashlib.md5(self.repo_id + '/foo').hexdigest() - md5 = FileUUIDMap.md5_repo_id_parent_path('xxx', '/foo/') - assert md5 == hashlib.md5('xxx' + '/foo').hexdigest() + md5 = FileUUIDMap.md5_repo_id_parent_path(self.repo_id, '/foo/') + assert md5 == hashlib.md5(self.repo_id + '/foo').hexdigest() def test_normalize_path(self): - o = FileComment.objects.add(repo_id='xxx', parent_path='/foo/bar/', + o = FileComment.objects.add(repo_id=self.repo_id, parent_path='/foo/bar/', item_name='test.txt', author=self.user.username, comment='test comment') @@ -70,7 +84,7 @@ class FileCommentTest(BaseTestCase): def test_can_save(self): assert len(FileComment.objects.all()) == 0 - uuid = FileUUIDMap.objects.get_or_create_fileuuidmap('xxx', '/foo/bar/', 'test.txt', False) + uuid = FileUUIDMap.objects.get_or_create_fileuuidmap(self.repo_id, '/foo/bar/', 'test.txt', False) FileComment(uuid=uuid, author=self.user.username, comment='test comment').save()