1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-18 08:16:07 +00:00

[api] Add repo last modifier and add cache for memcache ops

This commit is contained in:
zhengxie
2017-06-23 11:30:01 +08:00
parent 966e0bcb9c
commit eb2719fc59
8 changed files with 147 additions and 9 deletions

View File

@@ -417,6 +417,10 @@ class Repos(APIView):
email = request.user.username
# Use dict to reduce memcache fetch cost in large for-loop.
contact_email_dict = {}
nickname_dict = {}
repos_json = []
if filter_by['mine']:
if is_org_context(request):
@@ -427,6 +431,14 @@ class Repos(APIView):
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
@@ -439,6 +451,9 @@ class Repos(APIView):
"owner": email,
"name": r.name,
"mtime": r.last_modify,
"modifier_email": r.last_modifier,
"modifier_contact_email": contact_email_dict.get(r.last_modifier, ''),
"modifier_name": nickname_dict.get(r.last_modifier, ''),
"mtime_relative": translate_seahub_time(r.last_modify),
"size": r.size,
"size_formatted": filesizeformat(r.size),
@@ -461,6 +476,15 @@ class Repos(APIView):
shared_repos = seafile_api.get_share_in_repo_list(
email, -1, -1)
# 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:
r.password_need = is_passwd_set(r.repo_id, email)
@@ -469,9 +493,12 @@ class Repos(APIView):
"id": r.repo_id,
"owner": r.user,
"name": r.repo_name,
"owner_nickname": email2nickname(r.user),
"owner_nickname": nickname_dict.get(r.user, ''),
"mtime": r.last_modify,
"mtime_relative": translate_seahub_time(r.last_modify),
"modifier_email": r.last_modifier,
"modifier_contact_email": contact_email_dict.get(r.last_modifier, ''),
"modifier_name": nickname_dict.get(r.last_modifier, ''),
"size": r.size,
"size_formatted": filesizeformat(r.size),
"encrypted": r.encrypted,
@@ -487,6 +514,15 @@ class Repos(APIView):
groups = get_groups_by_user(request)
group_repos = get_group_repos(request, groups)
group_repos.sort(lambda x, y: cmp(y.last_modify, x.last_modify))
# Reduce memcache fetch ops.
modifiers_set = set([x.last_modifier for x in group_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)
for r in group_repos:
repo = {
"type": "grepo",
@@ -495,6 +531,9 @@ class Repos(APIView):
"groupid": r.group.id,
"name": r.name,
"mtime": r.last_modify,
"modifier_email": r.last_modifier,
"modifier_contact_email": contact_email_dict.get(r.last_modifier, ''),
"modifier_name": nickname_dict.get(r.last_modifier, ''),
"size": r.size,
"encrypted": r.encrypted,
"permission": check_permission(r.id, email),
@@ -506,6 +545,15 @@ class Repos(APIView):
if filter_by['org'] and request.user.permissions.can_view_org():
public_repos = list_inner_pub_repos(request)
# Reduce memcache fetch ops.
modifiers_set = set([x.last_modifier for x in public_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)
for r in public_repos:
repo = {
"type": "grepo",
@@ -514,6 +562,9 @@ class Repos(APIView):
"owner": "Organization",
"mtime": r.last_modified,
"mtime_relative": translate_seahub_time(r.last_modified),
"modifier_email": r.last_modifier,
"modifier_contact_email": contact_email_dict.get(r.last_modifier, ''),
"modifier_name": nickname_dict.get(r.last_modifier, ''),
"size": r.size,
"size_formatted": filesizeformat(r.size),
"encrypted": r.encrypted,
@@ -806,6 +857,9 @@ class Repo(APIView):
"encrypted":repo.encrypted,
"root":root_id,
"permission": check_permission(repo.id, username),
"modifier_email": repo.last_modifier,
"modifier_contact_email": email2contact_email(repo.last_modifier),
"modifier_name": email2nickname(repo.last_modifier),
}
if repo.encrypted:
repo_json["enc_version"] = repo.enc_version
@@ -3798,6 +3852,9 @@ class GroupRepos(APIView):
"owner": username,
"owner_nickname": email2nickname(username),
"share_from_me": True,
"modifier_email": repo.last_modifier,
"modifier_contact_email": email2contact_email(repo.last_modifier),
"modifier_name": email2nickname(repo.last_modifier),
}
return Response(group_repo, status=200)
@@ -3819,6 +3876,17 @@ class GroupRepos(APIView):
repos.sort(lambda x, y: cmp(y.last_modified, x.last_modified))
group.is_staff = is_group_staff(group, request.user)
# Use dict to reduce memcache fetch cost in large for-loop.
contact_email_dict = {}
nickname_dict = {}
owner_set = set([x.user for x in repos])
modifiers_set = set([x.modifier for x in repos])
for e in owner_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)
repos_json = []
for r in repos:
repo = {
@@ -3832,8 +3900,11 @@ class GroupRepos(APIView):
"encrypted": r.encrypted,
"permission": r.permission,
"owner": r.user,
"owner_nickname": email2nickname(r.user),
"owner_nickname": nickname_dict.get(r.user, ''),
"share_from_me": True if username == r.user else False,
"modifier_email": r.last_modifier,
"modifier_contact_email": contact_email_dict.get(r.last_modifier, ''),
"modifier_name": nickname_dict.get(r.last_modifier, ''),
}
repos_json.append(repo)