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
diff --git a/lib/lp/services/webapp/publication.py b/lib/lp/services/webapp/publication.py
index 3b86566..6324a64 100644
--- a/lib/lp/services/webapp/publication.py
+++ b/lib/lp/services/webapp/publication.py
@@ -53,7 +53,10 @@ from zope.publisher.interfaces.browser import (
53 )53 )
54from zope.publisher.publish import mapply54from zope.publisher.publish import mapply
55from zope.security.management import newInteraction55from zope.security.management import newInteraction
56from zope.security.proxy import removeSecurityProxy56from zope.security.proxy import (
57 isinstance as zope_isinstance,
58 removeSecurityProxy,
59 )
57from zope.traversing.interfaces import BeforeTraverseEvent60from zope.traversing.interfaces import BeforeTraverseEvent
5861
59from lp.app.interfaces.launchpad import ILaunchpadCelebrities62from lp.app.interfaces.launchpad import ILaunchpadCelebrities
@@ -85,6 +88,7 @@ from lp.services.webapp.interfaces import (
85 OffsiteFormPostError,88 OffsiteFormPostError,
86 )89 )
87from lp.services.webapp.opstats import OpStats90from lp.services.webapp.opstats import OpStats
91from lp.services.webapp.publisher import RedirectionView
88from lp.services.webapp.vhosts import allvhosts92from lp.services.webapp.vhosts import allvhosts
8993
9094
@@ -564,7 +568,10 @@ class LaunchpadBrowserPublication(
564 view = removeSecurityProxy(view)568 view = removeSecurityProxy(view)
565 # It's possible that the view is a bound method.569 # It's possible that the view is a bound method.
566 view = getattr(view, '__self__', view)570 view = getattr(view, '__self__', view)
567 context = removeSecurityProxy(getattr(view, 'context', None))571 if zope_isinstance(view, RedirectionView):
572 context = None
573 else:
574 context = removeSecurityProxy(getattr(view, 'context', None))
568 pageid = self.constructPageID(view, context)575 pageid = self.constructPageID(view, context)
569 request.setInWSGIEnvironment('launchpad.pageid', pageid)576 request.setInWSGIEnvironment('launchpad.pageid', pageid)
570 return pageid577 return pageid
diff --git a/lib/lp/services/webapp/tests/test_publication.py b/lib/lp/services/webapp/tests/test_publication.py
index 9bc4089..245fc28 100644
--- a/lib/lp/services/webapp/tests/test_publication.py
+++ b/lib/lp/services/webapp/tests/test_publication.py
@@ -388,3 +388,17 @@ class TestPublisherStats(StatsMixin, TestCaseWithFactory):
388 self.assertEqual(388 self.assertEqual(
389 'RootObject-index-html',389 'RootObject-index-html',
390 publication._prepPageIDForMetrics("RootObject:index.html"))390 publication._prepPageIDForMetrics("RootObject:index.html"))
391
392 def test_no_context_pageid(self):
393 # request context may not exist in redirect scenarios
394 owner = self.factory.makePerson()
395 ppa = self.factory.makeArchive(owner=owner)
396 redirect_url = ("http://launchpad.test/api/devel/~{}/"
397 "+archive/{}/testpackage".format(owner.name, ppa.name))
398 self.useFixture(FakeLogger())
399 browser = self.getUserBrowser()
400 # This shouldn't raise ValueError
401 self.assertRaises(
402 NotFound,
403 browser.open,
404 redirect_url)

Subscribers

People subscribed via source and target branches

to status/vote changes: