Merge ~twom/launchpad:stats-fix-valueerror-on-redirect into launchpad:master

Proposed by Tom Wardill
Status: Merged
Approved by: Tom Wardill
Approved revision: c222314ab9f7ca17fcfff814a1da80e05a6fce34
Merge reported by: Otto Co-Pilot
Merged at revision: not available
Proposed branch: ~twom/launchpad:stats-fix-valueerror-on-redirect
Merge into: launchpad:master
Diff against target: 58 lines (+23/-2)
2 files modified
lib/lp/services/webapp/publication.py (+9/-2)
lib/lp/services/webapp/tests/test_publication.py (+14/-0)
Reviewer Review Type Date Requested Status
Colin Watson (community) Approve
Review via email: mp+391687@code.launchpad.net

Commit message

Fix setting pageids in a redirect

Description of the change

Redirects don't have a view context. With the move to generating pageid at traversal rather than call time, we are now catching some redirects in this flow.
Check for RedirectionView on generation and allow the existing methods that deal with not having a context to DTRT.

To post a comment you must log in.
f804ccb... by Tom Wardill

Check for RedirectionView

Revision history for this message
Colin Watson (cjwatson) wrote :

LGTM - could you update the MP description as well please?

review: Approve
c222314... by Tom Wardill

Fix imports

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/lib/lp/services/webapp/publication.py b/lib/lp/services/webapp/publication.py
2index 3b86566..6324a64 100644
3--- a/lib/lp/services/webapp/publication.py
4+++ b/lib/lp/services/webapp/publication.py
5@@ -53,7 +53,10 @@ from zope.publisher.interfaces.browser import (
6 )
7 from zope.publisher.publish import mapply
8 from zope.security.management import newInteraction
9-from zope.security.proxy import removeSecurityProxy
10+from zope.security.proxy import (
11+ isinstance as zope_isinstance,
12+ removeSecurityProxy,
13+ )
14 from zope.traversing.interfaces import BeforeTraverseEvent
15
16 from lp.app.interfaces.launchpad import ILaunchpadCelebrities
17@@ -85,6 +88,7 @@ from lp.services.webapp.interfaces import (
18 OffsiteFormPostError,
19 )
20 from lp.services.webapp.opstats import OpStats
21+from lp.services.webapp.publisher import RedirectionView
22 from lp.services.webapp.vhosts import allvhosts
23
24
25@@ -564,7 +568,10 @@ class LaunchpadBrowserPublication(
26 view = removeSecurityProxy(view)
27 # It's possible that the view is a bound method.
28 view = getattr(view, '__self__', view)
29- context = removeSecurityProxy(getattr(view, 'context', None))
30+ if zope_isinstance(view, RedirectionView):
31+ context = None
32+ else:
33+ context = removeSecurityProxy(getattr(view, 'context', None))
34 pageid = self.constructPageID(view, context)
35 request.setInWSGIEnvironment('launchpad.pageid', pageid)
36 return pageid
37diff --git a/lib/lp/services/webapp/tests/test_publication.py b/lib/lp/services/webapp/tests/test_publication.py
38index 9bc4089..245fc28 100644
39--- a/lib/lp/services/webapp/tests/test_publication.py
40+++ b/lib/lp/services/webapp/tests/test_publication.py
41@@ -388,3 +388,17 @@ class TestPublisherStats(StatsMixin, TestCaseWithFactory):
42 self.assertEqual(
43 'RootObject-index-html',
44 publication._prepPageIDForMetrics("RootObject:index.html"))
45+
46+ def test_no_context_pageid(self):
47+ # request context may not exist in redirect scenarios
48+ owner = self.factory.makePerson()
49+ ppa = self.factory.makeArchive(owner=owner)
50+ redirect_url = ("http://launchpad.test/api/devel/~{}/"
51+ "+archive/{}/testpackage".format(owner.name, ppa.name))
52+ self.useFixture(FakeLogger())
53+ browser = self.getUserBrowser()
54+ # This shouldn't raise ValueError
55+ self.assertRaises(
56+ NotFound,
57+ browser.open,
58+ redirect_url)

Subscribers

People subscribed via source and target branches

to status/vote changes: