def get(self, request, repo_id, commit_id, format=None): """ List dir by commit used when get files/dirs in a trash or history dir Permission checking: 1. all authenticated user can perform this action. """ # argument check path = request.GET.get('path', '/') path = normalize_dir_path(path) # 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) commit = seafile_api.get_commit(repo.id, repo.version, commit_id) if not commit: error_msg = 'Commit %s not found.' % commit_id return api_error(status.HTTP_404_NOT_FOUND, error_msg) dir_id = seafile_api.get_dir_id_by_commit_and_path( repo_id, commit_id, path) if not dir_id: error_msg = 'Folder %s not found.' % path return api_error(status.HTTP_404_NOT_FOUND, error_msg) # permission check if not check_folder_permission(request, repo_id, '/'): error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) # # seafile_api.list_dir_by_commit_and_path # def list_dir_by_commit_and_path(self, repo_id, commit_id, path, offset=-1, limit=-1): # dir_id = seafserv_threaded_rpc.get_dir_id_by_commit_and_path(repo_id, commit_id, path) # if dir_id is None: # return None # return seafserv_threaded_rpc.list_dir(repo_id, dir_id, offset, limit) dir_entries = seafile_api.list_dir_by_dir_id(repo_id, dir_id) items = [] for dirent in dir_entries: items.append(self._get_item_info(dirent, path)) return Response({'dirent_list': items})
def post(self, request, repo_id, commit_id, format=None): """ revert commit in repo history Permission checking: 1. only repo owner can perform this action. """ username = request.user.username # 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) commit = seafile_api.get_commit(repo.id, repo.version, commit_id) if not commit: error_msg = 'Commit %s not found.' % commit_id return api_error(status.HTTP_404_NOT_FOUND, error_msg) # permission check has_perm = is_repo_owner(request, repo.id, username) if not has_perm: repo_owner = get_repo_owner(request, repo_id) # department admin if '@seafile_group' in repo_owner: group_id = get_group_id_by_repo_owner(repo_owner) has_perm = is_group_admin(group_id, username) if not has_perm: error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) # main if repo.encrypted: ret = seafile_api.is_password_set(repo_id, username) is_decrypted = False if ret == 0 else True if not is_decrypted: error_msg = _('This library has not been decrypted.') return api_error(status.HTTP_403_FORBIDDEN, error_msg) try: seafile_api.revert_repo(repo_id, commit_id, username) 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})
def test_repo_manipulation(): #test get_system_default_repo_id t_default_repo_id = api.get_system_default_repo_id() assert t_default_repo_id #test create_repo t_repo_id = api.create_repo('test_repo_manipulation', '', USER, passwd=None) assert t_repo_id #test counts_repo t_repo_count = 0 t_repo_count = api.count_repos() assert t_repo_count != 0 #test get_repo ,edit_repo t_new_name = 'n_name' t_new_desc = 'n_desc' t_repo_version = 1 t_repo = api.get_repo(t_repo_id) assert t_repo api.edit_repo(t_repo_id, t_new_name, t_new_desc, USER) t_repo = api.get_repo(t_repo_id) assert t_repo.name == t_new_name and t_repo.desc == t_new_desc #test revert_repo and get_commit t_commit_id_before_changing = t_repo.head_cmmt_id api.post_dir(t_repo_id, '/', 'dir1', USER) t_repo = api.get_repo(t_repo_id) api.revert_repo(t_repo_id, t_commit_id_before_changing, USER) t_repo = api.get_repo(t_repo_id) t_commit_id_after_revert = t_repo.head_cmmt_id t_commit_before_changing = api.get_commit(t_repo_id, t_repo_version, t_commit_id_before_changing) t_commit_after_revert = api.get_commit(t_repo_id, t_repo_version, t_commit_id_after_revert) assert t_commit_before_changing.root_id == t_commit_after_revert.root_id #test is_repo_owner assert api.is_repo_owner(USER, t_repo_id) assert api.is_repo_owner(USER2, t_repo_id) == 0 #test get_repo_owner owner_get = api.get_repo_owner(t_repo_id) assert owner_get == USER #test set_repo_owner api.set_repo_owner(t_repo_id, USER2) assert api.is_repo_owner(USER2, t_repo_id) #test create_enc_repo t_enc_repo_id = '826d1b7b-f110-46f2-8d5e-7b5ac3e11f4d' t_enc_version = 2 t_passwd = '123' magic_and_random_key = api.generate_magic_and_random_key( t_enc_version, t_enc_repo_id, t_passwd) t_magic = magic_and_random_key.magic t_random_key = magic_and_random_key.random_key t_enc_repo_id = api.create_enc_repo(t_enc_repo_id, 'test_encrypted_repo', '', USER, t_magic, t_random_key, t_enc_version) assert t_enc_repo_id == '826d1b7b-f110-46f2-8d5e-7b5ac3e11f4d' #test get_repo_list t_start = -1 t_limit = -1 t_repo_list = api.get_repo_list(t_start, t_limit) assert t_repo_list and len(t_repo_list) t_start = 1 t_limit = 1 t_repo_list = api.get_repo_list(t_start, t_limit) assert t_repo_list and len(t_repo_list) == 1 #test get_owned_repo_list t_repo_list = api.get_owned_repo_list(USER2) assert t_repo_list and len(t_repo_list) #test get_commit_list t_offset = 0 t_limit = 0 t_commit_list = api.get_commit_list(t_repo_id, t_offset, t_limit) assert t_commit_list and len(t_commit_list) == 4 t_offset = 1 t_limit = 1 t_commit_list = api.get_commit_list(t_repo_id, t_offset, t_limit) assert t_commit_list and len(t_commit_list) == 1 #test remove_repo api.remove_repo(t_repo_id) t_repo = api.get_repo(t_repo_id) assert t_repo == None
def get(self, request, repo_id, commit_id, format=None): """ List commit info """ # 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) commit = seafile_api.get_commit(repo.id, repo.version, commit_id) if not commit: error_msg = 'Commit %s not found.' % commit_id return api_error(status.HTTP_404_NOT_FOUND, error_msg) # permission check if not check_folder_permission(request, repo_id, '/'): error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) # commit __dict__ # # {'_dict': {u'client_version': u'7.0.2', # u'conflict': False, # u'creator': u'30947ef13cd56439c72703ee74dcfa2a4d940cdb', # u'creator_name': u'*****@*****.**', # u'ctime': 1583893023, # u'desc': u'Added or modified "1.md" and 2 more files.\nDeleted "default.jpeg".\nRenamed "123.umind" and 1 more files.\nAdded "789" and 1 more directories.\nRemoved directory "456".\n', # u'device_name': u'lian mac pro work', # u'id': u'28c15cca4a8dbd5135fbe3ae75c3df7f5f355484', # u'new_merge': False, # u'next_start_commit': None, # u'parent_id': u'a12ece3a2ec69220bfa5f229682867faaf7448f7', # u'repo_id': u'8756ca9d-e3ed-44da-b43e-1bfd165b2377', # u'rev_file_id': None, # u'rev_file_size': 0, # u'rev_renamed_old_path': None, # u'root_id': u'0b7f91ad5137cf1d1e5be138ecd455ce76d2ee58', # u'second_parent_id': None, # u'version': 1}, # commit diff __dict__ # # {'_dict': {u'status': None, u'new_name': u'xyz', u'name': u'abc'}, 'props': <pysearpc.client._SearpcObj object at 0x7ff4f81b2090>} # {'_dict': {u'status': u'mov', u'new_name': u'123.jpg', u'name': u'd0efd88ejw1f6vqsjmjh9j20c846i4i6.jpg'}, 'props': <pysearpc.client._SearpcObj object at 0x7fbc5478c090>} # {'_dict': {u'status': u'mod', u'new_name': None, u'name': u'123/1.md'}, 'props': <pysearpc.client._SearpcObj object at 0x7fbc5478c290>} # {'_dict': {u'status': u'deldir', u'new_name': None, u'name': u'123/456'}, 'props': <pysearpc.client._SearpcObj object at 0x7fbc5478c2d0>} # {'_dict': {u'status': u'newdir', u'new_name': None, u'name': u'123/789'}, 'props': <pysearpc.client._SearpcObj object at 0x7fbc5478c310>} # {'_dict': {u'status': u'del', u'new_name': None, u'name': u'default.jpeg'}, 'props': <pysearpc.client._SearpcObj object at 0x7fbc5478c390>} # {'_dict': {u'status': u'add', u'new_name': None, u'name': u'departments copy.md'}, 'props': <pysearpc.client._SearpcObj object at 0x7fbc5478c3d0>} result = {} result['commit_info'] = { 'creator_email': commit.creator_name, 'creator_name': email2nickname(commit.creator_name), 'creator_contact_email': email2contact_email(commit.creator_name), 'time': timestamp_to_isoformat_timestr(commit.ctime), 'description': commit.desc, 'device_name': commit.device_name } result['commit_diffs'] = [] diffs = seafile_api.diff_commits(repo_id, '', commit_id) diff_status_dict = { "add": 'new', "del": 'removed', "mov": 'renamed', "mod": 'modified', "newdir": 'newdir', "deldir": 'deldir', } for diff in diffs: commit_diff = {} commit_diff['op_type'] = diff_status_dict.get(diff.status, '') commit_diff['path'] = '/' + diff.name if diff.name else '' commit_diff['new_path'] = '/' + diff.new_name if diff.new_name else '' result['commit_diffs'].append(commit_diff) return Response(result)