Skip to content

Instantly share code, notes, and snippets.

@href
Last active September 28, 2017 09:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save href/03bcb7ab78ab55c65af9b7e7f85479bf to your computer and use it in GitHub Desktop.
Save href/03bcb7ab78ab55c65af9b7e7f85479bf to your computer and use it in GitHub Desktop.
Internal redirects in Morepath
""" Simple redirects for renamed paths using a generic redirect model.
For static paths:
@App.path('/old-path')
class OldPathRedirect(Redirect):
to = '/new-path'
For wildcard paths (e.g. /old-pages/my-page to /new-pages/my-page):
@App.path('/old-path', absorb=True)
class OldPagesRedirect(Redirect):
to = '/new-pages
"""
import morepath
class App(morepath.App):
pass
class Redirect(object):
to = None
def __init__(self, absorb=None):
assert self.to and not self.to.endswith('/')
if absorb:
self.to += '/' + absorb
@App.view(model=Redirect)
def view_redirect(self, request):
return morepath.redirect(self.to)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment