def test_not_reshare_to_user_after_transfer_repo(self): # remove all share shared_repos = seafile_api.get_share_in_repo_list(self.admin.username, -1, -1) for repo in shared_repos: seafile_api.remove_share(repo.repo_id, self.admin.username, self.user.username) seafile_api.remove_share(repo.repo_id, self.user.username, self.admin.username) # share user's repo to admin with 'rw' permission seafile_api.share_repo(self.user_repo_id, self.user.username, self.admin.username, 'rw') # assert repo in admin's be shared repo list shared_repos = seafile_api.get_share_in_repo_list(self.admin.username, -1, -1) assert shared_repos[0].repo_name == self.repo.repo_name self.login_as(self.user) url = reverse("api2-repo-owner", args=[self.user_repo_id]) data = 'owner=%s' % self.admin.email resp = self.client.put(url, data, 'application/x-www-form-urlencoded') self.assertEqual(200, resp.status_code) # assert repo NOT in admin's be shared repo list shared_repos = seafile_api.get_share_in_repo_list(self.admin.username, -1, -1) assert len(shared_repos) == 0
def get_user_repos(username): """ Get all repos that user can access, including owns, shared, public, and repo in groups. NOTE: collumn names in shared_repo struct are not same as owned or group repos. """ email = username shared_repos = seafile_api.get_share_in_repo_list(email, -1, -1) if CLOUD_MODE: if user.org: org_id = user.org['org_id'] owned_repos = list_org_repos_by_owner(org_id, email) groups_repos = [] for group in get_org_groups_by_user(org_id, email): groups_repos += get_org_group_repos(org_id, group.id, email) public_repos = list_org_inner_pub_repos(org_id, email, -1, -1) else: owned_repos = list_personal_repos_by_owner(email) groups_repos = [] for group in get_personal_groups_by_user(email): groups_repos += get_group_repos(group.id, email) public_repos = [] else: owned_repos = list_personal_repos_by_owner(email) groups_repos = [] for group in get_personal_groups_by_user(email): groups_repos += get_group_repos(group.id, email) public_repos = list_inner_pub_repos(email) return (owned_repos, shared_repos, groups_repos, public_repos)
def get(self, request, format=None): username = request.user.username shared_repos = [] shared_repos += seafile_api.get_share_in_repo_list(username, -1, -1) joined_groups = get_personal_groups_by_user(username) for grp in joined_groups: # Get group repos, and for each group repos... for r_id in get_group_repoids(grp.id): # No need to list my own repo if seafile_api.is_repo_owner(username, r_id): continue # Convert repo properties due to the different collumns in Repo # and SharedRepo r = get_repo(r_id) if not r: continue r.repo_id = r.id r.repo_name = r.name r.repo_desc = r.desc cmmts = get_commits(r_id, 0, 1) last_commit = cmmts[0] if cmmts else None r.last_modified = last_commit.ctime if last_commit else 0 r.share_type = 'group' r.user = seafile_api.get_repo_owner(r_id) r.user_perm = check_permission(r_id, username) shared_repos.append(r) if not CLOUD_MODE: shared_repos += list_inner_pub_repos(username) return HttpResponse(json.dumps(shared_repos, cls=SearpcObjEncoder), status=200, content_type=json_content_type)
def delete(self): """ When delete user, we should also delete group relationships. """ if self.source == "DB": source = "DB" else: source = "LDAP" username = self.username orgs = [] if is_pro_version(): orgs = ccnet_api.get_orgs_by_user(username) # remove owned repos owned_repos = [] if orgs: for org in orgs: owned_repos += seafile_api.get_org_owned_repo_list( org.org_id, username) else: owned_repos += seafile_api.get_owned_repo_list(username) for r in owned_repos: seafile_api.remove_repo(r.id) # remove shared in repos shared_in_repos = [] if orgs: for org in orgs: org_id = org.org_id shared_in_repos = seafile_api.get_org_share_in_repo_list( org_id, username, -1, -1) for r in shared_in_repos: seafile_api.org_remove_share(org_id, r.repo_id, r.user, username) else: shared_in_repos = seafile_api.get_share_in_repo_list( username, -1, -1) for r in shared_in_repos: seafile_api.remove_share(r.repo_id, r.user, username) ExtraSharePermission.objects.filter(share_to=username).delete() # clear web api and repo sync token # when delete user try: clear_token(self.username) except Exception as e: logger.error(e) # remove current user from joined groups ccnet_api.remove_group_user(username) ccnet_api.remove_emailuser(source, username) Profile.objects.delete_profile_by_user(username) if settings.ENABLE_TERMS_AND_CONDITIONS: from termsandconditions.models import UserTermsAndConditions UserTermsAndConditions.objects.filter(username=username).delete()
def test_share_repo_to_user(repo, permission): assert api.check_permission(repo.id, USER) == 'rw' assert api.check_permission(repo.id, USER2) is None assert api.repo_has_been_shared(repo.id) == False api.share_repo(repo.id, USER, USER2, permission) assert api.check_permission(repo.id, USER2) == permission assert api.repo_has_been_shared(repo.id) repos = api.get_share_in_repo_list(USER2, 0, 1) assert_repo_with_permission(repo, repos, permission) repos = api.get_share_out_repo_list(USER, 0, 1) assert_repo_with_permission(repo, repos, permission) users = api.list_repo_shared_to(USER, repo.id) assert len(users) == 1 assert users[0].repo_id == repo.id assert users[0].user == USER2 assert users[0].perm == permission api.remove_share(repo.id, USER, USER2) assert api.check_permission(repo.id, USER2) is None
def user_share_list(request, id_or_email): """List sharing repos with ``to_email``. """ try: uid = int(id_or_email) try: user = User.objects.get(id=uid) except User.DoesNotExist: user = None if not user: return render_to_response("user_404.html", {}, context_instance=RequestContext(request)) to_email = user.email except ValueError: to_email = id_or_email share_list = [] username = request.user.username share_in = seafile_api.get_share_in_repo_list(username, -1, -1) share_out = seafile_api.get_share_out_repo_list(username, -1, -1) for e in (share_in + share_out): if e.share_type == 'personal' and e.user == to_email: share_list.append(e) c = Contact.objects.get_contact_by_user(username, to_email) add_to_contacts = True if c is None else False return render_to_response('repo/user_share_list.html', { 'to_email': to_email, 'share_list': share_list, 'add_to_contacts': add_to_contacts, }, context_instance=RequestContext(request))
def get_shared_repos(username, org_id=None): if org_id is None: shared_repos = seafile_api.get_share_in_repo_list(username, -1, -1) else: shared_repos = seafile_api.get_org_share_in_repo_list(org_id, username, -1, -1) return shared_repos
def user_share_list(request, id_or_email): """List sharing repos with ``to_email``. """ try: uid = int(id_or_email) try: user = User.objects.get(id=uid) except User.DoesNotExist: user = None if not user: return render_to_response("user_404.html",{}, context_instance=RequestContext(request)) to_email = user.email except ValueError: to_email = id_or_email share_list = [] username = request.user.username share_in = seafile_api.get_share_in_repo_list(username, -1, -1) share_out = seafile_api.get_share_out_repo_list(username, -1, -1) for e in (share_in+share_out): if e.share_type == 'personal' and e.user == to_email: share_list.append(e) c = Contact.objects.get_contact_by_user(username, to_email) add_to_contacts = True if c is None else False return render_to_response('repo/user_share_list.html', { 'to_email': to_email, 'share_list': share_list, 'add_to_contacts': add_to_contacts, }, context_instance=RequestContext(request))
def get(self, request, format=None): username = request.user.username shared_repos = [] shared_repos += seafile_api.get_share_in_repo_list(username, -1, -1) joined_groups = get_personal_groups_by_user(username) for grp in joined_groups: # Get group repos, and for each group repos... for r_id in get_group_repoids(grp.id): # No need to list my own repo if seafile_api.is_repo_owner(username, r_id): continue # Convert repo properties due to the different collumns in Repo # and SharedRepo r = get_repo(r_id) if not r: continue r.repo_id = r.id r.repo_name = r.name r.repo_desc = r.desc cmmts = get_commits(r_id, 0, 1) last_commit = cmmts[0] if cmmts else None r.last_modified = last_commit.ctime if last_commit else 0 r.share_type = 'group' r.user = seafile_api.get_repo_owner(r_id) r.user_perm = check_permission(r_id, username) shared_repos.append(r) if not CLOUD_MODE: shared_repos += list_inner_pub_repos(username) return HttpResponse(json.dumps(shared_repos, cls=SearpcObjEncoder), status=200, content_type=json_content_type)
def get_share_in_repo_list(username, org_id): """List share in repos. """ if org_id: repo_list = seafile_api.get_org_share_in_repo_list( org_id, username, -1, -1) else: repo_list = seafile_api.get_share_in_repo_list(username, -1, -1) return repo_list
def get(self, request, email): """ List 'all' libraries shared to a user Permission checking: 1. only admin can perform this action. """ if not request.user.admin_permissions.can_manage_user(): return api_error(status.HTTP_403_FORBIDDEN, 'Permission denied.') try: User.objects.get(email=email) except User.DoesNotExist as e: logger.error(e) error_msg = 'User %s not found.' % email return api_error(status.HTTP_404_NOT_FOUND, error_msg) try: beshared_repos = seafile_api.get_share_in_repo_list(email, -1, -1) except Exception as e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) # Use dict to reduce memcache fetch cost in large for-loop. nickname_dict = {} owner_set = set([x.user for x in beshared_repos]) for email in owner_set: if email not in nickname_dict: if '@seafile_group' in email: group_id = get_group_id_by_repo_owner(email) group_name = group_id_to_name(group_id) nickname_dict[email] = group_name else: nickname_dict[email] = email2nickname(email) repos_info = [] for repo in beshared_repos: repo_info = {} repo_info['id'] = repo.repo_id repo_info['name'] = repo.repo_name repo_info['owner_email'] = repo.user repo_info['owner_name'] = nickname_dict.get(repo.user, '') repo_info['size'] = repo.size repo_info['encrypted'] = repo.encrypted repo_info['file_count'] = repo.file_count repo_info['status'] = normalize_repo_status_code(repo.status) repo_info['last_modify'] = timestamp_to_isoformat_timestr( repo.last_modify) repos_info.append(repo_info) return Response({'repo_list': repos_info})
def test_share_repo_to_user(repo, permission): assert api.check_permission(repo.id, USER) == 'rw' assert api.check_permission(repo.id, USER2) is None api.share_repo(repo.id, USER, USER2, permission) assert api.check_permission(repo.id, USER2) == permission repos = api.get_share_in_repo_list(USER2, 0, 1) assert_repo_with_permission(repo, repos, permission) api.remove_share(repo.id, USER, USER2) assert api.check_permission(repo.id, USER2) is None
def get_share_in_repo_list(username, org_id): """List share in repos. """ if org_id: repo_list = seafile_api.get_org_share_in_repo_list( org_id, username, -1, -1) else: repo_list = seafile_api.get_share_in_repo_list(username, -1, -1) # for repo in repo_list: # repo.user_perm = seafile_api.check_repo_access_permission(repo.repo_id, # username) return repo_list
def get_share_in_repo_list(username, org_id): """List share in repos. """ if org_id: repo_list = seafile_api.get_org_share_in_repo_list(org_id, username, -1, -1) else: repo_list = seafile_api.get_share_in_repo_list(username, -1, -1) # for repo in repo_list: # repo.user_perm = seafile_api.check_repo_access_permission(repo.repo_id, # username) return repo_list
def test_not_reshare_to_user_after_transfer_repo(self): # Remove share if repo already shared to new owner # share user's repo to admin with 'rw' permission seafile_api.share_repo(self.user_repo_id, self.user.username, self.admin.username, 'rw') # repo in admin's be shared repo list shared_repos = seafile_api.get_share_in_repo_list(self.admin.username, -1, -1) assert shared_repos[0].repo_name == self.repo.repo_name self.login_as(self.user) url = reverse("api2-repo-owner", args=[self.user_repo_id]) data = 'owner=%s' % self.admin.email resp = self.client.put(url, data, 'application/x-www-form-urlencoded') self.assertEqual(200, resp.status_code) # repo NOT in admin's be shared repo list shared_repos = seafile_api.get_share_in_repo_list(self.admin.username, -1, -1) assert len(shared_repos) == 0
def get_share_in_repo_list(request, start, limit): """List share in repos. """ username = request.user.username if is_org_context(request): org_id = request.user.org.org_id repo_list = seafile_api.get_org_share_in_repo_list(org_id, username, -1, -1) else: repo_list = seafile_api.get_share_in_repo_list(username, -1, -1) for repo in repo_list: repo.user_perm = check_folder_permission(request, repo.repo_id, '/') return repo_list
def test_share_repo(repo, permission): assert api.check_permission(repo.id, USER2) is None api.share_repo(repo.id, USER, USER2, permission) assert api.check_permission(repo.id, USER2) == permission repos = api.get_share_in_repo_list(USER2, 0, 1) assert len(repos) == 1 r = repos[0] assert r.id == repo.id assert r.permission == permission api.remove_share(repo.id, USER, USER2) assert api.check_permission(repo.id, USER2) is None
def delete(self): """ When delete user, we should also delete group relationships. """ if self.source == "DB": source = "DB" else: source = "LDAP" username = self.username orgs = ccnet_threaded_rpc.get_orgs_by_user(username) # remove owned repos owned_repos = [] if orgs: for org in orgs: owned_repos += seafile_api.get_org_owned_repo_list( org.org_id, username) else: owned_repos += seafile_api.get_owned_repo_list(username) for r in owned_repos: seafile_api.remove_repo(r.id) # remove shared in repos shared_in_repos = [] if orgs: for org in orgs: org_id = org.org_id shared_in_repos = seafile_api.get_org_share_in_repo_list( org_id, username, -1, -1) for r in shared_in_repos: seafile_api.org_remove_share(org_id, r.repo_id, r.user, username) else: shared_in_repos = seafile_api.get_share_in_repo_list( username, -1, -1) for r in shared_in_repos: seafile_api.remove_share(r.repo_id, r.user, username) clear_token(username) # remove current user from joined groups ccnet_api.remove_group_user(username) ccnet_api.remove_emailuser(source, username) Profile.objects.delete_profile_by_user(username)
def get_user_repos(username, org_id=None): """ Get all repos that user can access, including owns, shared, public, and repo in groups. If ``org_id`` is not None, get org repos that user can access. """ if org_id is None: owned_repos = seafile_api.get_owned_repo_list(username) shared_repos = seafile_api.get_share_in_repo_list(username, -1, -1) groups_repos = [] for group in seaserv.get_personal_groups_by_user(username): # TODO: use seafile_api.get_group_repos groups_repos += seaserv.get_group_repos(group.id, username) if CLOUD_MODE: public_repos = [] else: public_repos = seaserv.list_inner_pub_repos(username) for r in shared_repos + public_repos: # collumn names in shared_repo struct are not same as owned or group # repos. r.id = r.repo_id r.name = r.repo_name r.desc = r.repo_desc r.last_modify = r.last_modified else: owned_repos = seafile_api.get_org_owned_repo_list(org_id, username) shared_repos = seafile_api.get_org_share_in_repo_list( org_id, username, -1, -1) groups_repos = [] for group in seaserv.get_org_groups_by_user(org_id, username): groups_repos += seafile_api.get_org_group_repos(org_id, group.id) public_repos = seaserv.seafserv_threaded_rpc.list_org_inner_pub_repos( org_id) for r in shared_repos + groups_repos + public_repos: # collumn names in shared_repo struct are not same as owned # repos. r.id = r.repo_id r.name = r.repo_name r.desc = r.repo_desc r.last_modify = r.last_modified return (owned_repos, shared_repos, groups_repos, public_repos)
def get_user_repos(username, org_id=None): """ Get all repos that user can access, including owns, shared, public, and repo in groups. If ``org_id`` is not None, get org repos that user can access. """ if org_id is None: owned_repos = seaserv.list_personal_repos_by_owner(username) shared_repos = seafile_api.get_share_in_repo_list(username, -1, -1) groups_repos = [] for group in seaserv.get_personal_groups_by_user(username): # TODO: use seafile_api.get_group_repos groups_repos += seaserv.get_group_repos(group.id, username) if CLOUD_MODE: public_repos = [] else: public_repos = seaserv.list_inner_pub_repos(username) for r in shared_repos + public_repos: # collumn names in shared_repo struct are not same as owned or group # repos. r.id = r.repo_id r.name = r.repo_name r.desc = r.repo_desc r.last_modify = r.last_modified else: owned_repos = seafile_api.get_org_owned_repo_list(org_id, username) shared_repos = seafile_api.get_org_share_in_repo_list(org_id, username, -1, -1) groups_repos = [] for group in seaserv.get_org_groups_by_user(org_id, username): groups_repos += seafile_api.get_org_group_repos(org_id, group.id) public_repos = seaserv.seafserv_threaded_rpc.list_org_inner_pub_repos(org_id) for r in shared_repos + groups_repos + public_repos: # collumn names in shared_repo struct are not same as owned # repos. r.id = r.repo_id r.name = r.repo_name r.desc = r.repo_desc r.last_modify = r.last_modified return (owned_repos, shared_repos, groups_repos, public_repos)
def post(self, request, email, format=None): # migrate an account's repos and groups to an exist account if not is_valid_username(email): return api_error(status.HTTP_400_BAD_REQUEST, 'Email %s invalid.' % email) op = request.data.get('op', '').lower() if op == 'migrate': from_user = email to_user = request.data.get('to_user', '') if not is_valid_username(to_user): return api_error(status.HTTP_400_BAD_REQUEST, 'Email %s invalid.' % to_user) try: user2 = User.objects.get(email=to_user) except User.DoesNotExist: return api_error(status.HTTP_404_NOT_FOUND, 'User %s not found.' % to_user) # transfer owned repos to new user for r in seafile_api.get_owned_repo_list(from_user): seafile_api.set_repo_owner(r.id, user2.username) # transfer shared repos to new user for r in seafile_api.get_share_in_repo_list(from_user, -1, -1): owner = seafile_api.get_repo_owner(r.repo_id) seafile_api.share_repo(r.repo_id, owner, to_user, r.permission) # transfer joined groups to new user for g in ccnet_api.get_groups(from_user): if not is_group_member(g.id, user2.username): # add new user to the group on behalf of the group creator ccnet_threaded_rpc.group_add_member( g.id, g.creator_name, to_user) if from_user == g.creator_name: ccnet_threaded_rpc.set_group_creator(g.id, to_user) return Response({'success': True}) else: return api_error(status.HTTP_400_BAD_REQUEST, 'op can only be migrate.')
def my_shared_repos(request): """Return html snippet of repos that shared to user. Arguments: - `request`: """ if not request.is_ajax(): raise Http404 username = request.user.username shared_repos = seafile_api.get_share_in_repo_list(username, -1, -1) shared_repos.sort(lambda x, y: cmp(y.last_modified, x.last_modified)) ctx = { "shared_repos": shared_repos, } html = render_to_string('my_shared_repos.html', ctx, context_instance=RequestContext(request)) return HttpResponse(html)
def get_user_repos(username): """ Get all repos that user can access, including owns, shared, public, and repo in groups. NOTE: collumn names in shared_repo struct are not same as owned or group repos. """ email = username shared_repos = seafile_api.get_share_in_repo_list(email, -1, -1) owned_repos = list_personal_repos_by_owner(email) groups_repos = [] for group in get_personal_groups_by_user(email): groups_repos += get_group_repos(group.id, email) if CLOUD_MODE: public_repos = [] else: public_repos = list_inner_pub_repos(email) return (owned_repos, shared_repos, groups_repos, public_repos)
def get(self, request): """ Return repos user can access. Permission checking: 1. all authenticated user can perform this action. """ filter_by = { 'mine': False, 'shared': False, 'group': False, 'public': False, } request_type_list = request.GET.getlist('type', "") if not request_type_list: # set all to True, no filter applied filter_by = filter_by.fromkeys(filter_by.iterkeys(), True) for request_type in request_type_list: request_type = request_type.strip() filter_by[request_type] = True email = request.user.username # Use dict to reduce memcache fetch cost in large for-loop. contact_email_dict = {} nickname_dict = {} org_id = None if is_org_context(request): org_id = request.user.org.org_id repo_info_list = [] if filter_by['mine']: if org_id: owned_repos = seafile_api.get_org_owned_repo_list(org_id, email, ret_corrupted=True) else: owned_repos = seafile_api.get_owned_repo_list(email, ret_corrupted=True) # Reduce memcache fetch ops. modifiers_set = set([x.last_modifier for x in owned_repos]) for e in modifiers_set: if e not in contact_email_dict: contact_email_dict[e] = email2contact_email(e) if e not in nickname_dict: nickname_dict[e] = email2nickname(e) owned_repos.sort(lambda x, y: cmp(y.last_modify, x.last_modify)) for r in owned_repos: # do not return virtual repos if r.is_virtual: continue repo_info = { "type": "mine", "repo_id": r.id, "repo_name": r.name, "owner_email": email, "owner_name": email2nickname(email), "owner_contact_email": email2contact_email(email), "last_modified": timestamp_to_isoformat_timestr(r.last_modify), "modifier_email": r.last_modifier, "modifier_name": nickname_dict.get(r.last_modifier, ''), "modifier_contact_email": contact_email_dict.get(r.last_modifier, ''), "size": r.size, "encrypted": r.encrypted, "permission": 'rw', # Always have read-write permission to owned repo } repo_info_list.append(repo_info) if filter_by['shared']: if org_id: shared_repos = seafile_api.get_org_share_in_repo_list(org_id, email, -1, -1) else: shared_repos = seafile_api.get_share_in_repo_list( email, -1, -1) repos_with_admin_share_to = ExtraSharePermission.objects.\ get_repos_with_admin_permission(email) # Reduce memcache fetch ops. owners_set = set([x.user for x in shared_repos]) modifiers_set = set([x.last_modifier for x in shared_repos]) for e in owners_set | modifiers_set: if e not in contact_email_dict: contact_email_dict[e] = email2contact_email(e) if e not in nickname_dict: nickname_dict[e] = email2nickname(e) shared_repos.sort(lambda x, y: cmp(y.last_modify, x.last_modify)) for r in shared_repos: repo_info = { "type": "shared", "repo_id": r.repo_id, "repo_name": r.repo_name, "last_modified": timestamp_to_isoformat_timestr(r.last_modify), "modifier_email": r.last_modifier, "modifier_name": nickname_dict.get(r.last_modifier, ''), "modifier_contact_email": contact_email_dict.get(r.last_modifier, ''), "size": r.size, "encrypted": r.encrypted, "permission": r.permission, } if r.repo_id in repos_with_admin_share_to: repo_info['is_admin'] = True else: repo_info['is_admin'] = False repo_info_list.append(repo_info) if filter_by['group']: if org_id: group_repos = seafile_api.get_org_group_repos_by_user(email, org_id) else: group_repos = seafile_api.get_group_repos_by_user(email) group_repos.sort(lambda x, y: cmp(y.last_modify, x.last_modify)) # Reduce memcache fetch ops. share_from_set = set([x.user for x in group_repos]) modifiers_set = set([x.last_modifier for x in group_repos]) for e in modifiers_set | share_from_set: if e not in contact_email_dict: contact_email_dict[e] = email2contact_email(e) if e not in nickname_dict: nickname_dict[e] = email2nickname(e) for r in group_repos: repo_info = { "type": "group", "group_id": r.group_id, "group_name": r.group_name, "repo_id": r.repo_id, "repo_name": r.repo_name, "last_modified": timestamp_to_isoformat_timestr(r.last_modify), "modifier_email": r.last_modifier, "modifier_name": nickname_dict.get(r.last_modifier, ''), "modifier_contact_email": contact_email_dict.get(r.last_modifier, ''), "size": r.size, "encrypted": r.encrypted, "permission": r.permission, } repo_info_list.append(repo_info) if filter_by['public'] and request.user.permissions.can_view_org(): public_repos = list_inner_pub_repos(request) # Reduce memcache fetch ops. share_from_set = set([x.user for x in public_repos]) modifiers_set = set([x.last_modifier for x in public_repos]) for e in modifiers_set | share_from_set: if e not in contact_email_dict: contact_email_dict[e] = email2contact_email(e) if e not in nickname_dict: nickname_dict[e] = email2nickname(e) for r in public_repos: repo_info = { "type": "public", "repo_id": r.repo_id, "repo_name": r.repo_name, "last_modified": timestamp_to_isoformat_timestr(r.last_modify), "modifier_email": r.last_modifier, "modifier_name": nickname_dict.get(r.last_modifier, ''), "modifier_contact_email": contact_email_dict.get(r.last_modifier, ''), "size": r.size, "encrypted": r.encrypted, "permission": r.permission, } repo_info_list.append(repo_info) return Response({'repos': repo_info_list})
def get(self, request, format=None): email = request.user.username repos_json = [] owned_repos = list_personal_repos_by_owner(email) calculate_repo_info(owned_repos, email) owned_repos.sort(lambda x, y: cmp(y.latest_modify, x.latest_modify)) for r in owned_repos: repo = { "type":"repo", "id":r.id, "owner":email, "name":r.name, "desc":r.desc, "mtime":r.latest_modify, "root":r.root, "size":r.size, "encrypted":r.encrypted, "permission": 'rw', # Always have read-write permission to owned repo } repos_json.append(repo) shared_repos = seafile_api.get_share_in_repo_list(email, -1, -1) for r in shared_repos: commit = get_commits(r.repo_id, 0, 1)[0] if not commit: continue r.latest_modify = commit.ctime r.root = commit.root_id r.size = server_repo_size(r.repo_id) r.password_need = is_passwd_set(r.repo_id, email) r.permission = check_permission(r.repo_id, email) repo = { "type":"srepo", "id":r.repo_id, "owner":r.user, "name":r.repo_name, "desc":r.repo_desc, "mtime":r.latest_modify, "root":r.root, "size":r.size, "encrypted":r.encrypted, "permission": r.permission, } repos_json.append(repo) groups = get_personal_groups_by_user(email) for group in groups: g_repos = get_group_repos(group.id, email) calculate_repo_info (g_repos, email) g_repos.sort(lambda x, y: cmp(y.latest_modify, x.latest_modify)) for r in g_repos: repo = { "type":"grepo", "id":r.id, "owner":group.group_name, "name":r.name, "desc":r.desc, "mtime":r.latest_modify, "root":r.root, "size":r.size, "encrypted":r.encrypted, "permission": check_permission(r.id, email), } repos_json.append(repo) return Response(repos_json)
quota = seafserv_threaded_rpc. \ get_org_user_quota(org_id, email) if CALC_SHARE_USAGE: try: share_usage = seafile_api.get_user_share_usage(email) except SearpcError, e: logger.error(e) share_usage = 0 quota_usage = my_usage + share_usage else: share_usage = 0 quota_usage = my_usage # Repos that are share to user in_repos = seafile_api.get_share_in_repo_list(email, -1, -1) # get user profile profile = Profile.objects.get_profile_by_user(email) d_profile = DetailedProfile.objects.get_detailed_profile_by_user(email) return render_to_response('sysadmin/userinfo.html', { 'owned_repos': owned_repos, 'quota': quota, 'quota_usage': quota_usage, 'CALC_SHARE_USAGE': CALC_SHARE_USAGE, 'share_usage': share_usage, 'my_usage': my_usage, 'in_repos': in_repos, 'email': email, 'profile': profile,
quota_usage = 0 share_usage = 0 my_usage = 0 my_usage = seafile_api.get_user_self_usage(email) if CALC_SHARE_USAGE: try: share_usage = seafile_api.get_user_share_usage(email) except SearpcError, e: logger.error(e) share_usage = 0 quota_usage = my_usage + share_usage else: quota_usage = my_usage # Repos that are share to user in_repos = seafile_api.get_share_in_repo_list(email, -1, -1) # get user profile profile = Profile.objects.get_profile_by_user(email) d_profile = DetailedProfile.objects.get_detailed_profile_by_user(email) return render_to_response( 'sysadmin/userinfo.html', { 'owned_repos': owned_repos, 'quota': quota, 'quota_usage': quota_usage, 'CALC_SHARE_USAGE': CALC_SHARE_USAGE, 'share_usage': share_usage, 'my_usage': my_usage, 'in_repos': in_repos, 'email': email,
def my_shared_and_group_repos(request): """Return html snippet of repos that shared to user and group repos. Arguments: - `request`: """ if not request.is_ajax(): raise Http404 content_type = 'application/json; charset=utf-8' username = request.user.username shared_repos = seafile_api.get_share_in_repo_list(username, -1, -1) for repo in shared_repos: repo.user_perm = seafile_api.check_repo_access_permission(repo.repo_id, username) shared_repos.sort(lambda x, y: cmp(y.last_modified, x.last_modified)) group_repos = [] # Get all personal groups I joined. joined_groups = request.user.joined_groups # For each group I joined... for grp in joined_groups: # Get group repos, and for each group repos... for r_id in seaserv.get_group_repoids(grp.id): # No need to list my own repo repo_owner = seafile_api.get_repo_owner(r_id) if repo_owner == username: continue # Convert repo properties due to the different collumns in Repo # and SharedRepo r = seaserv.get_repo(r_id) if not r: continue r.repo_id = r.id r.repo_name = r.name r.repo_desc = r.desc r.last_modified = get_repo_last_modify(r) r.share_type = 'group' r.user = repo_owner r.user_perm = seaserv.check_permission(r_id, username) r.group = grp group_repos.append(r) group_repos.sort(key=lambda x: x.group.group_name) for i, repo in enumerate(group_repos): if i == 0: repo.show_group_name = True else: if repo.group.group_name != group_repos[i-1].group.group_name: repo.show_group_name = True ctx_shared = { "shared_repos": shared_repos, } ctx_group = { "group_repos": group_repos, } shared_repos_html = render_to_string('snippets/my_shared_repos.html', ctx_shared, context_instance=RequestContext(request)) group_repos_html = render_to_string('snippets/my_group_repos.html', ctx_group, context_instance=RequestContext(request)) return HttpResponse(json.dumps({"shared": shared_repos_html, "group": group_repos_html}), content_type=content_type)
repo = seafile_api.get_repo(repo_id) if repo: all_repos[repo_id] = repo except SearpcError, e: util.warn("Failed to get repo %.8s: %s" % (repo_id, e.msg)) try: owned_repos = seafile_api.get_owned_repo_list(username) except SearpcError, e: util.warn("Failed to list owned repos: %s" % e.msg) for orepo in owned_repos: addRepo(orepo.id) try: shared_repos = seafile_api.get_share_in_repo_list(username, -1, -1) except SearpcError, e: util.warn("Failed to list shared repos: %s" % e.msg) for srepo in shared_repos: addRepo(srepo.repo_id) try: joined_groups = seaserv.get_personal_groups_by_user(username) except SearpcError, e: util.warn("Failed to get groups for %s" % username) for g in joined_groups: try: group_repos = seafile_api.get_group_repo_list(g.id) for repo in group_repos: if all_repos.has_key(repo.id):
def get(self, request, format=None): email = request.user.username repos_json = [] owned_repos = list_personal_repos_by_owner(email) calculate_repo_info(owned_repos, email) owned_repos.sort(lambda x, y: cmp(y.latest_modify, x.latest_modify)) for r in owned_repos: repo = { "type": "repo", "id": r.id, "owner": email, "name": r.name, "desc": r.desc, "mtime": r.latest_modify, "root": r.root, "size": r.size, "encrypted": r.encrypted, "permission": 'rw', # Always have read-write permission to owned repo } repos_json.append(repo) shared_repos = seafile_api.get_share_in_repo_list(email, -1, -1) for r in shared_repos: commit = get_commits(r.repo_id, 0, 1)[0] if not commit: continue r.latest_modify = commit.ctime r.root = commit.root_id r.size = server_repo_size(r.repo_id) r.password_need = is_passwd_set(r.repo_id, email) r.permission = check_permission(r.repo_id, email) repo = { "type": "srepo", "id": r.repo_id, "owner": r.user, "name": r.repo_name, "desc": r.repo_desc, "mtime": r.latest_modify, "root": r.root, "size": r.size, "encrypted": r.encrypted, "permission": r.permission, } repos_json.append(repo) groups = get_personal_groups_by_user(email) for group in groups: g_repos = get_group_repos(group.id, email) calculate_repo_info(g_repos, email) g_repos.sort(lambda x, y: cmp(y.latest_modify, x.latest_modify)) for r in g_repos: repo = { "type": "grepo", "id": r.id, "owner": group.group_name, "name": r.name, "desc": r.desc, "mtime": r.latest_modify, "root": r.root, "size": r.size, "encrypted": r.encrypted, "permission": check_permission(r.id, email), } repos_json.append(repo) return Response(repos_json)
repo = seafile_api.get_repo(repo_id) if repo: all_repos[repo_id] = repo except SearpcError, e: util.warn("Failed to get repo %.8s: %s" % (repo_id, e.msg)) try: owned_repos = seafile_api.get_owned_repo_list(username) except SearpcError, e: util.warn("Failed to list owned repos: %s" % e.msg) for orepo in owned_repos: addRepo(orepo.id) try: shared_repos = seafile_api.get_share_in_repo_list(username, -1, -1) except SearpcError, e: util.warn("Failed to list shared repos: %s" % e.msg) for srepo in shared_repos: addRepo(srepo.repo_id) try: joined_groups = seaserv.get_personal_groups_by_user(username) except SearpcError, e: util.warn("Failed to get groups for %s" % username) for g in joined_groups: try: group_repos = seafile_api.get_group_repo_list(g.id) for repo in group_repos: if all_repos.has_key(repo.id):
def user_info(request, email): owned_repos = seafile_api.get_owned_repo_list(email) org = ccnet_threaded_rpc.get_orgs_by_user(email) org_name = None if not org: space_usage = seafile_api.get_user_self_usage(email) space_quota = seafile_api.get_user_quota(email) if CALC_SHARE_USAGE: share_usage = seafile_api.get_user_share_usage(email) share_quota = seafile_api.get_user_share_quota(email) else: share_quota = share_usage = 0 else: org_id = org[0].org_id org_name = org[0].org_name space_usage = seafserv_threaded_rpc.get_org_user_quota_usage( org_id, email) space_quota = seafserv_threaded_rpc.get_org_user_quota(org_id, email) share_usage = share_quota = 0 # Repos that are share to user in_repos = seafile_api.get_share_in_repo_list(email, -1, -1) # get user profile profile = Profile.objects.get_profile_by_user(email) d_profile = DetailedProfile.objects.get_detailed_profile_by_user(email) user_shared_links = [] # download links p_fileshares = [] fileshares = list(FileShare.objects.filter(username=email)) for fs in fileshares: r = seafile_api.get_repo(fs.repo_id) if not r: fs.delete() continue if fs.is_file_share_link(): if seafile_api.get_file_id_by_path(r.id, fs.path) is None: fs.delete() continue fs.filename = os.path.basename(fs.path) path = fs.path.rstrip('/') # Normalize file path obj_id = seafile_api.get_file_id_by_path(r.id, path) fs.file_size = seafile_api.get_file_size(r.store_id, r.version, obj_id) else: if seafile_api.get_dir_id_by_path(r.id, fs.path) is None: fs.delete() continue fs.filename = os.path.basename(fs.path.rstrip('/')) path = fs.path if path[-1] != '/': # Normalize dir path path += '/' # get dir size dir_id = seafserv_threaded_rpc.get_dirid_by_path( r.id, r.head_cmmt_id, path) fs.dir_size = seafserv_threaded_rpc.get_dir_size( r.store_id, r.version, dir_id) fs.is_download = True p_fileshares.append(fs) p_fileshares.sort(key=lambda x: x.view_cnt, reverse=True) user_shared_links += p_fileshares # upload links uploadlinks = list(UploadLinkShare.objects.filter(username=email)) p_uploadlinks = [] for link in uploadlinks: r = seafile_api.get_repo(link.repo_id) if not r: link.delete() continue if seafile_api.get_dir_id_by_path(r.id, link.path) is None: link.delete() continue link.dir_name = os.path.basename(link.path.rstrip('/')) link.is_upload = True p_uploadlinks.append(link) p_uploadlinks.sort(key=lambda x: x.view_cnt, reverse=True) user_shared_links += p_uploadlinks return render_to_response('sysadmin/userinfo.html', { 'owned_repos': owned_repos, 'space_quota': space_quota, 'space_usage': space_usage, 'share_quota': share_quota, 'share_usage': share_usage, 'CALC_SHARE_USAGE': CALC_SHARE_USAGE, 'in_repos': in_repos, 'email': email, 'profile': profile, 'd_profile': d_profile, 'org_name': org_name, 'user_shared_links': user_shared_links, }, context_instance=RequestContext(request))
def user_info(request, email): owned_repos = seafile_api.get_owned_repo_list(email) org = ccnet_threaded_rpc.get_orgs_by_user(email) org_name = None if not org: space_usage = seafile_api.get_user_self_usage(email) space_quota = seafile_api.get_user_quota(email) if CALC_SHARE_USAGE: share_usage = seafile_api.get_user_share_usage(email) share_quota = seafile_api.get_user_share_quota(email) else: share_quota = share_usage = 0 else: org_id = org[0].org_id org_name = org[0].org_name space_usage = seafserv_threaded_rpc.get_org_user_quota_usage(org_id, email) space_quota = seafserv_threaded_rpc.get_org_user_quota(org_id, email) share_usage = share_quota = 0 # Repos that are share to user in_repos = seafile_api.get_share_in_repo_list(email, -1, -1) # get user profile profile = Profile.objects.get_profile_by_user(email) d_profile = DetailedProfile.objects.get_detailed_profile_by_user(email) user_shared_links = [] # download links p_fileshares = [] fileshares = list(FileShare.objects.filter(username=email)) for fs in fileshares: r = seafile_api.get_repo(fs.repo_id) if not r: fs.delete() continue if fs.is_file_share_link(): if seafile_api.get_file_id_by_path(r.id, fs.path) is None: fs.delete() continue fs.filename = os.path.basename(fs.path) path = fs.path.rstrip('/') # Normalize file path obj_id = seafile_api.get_file_id_by_path(r.id, path) fs.file_size = seafile_api.get_file_size(r.store_id, r.version, obj_id) else: if seafile_api.get_dir_id_by_path(r.id, fs.path) is None: fs.delete() continue fs.filename = os.path.basename(fs.path.rstrip('/')) path = fs.path if path[-1] != '/': # Normalize dir path path += '/' # get dir size dir_id = seafserv_threaded_rpc.get_dirid_by_path(r.id, r.head_cmmt_id, path) fs.dir_size = seafserv_threaded_rpc.get_dir_size(r.store_id, r.version, dir_id) fs.is_download = True p_fileshares.append(fs) p_fileshares.sort(key=lambda x: x.view_cnt, reverse=True) user_shared_links += p_fileshares # upload links uploadlinks = list(UploadLinkShare.objects.filter(username=email)) p_uploadlinks = [] for link in uploadlinks: r = seafile_api.get_repo(link.repo_id) if not r: link.delete() continue if seafile_api.get_dir_id_by_path(r.id, link.path) is None: link.delete() continue link.dir_name = os.path.basename(link.path.rstrip('/')) link.is_upload = True p_uploadlinks.append(link) p_uploadlinks.sort(key=lambda x: x.view_cnt, reverse=True) user_shared_links += p_uploadlinks return render_to_response( 'sysadmin/userinfo.html', { 'owned_repos': owned_repos, 'space_quota': space_quota, 'space_usage': space_usage, 'share_quota': share_quota, 'share_usage': share_usage, 'CALC_SHARE_USAGE': CALC_SHARE_USAGE, 'in_repos': in_repos, 'email': email, 'profile': profile, 'd_profile': d_profile, 'org_name': org_name, 'user_shared_links': user_shared_links, }, context_instance=RequestContext(request))
def get(self, request): """ Return repos user can access. Permission checking: 1. all authenticated user can perform this action. """ filter_by = { 'mine': False, 'shared': False, 'group': False, 'public': False, } request_type_list = request.GET.getlist('type', "") if not request_type_list: # set all to True, no filter applied filter_by = filter_by.fromkeys(filter_by.iterkeys(), True) for request_type in request_type_list: request_type = request_type.strip() filter_by[request_type] = True email = request.user.username # Use dict to reduce memcache fetch cost in large for-loop. contact_email_dict = {} nickname_dict = {} org_id = None if is_org_context(request): org_id = request.user.org.org_id try: starred_repos = UserStarredFiles.objects.get_starred_repos_by_user( email) starred_repo_id_list = [item.repo_id for item in starred_repos] except Exception as e: logger.error(e) starred_repo_id_list = [] repo_info_list = [] if filter_by['mine']: if org_id: owned_repos = seafile_api.get_org_owned_repo_list( org_id, email, ret_corrupted=True) else: owned_repos = seafile_api.get_owned_repo_list( email, ret_corrupted=True) # Reduce memcache fetch ops. modifiers_set = set([x.last_modifier for x in owned_repos]) for e in modifiers_set: if e not in contact_email_dict: contact_email_dict[e] = email2contact_email(e) if e not in nickname_dict: nickname_dict[e] = email2nickname(e) owned_repos.sort(lambda x, y: cmp(y.last_modify, x.last_modify)) for r in owned_repos: # do not return virtual repos if r.is_virtual: continue repo_info = { "type": "mine", "repo_id": r.id, "repo_name": r.name, "owner_email": email, "owner_name": email2nickname(email), "owner_contact_email": email2contact_email(email), "last_modified": timestamp_to_isoformat_timestr(r.last_modify), "modifier_email": r.last_modifier, "modifier_name": nickname_dict.get(r.last_modifier, ''), "modifier_contact_email": contact_email_dict.get(r.last_modifier, ''), "size": r.size, "encrypted": r.encrypted, "permission": 'rw', # Always have read-write permission to owned repo "starred": r.repo_id in starred_repo_id_list, "status": normalize_repo_status_code(r.status), } if is_pro_version() and ENABLE_STORAGE_CLASSES: repo_info['storage_name'] = r.storage_name repo_info['storage_id'] = r.storage_id repo_info_list.append(repo_info) if filter_by['shared']: if org_id: shared_repos = seafile_api.get_org_share_in_repo_list( org_id, email, -1, -1) else: shared_repos = seafile_api.get_share_in_repo_list( email, -1, -1) repos_with_admin_share_to = ExtraSharePermission.objects.\ get_repos_with_admin_permission(email) # Reduce memcache fetch ops. owners_set = set([x.user for x in shared_repos]) modifiers_set = set([x.last_modifier for x in shared_repos]) for e in owners_set | modifiers_set: if e not in contact_email_dict: contact_email_dict[e] = email2contact_email(e) if e not in nickname_dict: nickname_dict[e] = email2nickname(e) shared_repos.sort(lambda x, y: cmp(y.last_modify, x.last_modify)) for r in shared_repos: owner_email = r.user group_name = '' is_group_owned_repo = False if '@seafile_group' in owner_email: is_group_owned_repo = True group_id = get_group_id_by_repo_owner(owner_email) group_name = group_id_to_name(group_id) owner_name = group_name if is_group_owned_repo else \ nickname_dict.get(owner_email, '') owner_contact_email = '' if is_group_owned_repo else \ contact_email_dict.get(owner_email, '') repo_info = { "type": "shared", "repo_id": r.repo_id, "repo_name": r.repo_name, "last_modified": timestamp_to_isoformat_timestr(r.last_modify), "modifier_email": r.last_modifier, "modifier_name": nickname_dict.get(r.last_modifier, ''), "modifier_contact_email": contact_email_dict.get(r.last_modifier, ''), "owner_email": owner_email, "owner_name": owner_name, "owner_contact_email": owner_contact_email, "size": r.size, "encrypted": r.encrypted, "permission": r.permission, "starred": r.repo_id in starred_repo_id_list, "status": normalize_repo_status_code(r.status), } if r.repo_id in repos_with_admin_share_to: repo_info['is_admin'] = True else: repo_info['is_admin'] = False repo_info_list.append(repo_info) if filter_by['group']: if org_id: group_repos = seafile_api.get_org_group_repos_by_user( email, org_id) else: group_repos = seafile_api.get_group_repos_by_user(email) group_repos.sort(lambda x, y: cmp(y.last_modify, x.last_modify)) # Reduce memcache fetch ops. share_from_set = set([x.user for x in group_repos]) modifiers_set = set([x.last_modifier for x in group_repos]) for e in modifiers_set | share_from_set: if e not in contact_email_dict: contact_email_dict[e] = email2contact_email(e) if e not in nickname_dict: nickname_dict[e] = email2nickname(e) for r in group_repos: repo_info = { "type": "group", "group_id": r.group_id, "group_name": r.group_name, "repo_id": r.repo_id, "repo_name": r.repo_name, "last_modified": timestamp_to_isoformat_timestr(r.last_modify), "modifier_email": r.last_modifier, "modifier_name": nickname_dict.get(r.last_modifier, ''), "modifier_contact_email": contact_email_dict.get(r.last_modifier, ''), "size": r.size, "encrypted": r.encrypted, "permission": r.permission, "starred": r.repo_id in starred_repo_id_list, "status": normalize_repo_status_code(r.status), } repo_info_list.append(repo_info) if filter_by['public'] and request.user.permissions.can_view_org(): public_repos = list_inner_pub_repos(request) # get repo id owner dict all_repo_owner = [] repo_id_owner_dict = {} for repo in public_repos: repo_id = repo.repo_id if repo_id not in repo_id_owner_dict: repo_owner = get_repo_owner(request, repo_id) all_repo_owner.append(repo_owner) repo_id_owner_dict[repo_id] = repo_owner # Reduce memcache fetch ops. owner_set = set(all_repo_owner) share_from_set = set([x.user for x in public_repos]) modifiers_set = set([x.last_modifier for x in public_repos]) for e in modifiers_set | share_from_set | owner_set: if e not in contact_email_dict: contact_email_dict[e] = email2contact_email(e) if e not in nickname_dict: nickname_dict[e] = email2nickname(e) for r in public_repos: repo_owner = repo_id_owner_dict[r.repo_id] repo_info = { "type": "public", "repo_id": r.repo_id, "repo_name": r.repo_name, "last_modified": timestamp_to_isoformat_timestr(r.last_modify), "modifier_email": r.last_modifier, "modifier_name": nickname_dict.get(r.last_modifier, ''), "modifier_contact_email": contact_email_dict.get(r.last_modifier, ''), "owner_email": repo_owner, "owner_name": nickname_dict.get(repo_owner, ''), "owner_contact_email": contact_email_dict.get(repo_owner, ''), "size": r.size, "encrypted": r.encrypted, "permission": r.permission, "starred": r.repo_id in starred_repo_id_list, "status": normalize_repo_status_code(r.status), } repo_info_list.append(repo_info) utc_dt = datetime.datetime.utcnow() timestamp = utc_dt.strftime('%Y-%m-%d %H:%M:%S') org_id = request.user.org.org_id if is_org_context(request) else -1 try: send_message('seahub.stats', 'user-login\t%s\t%s\t%s' % (email, timestamp, org_id)) except Exception as e: logger.error('Error when sending user-login message: %s' % str(e)) return Response({'repos': repo_info_list})
def get(self, request): """ Return repos user can access. Permission checking: 1. all authenticated user can perform this action. """ filter_by = { 'mine': False, 'shared': False, 'group': False, 'public': False, } request_type_list = request.GET.getlist('type', "") if not request_type_list: # set all to True, no filter applied filter_by = filter_by.fromkeys(filter_by.iterkeys(), True) for request_type in request_type_list: request_type = request_type.strip() filter_by[request_type] = True email = request.user.username # Use dict to reduce memcache fetch cost in large for-loop. contact_email_dict = {} nickname_dict = {} org_id = None if is_org_context(request): org_id = request.user.org.org_id try: starred_repos = UserStarredFiles.objects.get_starred_repos_by_user(email) starred_repo_id_list = [item.repo_id for item in starred_repos] except Exception as e: logger.error(e) starred_repo_id_list = [] repo_info_list = [] if filter_by['mine']: if org_id: owned_repos = seafile_api.get_org_owned_repo_list(org_id, email, ret_corrupted=True) else: owned_repos = seafile_api.get_owned_repo_list(email, ret_corrupted=True) # Reduce memcache fetch ops. modifiers_set = set([x.last_modifier for x in owned_repos]) for e in modifiers_set: if e not in contact_email_dict: contact_email_dict[e] = email2contact_email(e) if e not in nickname_dict: nickname_dict[e] = email2nickname(e) owned_repos.sort(lambda x, y: cmp(y.last_modify, x.last_modify)) for r in owned_repos: # do not return virtual repos if r.is_virtual: continue repo_info = { "type": "mine", "repo_id": r.id, "repo_name": r.name, "owner_email": email, "owner_name": email2nickname(email), "owner_contact_email": email2contact_email(email), "last_modified": timestamp_to_isoformat_timestr(r.last_modify), "modifier_email": r.last_modifier, "modifier_name": nickname_dict.get(r.last_modifier, ''), "modifier_contact_email": contact_email_dict.get(r.last_modifier, ''), "size": r.size, "encrypted": r.encrypted, "permission": 'rw', # Always have read-write permission to owned repo "starred": r.repo_id in starred_repo_id_list, } if is_pro_version() and ENABLE_STORAGE_CLASSES: repo_info['storage_name'] = r.storage_name repo_info['storage_id'] = r.storage_id repo_info_list.append(repo_info) if filter_by['shared']: if org_id: shared_repos = seafile_api.get_org_share_in_repo_list(org_id, email, -1, -1) else: shared_repos = seafile_api.get_share_in_repo_list( email, -1, -1) repos_with_admin_share_to = ExtraSharePermission.objects.\ get_repos_with_admin_permission(email) # Reduce memcache fetch ops. owners_set = set([x.user for x in shared_repos]) modifiers_set = set([x.last_modifier for x in shared_repos]) for e in owners_set | modifiers_set: if e not in contact_email_dict: contact_email_dict[e] = email2contact_email(e) if e not in nickname_dict: nickname_dict[e] = email2nickname(e) shared_repos.sort(lambda x, y: cmp(y.last_modify, x.last_modify)) for r in shared_repos: owner_email = r.user group_name = '' is_group_owned_repo = False if '@seafile_group' in owner_email: is_group_owned_repo = True group_id = get_group_id_by_repo_owner(owner_email) group_name= group_id_to_name(group_id) owner_name = group_name if is_group_owned_repo else \ nickname_dict.get(owner_email, '') owner_contact_email = '' if is_group_owned_repo else \ contact_email_dict.get(owner_email, '') repo_info = { "type": "shared", "repo_id": r.repo_id, "repo_name": r.repo_name, "last_modified": timestamp_to_isoformat_timestr(r.last_modify), "modifier_email": r.last_modifier, "modifier_name": nickname_dict.get(r.last_modifier, ''), "modifier_contact_email": contact_email_dict.get(r.last_modifier, ''), "owner_email": owner_email, "owner_name": owner_name, "owner_contact_email": owner_contact_email, "size": r.size, "encrypted": r.encrypted, "permission": r.permission, "starred": r.repo_id in starred_repo_id_list, } if r.repo_id in repos_with_admin_share_to: repo_info['is_admin'] = True else: repo_info['is_admin'] = False repo_info_list.append(repo_info) if filter_by['group']: if org_id: group_repos = seafile_api.get_org_group_repos_by_user(email, org_id) else: group_repos = seafile_api.get_group_repos_by_user(email) group_repos.sort(lambda x, y: cmp(y.last_modify, x.last_modify)) # Reduce memcache fetch ops. share_from_set = set([x.user for x in group_repos]) modifiers_set = set([x.last_modifier for x in group_repos]) for e in modifiers_set | share_from_set: if e not in contact_email_dict: contact_email_dict[e] = email2contact_email(e) if e not in nickname_dict: nickname_dict[e] = email2nickname(e) for r in group_repos: repo_info = { "type": "group", "group_id": r.group_id, "group_name": r.group_name, "repo_id": r.repo_id, "repo_name": r.repo_name, "last_modified": timestamp_to_isoformat_timestr(r.last_modify), "modifier_email": r.last_modifier, "modifier_name": nickname_dict.get(r.last_modifier, ''), "modifier_contact_email": contact_email_dict.get(r.last_modifier, ''), "size": r.size, "encrypted": r.encrypted, "permission": r.permission, "starred": r.repo_id in starred_repo_id_list, } repo_info_list.append(repo_info) if filter_by['public'] and request.user.permissions.can_view_org(): public_repos = list_inner_pub_repos(request) # get repo id owner dict all_repo_owner = [] repo_id_owner_dict = {} for repo in public_repos: repo_id = repo.repo_id if repo_id not in repo_id_owner_dict: repo_owner = get_repo_owner(request, repo_id) all_repo_owner.append(repo_owner) repo_id_owner_dict[repo_id] = repo_owner # Reduce memcache fetch ops. owner_set = set(all_repo_owner) share_from_set = set([x.user for x in public_repos]) modifiers_set = set([x.last_modifier for x in public_repos]) for e in modifiers_set | share_from_set | owner_set: if e not in contact_email_dict: contact_email_dict[e] = email2contact_email(e) if e not in nickname_dict: nickname_dict[e] = email2nickname(e) for r in public_repos: repo_owner = repo_id_owner_dict[r.repo_id] repo_info = { "type": "public", "repo_id": r.repo_id, "repo_name": r.repo_name, "last_modified": timestamp_to_isoformat_timestr(r.last_modify), "modifier_email": r.last_modifier, "modifier_name": nickname_dict.get(r.last_modifier, ''), "modifier_contact_email": contact_email_dict.get(r.last_modifier, ''), "owner_email": repo_owner, "owner_name": nickname_dict.get(repo_owner, ''), "owner_contact_email": contact_email_dict.get(repo_owner, ''), "size": r.size, "encrypted": r.encrypted, "permission": r.permission, "starred": r.repo_id in starred_repo_id_list, } repo_info_list.append(repo_info) utc_dt = datetime.datetime.utcnow() timestamp = utc_dt.strftime('%Y-%m-%d %H:%M:%S') org_id = request.user.org.org_id if is_org_context(request) else -1 try: send_message('seahub.stats', 'user-login\t%s\t%s\t%s' % (email, timestamp, org_id)) except Exception as e: logger.error('Error when sending user-login message: %s' % str(e)) return Response({'repos': repo_info_list})