Merge lp:~jml/launchpad/remove-get-current-principal into lp:launchpad

Proposed by Jonathan Lange
Status: Merged
Approved by: Robert Collins
Approved revision: no longer in the source branch.
Merged at revision: 11114
Proposed branch: lp:~jml/launchpad/remove-get-current-principal
Merge into: lp:launchpad
Diff against target: 114 lines (+20/-26)
2 files modified
lib/canonical/launchpad/webapp/interaction.py (+14/-5)
lib/canonical/launchpad/webapp/launchbag.py (+6/-21)
To merge this branch: bzr merge lp:~jml/launchpad/remove-get-current-principal
Reviewer Review Type Date Requested Status
Robert Collins (community) Approve
Review via email: mp+29542@code.launchpad.net

Description of the change

I was poking around in the login code and noticed that there are two nearly-identical ways for getting the current principal: get_principal in launchbag and get_current_principal in interaction.

Since they were both nearly the same, I decided to delete one. get_current_principal is more widely used, has a better name and is in a better location, so I kept that. But get_principal has the better implementation, so I changed get_current_principal to have get_principal's implementation.

To post a comment you must log in.
Revision history for this message
Robert Collins (lifeless) wrote :

It might be nice if there was an Interaction object. I know it would be global, but it would at least allow nonglobal methods which are easier to test.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'lib/canonical/launchpad/webapp/interaction.py'
--- lib/canonical/launchpad/webapp/interaction.py 2010-04-16 23:56:47 +0000
+++ lib/canonical/launchpad/webapp/interaction.py 2010-07-09 12:22:44 +0000
@@ -30,14 +30,23 @@
3030
3131
32def get_current_principal():32def get_current_principal():
33 """Get the principal from the current interaction."""33 """Get the principal from the current interaction.
34
35 :return: The current principal if there is an interaction, None otherwise.
36 """
34 interaction = queryInteraction()37 interaction = queryInteraction()
38 if interaction is None:
39 return None
35 principals = [40 principals = [
36 participation.principal41 participation.principal
37 for participation in interaction.participations]42 for participation in interaction.participations
38 assert len(principals) == 1, (43 if participation.principal is not None]
39 "There should be only one principal in the current interaction.")44 if not principals:
40 return principals[0]45 return None
46 elif len(principals) > 1:
47 raise ValueError('Too many principals')
48 else:
49 return principals[0]
4150
4251
43def setupInteraction(principal, login=None, participation=None):52def setupInteraction(principal, login=None, participation=None):
4453
=== modified file 'lib/canonical/launchpad/webapp/launchbag.py'
--- lib/canonical/launchpad/webapp/launchbag.py 2010-02-17 11:13:06 +0000
+++ lib/canonical/launchpad/webapp/launchbag.py 2010-07-09 12:22:44 +0000
@@ -12,7 +12,6 @@
1212
13from zope.interface import implements13from zope.interface import implements
14from zope.component import getUtility14from zope.component import getUtility
15from zope.security import management
16from zope import thread15from zope import thread
1716
18from canonical.database.sqlbase import block_implicit_flushes17from canonical.database.sqlbase import block_implicit_flushes
@@ -20,30 +19,13 @@
20 IAccount, IBug, IDistribution, IDistroSeries, IPerson,19 IAccount, IBug, IDistribution, IDistroSeries, IPerson,
21 IProjectGroup, IProduct, ISourcePackage, IDistroArchSeries,20 IProjectGroup, IProduct, ISourcePackage, IDistroArchSeries,
22 ISpecification, IBugTask, ILaunchpadCelebrities)21 ISpecification, IBugTask, ILaunchpadCelebrities)
22from canonical.launchpad.webapp.interaction import get_current_principal
23from canonical.launchpad.webapp.interfaces import (23from canonical.launchpad.webapp.interfaces import (
24 ILaunchBag, ILaunchpadApplication, ILoggedInEvent, IOpenLaunchBag)24 ILaunchBag, ILaunchpadApplication, ILoggedInEvent, IOpenLaunchBag)
2525
26_utc_tz = pytz.timezone('UTC')26_utc_tz = pytz.timezone('UTC')
2727
2828
29def get_principal():
30 """Return the principal for the current interaction."""
31 interaction = management.queryInteraction()
32 if interaction is None:
33 return None
34 principals = [
35 participation.principal
36 for participation in list(interaction.participations)
37 if participation.principal is not None
38 ]
39 if not principals:
40 return None
41 elif len(principals) > 1:
42 raise ValueError('Too many principals')
43 else:
44 return principals[0]
45
46
47class LaunchBag:29class LaunchBag:
4830
49 implements(IOpenLaunchBag)31 implements(IOpenLaunchBag)
@@ -84,12 +66,12 @@
84 @property66 @property
85 @block_implicit_flushes67 @block_implicit_flushes
86 def account(self):68 def account(self):
87 return IAccount(get_principal(), None)69 return IAccount(get_current_principal(), None)
8870
89 @property71 @property
90 @block_implicit_flushes72 @block_implicit_flushes
91 def user(self):73 def user(self):
92 return IPerson(get_principal(), None)74 return IPerson(get_current_principal(), None)
9375
94 def add(self, obj):76 def add(self, obj):
95 store = self._store77 store = self._store
@@ -192,6 +174,7 @@
192 else:174 else:
193 launchbag.setLogin(loggedinevent.login)175 launchbag.setLogin(loggedinevent.login)
194176
177
195def set_developer_in_launchbag_before_traversal(event):178def set_developer_in_launchbag_before_traversal(event):
196 """Subscriber for IBeforeTraverseEvent179 """Subscriber for IBeforeTraverseEvent
197180
@@ -208,12 +191,14 @@
208 is_developer = user.inTeam(celebrities.launchpad_developers)191 is_developer = user.inTeam(celebrities.launchpad_developers)
209 launchbag.setDeveloper(is_developer)192 launchbag.setDeveloper(is_developer)
210193
194
211def reset_login_in_launchbag_on_logout(event):195def reset_login_in_launchbag_on_logout(event):
212 """Subscriber for ILoggedOutEvent that sets 'login' in launchbag to None.196 """Subscriber for ILoggedOutEvent that sets 'login' in launchbag to None.
213 """197 """
214 launchbag = getUtility(IOpenLaunchBag)198 launchbag = getUtility(IOpenLaunchBag)
215 launchbag.setLogin(None)199 launchbag.setLogin(None)
216200
201
217def reset_developer_in_launchbag_on_logout(event):202def reset_developer_in_launchbag_on_logout(event):
218 """Subscriber for ILoggedOutEvent that resets the developer flag."""203 """Subscriber for ILoggedOutEvent that resets the developer flag."""
219 launchbag = getUtility(IOpenLaunchBag)204 launchbag = getUtility(IOpenLaunchBag)