Merge lp:~lifeless/launchpad/bug-787765 into lp:launchpad

Proposed by Robert Collins
Status: Merged
Approved by: Robert Collins
Approved revision: no longer in the source branch.
Merged at revision: 13126
Proposed branch: lp:~lifeless/launchpad/bug-787765
Merge into: lp:launchpad
Diff against target: 74 lines (+25/-16)
2 files modified
lib/lp/bugs/browser/structuralsubscription.py (+23/-15)
lib/lp/bugs/browser/tests/test_expose.py (+2/-1)
To merge this branch: bzr merge lp:~lifeless/launchpad/bug-787765
Reviewer Review Type Date Requested Status
Gavin Panella (community) Approve
Review via email: mp+62425@code.launchpad.net

Commit message

[r=allenap][bug=787765] Optimise expose_user_administered_teams_to_js.

Description of the change

expose_user_administered_teams_to_js is both slow and used many times. Its not meant to be slow, so I've slightly tuned it (set intersection is faster than list intersection - way faster with storm objects), and its not meant to be used many times so I've worked around that by short-circuiting it for now as a pragmatic tradeoff. I've filed a techdebt bug about the fact we're using it inappropriately, but fixing that is high, not critical - and likely to be seriously fiddly so I'm not attempting that now. I've run this past Gary as a preimp.

To post a comment you must log in.
Revision history for this message
Gavin Panella (allenap) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'lib/lp/bugs/browser/structuralsubscription.py'
2--- lib/lp/bugs/browser/structuralsubscription.py 2011-05-17 15:41:09 +0000
3+++ lib/lp/bugs/browser/structuralsubscription.py 2011-05-26 22:20:31 +0000
4@@ -431,33 +431,41 @@
5 def expose_user_administered_teams_to_js(request, user, context,
6 absoluteURL=absoluteURL):
7 """Make the list of teams the user administers available to JavaScript."""
8+ # XXX: Robert Collins workaround multiple calls making this cause timeouts:
9+ # see bug 788510.
10+ objects = IJSONRequestCache(request).objects
11+ if 'administratedTeams' in objects:
12+ return
13 info = []
14 api_request = IWebServiceClientRequest(request)
15 is_distro = IDistribution.providedBy(context)
16+ if is_distro:
17+ # If the context is a distro AND a bug supervisor is set then we only
18+ # allow subscriptions from members of the bug supervisor team.
19+ bug_supervisor = context.bug_supervisor
20+ else:
21+ bug_supervisor = None
22 if user is not None:
23- administrated_teams = user.administrated_teams
24+ administrated_teams = set(user.administrated_teams)
25 if administrated_teams:
26 # Get this only if we need to.
27- membership = list(user.teams_participated_in)
28- for team in administrated_teams:
29- # If the user is not a member of the team itself, then
30- # skip it, because structural subscriptions and their
31- # filters can only be edited by the subscriber.
32- # This can happen if the user is an owner but not a member.
33- if not team in membership:
34- continue
35- # If the context is a distro AND a bug supervisor is set
36- # AND the admininistered team is not a member of the bug
37- # supervisor team THEN skip it.
38- if (is_distro and context.bug_supervisor is not None and
39- not team.inTeam(context.bug_supervisor)):
40+ membership = set(user.teams_participated_in)
41+ # Only consider teams the user is both in and administers:
42+ # If the user is not a member of the team itself, then
43+ # skip it, because structural subscriptions and their
44+ # filters can only be edited by the subscriber.
45+ # This can happen if the user is an owner but not a member.
46+ administers_and_in = membership.intersection(administrated_teams)
47+ for team in administers_and_in:
48+ if (bug_supervisor is not None and
49+ not team.inTeam(bug_supervisor)):
50 continue
51 info.append({
52 'link': absoluteURL(team, api_request),
53 'title': team.title,
54 'url': canonical_url(team),
55 })
56- IJSONRequestCache(request).objects['administratedTeams'] = info
57+ objects['administratedTeams'] = info
58
59
60 def expose_user_subscriptions_to_js(user, subscriptions, request,
61
62=== modified file 'lib/lp/bugs/browser/tests/test_expose.py'
63--- lib/lp/bugs/browser/tests/test_expose.py 2011-05-03 13:38:30 +0000
64+++ lib/lp/bugs/browser/tests/test_expose.py 2011-05-26 22:20:31 +0000
65@@ -44,7 +44,8 @@
66 """
67 implements(IWebServiceClientRequest, IJSONRequestCache)
68
69- objects = {}
70+ def __init__(self):
71+ self.objects = {}
72
73
74 class FakeTeam: